2
votes

I try to parse a JSON structure with JSON.NET. All properties of the first level is deserializing correctly.

I have a problem to deserialize a Dictionary. The object type can be anything: string, double, int, bool, List, Dictionary and recursively.

Example JSON:

 {
     "id":"56d4f8sd4f86ds4f",
     "name":"Azerty",
     "dynProp": {
         "xp":100,
         "life":100,
         "bonus": {
             "force": 100,
             "defense": 100
         },
         "ennemies": {
             "beast": {
                 "wolf": 100,
                 "bear": 20
             }
         }
     }
 }

All datas in the dynProp node is dynamic and I can't know the object type, the depth or anything.

The ouput for the dynProp object is something like that:

{{"dynProp","{xp:100, life: 100, bonus: {...}, ...}"}}

In fact, JSON.NET put in the object value the jobject and not at all inners dictionaries or stuff like that.

I try to add Serialization Settings like that:

new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
    }

But with no success...

Thanks in advance for your help!

Edit: I'm open to another framework but it must work on .net 2.0.

1

1 Answers

0
votes

I know this question is explicitly about JSON.NET but in the hopes that you don't know about the awesome ServiceStack library, I'd like to add a recommendation that you move to that instead. Specifically, the ServiceStack.Text namespace has a JSON Serializer that's much faster than JSON.NET's serializer.

In addition, they have a JSONObject.Parse method that handles your use case better (I believe). See this stack overflow post which pretty much matches what you are looking for:

ServiceStack.Text.JsonObject.Parse vs. NewtonSoft.Json.Linq.JObject.Parse for nested tree of 'dynamic' instances?