question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for generic JsonConverter instantiation

See original GitHub issue

Consider the following value wrapper struct and its associated json converter.

[JsonConverter(typeof(ValueConverter<>))]
struct Value<T>
{
    public static implicit operator T(Value<T> value) => value._value;

    public static implicit operator Value<T>(T value) => new Value<T>(value);

    private readonly T _value;

    public Value(T value)
    {
        _value = value;
    }
}

class ValueConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType) => objectType == typeof(Value<T>);

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => new Value<T>(serializer.Deserialize<T>(reader));

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => serializer.Serialize(writer, (T)(Value<T>)value, typeof(T));
}

Running the following code I currently get an ArgumentException of “Invalid type owner for DynamicMethod.”

JsonConvert.SerializeObject(new Value<bool>(true));

Obviously the problem is that the converter type provided with the JsonConverterAttribute is an open generic type and Json.NET doesn’t know how to instantiate it. The provided converter type is open because currently C# doesn’t allow use of a types’ type parameters in its attributes.

When an open generic converter type is provided I would like Json.NET to implicitly use the generic type arguments of the type the attribute is applied to using the MakeGenericType method on Type in order to instantiate the json converter.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
thomaslevesquecommented, Apr 25, 2018

I just found a case where I needed this too. Basically, this can be useful any time you want to customize the serialization of a generic type.

3reactions
mchandschuhcommented, Mar 11, 2018

I would also find use in this feature. Thanks for providing the custom contract resolver work around.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Json.Net specify custom converter of a property of a ...
I know I can instantiate a generic converter and pass it with JsonSerializerSettings when calling JsonConvert.
Read more >
How to write custom converters for JSON serialization - .NET
Create a class that derives from JsonConverter<T> where T is the type to be serialized and deserialized. Override the Read method to deserialize ......
Read more >
JSON IList Covariance - Erik the <Coder>
When the JSON text is de-serialized, a type name (if present) indicates which class should be instantiated for the object it annotates. A...
Read more >
How to dynamically load classes to instantiate game things ...
Hi, I am trying to figure out how to dynamically load classes and JSON metadata from the StreamingAssets folder. I can read the...
Read more >
Using System.Text.Json to do polymorphic Json conversion in ...
So, inspired by this post I made a generic approach that can easily be added to the Json Converters: var options = new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found