Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8037562

Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys

XMLWordPrintable

    • b08
    • x86
    • os_x

        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.)

              sundar Sundararajan Athijegannathan
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: