8025149: JSON.stringify does not handle 'space' argument as per the spec.

Fri, 20 Sep 2013 22:37:08 +0530

author
sundar
date
Fri, 20 Sep 2013 22:37:08 +0530
changeset 569
16b6db9f7225
parent 568
279f47b353f3
child 570
b8d9a63578e2

8025149: JSON.stringify does not handle 'space' argument as per the spec.
Reviewed-by: jlaskey, hannesw

src/jdk/nashorn/internal/objects/NativeJSON.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025149.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025149.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/objects/NativeJSON.java	Fri Sep 20 20:55:43 2013 +0530
     1.2 +++ b/src/jdk/nashorn/internal/objects/NativeJSON.java	Fri Sep 20 22:37:08 2013 +0530
     1.3 @@ -162,22 +162,27 @@
     1.4  
     1.5          String gap;
     1.6  
     1.7 -        if (space instanceof Number || space instanceof NativeNumber) {
     1.8 -            int indent;
     1.9 -            if (space instanceof NativeNumber) {
    1.10 -                indent = ((NativeNumber)space).intValue();
    1.11 +        // modifiable 'space' - parameter is final
    1.12 +        Object modSpace = space;
    1.13 +        if (modSpace instanceof NativeNumber) {
    1.14 +            modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    1.15 +        } else if (modSpace instanceof NativeString) {
    1.16 +            modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    1.17 +        }
    1.18 +
    1.19 +        if (modSpace instanceof Number) {
    1.20 +            int indent = Math.min(10, JSType.toInteger(modSpace));
    1.21 +            if (indent < 1) {
    1.22 +                gap = "";
    1.23              } else {
    1.24 -                indent = ((Number)space).intValue();
    1.25 +                final StringBuilder sb = new StringBuilder();
    1.26 +                for (int i = 0; i < indent; i++) {
    1.27 +                    sb.append(' ');
    1.28 +                }
    1.29 +                gap = sb.toString();
    1.30              }
    1.31 -
    1.32 -            final StringBuilder sb = new StringBuilder();
    1.33 -            for (int i = 0; i < Math.min(10, indent); i++) {
    1.34 -                sb.append(' ');
    1.35 -            }
    1.36 -            gap = sb.toString();
    1.37 -
    1.38 -        } else if (space instanceof String || space instanceof ConsString || space instanceof NativeString) {
    1.39 -            final String str = (space instanceof String) ? (String)space : space.toString();
    1.40 +        } else if (modSpace instanceof String || modSpace instanceof ConsString) {
    1.41 +            final String str = modSpace.toString();
    1.42              gap = str.substring(0, Math.min(10, str.length()));
    1.43          } else {
    1.44              gap = "";
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/script/basic/JDK-8025149.js	Fri Sep 20 22:37:08 2013 +0530
     2.3 @@ -0,0 +1,47 @@
     2.4 +/*
     2.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + * 
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + * 
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + * 
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + * 
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/**
    2.28 + * JDK-8025149: JSON.stringify does not handle 'space' argument as per the spec.
    2.29 + *
    2.30 + * @test
    2.31 + * @run
    2.32 + */
    2.33 +
    2.34 +print(JSON.stringify({ foo : 23, bar: { x : 22} }, undefined ,new Number(Infinity)));
    2.35 +
    2.36 +print(JSON.stringify({ foo : 23, bar: { x : 22} }, undefined ,new Number(-Infinity)));
    2.37 +
    2.38 +try {
    2.39 +    JSON.stringify({},[],
    2.40 +    (n = new Number(0), n.valueOf = function() { throw ("inside n.valueOf") }, n));
    2.41 +} catch (e) {
    2.42 +    print(e);
    2.43 +}
    2.44 +
    2.45 +try {
    2.46 +    JSON.stringify({},[],
    2.47 +    (s = new String(""), s.toString = function() { throw ("inside s.toString") }, s));
    2.48 +} catch (e) {
    2.49 +    print(e);
    2.50 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8025149.js.EXPECTED	Fri Sep 20 22:37:08 2013 +0530
     3.3 @@ -0,0 +1,9 @@
     3.4 +{
     3.5 +          "foo": 23,
     3.6 +          "bar": {
     3.7 +                    "x": 22
     3.8 +          }
     3.9 +}
    3.10 +{"foo":23,"bar":{"x":22}}
    3.11 +inside n.valueOf
    3.12 +inside s.toString

mercurial