I am using Json.NET version 4.5, I am very new to Json.Net.
Problem: I need to know how versioning of classes can be supported in Json.NET.
Example: As given in below example I have the EmployeeDetail class in second version, Name property is splits in to FirstName and LastName; and single Address become Addresses.
I tried using custom JsonConverter for providing backward compatibility while de-serializing object, but I faced problem in using multiple converter as I am already using common Custom Creation JsonConverter which maps Interface To ConcreteType as shown in below example.
// Employee Detail Version 1.0
[JsonObject()]
public class EmployeeDetail
{
public EmployeeDetail()
{
}
public EmployeeDetail( string name )
{
Name = name;
}
[JsonProperty]
public string Name { get; set; }
[JsonConverterAttribute( typeof( CustomObjectCreationConverter<iAddress, Address> ) )]
public iAddress Address { get; set; }
}
// Employee Detail Version 2.0
[JsonObject()]
public class EmployeeDetail
{
public EmployeeDetail()
{
}
public EmployeeDetail( string firstName, string lastName )
{
FirstName = firstName;
LastName = lastName;
}
[JsonProperty]
public string FirstName { get; set; }
[JsonProperty]
public string LastName { get; set; }
[JsonConverterAttribute( typeof( CustomArrayCreationConverter<iAddress, Address> ) )]
public IEnumerable<iAddress> Addresses { get; set; }
}
Adding attribute [JsonConverterAttribute] to the property is not a nice option here. Instead of this try setting
JsonSerializerSettingswithTypeNameHandling = TypeNameHandling.ObjectsandTypeNameAssemblyFormat = FormatterAssemblyStyle.Full.So after the serialisation with the setting, the Json string will contain tag like
“Address”:{“$type”:”…
Which you can get it using jObject.Property( “Address” ).Value
Now if the Address object has not been changed then it should serialise it for you otherwise you need to have another converter for this too.
And both these converters you need to pass to the
DeserializeObject.I have not actually tested this code, so do let me know if things are not clear