You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.2 KiB
35 lines
1.2 KiB
using Newtonsoft.Json; |
|
using System; |
|
using System.Reflection; |
|
|
|
namespace Monaco.Helpers |
|
{ |
|
/// <summary> |
|
/// Used to upcast an interface to its object type during deserialization of JSON. |
|
/// </summary> |
|
/// <typeparam name="TInterface">Type of base Interface.</typeparam> |
|
/// <typeparam name="TClass">Type of class to use for deserializing object with interface.</typeparam> |
|
internal class InterfaceToClassConverter<TInterface, TClass> : JsonConverter where TClass : TInterface, new() |
|
{ |
|
public override bool CanConvert(Type objectType) |
|
{ |
|
// We only want to convert objects that are of the interface. |
|
return objectType == typeof(TInterface); |
|
} |
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
|
{ |
|
// Use the implementation type for the deserialization of the interface. |
|
var pop = new TClass(); |
|
|
|
serializer.Populate(reader, pop); |
|
|
|
return pop; |
|
} |
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
|
{ |
|
serializer.Serialize(writer, value); |
|
} |
|
} |
|
}
|
|
|