Example usage for jdk.nashorn.api.scripting ScriptObjectMirror isEmpty

List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror isEmpty

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting ScriptObjectMirror isEmpty.

Prototype

@Override
    public boolean isEmpty() 

Source Link

Usage

From source file:com.qwazr.utils.ScriptUtils.java

License:Apache License

public static <T> Map<String, T> toMap(ScriptObjectMirror som, Class<T> type) throws ScriptException {
    if (som == null)
        return null;
    if (som.isArray())
        throw new ScriptException("The JS object is an array");

    Map<String, T> map = new LinkedHashMap<String, T>();
    if (som.isEmpty())
        return map;
    som.forEach((s, o) -> map.put(s, ((ScriptObjectMirror) o).to(type)));
    return map;/*from  ww  w . ja v a 2  s. co m*/
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public static boolean isNull(Object object) {
    if (object == null)
        return true;
    if (object instanceof ScriptObjectMirror) {
        ScriptObjectMirror m = (ScriptObjectMirror) object;
        if (m.isInstanceOf(NativeString.class) && m.isEmpty())
            return true; //  isEmpty()  true)
        if (ScriptObjectMirror.isUndefined(m)) { //  null?
            // undefined
            return true;
        }/*from w w  w  .  j av a 2  s  . co  m*/
    }

    if (object instanceof String && ((String) object).length() == 0) // ?
        // sobj
        // 
        // NativeString
        // 
        // SOM
        return true;

    if (object instanceof Undefined) {
        return true;
    }
    return false;
}

From source file:org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils.java

License:Open Source License

public static Object toJsSerializable(Object value) {

    if (value instanceof Serializable) {
        if (value instanceof HashMap) {
            Map<String, Object> map = new HashMap<>();
            ((HashMap) value).forEach((k, v) -> map.put((String) k, FrameworkUtils.toJsSerializable(v)));
            return map;
        } else {/*from w  w  w . j av a2s  . c  o m*/
            return value;
        }
    } else if (value instanceof ScriptObjectMirror) {
        ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) value;
        if (scriptObjectMirror.isFunction()) {
            return SerializableJsFunction.toSerializableForm(scriptObjectMirror);
        } else if (scriptObjectMirror.isArray()) {
            List<Serializable> arrayItems = new ArrayList<>(scriptObjectMirror.size());
            scriptObjectMirror.values().forEach(v -> {
                Object serializedObj = toJsSerializable(v);
                if (serializedObj instanceof Serializable) {
                    arrayItems.add((Serializable) serializedObj);
                    if (log.isDebugEnabled()) {
                        log.debug("Serialized the value of array item as : " + serializedObj);
                    }
                } else {
                    log.warn(String.format("Non serializable array item: %s. and will not be persisted.",
                            serializedObj));
                }
            });
            return arrayItems;
        } else if (!scriptObjectMirror.isEmpty()) {
            Map<String, Serializable> serializedMap = new HashMap<>();
            scriptObjectMirror.forEach((k, v) -> {
                Object serializedObj = toJsSerializable(v);
                if (serializedObj instanceof Serializable) {
                    serializedMap.put(k, (Serializable) serializedObj);
                    if (log.isDebugEnabled()) {
                        log.debug("Serialized the value for key : " + k);
                    }
                } else {
                    log.warn(String.format("Non serializable object for key : %s, and will not be persisted.",
                            k));
                }

            });
            return serializedMap;
        } else {
            return Collections.EMPTY_MAP;
        }
    }
    return value;
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.ScriptObjectMirrorSerializer.java

License:Open Source License

/**
 * {@inheritDoc}/* w  w  w.  j  a  va 2  s  . co 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;
}