views:

107

answers:

2

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.

+1  A: 

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.

Pointy
Thanks. I imagined it was a limitation of JavaScript, but I couldn't think of how to word it since I rarely work with any type of data where the accuracy of numbers is critical.The numbers are just ID's representing a unique row in a database table. Why they chose to store them as such huge decimals, I have no idea - the project is inherited.At any rate, your suggestion for strings will work because I indeed don't need to do that math with them. Thanks. :)
WesleyJohnson
+1  A: 

There was a similar question I answered sometime ago - Arbitrary precision in GWT

A more up to date answer is that BigDecimal support looks on track for GWT 2.1

Until then, if you don't need to do calculation with the numbers client side, I recommend passing them around as strings.

Additionally, looking at your example, you could transfer them as strings and maybe use the emulated GWT java.lang.Long.

Last ditch, you can try the svn version of BigDecimal GWT-Math - it shouldn't be all that bad to drop the java files into your jar (It would not need to be compiled, since it's all emul code)

If you go that route, you would still have to pass the numbers as JSON strings, but you could perform meaningful math.

Adam Malter