frenchie
frenchie

Reputation: 51927

deserializing json object with nested lists

I have an object that contains nested lists and a method to deserialize it from json using custom converters and .net's javascript serializer. Something like this:

public class MyObject{
      int TheID { get; set; }
      public List<MyNestedObject1> ListOfMyNestedObject1 { get; set; }  
      public List<MyNestedObject2> ListOfMyNestedObject2 { get; set; }

      public MyObject ObjectFromJson(string TheObjectInJson) {

        JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
        TheSerializer.RegisterConverters(new JavaScriptConverter[] {
            new MyObjectConvert()
        });
        TheSerializer.RegisterConverters(new JavaScriptConverter[] {
            new MyNestedObject1Convert()
        });
        TheSerializer.RegisterConverters(new JavaScriptConverter[] {
            new MyNestedObject2Convert()
        });
        //if I comment out the registrations of the converters, it works
        //but I need the converters of the nested objects to kick in
        return TheSerializer.Deserialize<MyObject>(TheObjectInJson);       
    }
}

The json converters for the nested objects both look like this:

public class MyNestedObject1Convert : JavaScriptConverter {

  public override IEnumerable<Type> SupportedTypes {
      get { return new Type[] { typeof(MyNestedObject1Convert) };
  }
  public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) 
  { //deserialization logic
    return MyNestedObject1;}
}

And the converter for MyObject looks like this:

public class MyObjectConvert : JavaScriptConverter {

  public override IEnumerable<Type> SupportedTypes { get { return new Type[] { typeof(MyObjectConvert) }; }  

  public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {

    int TheID;
    MyObject TheObject = new MyObject();

    int.TryParse(serializer.ConvertToType<string>(dictionary["TheID"]), out TheID))
    TheObject.ID = TheID;

    return TheObject;
    }
}

Now the calling function that receives the json string and looks to return the c# object looks like this:

MyObject AMyObject = new MyObject();
MyObject TheMyObject = new MyObject();

TheMyObject = AMyObject.ObjectFromJson(JsonString);

When I run this code, the returned object contains TheID but the nested objects are null. I'm registering the converters in the object method but I'm guessing that's not the way to do it. If I remove the registration of the converters, the object DOES contain the nested objects, but then the converters don't kick in.

What do I need to change? Note: I'm not looking to use another library, just to make the native deserializer work.

Thanks for your suggestions.

Upvotes: 0

Views: 2961

Answers (1)

frenchie
frenchie

Reputation: 51927

Ok, so I got it to work. If you're looking to deserialize nested lists, this is how you do it.

First, don't register the converters in the MyObject ObjectFromJson method.

Second, it's in the custom converter of MyObject that you do the deserialization of the nested lists. Like this:

 public class MyObjectConvert : JavaScriptConverter {

  public override IEnumerable<Type> SupportedTypes { get { return new Type[] { typeof(MyObjectConvert) }; }  

  public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {

    int TheID;
    MyObject TheObject = new MyObject();

    int.TryParse(serializer.ConvertToType<string>(dictionary["TheID"]), out TheID))
    TheObject.ID = TheID;

    if (dictionary.ContainsKey("ListOfMyNestedObject1"))
    {
      serializer.RegisterConverters(new JavaScriptConverter[] { new MyNestedObject1Convert() });
      var TheList = serializer.ConvertToType<List<MyNestedObject1>>(dictionary["ListOfMyNestedObject1"]);
      TheObject.ListOfMyNestedObject1 = TheList
    }

    return TheObject;
    }
}

And voila: json deserialization of nested lists with .net's javascriptserializer using custom javascript converters.

Upvotes: 1

Related Questions