首页 > 代码库 > Newtonsoft.Json 序列化器的重写
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 = http://www.mamicode.com/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 序列化器的重写
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。