Example usage for jdk.nashorn.internal.runtime ScriptObject put

List of usage examples for jdk.nashorn.internal.runtime ScriptObject put

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime ScriptObject put.

Prototype

public Object put(final Object key, final Object value, final boolean strict) 

Source Link

Document

Put a property in the ScriptObject (java.util.Map-like method to help ScriptObjectMirror implementation)

Usage

From source file:com.threecrickets.jvm.json.nashorn.util.NashornNativeUtil.java

License:Mozilla Public License

public static Object unwrap(Object object) {
    // Nashorn creates these mirrors when they pass through certain
    // boundaries. However, we are only allowed to unwrap them from the same
    // global context in which they were wrapped. We will try to unwrap them
    // here, but if that doesn't work we will just create a shallow clone.
    // The clone won't function like the original, but will be good enough
    // for our purposes here.

    object = ScriptObjectMirror.unwrap(object, Context.getGlobal());
    if (object instanceof ScriptObjectMirror) {
        ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object;
        ScriptObject scriptObject = NashornNativeUtil.newObject();
        for (String key : scriptObjectMirror.getOwnKeys(true)) {
            Object value = scriptObjectMirror.get(key);
            scriptObject.put(key, value, false);
        }/*from  w  w w  .  ja v a2s.  c  o  m*/
        object = scriptObject;
    }
    return object;
}

From source file:com.threecrickets.jvm.json.nashorn.util.NashornTokener.java

License:Open Source License

/**
 * Create a Rhino NativeObject.//from w  w  w.j a va2  s  . c  om
 * 
 * @return A NativeObject
 * @throws JsonSyntaxError
 *         In case of a JSON conversion error
 */
public ScriptObject createNativeObject() throws JsonSyntaxError {
    ScriptObject scriptObject = NashornNativeUtil.newObject();
    char c;
    String key;

    if (nextClean() != '{') {
        throw syntaxError("A JSON object text must begin with '{'");
    }
    for (;;) {
        c = nextClean();
        switch (c) {
        case 0:
            throw syntaxError("A JSON object text must end with '}'");
        case '}':
            return scriptObject;
        default:
            back();
            key = nextValue().toString();
        }

        /*
         * The key is followed by ':'. We will also tolerate '=' or '=>'.
         */

        c = nextClean();
        if (c == '=') {
            if (next() != '>') {
                back();
            }
        } else if (c != ':') {
            throw syntaxError("Expected a ':' after a key");
        }
        scriptObject.put(key, nextValue(), true);

        /*
         * Pairs are separated by ','. We will also tolerate ';'.
         */

        switch (nextClean()) {
        case ';':
        case ',':
            if (nextClean() == '}') {
                return scriptObject;
            }
            back();
            break;
        case '}':
            return scriptObject;
        default:
            throw syntaxError("Expected a ',' or '}'");
        }
    }
}

From source file:org.bson.jvm.nashorn.ScriptObjectCodec.java

License:Apache License

public Object decode(BsonReader reader, DecoderContext decoderContext) {
    ScriptObject scriptObject = Global.newEmptyInstance();

    reader.readStartDocument();/*from w w  w  . j  a va  2s. c  o  m*/
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        Object value = BsonUtil.read(reader, decoderContext, codecRegistry, bsonTypeClassMap);
        scriptObject.put(key, value, false);
    }
    reader.readEndDocument();

    // The driver does not support decoding DBRef, so we'll do it here
    Object dbRef = new DBRefTransformer().transform(scriptObject, null);
    if (dbRef != null)
        return dbRef;

    return scriptObject;
}