Accessing existing property value from JsonConverter.ReadJson

Feb 28, 2010 at 6:36 PM

Hi,
Im trying to serialize/deserialize an object graph like this:

class ObjA
{
    public int Id;
    public string Descriptopn;
    [JsonConverter(typeof(CustomJsonConverter))]
    ICollection<ObjA> InnerA = new List<ObjA>();
}

What i need is the following JSON
{
    Key: 1,
    Name: "blahblah",
    InnerA: [
        {
            Id: 2,
            Description: "something"
        },
        {
            Id: 3,
            Description: "something else"
        }
    ]
}

So i need to use CustomJsonConverter to control how InnerA property is serialized. The problem is that i want to fill initialized InnerA collection, but now i can only create a new collection and return it from JsonConverter.ReadJson.

Looking at the code i see that it will be very easy to add a parameter to JsonConverter.ReadJson with the current value for the property.

I've prepared a patch for this.

What do you think?

Coordinator
Mar 17, 2010 at 7:29 AM

Done. Turned out to be surprisingly difficult to insure the existing value was always passed to the converter.

http://json.codeplex.com/SourceControl/list/changesets