neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

Create single arity generic type formatter from JsonFormatterAttribute #130

Closed russcam closed 5 years ago

russcam commented 5 years ago

This PR allows the construction of a closed generic type IJsonFormatter<T> from a JsonFormatterAttribute on a property of a generic type, where the property type is a generic parameter, and the IJsonFormatter<T> is an open generic type.

Consider

public class Foo<T>
{
    public T Bar { get; set; }
}

We may wish to define an IJsonFormatter<T> for property Bar. This commit allows the following

public class Foo<T>
{
    [JsonFormatter(typeof(BarFormatter<>)]
    public T Bar { get; set; }
}

without needing to introduce a formatter for Foo<T>