【发布时间】:2016-12-05 23:59:29
【问题描述】:
我有一个包含多个嵌套类型的数据结构。
public class MyDataType {
public int InferrableInt { get; set; }
public float InferrableFloat { get; set; }
public InferrableNestedDataType MyINDT { get; set; }
public CustomNestedDataType MyCNDT { get; set; }
public class InferrableNestedDataType {
public int X { get; set; }
public int Y { get; set; }
}
public class CustomNestedDataType {
// Data processed in a way that can't be expressed through JSON
public string CustomData { get; set; }
public int NestedInferrableInt { get; set; }
}
}
这些嵌套类型中的大多数都可以通过默认行为推断出来。
我还收到了来自另一部分代码的JObject。我希望能够:
var mydt = jobjectInstance.ToObject<MyDataType>()
...并且仅在反序列化 MyDataType.CustomNestedDataType 时运行自定义代码。我发现的每个解决方案都扩展了JsonConverter,但据我所知,这仅在从原始 JSON 解析时有效,而不是预解析的 JObject。不为可推断的数据类型编写自定义代码的正确方法是什么?
【问题讨论】:
标签: c# json linq json.net json-deserialization