【问题标题】:How to implement a Newtonsoft JSON converter that will serialize/deserialize a complex type into a simple type如何实现将复杂类型序列化/反序列化为简单类型的 Newtonsoft JSON 转换器
【发布时间】:2018-01-21 13:30:14
【问题描述】:

我希望实现https://github.com/HeadspringLabs/Enumeration。目前,当我尝试序列化/反序列化一个枚举时,它会将它序列化为一个复杂的对象。例如对象颜色:

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
}

我可以通过使用 Enumeration 类中的 FromValue 方法从值创建正确的 Color 对象。我似乎无法让 json convert 将属性值作为 Color 对象的“值”。

为了实现这一点,我可以通过什么方式编写转换器的 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)
            {
            }
        }

【问题讨论】:

    标签: c# json serialization json.net json-deserialization


    【解决方案1】:

    您可以为您的Headspring.Enumeration&lt;T, int&gt; 派生类创建一个通用转换器,如下所示:

    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] 属性添加到您的枚举类中,如下所示:

    [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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多