0

I have an object that contains lists of other objects and that looks like this:

public class MyObject {

 public int Prop1 { get; set; }
 public List<Object1> TheListOfObject1 { get; set; }
 public List<Object2> TheListOfObject2 { get; set; }
 public string MyObjectInJson { get; set;}

 public void MyObjectToJson()
 {
  JavascriptSerializer TheSerializer = new JavascriptSerializer();
  TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object1ToJson() });
  TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object2ToJson() });
  MyObjectInJson = TheSerializer.Serialize(this);
}

Now I have another class that's getting a json string of MyObject and I need to deserializse the string and create a MyObject from the string.

I know there's JSON.net and other library available but I want to use .net's JavascriptSerializer.

Suppose that the converters I have also handle the deserializtion. Do I simply do something like this:

1) add FromJson method to MyObject

  public this MyObjectFromJson (string MyObjectInJson)
    {
      JavascriptSerializer TheSerializer = new JavascriptSerializer();
      TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object1ToJson() });
      TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object2ToJson() });
      this = TheSerializer.DeSerialize(MyObjectInJson);
    }

2) and in the calling class write this:

MyObject TheObject = new MyObject();
TheObject = TheObject.MyObjectFromJson(TheIncomingString);

I'm not sure how to proceed. Will this kind of approach work?

Thanks.

2
  • No because I haven't done the deserializers yet. I did try to add this line public this MyObjectFromJson (string MyObjectInJson) and it's underlined red. Tu peux m'aider? Commented Dec 10, 2011 at 23:01
  • I don't have experience with JavascriptSerializer, but I have used DataContractJsonSerializer successfully in a couple of projects, and it seems to be easier to use. Commented Dec 10, 2011 at 23:18

1 Answer 1

0

Unless your lists of Object1 and Object2 are incredibly complex, you probably don't need the Converters.

Here is a quick sample that I threw together based on your code. Note that the deserializer is static since you will want it to create a new object and you can't set this because it is readonly.

public class MyObject
{
    public int Prop1 { get; set; }
    public List<Object1> TheListOfObject1 { get; set; }
    public List<Object2> TheListOfObject2 { get; set; }

    public MyObject()
    {
        TheListOfObject1 = new List<Object1>();
        TheListOfObject2 = new List<Object2>();
    }

    public string ToJson()
    {
        JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
        return TheSerializer.Serialize(this);
    }
    public static MyObject FromJson(string sValue)
    {
        JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
        return TheSerializer.Deserialize<MyObject>(sValue);
    }
}

This is then called by

        MyObject oObject = new MyObject();
        oObject.TheListOfObject1.Add(new Object1(1));
        oObject.TheListOfObject2.Add(new Object2(2));
        oObject.Prop1 = 3;

        string sJSON = oObject.ToJson();

        System.Diagnostics.Debug.WriteLine(sJSON);

        oObject = MyObject.FromJson(sJSON);

        System.Diagnostics.Debug.WriteLine(oObject.Prop1);
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, thanks for this quick example. My lists do need the converter because there's some if/then logic to extract.
What would happen if we were to remove the static? Can we created two objects: one to refer to the method and one to deserialize the json? MyObject Object1 = new MyObject(); MyObject Object2 = new MyObject(); Object2 = Object1.FromJson(sJSON)? Would this work?
@frenchie: removing the static is ok, but then you have to create a new object in order to create a new object from the json string, which is a waste of resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.