2

I have a problem with a piece of JSON containing a float number of 0000000000000000E+00 (essentially zero). Consider, for example:

{
    "a": 3199999999999999E+01,
    "b": 0000000000000000E+00,
    "c": 0,
    "d": 5
}

The zero floating point number gives an error under the following circumstances:

  • Running it through JSONLint (http://jsonlint.com/)
  • Evaluating it in the browser on Windows with jQuery.parseJSON (IE and Firefox, various versions of jQuery

Changing the zero floating point number to any non-zero value gives no hassles.

Look also at the following JSFiddle: http://jsfiddle.net/Gr6fq/. When I run this in Linux, it works. On Windows, it gives an error.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95

1 Answers1

6

It looks like the Javascript parser interprets the leftmost leading zero as the octal modifier, and proceeds to parse the current token as an octal number. It then chokes on the E token it encounters afterwards.

Using Firefox 5.0's console:

0E+00   // Okay, parsed as 0.
00E+00  // Syntax error, identifier starts immediately after numeric literal.
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • It is a syntax error, different browsers seem to report a different error so I guess it just depends on how they are tokenising and parsing the code as to which error they throw. – RobG Jul 15 '11 at 11:33
  • Thanks Frederic for that observation. Now as to how this relates to my JSON parsing question: When I try evaluate either of these through jQuery's parseJSON method, it evaluates without a problem on Linux, though in Windows, it gives an error. JSONLint treats them the same way you describe here. (I guess I should be more specific and ask how to get around this. I am getting the values from an SAP system, so I have limited control over them). – mydoghasworms Jul 15 '11 at 11:37
  • @mydoghasworms, how are you generating the JSON expression in your server-side code? You're not fetching the string directly from SAP, right? :) – Frédéric Hamidi Jul 15 '11 at 11:39
  • It is an ABAP program that is generating the JSON. It specifically does not surround numbers (incl. floating point) with double quotes, so that it can be interpreted as a number by the browser. Admittedly, it is my own program, and I could theoretically return it with double quotes, but I want to avoid having to additionally parse the string value as a floating point number to convert it back again. – mydoghasworms Jul 15 '11 at 11:53
  • @mydoghasworms, you don't need to pass strings, only to get rid of the leading zeroes. Format the numbers as `42E+01` or `0E+00` instead of `0000000000000042E+01` or `0000000000000000E+00` and you should be fine :) – Frédéric Hamidi Jul 15 '11 at 12:02
  • Thanks Frédéric. You have put me on the right path with your last comment. I went back to the ABAP program and discovered a subtle bug, whereby, writing out the floating point value was using the current user settings, in which decimal points were outputting out as commas, thereby producing an extra value in the JSON array it was producing. (I hadn't even noticed this). The result was that the next number was comprised of the many zeros before the exponent. Many thanks for your help! – mydoghasworms Jul 15 '11 at 12:31