Deserializing JSON child object values into parent object using JsonConvert.DeserializeObject<T> using nested converters

眉间皱痕 提交于 2019-12-11 09:53:04

问题


I'm receiving some oddly formatted JSON that I want to store in a single object. It looks something like this:

{
    "parentObject": 
    {
        "id": 123456,
        "pointlessChildObject": 
        {
            "stringThatCouldBeStoredInParent": "test",
            "epochTimeThatCouldBeStoredInParent": "1520452800"
        }
    }
}

At first, I thought this would be simple thanks to a solution by Brian Rogers that I found: Can I specify a path in an attribute to map a property in my class to a child property in my JSON?

However, if you implement this solution, you can't use another converter for converting those values into something else at the same time (A nested [JsonConverter(typeof(SecondsEpochConverter))] for the epoch time for example). And of course, because this is C#, you can't have a set that's different than your type (convert int to DateTime in set). I'm trying to add code to Brian's solution such that it calls the nested converter, but now we're getting into ridiculous territory; I'm creating a single use JSONTextReader just to have the value passed to the nested converters, and I'm pretty sure I'm overthinking this.

Does anyone know of a better solution?

来源:https://stackoverflow.com/questions/49159697/deserializing-json-child-object-values-into-parent-object-using-jsonconvert-dese

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!