rpgmaker / NetJSON

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

dictionary wrong key #193

Closed cubed-it closed 6 years ago

cubed-it commented 6 years ago

Hey there,

I would like to deserialize a Dictionary. e.g. I serialize a key of 666, but netjson returns a random number e.g. 75082016 (it is every cycle a different key). Whats' going on here?

void Main()
{
    var xy = new A();
    xy.Details.Add(666, null);

    var json = NetJSON.NetJSON.Serialize(typeof(A), xy);
    var obj = NetJSON.NetJSON.Deserialize<A>(json);

    obj.Dump();
}

public class A
{
    public A()
    {
        Details = new Dictionary<int, B>();
    }

    public Dictionary<int, B> Details { get; set; }
}

public class B
{
}
rpgmaker commented 6 years ago

Thanks for the finding. Can you post your object class structure?

Thanks,

cubed-it commented 6 years ago

I have given the complete example above. It runs like this on LinqPad.

rpgmaker commented 6 years ago

Sorry, I did not see it initially... thanks

rpgmaker commented 6 years ago

What happens if you don't set A to null and use a specific instance?

cubed-it commented 6 years ago

It should be type B, instead of type A. I changed the example. Any way, it makes no difference whether it is a null or a concrete object. The concrete object is deserialized correct, just the key is a pure random value.

cubed-it commented 6 years ago

If I use a int as key and value, the value is deserialized correctly.

void Main()
{
    var xy = new A();
    xy.Details.Add(666, 777);

    var json = NetJSON.NetJSON.Serialize(typeof(A), xy);
    var obj = NetJSON.NetJSON.Deserialize<A>(json);

    obj.Dump();
}

public class A
{
    public A()
    {
        Details = new Dictionary<int, int>();
    }

    public Dictionary<int, int> Details { get; set; }
}

=> Key 74613460 Value 777

rpgmaker commented 6 years ago

I see. I have never noticed the behavior before. What version are you using? The random value you are getting is the address at runtime that the int value points to before it is set. It looks like the key is not been read from the json even though it is supposed to be.

I will look into it tonight. Thanks for the details so far.

cubed-it commented 6 years ago

So far I used version 1.2.2. However, with version 1.2.3 I see no difference. I just replaced the key with a own object with an int id. Now I get a constant wrong result ;-) So e.g. key of new B() { Id = 666 } returns always B.Id with 11. ;-)

Would be great if this could be solved. However, thanks for the quick response!

rpgmaker commented 6 years ago

Thank you so much for this find. I broke it and did not notice it, but now i have a basic test for it.

rpgmaker commented 6 years ago

I also went ahead and publish the latest version.

https://www.nuget.org/packages/NetJSON/1.2.4

cubed-it commented 6 years ago

Great! That was fast. Thank you very much!