[英]How to implement a Newtonsoft JSON converter that will serialize/deserialize a complex type into a simple type
I am looking to implement https://github.com/HeadspringLabs/Enumeration . 我希望实现https://github.com/HeadspringLabs/Enumeration 。 Currently when I try to serialize/deserialize an Enumeration it serializes it like a complex object.
目前,当我尝试序列化/反序列化Enumeration时,它将它序列化为一个复杂的对象。 For example, the object 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) { }
}
will serialize to 将序列化为
{
"_value": 2,
"_displayName": "Something"
}
In a complex object like this: 在像这样的复杂对象中:
public class OtherClass
{
public Color Color {get; set;}
public string Description {get; set;}
}
it will serialize like this: 它会像这样序列化:
{
"Description" : "Other Description",
"Color" :
{
"_value": 2,
"_displayName": "Something"
}
}
Is there any way to make json convert serialize the complex object like this: 有没有办法让json转换序列化这样的复杂对象:
{
"Description" : "Other Description",
"Color" : 2
}
I can create the correct Color object just from the value by using the method FromValue in the Enumeration class. 我可以使用Enumeration类中的方法FromValue从值中创建正确的Color对象。 I just can't seem to make json convert take the property value as the "value" for the Color object.
我似乎无法使json转换将属性值作为Color对象的“值”。
In what way can I write the WriteJson and Create methods of the converter in order to achieve that? 我可以用什么方式编写转换器的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)
{
}
}
You can make a generic converter for your Headspring.Enumeration<T, int>
-derived class(es) like this: 您可以为您的
Headspring.Enumeration<T, int>
-derived类创建一个通用转换器,如下所示:
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);
}
}
To use the converter, add a [JsonConverter]
attribute to your enum class(es) like this: 要使用转换器,请将
[JsonConverter]
属性添加到您的枚举类中,如下所示:
[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) { }
}
Here's a round-trip demo: https://dotnetfiddle.net/CZsQab 这是一个往返演示: https : //dotnetfiddle.net/CZsQab
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.