JDK-8157160.js revision 1690:c24beef07d1b
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 */ 23 24/** 25 * JDK-8157160: JSON.stringify does not work on ScriptObjectMirror objects 26 * 27 * @test 28 * @option -scripting 29 * @run 30 */ 31 32var SM = Java.type("javax.script.ScriptEngineManager"); 33var AJSO = Java.type("jdk.nashorn.api.scripting.AbstractJSObject"); 34var Supplier = Java.type("java.util.function.Supplier"); 35 36var engine = new SM().getEngineByName("nashorn"); 37 38// JSON stringify ScriptObjectMirror instances 39print(JSON.stringify(engine.eval("({ foo : 42 })"))); 40print(JSON.stringify(engine.eval("([5, 6, 76, 7])"))); 41print(JSON.stringify(engine.eval(<<EOF 42 ({ 43 toJSON: function() "hello" 44 }) 45EOF 46))); 47 48print(JSON.stringify(engine.eval(<<EOF 49obj = { 50 name: 'nashorn', 51 versions: [ 'es5.1', 'es6' ] 52} 53EOF 54))); 55 56var dm = engine.eval("new Date()"); 57print('"' + dm.toJSON() + '"' == JSON.stringify(dm)); 58 59// JSON stringifying an arbitrary JSObject impl. 60var 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 }, 67 68 getMember: function(name) { 69 if (name == "x") { 70 return 42; 71 } else if (name == "y") { 72 return "hello"; 73 } 74 } 75}; 76print(JSON.stringify(jsObj)); 77 78// try toJSON implementation on JSObject 79var jsObj2 = new AJSO() { 80 getMember: function(name) { 81 if (name == 'toJSON') { 82 return function() { 83 return "my json representation"; 84 } 85 } 86 } 87}; 88print(JSON.stringify(jsObj2)); 89 90var 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}; 101print(JSON.stringify(jsObj3)); 102 103// replacer function from another script world 104print(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 } 113EOF))); 114