A custom JSON.NET converter can do the trick here. It is not that difficult.
For the DateTime property, you can do this as follows. Just format the corresponding property using a custom converter.
[JsonObject(MemberSerialization.OptIn)] public class MyClass { [JsonProperty(PropertyName = "creation_date")] [JsonConverter(typeof(UnixDateTimeConverter))] public DateTime CreationDate { get; set; } }
JSON.NET provides most of the plumbing. Just get from the base converter.
public class UnixDateTimeConverter : DateTimeConverterBase { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { ...} public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { ... } }
All you have to do is implement the ReadJson (deserialization) and WriteJson (serialization) methods.
Here you can find the full example:
Writing a Custom Json.NET DateTime Converter
For your specific problem you need a little more control. Try the following type of converter:
public class Contact { private List<Address> _addresses = new List<Address>(); public IEnumerable<Address> Addresses { get { return _addresses; } } public class ContactConverter : CustomCreationConverter<Contact> { public override Contact Create(Type objectType) { return new Contact(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var mappedObj = new Contact();
Using your own converter, similar to the one above, you can analyze the JSON data yourself and make contact objects, as you wish.
I changed the example I found here:
Custom JSON.NET Converters - Quick View
In this case, you need to pass the custom converter during deserialization.
Contact contact = JsonConvert.DeserializeObject<Contact>(json, new ContactConverter());