0

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?

  • Could you clarify exactly what you need? Are you just applying a default `[JsonObject]`, or are you looking to set some of the [properties](https://www.newtonsoft.com/json/help/html/Properties_T_Newtonsoft_Json_JsonObjectAttribute.htm) of `JsonObjectAttribute` such as `[JsonObject(MemberSerialization = MemberSerialization.Fields)]`? – dbc Oct 02 '19 at 16:29
  • For instance if you need `[JsonObject(MemberSerialization = MemberSerialization.Fields)]`, this might be related: [C# Serialize with JSON.NET inherited private fields](https://stackoverflow.com/a/41171607/3744182) (though the requirement there is more complicated than yours). – dbc Oct 02 '19 at 16:33
  • @dbc: What I want is to be able to serialise / deserialise an object which implements `IList` as an `Object` not as an `IEnumerable`, without writing my own `JsonConverter`. There must already be a converter which has this behaviour in `Json.Net`, and is the one which `JsonObjectAttribute` must force `Json.Net` to use – LightningRider Oct 03 '19 at 23:21
  • No, there's no converter. [`DefaultContractResolver.CreateContract()`](https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs#L1189) notices the presence of the `JsonObjectAttribute` and calls `CreateObjectContract(objectType);`. That's easy to do with a custom contract resolver. Harder would be to emulate the functioning of all the [properties](https://www.newtonsoft.com/json/help/html/Properties_T_Newtonsoft_Json_JsonObjectAttribute.htm) of `JsonObjectAttribute`. So, do you need them? Or do you just need `[JsonObject]`. – dbc Oct 03 '19 at 23:27
  • @dbc `[JsonObject]` will suffice. All that is required then, is to `override` `CreateContract` and return `CreateObjectContract`? – LightningRider Oct 07 '19 at 22:18

0 Answers0