Sun, 24 Jun 2018 23:15:05 +0100
Merge
1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /**
25 * JDK-8157160: JSON.stringify does not work on ScriptObjectMirror objects
26 *
27 * @test
28 * @option -scripting
29 * @run
30 */
32 var SM = Java.type("javax.script.ScriptEngineManager");
33 var AJSO = Java.type("jdk.nashorn.api.scripting.AbstractJSObject");
34 var Supplier = Java.type("java.util.function.Supplier");
36 var engine = new SM().getEngineByName("nashorn");
38 // JSON stringify ScriptObjectMirror instances
39 print(JSON.stringify(engine.eval("({ foo : 42 })")));
40 print(JSON.stringify(engine.eval("([5, 6, 76, 7])")));
41 print(JSON.stringify(engine.eval(<<EOF
42 ({
43 toJSON: function() "hello"
44 })
45 EOF
46 )));
48 print(JSON.stringify(engine.eval(<<EOF
49 obj = {
50 name: 'nashorn',
51 versions: [ 'es5.1', 'es6' ]
52 }
53 EOF
54 )));
56 var dm = engine.eval("new Date()");
57 print('"' + dm.toJSON() + '"' == JSON.stringify(dm));
59 // JSON stringifying an arbitrary JSObject impl.
60 var jsObj = new AJSO() {
61 keySet: function() {
62 var keys = new java.util.HashSet();
63 keys.add("x");
64 keys.add("y");
65 return keys;
66 },
68 getMember: function(name) {
69 if (name == "x") {
70 return 42;
71 } else if (name == "y") {
72 return "hello";
73 }
74 }
75 };
76 print(JSON.stringify(jsObj));
78 // try toJSON implementation on JSObject
79 var jsObj2 = new AJSO() {
80 getMember: function(name) {
81 if (name == 'toJSON') {
82 return function() {
83 return "my json representation";
84 }
85 }
86 }
87 };
88 print(JSON.stringify(jsObj2));
90 var jsObj3 = new AJSO() {
91 getMember: function(name) {
92 if (name == 'toJSON') {
93 return new Supplier() {
94 "get": function() {
95 return "value from toJSON function";
96 }
97 };
98 }
99 }
100 };
101 print(JSON.stringify(jsObj3));
103 // replacer function from another script world
104 print(JSON.stringify({
105 foo: "hello"
106 }, engine.eval(<<EOF
107 function (key, value) {
108 if (key == "foo") {
109 return value.toUpperCase()
110 }
111 return value;
112 }
113 EOF)));