Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dictionary wrong key #193

Closed
cubed-it opened this issue May 2, 2018 · 11 comments
Closed

dictionary wrong key #193

cubed-it opened this issue May 2, 2018 · 11 comments

Comments

@cubed-it
Copy link

cubed-it commented May 2, 2018

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
Copy link
Owner

rpgmaker commented May 2, 2018

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

Thanks,

@cubed-it
Copy link
Author

cubed-it commented May 2, 2018

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

@rpgmaker
Copy link
Owner

rpgmaker commented May 2, 2018

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

@rpgmaker
Copy link
Owner

rpgmaker commented May 2, 2018

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

@cubed-it
Copy link
Author

cubed-it commented May 2, 2018

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
Copy link
Author

cubed-it commented May 2, 2018

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
Copy link
Owner

rpgmaker commented May 2, 2018

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
Copy link
Author

cubed-it commented May 2, 2018

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
Copy link
Owner

rpgmaker commented May 3, 2018

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
Copy link
Owner

rpgmaker commented May 3, 2018

I also went ahead and publish the latest version.

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

@cubed-it
Copy link
Author

cubed-it commented May 3, 2018

Great! That was fast. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants