I am trying to switch over to System.Text.Json and I had some converters that don't seem to be portion over very well. My classes are all based on "pure" interfaces so I have a lot of List and I'm running into a brick wall trying to resolve it. With Json.Net I had a class that looked like this that I could decorate the properties with to do the conversion.
public class ConcreteListConverter<TInterface,TConcrete> : JsonConverter where TInterface : class
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson([NotNull] JsonReader reader, Type objectType, object existingValue, [NotNull] JsonSerializer serializer)
{
var results = serializer.Deserialize<List<TConcrete>>(reader);
return results.Select(item => item as TInterface).ToList();
}
public override void WriteJson([NotNull] JsonWriter writer, [NotNull] object value, [NotNull] JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}