Radai
Radai

Reputation: 240

List<T> inside serializable object not deserializing correctly in C#

I have this object called MyObject in C# and I've been trying to make it so that we can take this object, serialize it to JSON, use that JSON to populate other lists in JavaScript, etc, and then use the resulting changed JSON string to be fed back into the MyObject object and change its values. However, whenever I call Json() on this object, it gives me correctly serialized JSON, but when I feed that exact JSON back into the UpdateFromJson(string json) method, it claims that the List is of count=0 and I get none of my List objects in the new object. The other properties populate just fine; the only problem is with the List.

Here is the (dumbed down) class without the properties but with the relevant methods and List of objects. It should be noted that the List contains objects that I defined elsewhere in the namespace, i.e. they're not in the C# framework anywhere, they're custom.

[Serializable]
public class MyObject
{
  public MyObject()
  {
    this.Elements = new List<MyObjectElement>();
  }

  public List<MyObjectElement> Elements { get; private set; }

  public string Json()
  {
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Serialize(this);
  }

  public void UpdateFromJson(string json)
  {
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new JavaScriptSerializer();
    MyObject temp = serializer.Deserialize<MyObject>(json);

    foreach (PropertyInfo property in temp.GetType().GetProperties())
    {
      object value = property.GetValue(temp, null);
      if (value != null)
      {
        if (value.GetType() == typeof (List<MyObjectElement>))
        {
          foreach(MyObjectElement le in (List<MyObjectElement>)value)
          {
            this.Elements.Add(le);
          }
        }
      }
      property.SetValue(this, value, null);
    }
  }
}

Like I said, the only issue I'm having is, for example when serializer.Deserialize<MyObject>(json) happens, the List has nothing in it when, in fact, the actual JSON string (I've had it printed to a browser and have tested it in JSONLint) contains data for Elements. It just seems that Deserialize(json) is not able to retrieve the List in a format it can parse, or something, I'm not sure.

Upvotes: 0

Views: 3162

Answers (2)

Radai
Radai

Reputation: 240

As Ralf had suggested, I had the setter for my internal List as private. I changed that and it triggered a bunch of refactoring. What ended up happening is that I chose to have explicit Lists for each Type that extended MyObjectElement so that when serializing to JSON it can deserialize without loss of precision.

Upvotes: 2

Haney
Haney

Reputation: 34852

I'd guess that the equality check for typeof(List<MyObjectElement>) is failing. Possibly the type that is being deserialized is array or IList or otherwise. Can you not just use the object that is deserialized in place of this object and map it outside of the object? Why map it within itself?

Upvotes: 1

Related Questions