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

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

Introduction

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

Prototype

long length

To view the source code for jdk.nashorn.internal.runtime.arrays ArrayData length.

Click Source Link

Document

Length of the array data.

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