NewtonSoft.Json Serialize and Deserialize class with property of type IEnumerable

You don’t need to use JsonConverterAttribute, just keep your model clean and use CustomCreationConverter instead, the code is simpler:

public class SampleConverter : CustomCreationConverter<ISample>
{
    public override ISample Create(Type objectType)
    {
        return new Sample();
    }
}

Then:

var sz = JsonConvert.SerializeObject( sampleGroupInstance );
JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter());

Documentation: Deserialize with CustomCreationConverter

Leave a Comment

tech