Example usage for jdk.nashorn.internal.runtime.arrays ArrayData getObject

List of usage examples for jdk.nashorn.internal.runtime.arrays ArrayData getObject

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime.arrays ArrayData getObject.

Prototype

public abstract Object getObject(final int index);

Source Link

Document

Get an Object value from a given index

Usage

From source file:com.threecrickets.jvm.json.nashorn.NativeArrayEncoder.java

License:Mozilla Public License

public void encode(Object object, JsonContext context) throws IOException {
    ArrayData data = ((NativeArray) object).getArray();

    context.out.append('[');

    int length = (int) data.length();
    if (length > 0) {
        context.newline();//from ww w.  j  a va2  s . c  o m

        for (int i = 0; i < length; i++) {
            Object value = data.getObject(i);

            context.indentNested();
            context.nest().encode(value);

            if (i < length - 1)
                context.comma();
        }

        context.newline();
        context.indent();
    }

    context.out.append(']');
}

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

License:Apache License

public void encode(BsonWriter writer, NativeArray nativeArray, EncoderContext encoderContext) {
    ArrayData data = nativeArray.getArray();

    writer.writeStartArray();//  www .j  av a  2 s.c o  m
    for (int i = 0, length = (int) data.length(); i < length; i++) {
        Object item = data.getObject(i);
        BsonUtil.writeChild(item, writer, encoderContext, codecRegistry);
    }
    writer.writeEndArray();
}

From source file:org.xbib.elasticsearch.script.nashorn.NashornUnwrapper.java

License:Apache License

/**
 * Convert an object from a script wrapper value to a serializable value valid outside
 * of the Nashorn script processor context.
 *
 * This includes converting Array objects to Lists of valid objects.
 *
 * @param value the value to convert from script wrapper object to external object value
 * @return unwrapped and converted value
 *//*  w  w  w  .j  a va2  s  . c  om*/
public static Object unwrapValue(Object value) {
    if (value == null) {
        return null;
    }
    if (value instanceof NativeArray) {
        NativeArray nativeArray = (NativeArray) value;
        ArrayData data = nativeArray.getArray();
        int length = (int) data.length();
        ArrayList<Object> list = new ArrayList<Object>(length);
        for (int i = 0; i < length; i++) {
            list.add(unwrapValue(data.getObject(i)));
        }
        return list;
    } else if (value instanceof Map) {
        Map map = (Map) value;
        Map<Object, Object> result = new LinkedHashMap<Object, Object>();
        for (Object key : map.keySet()) {
            result.put(key, unwrapValue(map.get(key)));
        }
        return result;
    } else if (value instanceof ScriptObject) {
        ScriptObject object = (ScriptObject) value;
        Map<Object, Object> result = new LinkedHashMap<Object, Object>();
        for (Object key : object.getOwnKeys(true)) {
            result.put(key, unwrapValue(object.get(key)));
        }
        return result;
    } else if (value instanceof Undefined) {
        return null;
    } else if (value instanceof ConsString) {
        return value.toString();
    } else {
        return value;
    }
}