JDK-8037562 : Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2014-03-18
  • Updated: 2014-07-29
  • Resolved: 2014-03-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u20Fixed 9 b08Fixed
Description
FULL PRODUCT VERSION :
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.9.2
Darwin sethr.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
For JSON with integer keys (that is, strings containing integers), JSON.parse hallucinates nonexistent entries if there are gaps between the keys.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
% jjs
jjs> JSON.stringify(JSON.parse(JSON.stringify({"0":0,"2":2})))
{"0":0,"1":0,"2":2}
jjs> JSON.stringify(JSON.parse(JSON.stringify({"0":"","2":""})))
{"0":"","1":null,"2":""}

Note that the "gap" between 0 and 2 has been filled. The value used depends on the types of the values of the entries.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect the same behavior that V8 and Rhino both give:

V8 version 3.21.17 [sample shell]
> JSON.stringify(JSON.parse(JSON.stringify({"0":0,"2":2})))
{"0":0,"2":2}
> JSON.stringify(JSON.parse(JSON.stringify({"0":"","2":""})))
{"0":"","2":""}

% jrunscript 
js> JSON.stringify(JSON.parse(JSON.stringify({"0":0,"2":2})))
{"0":0,"2":2}
js> JSON.stringify(JSON.parse(JSON.stringify({"0":"","2":""})))
{"0":"","2":""}

ACTUAL -
shown in steps to reproduce

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
I'm walking the results of JSON.parse and stripping out key/value pairs where the value is null. (Fortunately my data is such that this is valid.)


Comments
Release team: Approved for deferral. Cleaning up all remaining bugs still targeted to 8. I pushed this to 8u20 so you can investigate further. Please review ILW/prio on this since I'm not sure if that has been done yet as it's a web bug.
21-03-2014