如何实现将 serialize/deserialize 复杂类型转换为简单类型的 Newtonsoft JSON 转换器
How to implement a Newtonsoft JSON converter that will serialize/deserialize a complex type into a simple type
我希望实现 https://github.com/HeadspringLabs/Enumeration。目前,当我尝试 serialize/deserialize 枚举时,它会将其序列化为复杂对象。例如对象 Color:
public class Color : Enumeration<Color, int>
{
public static readonly Color Red = new Color(1, "Red");
public static readonly Color Blue = new Color(2, "Blue");
public static readonly Color Green = new Color(3, "Green");
private Color(int value, string displayName) : base(value, displayName) { }
}
将序列化为
{
"_value": 2,
"_displayName": "Something"
}
在像这样的复杂对象中:
public class OtherClass
{
public Color Color {get; set;}
public string Description {get; set;}
}
它将像这样序列化:
{
"Description" : "Other Description",
"Color" :
{
"_value": 2,
"_displayName": "Something"
}
}
有什么方法可以使 json 像这样转换序列化复杂对象:
{
"Description" : "Other Description",
"Color" : 2
}
我可以使用枚举中的 FromValue 方法从值中创建正确的 Color 对象 class。我似乎无法使 json convert 将 属性 值作为 Color 对象的 "value"。
我可以用什么方式编写转换器的 WriteJson 和 Create 方法来实现这一点?
public class EnumerationConverter : JsonCreationConverter<IEnumeration>
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
protected override IEnumeration Create(Type objectType, JObject jObject)
{
}
}
您可以像这样为您的 Headspring.Enumeration<T, int>
派生的 class(es) 创建一个通用转换器:
class EnumerationConverter<T> : JsonConverter where T : Headspring.Enumeration<T, int>
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
int val = Convert.ToInt32(reader.Value);
return Headspring.Enumeration<T, int>.FromValue(val);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var enumVal = (Headspring.Enumeration<T, int>)value;
writer.WriteValue(enumVal.Value);
}
}
要使用转换器,请将 [JsonConverter]
属性添加到您的枚举 class(es),如下所示:
[JsonConverter(typeof(EnumerationConverter<Color>))]
public class Color : Headspring.Enumeration<Color, int>
{
public static readonly Color Red = new Color(1, "Red");
public static readonly Color Blue = new Color(2, "Blue");
public static readonly Color Green = new Color(3, "Green");
private Color(int value, string displayName) : base(value, displayName) { }
}
这是一个往返演示:https://dotnetfiddle.net/CZsQab
我希望实现 https://github.com/HeadspringLabs/Enumeration。目前,当我尝试 serialize/deserialize 枚举时,它会将其序列化为复杂对象。例如对象 Color:
public class Color : Enumeration<Color, int>
{
public static readonly Color Red = new Color(1, "Red");
public static readonly Color Blue = new Color(2, "Blue");
public static readonly Color Green = new Color(3, "Green");
private Color(int value, string displayName) : base(value, displayName) { }
}
将序列化为
{
"_value": 2,
"_displayName": "Something"
}
在像这样的复杂对象中:
public class OtherClass
{
public Color Color {get; set;}
public string Description {get; set;}
}
它将像这样序列化:
{
"Description" : "Other Description",
"Color" :
{
"_value": 2,
"_displayName": "Something"
}
}
有什么方法可以使 json 像这样转换序列化复杂对象:
{
"Description" : "Other Description",
"Color" : 2
}
我可以使用枚举中的 FromValue 方法从值中创建正确的 Color 对象 class。我似乎无法使 json convert 将 属性 值作为 Color 对象的 "value"。
我可以用什么方式编写转换器的 WriteJson 和 Create 方法来实现这一点?
public class EnumerationConverter : JsonCreationConverter<IEnumeration>
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
protected override IEnumeration Create(Type objectType, JObject jObject)
{
}
}
您可以像这样为您的 Headspring.Enumeration<T, int>
派生的 class(es) 创建一个通用转换器:
class EnumerationConverter<T> : JsonConverter where T : Headspring.Enumeration<T, int>
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
int val = Convert.ToInt32(reader.Value);
return Headspring.Enumeration<T, int>.FromValue(val);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var enumVal = (Headspring.Enumeration<T, int>)value;
writer.WriteValue(enumVal.Value);
}
}
要使用转换器,请将 [JsonConverter]
属性添加到您的枚举 class(es),如下所示:
[JsonConverter(typeof(EnumerationConverter<Color>))]
public class Color : Headspring.Enumeration<Color, int>
{
public static readonly Color Red = new Color(1, "Red");
public static readonly Color Blue = new Color(2, "Blue");
public static readonly Color Green = new Color(3, "Green");
private Color(int value, string displayName) : base(value, displayName) { }
}
这是一个往返演示:https://dotnetfiddle.net/CZsQab