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

Support for custom non-generic class implementing IDictionary<TKey,TValue> #207

Open TheRSP opened 4 years ago

TheRSP commented 4 years ago

Currently, Utf8Json supports serialising to and from classes implementing System.Collections.Generic.IDictionary<,> if and only if that class has two generic type arguments. I ask whether that restriction could be lifted?

Given the class

class MyDictionary: IDictionary<string, object>{
  // ...
}

The following test should pass

// using FluentAssertions
[Test]
public void Can_deserialise_with_Utf8Json()
{
    var Subject = new MyDictionary();
    Subject.Add("a", "alpha");
    Subject.Add("b", 2);
    Subject.Add("c", new object());

    var serialised = Utf8Json.JsonSerializer.Serialize(Subject);
    var deserialised = Utf8Json.JsonSerializer.Deserialize<MyDictionary>(serialised);

    deserialised.Should().BeEquivalentTo(Subject);
}

Currently, I can only achieve this if I also implement System.Collections.IDictionary