内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/22537233/json-net-how-to-deserialize-interface-property-based-on-parent-holder-object
我有这样的课
class Holder { public int ObjType { get; set; } public List<Base> Objects { get; set; } } abstract class Base { // ... doesn't matter } class DerivedType1 : Base { // ... doesn't matter } class DerivedType2 : Base { // ... doesn't matter }
使用WebAPI我想要接收对象持有者并正确反序列化它.基于ObjType值,我需要将Objects属性反序列化为List<DerivedType1> (ObjType == 1)或List<DerivedType2> (ObjType == 2).
目前我搜索了SO和互联网的最佳方法,但是我发现最好的是这个答案 http://stackoverflow.com/a/8031283/1038496 .这个解决方案的问题是,它松动父对象的上下文,所以我找不到ObjType的值.好的,我可以通过为Holder创建自定义的JsonConverter并重写ObjType值来解决这个问题,但是我仍然害怕这一行
serializer.Populate(jObject.CreateReader(), target);
正如这个答案所说的那样
The new JsonReader created in the ReadJson method does not inherit any of the original reader’s configuration values (Culture, DateParseHandling, DateTimeZoneHandling, FloatParseHandling, etc…). These values should be copied over before using the new JsonReader in serializer.Populate().
这对我来说是个问题,我自己复制这些价值观对我来说看起来并不干净(如果我想念某件事情呢?)
所以问题是:有没有更好的方法我错过了反序列化抽象对象属性基于父属性值?
你在正确的轨道上.您确实需要为您的Holder类实现一个自定义JsonConverter来处理这种情况,就像您所建议的那样.但是,不要担心,可以以这样的方式编写转换器,即可以使用传递给转换器的原始读取器和串行器实例,而无需将设置复制到新的实例.这是我如何写:
class HolderConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(Holder)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); Holder holder = new Holder(); holder.ObjType = (int)jo["ObjType"]; holder.Objects = new List<Base>(); foreach (JObject obj in jo["Objects"]) { if (holder.ObjType == 1) holder.Objects.Add(obj.ToObject<DerivedType1>(serializer)); else holder.Objects.Add(obj.ToObject<DerivedType2>(serializer)); } return holder; } public override bool CanWrite { get { return false; } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } }
这是一个快速演示:
class Program { static void Main(string[] args) { string json = @" [ { ""ObjType"" : 1, ""Objects"" : [ { ""Id"" : 1, ""Foo"" : ""One"" }, { ""Id"" : 2, ""Foo"" : ""Two"" }, ] }, { ""ObjType"" : 2, ""Objects"" : [ { ""Id"" : 3, ""Bar"" : ""Three"" }, { ""Id"" : 4, ""Bar"" : ""Four"" }, ] }, ]"; List<Holder> list = JsonConvert.DeserializeObject<List<Holder>>(json); foreach (Holder holder in list) { if (holder.ObjType == 1) { foreach (DerivedType1 obj in holder.Objects) { Console.WriteLine("Id: " + obj.Id + " Foo: " + obj.Foo); } } else { foreach (DerivedType2 obj in holder.Objects) { Console.WriteLine("Id: " + obj.Id + " Bar: " + obj.Bar); } } } } } [JsonConverter(typeof(HolderConverter))] class Holder { public int ObjType { get; set; } public List<Base> Objects { get; set; } } abstract class Base { public int Id { get; set; } } class DerivedType1 : Base { public string Foo { get; set; } } class DerivedType2 : Base { public string Bar { get; set; } }
输出:
Id: 1 Foo: One Id: 2 Foo: Two Id: 3 Bar: Three Id: 4 Bar: Four
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/22537233/json-net-how-to-deserialize-interface-property-based-on-parent-holder-object
以上所述就是小编给大家介绍的《c# – JSON.NET:如何反序列化基于parent(holder)对象值的接口属性?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。