I want to serialize and deserialize a custom class so that it behaves like a simple enum.
For example:
public enum Channel
{
EMail,
Telephone
}
Gives the following Json-Representation:
{
"Channel": "EMail"
}
My current approach:
[Serializable]
public sealed class Channel : ISerializable
{
public static readonly Channel Email = new Channel("Email");
public static readonly Channel Telephone = new Channel("Telephone");
public string Key { get; private set; }
//// Deserialization
private Channel(SerializationInfo info, StreamingContext context)
{
Key = (string)info.GetValue(nameof(Channel), typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(Channel), Key);
}
}
But this brings up the following json-representation:
{
"Channel":{"Channel":"Email"}
}
So I want to get rid of the "complex structure" of the class. In the context of serialization it should behave like an enum or string. How can I achieve that behavior? All in all I want to get rid of the encapsulation in the json-representation.