Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.nashorn.internal.objects;
import static org.openjdk.nashorn.internal.runtime.ECMAErrors.typeError;
import static org.openjdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.openjdk.nashorn.api.scripting.JSObject;
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
import org.openjdk.nashorn.internal.objects.annotations.Attribute;
import org.openjdk.nashorn.internal.objects.annotations.Function;
import org.openjdk.nashorn.internal.objects.annotations.ScriptClass;
import org.openjdk.nashorn.internal.objects.annotations.Where;
import org.openjdk.nashorn.internal.runtime.ConsString;
import org.openjdk.nashorn.internal.runtime.JSONFunctions;
import org.openjdk.nashorn.internal.runtime.JSType;
import org.openjdk.nashorn.internal.runtime.PropertyMap;
import org.openjdk.nashorn.internal.runtime.ScriptObject;
import org.openjdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
import org.openjdk.nashorn.internal.runtime.linker.Bootstrap;
import org.openjdk.nashorn.internal.runtime.linker.InvokeByName;
/**
* ECMAScript 262 Edition 5, Section 15.12 The NativeJSON Object
*
*/
@ScriptClass("JSON")
public final class NativeJSON extends ScriptObject {
private static final Object TO_JSON = new Object();
private static InvokeByName getTO_JSON() {
return Global.instance().getInvokeByName(TO_JSON,
() -> new InvokeByName("toJSON", ScriptObject.class, Object.class, Object.class)
);
}
private static final Object JSOBJECT_INVOKER = new Object();
private static MethodHandle getJSOBJECT_INVOKER() {
return Global.instance().getDynamicInvoker(JSOBJECT_INVOKER,
() -> Bootstrap.createDynamicCallInvoker(Object.class, Object.class, Object.class)
);
}
private static final Object REPLACER_INVOKER = new Object();
private static MethodHandle getREPLACER_INVOKER() {
return Global.instance().getDynamicInvoker(REPLACER_INVOKER,
() -> Bootstrap.createDynamicCallInvoker(Object.class,Object.class, Object.class, Object.class, Object.class)
);
}
// initialized by nasgen
@SuppressWarnings("unused")
private static PropertyMap $nasgenmap$;
private NativeJSON() {
// don't create me!!
throw new UnsupportedOperationException();
}
/**
* ECMA 15.12.2 parse ( text [ , reviver ] )
*
* @param self self reference
* @param text a JSON formatted string
* @param reviver optional value: function that takes two parameters (key, value)
*
* @return an ECMA script value
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object parse(final Object self, final Object text, final Object reviver) {
return JSONFunctions.parse(text, reviver);
}
/**
* ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
*
* @param self self reference
* @param value ECMA script value (usually object or array)
* @param replacer either a function or an array of strings and numbers
* @param space optional parameter - allows result to have whitespace injection
*
* @return a string in JSON format
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
// The stringify method takes a value and an optional replacer, and an optional
// space parameter, and returns a JSON text. The replacer can be a function
// that can replace values, or an array of strings that will select the keys.
// A default replacer method can be provided. Use of the space parameter can
// produce text that is more easily readable.
final StringifyState state = new StringifyState();
// If there is a replacer, it must be a function or an array.
if (Bootstrap.isCallable(replacer)) {
state.replacerFunction = replacer;
} else if (isArray(replacer) ||
isJSObjectArray(replacer) ||
replacer instanceof Iterable ||
(replacer != null && replacer.getClass().isArray())) {
state.propertyList = new ArrayList<>();
final Iterator