Newtonsoft.Json 序列化器的重写

Posted 大雄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Newtonsoft.Json 序列化器的重写相关的知识,希望对你有一定的参考价值。

 public class TestConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            //Type valueType = value.GetType();
            //if (valueType.IsArray)
            //{
            //    var jArray = new JArray();
            //    foreach (var item in (IEnumerable)value)
            //        jArray.Add(JToken.FromObject(item, serializer));

            //    jArray.WriteTo(writer);
            //}
            //else
            //{
            //    JProperty typeHintProperty = TypeHintPropertyForType(valueType);

            //    var jObj = new JObject();
            //    if (typeHintProperty != null)
            //        jObj.Add(typeHintProperty);

            //    foreach (PropertyInfo property in valueType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            //    {
            //        if (property.CanRead)
            //        {
            //            object propertyValue = property.GetValue(value, null);
            //            if (propertyValue != null)
            //                jObj.Add(property.Name, JToken.FromObject(propertyValue, serializer));
            //        }
            //    }
            //    jObj.WriteTo(writer);
            //}
        }
        public override bool CanWrite => false;

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            // Load JObject from stream
            JObject jObject = JObject.Load(reader);
            Type targetType;
            JToken typeStr;
            //JToken varl;
            if (!jObject.TryGetValue("_t", out typeStr))
            {
                targetType = objectType;
            }
            else
            {
                var scrName = typeStr.ToString();
                if (scrName.Contains("."))
                {
                    scrName = scrName.Substring(scrName.LastIndexOf(".", StringComparison.Ordinal)+1);
                }
                targetType = PropertyHintTypeForStr(scrName);
            }
            // Displaying output. 
            var target = Activator.CreateInstance(targetType);
            serializer.Populate(jObject.CreateReader(), target);
            return target;
        }


        public override bool CanConvert(Type objectType)
        {
            return typeof(BaseTest).IsAssignableFrom(objectType);
        }
        //public override bool CanRead=>false;

        private Type PropertyHintTypeForStr(string typeStr)
        {
            Type result;
            switch (typeStr)
            {
                case nameof(BaseTest):
                    result = typeof(BaseTest);break;
                case nameof(TestA):
                    result = typeof(TestA); break;
                case nameof(TestB):
                    result = typeof(TestB); break;
                case nameof(TestC):
                    result = typeof(TestC); break;
                case nameof(TestD):
                    result = typeof(TestD); break;
                case nameof(TestE):
                    result = typeof(TestE); break;
                case nameof(TestX):
                    result = typeof(TestX); break;
                default:
                    result = typeof(BaseTest); break;
            }
            return result;
        }

    }

 

以上是关于Newtonsoft.Json 序列化器的重写的主要内容,如果未能解决你的问题,请参考以下文章

初探Newtonsoft.json

Newtonsoft.Json反序列化(Deserialize)报错问题

具有 IEnumerable<ISomeInterface> 类型属性的 NewtonSoft.Json 序列化和反序列化类

Newtonsoft.Json(Json.Net)学习笔记

反序列化时的 Newtonsoft.JSON 空值

Newtonsoft.Json