0

I searched first, and basically I am trying to avoid having to use JObject in C# in favor of ANY Serializer. An excerpt of a large Microsoft Dynamics JSON trace has JSON formed like this

{
"InBlockOStuff" :
{
"type":"Yea You Know What this is",
 "values":[
    {
       "Key":"Target",
       "Value":{
          "type":"Yea You Know What this is",
          "LogicalName":"Thangie",
          "Id":"GuidAs String",
          "MagicalContent":{
             "type":"Yea You Know What this is",
             "values":[
                {
                   "Key":"firstname",
                   "Value":"New Test thangie One"
                },
                {
                   "Key":"Thangieid",
                   "Value":"some stupid Guid like thangie"
                }
             ]
          },
          "Shaggy":null,
          "SubCategory":{
             "type":"Yea You Know What this is",
             "values":[  ]
          },
          "Velma":{
             "type":"Yea You Know What this is",
             "values":[  ]
          },
          "Scoobie":null,
          "KeyAttributes":{
             "type":"Yea You Know What this is",
             "values":[  ]
          }
       }
    },
    {
       "Key":"some text",
       "Value":true
    },
    {
       "Key":"some text",
       "Value":0
    }
 ]
}
}

Notice the repeated instances of the "keys" value and values It is valid Json, the online validators say it is legit, the online generators will additionally create C# classes/objects but I'm sure you instantly see this mess will have two distinctly different "Values" classes. And that doesn't serialize cleanly.

So am I stuck having to learn JObject to get my data out of this mess?

This is just high level excerpt there exists, for example another MagicalContent key with a different set of key values inside of it, just for arguments sake.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Your "values" seems to be the same type every time: an array of objects containing (Key (string) and Value (object)). You can deserialize the whole thing like that, and then cast your Value objects to what they should be based on the Key (if that's the determiner). You can probably instruct a good parser to do that for you (I use Newton, and I know you can fine-tune its parsing quite extensively) – oerkelens Aug 17 '20 at 17:13
  • What are the values of `type`? I'm assuming it's not really "Yea You Know What this is" for every single item. Does that provide any clue about the structure? If so then a `JsonConverter` could be written to help decipher the JSON into a more coherent class structure. Otherwise, you could use [this method](https://stackoverflow.com/a/19140420/10263) which temporarily uses JObjects to convert arbitrary JSON into a nested hierarchy of standard .NET Dictionaries and Lists. Would that be more you your taste? – Brian Rogers Aug 17 '20 at 17:29
  • Thanks for the replies #1) oerkelens perhaps you overlooked my comment regarding repeated key names representing nested objects that are actually NOT the same object despite MSFT's lovely implementation of just tossing in the same generic name. #2) Brian yea type is another waste bloat from MSFT its pointless in this context – ChristianProgrammer Aug 17 '20 at 20:27
  • I think this looks perfectly normal. You build your models around the data. Just because the word "values" are used multiple places and has different meanings, doesn't mean you can't build multiple value models. 10 times out of 10 I would pick binding to a model than using `JToken` or any other pattern that would tightly-couple to a specific library. The one thing that *does* annoy me are models that don't stick to one naming convention... like, use `camelCase` or don't. annoying. – Andy Aug 24 '20 at 00:07
  • @Andy Duplicate $values classes are generated breaking serializers I don't follow. If you throw this JSON into an online converter you get like two or three ( full JSON not shown) classes named "$values" that doesn't automatically serialize ...please advise ... short of manually renaming every read into Values1 Values2 etc to match the classes... I don't see how this can be easily done – ChristianProgrammer Sep 01 '20 at 13:06

0 Answers0