解析过时的枚举属性时,JsonConvert.DeserializeObject函数将引发错误

时间:2018-08-13 11:55:55

标签: c# enums json.net deserialization obsolete

我想将现有的Enum属性标记为过时,并为相同的功能添加新的Enum属性。因此,我的项目中有两个枚举(新的一个和作废的标记为一个)。

使用jsonconvert.deserializeobject函数序列化此枚举值时,仅从Newtonsoft 11.0以上版本引发以下错误。

InvalidOperationException: Enum name 'stringedit' already exists on enum 'EditingType'. Newtonsoft.Json.Utilities.EnumUtils.InitializeValuesAndNames(Type enumType) JsonSerializationException: Error converting value "boolean" to type 'Student.Models.EditingType'. Path 'columns[0].editType', line 1, position 3997. Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, object value, CultureInfo culture, JsonContract contract, Type targetType)

如果我的项目中需要做任何更改,或者有任何计划从序列化功能解决此新错误。

我已使用下面的代码块进行过时的枚举属性序列化。

[DataContract]

public enum EditingType

{

    [Obsolete("String property has been deprecated. Use StringEdit property instead")]

    [EnumMember(Value = "stringedit")]

    String,

    [EnumMember(Value = "stringedit")]

    StringEdit

}

public class JsonPropertyAttribute : Attribute

{
    public JsonPropertyAttribute(string argName)
    {
        PropertyName = argName;
    }

    public string PropertyName { get; set; }
}

public class JsonConverterAttribute : Attribute
{
    public Type ConverterType
    {
        get; set;
    }
    public string ValueType
    {
        get; set;
    }
    public JsonConverterAttribute(Type type, string valueType)
    {
        this.ConverterType = type;
        this.ValueType = valueType;
    }
    public JsonConverterAttribute(Type type)
    {
        this.ConverterType = type;

    }

}

public abstract class Converter
{
    protected internal abstract IDictionary<string, object> BuildJsonDictionary(object value);

    public abstract string SerializeToJson(object inputObject);
}
public class StringEnumConverter : Converter
{
    protected internal override IDictionary<string, object> BuildJsonDictionary(object value)
    {

        IDictionary<string, object> jsonDictionary = new Dictionary<string, object>();
        Type objectType = value.GetType();

        FieldInfo member = objectType.GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);

        var attrList = member.GetCustomAttributes(typeof(EnumMemberAttribute), true);
        var enumMember = attrList.OfType<EnumMemberAttribute>().FirstOrDefault();

        string val = enumMember != null ? enumMember.Value : value.ToString();

        jsonDictionary.Add(value.GetType().Name, val);

        return jsonDictionary;
    }


    public override string SerializeToJson(object inputObject)
    {
        var attrList = inputObject.GetType().GetCustomAttributes(false);
        var listAttr = attrList.ToList();
        FlagsAttribute flagAttr = attrList.Count() != 0 ? (FlagsAttribute)listAttr.Find(item => item.GetType() == typeof(FlagsAttribute)) : null;
        bool flag = (flagAttr != null) ? true : false;
        if (flag)
        {
            int value = (int)inputObject;
            return value.ToString();
        }
        else
        {
            IDictionary<string, object> enumDictionary = BuildJsonDictionary(inputObject);
            object enumValue = enumDictionary.First().Value;
            string enumstring = "\"" + enumValue.ToString() + "\"";
            return enumstring;
        }
    }
}

[Serializable]
public class TestClass
{
    private EditingType _editingType = EditingType.StringEdit;

    [JsonProperty("editType")]
    [DefaultValue(EditingType.StringEdit)]
    [JsonConverter(typeof(StringEnumConverter))]
    public EditingType EditType
    {
        get { return _editingType; }
        set { _editingType = value; }
    }
}
  

下面的反序列化功能将在最新的NewtonSoft.json(11.X版本)中引发错误

public class HomeController : Controller
{
    public IActionResult Index()
    {
       // This function will throws the error
        JsonConvert.DeserializeObject("{\"editType\":\"string\"}", typeof(TestClass));
        return View();
    }
}

谢谢..

1 个答案:

答案 0 :(得分:0)

  

我调查了这,这是预期的行为。现在,Json.NET始终关心EnumMember,并且您拥有重复的值。

     

如果要自定义其序列化,请实施JsonConverter。我不知道您要如何处理样本中的转换类,但Json.NET不在乎它们。

Quoting Json.NET's author来自@dbc的GitHub链接