0

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.

7
  • Have you done anything by yourself for your custom converter without asking in SO? Commented Dec 16, 2011 at 20:58
  • @L.B: the questions are meant to be answers for others who'll need them too. That's what makes the SO concept great. Commented Dec 16, 2011 at 21:10
  • the questions are meant to be answers for others who'll need them too. :) Commented Dec 16, 2011 at 21:12
  • Why don't you use this: json2csharp.com to generate your classes? Commented Dec 16, 2011 at 22:22
  • @SamFisher83: I'm using JavascriptSerializer and it all works in the sense that if I comment out the registration of the converters, I do get the nested classes generated. I just need to figure out how to properly register the converters and I'm done. Native JavascriptSerializer works just fine. Commented Dec 16, 2011 at 22:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.