List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror getOwnKeys
public String[] getOwnKeys(final boolean all)
From source file:com.threecrickets.jvm.json.nashorn.ScriptObjectMirrorEncoder.java
License:Mozilla Public License
public void encode(Object object, JsonContext context) throws IOException { ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object; Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal()); if (!(wrapped instanceof ScriptObjectMirror)) { context.encode(wrapped);/* w ww .j av a 2s . com*/ return; } if (scriptObjectMirror.isArray()) { context.out.append('['); int length = scriptObjectMirror.size(); if (length > 0) { context.newline(); for (int i = 0; i < length; i++) { Object value = scriptObjectMirror.getSlot(i); context.indentNested(); context.nest().encode(value); if (i < length - 1) context.comma(); } context.newline(); context.indent(); } context.out.append(']'); } else { context.out.append('{'); String[] keys = scriptObjectMirror.getOwnKeys(true); int length = keys.length; if (length > 0) { context.newline(); for (int i = 0; i < length; i++) { String key = keys[i]; Object value = scriptObjectMirror.get(key); context.indentNested(); context.quoted(key); context.colon(); context.nest().encode(value); if (i < length - 1) context.comma(); } context.newline(); context.indent(); } context.out.append('}'); } }
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); }/* ww w .j ava 2 s . c om*/ object = scriptObject; } return object; }
From source file:io.lightlink.utils.Utils.java
License:Open Source License
public static Object tryConvertToJavaCollections(Object value) { if (value instanceof Object[]) { value = Arrays.asList((Object[]) value); } else if (value instanceof int[]) { List<Object> res = new ArrayList<Object>(); for (int i = 0; i < ((int[]) value).length; i++) { res.add(((int[]) value)[i]); }//from www . j a va 2 s. c o m return res; } else if (value instanceof double[]) { List<Object> res = new ArrayList<Object>(); for (int i = 0; i < ((double[]) value).length; i++) { res.add(((double[]) value)[i]); } return res; } else if (value instanceof float[]) { List<Object> res = new ArrayList<Object>(); for (int i = 0; i < ((float[]) value).length; i++) { res.add(((float[]) value)[i]); } return res; } else if (value instanceof ScriptObjectMirror && "Date".equalsIgnoreCase(((ScriptObjectMirror) value).getClassName())) { Double time = (Double) ((ScriptObjectMirror) value).callMember("getTime"); return new Date(time.longValue()); } else if (value instanceof ScriptObject) { ScriptObject scriptObject = (ScriptObject) value; if (scriptObject.isArray()) { String[] ownKeys = scriptObject.getOwnKeys(false); List<Object> res = new ArrayList<Object>(); for (String key : ownKeys) { Object propertyValue = scriptObject.get(key); propertyValue = tryConvertToJavaCollections(propertyValue); res.add(propertyValue); } return res; } else { String[] ownKeys = scriptObject.getOwnKeys(true); Map<String, Object> res = new LinkedHashMap<String, Object>(); for (String key : ownKeys) { Object propertyValue = scriptObject.get(key); propertyValue = tryConvertToJavaCollections(propertyValue); res.put(key, propertyValue); } return res; } } else if (value instanceof ScriptObjectMirror) { ScriptObjectMirror ScriptObjectMirror = (ScriptObjectMirror) value; if (ScriptObjectMirror.isArray()) { String[] ownKeys = ScriptObjectMirror.getOwnKeys(false); List<Object> res = new ArrayList<Object>(); for (String key : ownKeys) { Object propertyValue = ScriptObjectMirror.get(key); propertyValue = tryConvertToJavaCollections(propertyValue); res.add(propertyValue); } return res; } else { String[] ownKeys = ScriptObjectMirror.getOwnKeys(true); Map<String, Object> res = new LinkedHashMap<String, Object>(); for (String key : ownKeys) { Object propertyValue = ScriptObjectMirror.get(key); propertyValue = tryConvertToJavaCollections(propertyValue); res.put(key, propertyValue); } return res; } } return value; }
From source file:io.stallion.plugins.javascript.JavaToJsHelpers.java
License:Open Source License
public Object toJava(Object obj) { if (!(obj instanceof ScriptObjectMirror)) { return obj; }//w w w. j a va 2 s .c o m ScriptObjectMirror so = (ScriptObjectMirror) obj; if (so.isArray()) { List items = list(); for (String key : so.getOwnKeys(false)) { items.add(so.get(key)); } return items; } else { Map<String, Object> o = map(); for (String key : so.getOwnKeys(false)) { Object val = so.get(key); if (val instanceof ScriptObjectMirror && ((ScriptObjectMirror) val).isArray()) { val = toJava((ScriptObjectMirror) val); } o.put(key, val); } return o; } }
From source file:org.bson.jvm.nashorn.ScriptObjectMirrorCodec.java
License:Apache License
@SuppressWarnings("unchecked") public void encode(BsonWriter writer, ScriptObjectMirror scriptObjectMirror, EncoderContext encoderContext) { Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal()); if (!(wrapped instanceof ScriptObjectMirror)) { // Attempt to encode the wrapped object Codec<Object> codec = null; try {//from w w w . j av a2 s .co m codec = (Codec<Object>) codecRegistry.get(wrapped.getClass()); } catch (CodecConfigurationException x) { } if (codec != null) { codec.encode(writer, wrapped, encoderContext); return; } } if (scriptObjectMirror.isArray()) { writer.writeStartArray(); for (int i = 0, length = scriptObjectMirror.size(); i < length; i++) { Object item = scriptObjectMirror.getSlot(i); BsonUtil.writeChild(item, writer, encoderContext, codecRegistry); } writer.writeEndArray(); } else { writer.writeStartDocument(); for (String key : scriptObjectMirror.getOwnKeys(true)) { Object value = scriptObjectMirror.get(key); writer.writeName(key); BsonUtil.writeChild(value, writer, encoderContext, codecRegistry); } writer.writeEndDocument(); } }
From source file:org.siphon.jsmongo.MongoExecutor.java
License:Open Source License
public Document jsObjectToDocument(ScriptObjectMirror object) throws SqlExecutorException { Document document = new Document(); String[] names = object.getOwnKeys(false); for (int i = 0; i < names.length; i++) { String name = names[i];/*from www . ja va 2s .co m*/ Object value = object.get(name); document.append(name, jsValueToBson(value)); } return document; }
From source file:org.siphon.jsmongo.MongoExecutor.java
License:Open Source License
private BsonDocument jsObjectToBsonDocument(ScriptObjectMirror object) throws SqlExecutorException { BsonDocument document = new BsonDocument(); String[] names = object.getOwnKeys(false); for (int i = 0; i < names.length; i++) { String name = names[i];// w w w. j av a 2 s. co m Object value = object.get(name); document.append(name, jsValueToBson(value)); } return document; }
From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.ScriptObjectMirrorSerializer.java
License:Open Source License
/** * {@inheritDoc}/*from ww w.j a v a2 s. c o m*/ */ @Override public JsonElement serialize(ScriptObjectMirror jsObj, Type type, JsonSerializationContext context) { if ((jsObj == null) || ScriptObjectMirror.isUndefined(jsObj) || jsObj.isFunction()) { return JsonNull.INSTANCE; } if (jsObj.isArray()) { JsonArray jsonArray = new JsonArray(); for (Object item : jsObj.values()) { jsonArray.add(serializeFurther(context.serialize(item), context)); } return jsonArray; } if (jsObj.isEmpty()) { return new JsonObject(); } JsonObject jsonObject = new JsonObject(); for (String key : jsObj.getOwnKeys(false)) { jsonObject.add(key, serializeFurther(jsObj.getMember(key), context)); } return jsonObject; }
From source file:Runner.AdaptorEmail.java
public void send(ScriptObjectMirror mirror) { System.out.println(mirror.getClassName() + ": " + Arrays.toString(mirror.getOwnKeys(true))); String to = ""; String cc = ""; String subject = ""; String body = ""; String htmlEmail = ""; to = (String) mirror.get("to"); cc = (String) mirror.get("cc"); subject = (String) mirror.get("subject"); body = (String) mirror.get("body"); htmlEmail = (String) mirror.get("htmlEmail"); if (to.isEmpty() || subject.isEmpty() || body.isEmpty() || !htmlEmail.equalsIgnoreCase("true")) { throw new IllegalArgumentException("email.send received bad parameters"); }//ww w . j av a2 s. c o m Log.info("AdaptorEmail: " + to + " || " + cc + " || " + subject + " || " + body + " || " + htmlEmail); }