JSONFunctions.java revision 1245:c55ce3738888
1/* 2 * Copyright (c) 2010, 2013, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.runtime; 27 28import java.lang.invoke.MethodHandle; 29import java.util.Iterator; 30import java.util.concurrent.Callable; 31import jdk.nashorn.internal.objects.Global; 32import jdk.nashorn.internal.parser.JSONParser; 33import jdk.nashorn.internal.runtime.arrays.ArrayIndex; 34import jdk.nashorn.internal.runtime.linker.Bootstrap; 35 36/** 37 * Utilities used by "JSON" object implementation. 38 */ 39public final class JSONFunctions { 40 private JSONFunctions() {} 41 42 private static final Object REVIVER_INVOKER = new Object(); 43 44 private static MethodHandle getREVIVER_INVOKER() { 45 return Context.getGlobal().getDynamicInvoker(REVIVER_INVOKER, 46 new Callable<MethodHandle>() { 47 @Override 48 public MethodHandle call() { 49 return Bootstrap.createDynamicInvoker("dyn:call", Object.class, 50 ScriptFunction.class, ScriptObject.class, String.class, Object.class); 51 } 52 }); 53 } 54 55 /** 56 * Returns JSON-compatible quoted version of the given string. 57 * 58 * @param str String to be quoted 59 * @return JSON-compatible quoted string 60 */ 61 public static String quote(final String str) { 62 return JSONParser.quote(str); 63 } 64 65 /** 66 * Parses the given JSON text string and returns object representation. 67 * 68 * @param text JSON text to be parsed 69 * @param reviver optional value: function that takes two parameters (key, value) 70 * @return Object representation of JSON text given 71 */ 72 public static Object parse(final Object text, final Object reviver) { 73 final String str = JSType.toString(text); 74 final Global global = Context.getGlobal(); 75 final boolean dualFields = ((ScriptObject) global).useDualFields(); 76 final JSONParser parser = new JSONParser(str, global, dualFields); 77 final Object value; 78 79 try { 80 value = parser.parse(); 81 } catch (final ParserException e) { 82 throw ECMAErrors.syntaxError(e, "invalid.json", e.getMessage()); 83 } 84 85 return applyReviver(global, value, reviver); 86 } 87 88 // -- Internals only below this point 89 90 // parse helpers 91 92 // apply 'reviver' function if available 93 private static Object applyReviver(final Global global, final Object unfiltered, final Object reviver) { 94 if (reviver instanceof ScriptFunction) { 95 final ScriptObject root = global.newObject(); 96 root.addOwnProperty("", Property.WRITABLE_ENUMERABLE_CONFIGURABLE, unfiltered); 97 return walk(root, "", (ScriptFunction)reviver); 98 } 99 return unfiltered; 100 } 101 102 // This is the abstract "Walk" operation from the spec. 103 private static Object walk(final ScriptObject holder, final Object name, final ScriptFunction reviver) { 104 final Object val = holder.get(name); 105 if (val instanceof ScriptObject) { 106 final ScriptObject valueObj = (ScriptObject)val; 107 final Iterator<String> iter = valueObj.propertyIterator(); 108 109 while (iter.hasNext()) { 110 final String key = iter.next(); 111 final Object newElement = walk(valueObj, key, reviver); 112 113 if (newElement == ScriptRuntime.UNDEFINED) { 114 valueObj.delete(key, false); 115 } else { 116 setPropertyValue(valueObj, key, newElement); 117 } 118 } 119 } 120 121 try { 122 // Object.class, ScriptFunction.class, ScriptObject.class, String.class, Object.class); 123 return getREVIVER_INVOKER().invokeExact(reviver, holder, JSType.toString(name), val); 124 } catch(Error|RuntimeException t) { 125 throw t; 126 } catch(final Throwable t) { 127 throw new RuntimeException(t); 128 } 129 } 130 131 // add a new property if does not exist already, or else set old property 132 private static void setPropertyValue(final ScriptObject sobj, final String name, final Object value) { 133 final int index = ArrayIndex.getArrayIndex(name); 134 if (ArrayIndex.isValidArrayIndex(index)) { 135 // array index key 136 sobj.defineOwnProperty(index, value); 137 } else if (sobj.getMap().findProperty(name) != null) { 138 // pre-existing non-inherited property, call set 139 sobj.set(name, value, 0); 140 } else { 141 // add new property 142 sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value); 143 } 144 } 145 146} 147