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

JSON.parse(JSON.stringify([])).push(10) creates an array containing two elements

XMLWordPrintable

    • b105
    • generic
    • generic

        FULL PRODUCT VERSION :
        java version "1.8.0_72"
        Java(TM) SE Runtime Environment (build 1.8.0_72-b15)
        Java HotSpot(TM) 64-Bit Server VM (build 25.72-b15, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux dev01 3.13.0-49-generic #83-Ubuntu SMP Fri Apr 10 20:11:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        An empty array created by JSON.parse() behaves differently from an empty array created from an empty array literal.

        The empty array from JSON.parse() has length 0, but after calling push() once on the array it has length 2.

        The empty array literal behaves as expected - after calling push() once the array has length 1, and the single element in the array is the one pushed into the array.

        REGRESSION. Last worked in version 8u60

        ADDITIONAL REGRESSION INFORMATION:
        java version "1.8.0_45"
        Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
        Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Use the jjs command line tool to run the following:

        var a = JSON.parse(JSON.stringify([]));
        a.push(10);
        a
        a.length



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        jjs> var a = JSON.parse(JSON.stringify([]));
        jjs> a.push(10);
        1
        jjs> a
        10
        jjs> a.length
        1

        ACTUAL -
        jjs> var a = JSON.parse(JSON.stringify([]));
        jjs> a.push(10);
        1
        jjs> a
        0,10
        jjs> a.length
        2

        REPRODUCIBILITY :
        This bug can be reproduced always.

        CUSTOMER SUBMITTED WORKAROUND :
        Check for empty array before the push() call, and replace with empty array from array literal.

              mhaupt Michael Haupt
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: