Example usage for jdk.nashorn.internal.objects NativeArray getArray

List of usage examples for jdk.nashorn.internal.objects NativeArray getArray

Introduction

In this page you can find the example usage for jdk.nashorn.internal.objects NativeArray getArray.

Prototype

public final ArrayData getArray() 

Source Link

Document

Get the ArrayData for this ScriptObject if it is an array

Usage

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();/*from w  ww.  j a  v 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
 *///ww w  .j  a  v  a 2s .  com
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;
    }
}