- Home /
JsonUtility spitting out weird values for unsigned long integers
I'm having some issues with JsonUtility.FromJson. For some reason, whenever I try to parse an unsigned long int with a value of '0', it spits out some value around 89 million.
This is what I'm putting into JsonUtility:
{"charId":"2","entityName":"wod","entityLevel":"1","path":"0","sex":"female","exp":"0","zenny":"0","maxVita":"1","maxMana":"1","might":"1","will":"1","grace":"1","grit":"1","zeal":"1","sleight":"1","statPoints":"0","karma":"0","nation":"0"}
And this is what I get out:
All of the values which are showing up as 88.95 million are supposed to be zero. But, unlike the other zeros in the raw text, these zeros are being stuck into ulongs. So I'm assuming that JsonUtility just hates ulongs, which isn't awesome, but what can I do to work around this? Or am I making some error?
After switching to Newtonsoft.Json.Net, everything worked fine. So I guess this is just a bug with Unity's JsonUtility.
Answer by beStrange · Mar 30, 2017 at 04:15 PM
Unless you exceed the 9,223,372,036,854,775,807 limit of singed long, you should prevent using unsigned types where ever it is possible, even if you are only dealing with positive numbers. Otherwise you might introduce weird issues as you encountered here.
Also CLS (Common Language Specification) is a thing you should keep in mind, especially if you want to compile to other platforms, that don't support unsigned types.
See MSDN Language Independence and Language-Independent Components for further information on that topic.
That said, please do still file a bug report on this, as it should be handled better
But first he might want to try using actual "numbers" inside his JSON representation. Currently all values are stored as "strings".
I mean it should look like this:
{
"charId": 2,
"entityName": "wod",
"entityLevel": 1,
"path": 0,
"sex": "female",
"exp": 0,
"zenny": 0,
"maxVita": 1,
"max$$anonymous$$ana": 1,
"might": 1,
"will": 1,
"grace": 1,
"grit": 1,
"zeal": 1,
"sleight": 1,
"statPoints": 0,
"karma": 0,
"nation": 0
}
Since the three values we can see in the screenshot are all slightly different, it seems unlikely that they are returned by the JSONUtility.
Finally keep in $$anonymous$$d that the JSON format stores / expects numbers as double precision floating point values. So whenever your value gets "too large" it will be represented in scientific notation. Of course you could store a number as string, but in this case you can't rely on the automatic conversion as again, JSON uses double for numbers. You would need to read the value as actual string and parse it yourself as ulong.
Answer by truehatch · Apr 02, 2017 at 10:01 AM
You're right, Bunny83, those strings shouldn't be strings. I must have been using PHP's json_encode method wrongly, forcing it to stringify all my integers -- then turning around and asking JsonUtility to unstrigify them. No wonder it got confused
Your answer

Follow this Question
Related Questions
Best Way to Store Large Number of GameObjects? 1 Answer
unity 3d loading data from json problem?! 1 Answer
MiniJSON Multiple Rows 1 Answer
JsonUtility not found / working 0 Answers
JsonUtility.fromJson won't work with german Umlauts ä ü ö 1 Answer