I am trying to serialise/deserialise a class from another package which implements IEnumerable, but also contains other properties which I would like included in the serialisation stream.
public class MyList<T> : IList<T>
{
private List<T> _list;
public Foo Bar { get; set; }
// IList implementation ...
}
If I apply the [JsonObject]
attribute to the class, and use a custom ContractResolver
to serialise the private fields all works fine. As I stated before the class belongs to another package, so I am unable to apply the attribute to the class.
I noticed the following code in the documentation.
public class ConverterContractResolver : DefaultContractResolver
{
public new static readonly ConverterContractResolver Instance = new ConverterContractResolver();
protected override JsonContract CreateContract(Type objectType)
{
JsonContract contract = base.CreateContract(objectType);
// this will only be called once and then cached
if (objectType == typeof(DateTime) || objectType == typeof(DateTimeOffset))
{
contract.Converter = new JavaScriptDateTimeConverter();
}
return contract;
}
}
As I am already using a custom ContractResolver
to serialise/deserialise private fields, am I able to simply re-write the if
clause to use the same Converter
which is used when you apply the JsonObjectAttribute
?