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

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

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:JavaScriptTest.java

public static String serialize(Object obj) {
    StringBuilder ret = new StringBuilder();
    if (obj instanceof ScriptObjectMirror) {
        ScriptObjectMirror om = (ScriptObjectMirror) obj;
        //System.out.println(om+" isArray "+om.isArray());
        //System.out.println(om+" isEmpty "+om.isEmpty());
        //System.out.println(om+" isExtensible "+om.isExtensible());
        //System.out.println(om+" isFrozen "+om.isFrozen());
        //System.out.println(om+" isFunction "+om.isFunction());
        //System.out.println(om+" isSealed "+om.isSealed());
        //System.out.println(om+" isStrictFunction "+om.isStrictFunction());            
        //System.out.println(om+" getOwnKeys "+Arrays.asList(om.getOwnKeys(true)));  

        if (om.isFunction()) {
            ret.append(om.toString());
        } else if (om.isArray()) {
            ret.append("[");
            //ret.append("isArray:"+om.toString());
            for (int x = 0; x < om.size(); x++) {
                Object o = om.getSlot(x);
                ret.append(serialize(o));
                if (x + 1 < om.size()) {
                    ret.append(",");
                }/* ww  w  . j ava2  s .co  m*/
            }
            ret.append("]");
        } else if (om.toString().indexOf("global") > -1) {
            Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                ret.append("var " + entry.getKey() + "=" + serialize(entry.getValue()) + ";\n");
            }
        } else {
            ret.append("{");
            Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                ret.append(entry.getKey() + ":" + serialize(entry.getValue()));
                if (it.hasNext()) {
                    ret.append(",");
                }
            }
            ret.append("}");
        }
    } else if (obj instanceof String) {
        ret.append("\"" + obj + "\"");
    } else {
        ret.append(obj);
    }
    return ret.toString();
}

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

License:Open Source License

/**
 * {@inheritDoc}//from   w ww. j a v a2s.  c  om
 */
@Override
public JsonElement serialize(ScriptObjectMirror jsObj, Type type, JsonSerializationContext context) {
    if ((jsObj != null) && jsObj.isFunction()) {
        String functionSource = jsObj.toString();
        int openCurlyBraceIndex = functionSource.indexOf('{');
        if (openCurlyBraceIndex == -1) {
            return new JsonPrimitive("function ()");
        } else {
            return new JsonPrimitive(functionSource.substring(0, openCurlyBraceIndex).trim());
        }
    } else {
        return super.serialize(jsObj, type, context);
    }
}