This project has moved and is read-only. For the latest updates, please go here.

Object schema migration

Feb 22, 2013 at 7:05 AM
I'm using Json.NET for serializing an object graph to a persistent storage and to deserialize it back. With this approach I need to be able to handle changes of the object schema from one version of my software to another version. For example if a previous version of the software had a Name property for the class Person, and the new version have FirstName and LastName, I need to have some way of handling that.

I already concidered using existing JSON.NET extension points like the serialization hooks (OnDeserialized) or JsonConverters for handling schema changes, but they don't fit well. I decided to add my own extension point to JSON.NET. With my extension one can register a deserialization listener for a specific type:

interface IDeserializationListener {
void OnDeserialized(object deserializedObject, JToken jToken);
bool ListensTo(Type objectType);

The deserialization listener is informed with the deserialized object and the serialized JSON. Using this approach, I can easily convert between two versions of the software.

I did a short spike on this and found a good place to call the registered listeners (method OnDeserialized() in JsonSerializerInternalReader). But I have problems to create the JToken object. The JsonReader is forward only and at that point the partial JSON that I want to pass to the listener is no more accessible. I'm thinking about extending the JsonReader with access to the last read JSON. But I first wanted to ask if anybody sees a simpler way to create the JToken at that point in the deserialization sequence.

Any suggestions how I can access the JSON as a JToken during deserialization?