I’m work with GWT and parsing a JSON result from an ASP.NET webservice method that returns a DataTable. I can parse the result into a JSONvalue/JSONObject just fine. The issue I’m having is that one my columns in a DECIMAL(20, 0) and the values that are getting parsed into JSON aren’t exact. To demonstrate w/o the need for a WS call, in GWT I threw this together:
String jsonString = "{value:4768428229311981600}";
JSONObject jsonObject = JSONParser.parse( jsonString ).isObject();
Window.alert( jsonObject.toString() );
This in turn alerts:
{"value":4768428229311982000}
I’m under the understanding that GWT’s JSONParser is just using eval() to do the parsing, so is this some sort of number/precision issue with JavaScript that I’ve never been aware of. I’ll admit I don’t work with numbers that much in JavaScript and I might be able to work around this by changing the .NET WebService to return this column as string, but I’d really rather not do that.
Thanks for any help.
Well, Javascript just uses ordinary IEEE 754 64-bit floating point, so there’s an inherent precision limit. The language does not provide support for arbitrary-sized integers (or, really, any pure integer at all). You’re going to have to use a string representation when you need to manipulate the values in Javascript, and hopefully you won’t have to do any math.
edit: I’ve looked before at this: http://www-cs-students.stanford.edu/~tjw/jsbn/
It seems like a fairly hairy solution if you don’t need to do much manipulating of the numbers, but it might be worth looking at. There may be less ambitious variations on that idea out there.
In any case, that’s not going to help you with straight interpretation of JSON unless you also wired up a variant JSON parser to construct numeric values using such a library.