rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
225 stars 29 forks source link

IReadOnlyDictionary Support #207

Closed ghost closed 5 years ago

ghost commented 5 years ago

As with #205 , NetJSON doesn't currently support IReadOnlyDictionary, throwing ArgumentNullException on serialisation.

[TestMethod]
public void HandlesReadOnlyDictionary()
{
    var entity = new EntityWithReadOnlyDictionary { Map = new Dictionary<string, string> { { "One", "Eno" }, {"Two", "Owt"}, { "Three", "Eerht"} } };
    var serialised = NetJSON.NetJSON.Serialize(entity, Settings);
    var deserialised = NetJSON.NetJSON.Deserialize<EntityWithReadOnlyDictionary>(serialised, Settings);

    Assert.AreNotSame(entity.Map, deserialised.Map);
    Assert.AreEqual(entity.Map.Count, deserialised.Map.Count);
    foreach (var item in entity.Map)
    {
        Assert.IsTrue(deserialised.Map.ContainsKey(item.Key));
        Assert.AreEqual(item.Value, deserialised.Map[item.Key]);
    }
}

public class EntityWithReadOnlyDictionary
{
    public IReadOnlyDictionary<string, string> Map { get; set; }
}

There is also IReadOnlyList so perhaps worth adding support at the same time just in case someone else needs this in future.

ghost commented 5 years ago

Apologies, only noticed this as I started to test NetJSON with additional projects. I should have included the additional read-only interfaces in #205.

rpgmaker commented 5 years ago

Np, It is actually very easy to support. If you look at the code changes I made for it in regards to the read only collection. Could you make a pull request for it? If not, then I will take a look it when I get a chance.

Thanks

rpgmaker commented 5 years ago

The nuget package should be visible for download soon. Thanks

ghost commented 5 years ago

Working, thanks!

rpgmaker commented 5 years ago

Np