2

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?

RBeaulieu
  • 23
  • 5
  • I suggest using custom jsonconverter and check if fields exist, see link to post here https://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base –  Mar 07 '18 at 19:39
  • Why not just use a [DTO](https://en.wikipedia.org/wiki/Data_transfer_object)? – dbc Mar 07 '18 at 19:56
  • I don't get to control what I'm receiving (the data is provided in JSON by a vendor), only how its interpreted and stored. – RBeaulieu Mar 07 '18 at 20:08

0 Answers0