Example usage for com.google.gwt.json.client JSONValue toString

List of usage examples for com.google.gwt.json.client JSONValue toString

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue toString.

Prototype

@Override
public abstract String toString();

Source Link

Document

Returns a JSON-encoded string for this entity.

Usage

From source file:org.gk.engine.client.build.js.XJavaScript.java

License:Open Source License

/**
 * ????JavaScriptDNDEventData DNDList?//from  ww w . j  a va 2 s . co  m
 * 
 * @param jso
 */
private void updateData(JavaScriptObject jso) {
    JSONValue json = new JSONArray(jso);
    Object obj = JsonConvert.jsonString2Object(json.toString());
    dndEvent.setData(obj);
}

From source file:org.gwt.json.serialization.client.utils.JsonHashMapSerializer.java

License:Apache License

@Override
public JSONValue serialize(HashMap obj, GenericType[] genericTypes) {
    if (obj == null)
        return JSONNull.getInstance();
    JSONObject jsonObject = new JSONObject();
    GenericType keyType = genericTypes[0];
    GenericType valueType = genericTypes[1];

    for (Object entry : obj.entrySet()) {
        Map.Entry objAsEntry = (Map.Entry) entry;
        JSONValue key = serializer.getSerializerForType(keyType.getName()).serialize(objAsEntry.getKey(),
                keyType.getTypes());//w  w w  . j  a  va2  s.com
        JSONValue value = serializer.getSerializerForType(valueType.getName()).serialize(objAsEntry.getValue(),
                valueType.getTypes());

        //if key is a String, everything is just perfect. But if not, hm... let's use key.toString() for now
        jsonObject.put(key.isString() != null ? key.isString().stringValue() : key.toString(), value);
    }
    return jsonObject;
}

From source file:org.gwt.json.serialization.client.utils.JsonMapSerializer.java

License:Apache License

@Override
public JSONValue serialize(Map obj, GenericType[] genericTypes) {
    if (obj == null)
        return JSONNull.getInstance();
    JSONObject jsonObject = new JSONObject();
    GenericType keyType = genericTypes[0];
    GenericType valueType = genericTypes[1];

    for (Object entry : obj.entrySet()) {
        Map.Entry objAsEntry = (Map.Entry) entry;
        JSONValue key = serializer.getSerializerForType(keyType.getName()).serialize(objAsEntry.getKey(),
                keyType.getTypes());//from ww w .  j  ava 2s .c om
        JSONValue value = serializer.getSerializerForType(valueType.getName()).serialize(objAsEntry.getValue(),
                valueType.getTypes());

        //if key is a String, everything is just perfect. But if not, hm... let's use key.toString() for now
        jsonObject.put(key.isString() != null ? key.isString().stringValue() : key.toString(), value);
    }
    return jsonObject;
}

From source file:org.gwt.json.serialization.client.utils.JsonTreeMapSerializer.java

License:Apache License

@Override
public JSONValue serialize(TreeMap obj, GenericType[] genericTypes) {
    if (obj == null)
        return JSONNull.getInstance();
    JSONObject jsonObject = new JSONObject();
    GenericType keyType = genericTypes[0];
    GenericType valueType = genericTypes[1];

    for (Object entry : obj.entrySet()) {
        Map.Entry objAsEntry = (Map.Entry) entry;
        JSONValue key = serializer.getSerializerForType(keyType.getName()).serialize(objAsEntry.getKey(),
                keyType.getTypes());/*from w  w  w. j av a 2 s  .  c  om*/
        JSONValue value = serializer.getSerializerForType(valueType.getName()).serialize(objAsEntry.getValue(),
                valueType.getTypes());

        //if key is a String, everything is just perfect. But if not, hm... let's use key.toString() for now
        jsonObject.put(key.isString() != null ? key.isString().stringValue() : key.toString(), value);
    }
    return jsonObject;
}

From source file:org.jboss.bpm.console.client.model.DTOParser.java

License:Open Source License

public static TaskRef parseTaskReference(JSONObject item) {
    ConsoleLog.debug("parse " + item);

    long id = JSONWalk.on(item).next("id").asLong();

    // optional instanceId
    JSONWalk.JSONWrapper instanceIdWrapper = JSONWalk.on(item).next("processInstanceId");
    String executionId = instanceIdWrapper != null ? instanceIdWrapper.asString() : "n/a";

    // optional processId
    JSONWalk.JSONWrapper processIdWrapper = JSONWalk.on(item).next("processId");
    String processId = processIdWrapper != null ? processIdWrapper.asString() : "n/a";

    String name = JSONWalk.on(item).next("name").asString();
    String assignee = JSONWalk.on(item).next("assignee").asString();
    boolean isBlocking = JSONWalk.on(item).next("isBlocking").asBool();
    boolean isSignalling = JSONWalk.on(item).next("isSignalling").asBool();

    TaskRef ref = new TaskRef(id, executionId, processId, name, assignee, isSignalling, isBlocking);

    // task url reference maybe null
    JSONWalk.JSONWrapper jsonWrapper = JSONWalk.on(item).next("url");
    if (jsonWrapper != null) {
        String url = jsonWrapper.asString();
        ref.setUrl(url);/* w ww.  jav a  2s .  co  m*/
    } else {
        ref.setUrl("");
    }

    // participant users
    JSONArray arrUsers = JSONWalk.on(item).next("participantUsers").asArray();
    for (int k = 0; k < arrUsers.size(); ++k) {
        JSONValue jsonValue = arrUsers.get(k);
        ParticipantRef p = parseParticipant(jsonValue, k);
        ref.getParticipantUsers().add(p);
    }

    JSONArray arrGroups = JSONWalk.on(item).next("participantGroups").asArray();
    for (int k = 0; k < arrGroups.size(); ++k) {
        JSONValue jsonValue = arrGroups.get(k);
        ParticipantRef p = parseParticipant(jsonValue, k);
        ref.getParticipantGroups().add(p);
    }

    if (isSignalling) {
        JSONArray arr = JSONWalk.on(item).next("outcomes").asArray();
        for (int k = 0; k < arr.size(); ++k) {
            JSONValue jsonValue = arr.get(k);
            if (jsonValue.toString().equals("null")) {
                ConsoleLog.warn("FIXME JBPM-1828: Null value on outcomes:" + arr.toString());
                continue; // TODO: JBPM-1828
            }
            JSONString t = jsonValue.isString();
            ref.getOutcomes().add(t.stringValue());
        }

    }

    int prio = JSONWalk.on(item).next("priority").asInt();
    ref.setPriority(prio);

    JSONWalk.JSONWrapper dueDate = JSONWalk.on(item).next("dueDate");
    if (dueDate != null) // optional
    {
        Date due = dueDate.asDate();
        ref.setDueDate(due);
    }

    JSONWalk.JSONWrapper createDate = JSONWalk.on(item).next("createDate");
    if (createDate != null) // optional
    {
        Date due = createDate.asDate();
        ref.setDueDate(due);
    }

    return ref;
}

From source file:org.jboss.bpm.console.client.model.DTOParser.java

License:Open Source License

public static TokenReference parseTokenReference(JSONObject jso) {
    ConsoleLog.debug("parse " + jso);

    String rootTokenId = JSONWalk.on(jso).next("id").asString();
    //String name = JSONWalk.on(jso).next("name").asString();
    JSONWalk.JSONWrapper nodeNameWrapper = JSONWalk.on(jso).next("currentNodeName");
    String nodeName = nodeNameWrapper != null ? nodeNameWrapper.asString() : "";

    // TDOD: Fix token name
    TokenReference rt = new TokenReference(rootTokenId, "", nodeName);

    boolean canBeSignaled = JSONWalk.on(jso).next("canBeSignaled").asBool();
    rt.setCanBeSignaled(canBeSignaled);/*from w ww .j  av  a  2  s . co m*/

    JSONArray signals = JSONWalk.on(jso).next("availableSignals").asArray();
    for (int i = 0; i < signals.size(); i++) {
        JSONValue jsonValue = signals.get(i);
        if (jsonValue.toString().equals("null")) {
            ConsoleLog.warn("FIXME JBPM-1828: Null value on availableSignals:" + signals.toString());
            continue; // TODO: JBPM-1828
        }
        JSONString item = jsonValue.isString();
        rt.getAvailableSignals().add(item.stringValue());
    }

    JSONArray childArr = JSONWalk.on(jso).next("children").asArray();
    for (int i = 0; i < childArr.size(); i++) {
        JSONObject item = childArr.get(i).isObject();
        rt.getChildren().add(parseTokenReference(item));
    }

    return rt;
}

From source file:org.jboss.bpm.console.client.model.DTOParser.java

License:Open Source License

public static List<String> parseStringArray(JSONValue jso) {
    List<String> result = new ArrayList<String>();

    JSONArray jsonArray = jso.isArray();

    if (null == jsonArray)
        throw new IllegalArgumentException("Not an array: " + jso);

    for (int i = 0; i < jsonArray.size(); i++) {
        JSONValue jsonValue = jsonArray.get(i);
        if (jsonValue.toString().equals("null")) {
            ConsoleLog.warn("FIXME JBPM-1828: Null value on string array:" + jsonArray.toString());
            continue; // TODO: JBPM-1828
        }/* w w  w .  j  a  va2s  . c  o  m*/
        JSONString item = jsonValue.isString();
        result.add(item.stringValue());
    }

    return result;
}

From source file:org.jboss.errai.bus.client.json.JSONUtilCli.java

License:Apache License

public static ArrayList<MarshalledMessage> decodePayload(String value) {
    if (value == null || value.trim().length() == 0)
        return EMPTYLIST;

    /**//from  ww  w . ja v  a2  s .  c om
     * We have to do a two-stage decoding of the message.  We cannot fully decode the message here, as we
     * cannot be sure the destination endpoint exists within this Errai bundle.  So we extract the ToSubject
     * field and send the unparsed JSON object onwards.
     *
     */
    try {
        JSONValue val = JSONParser.parseStrict(value);
        if (val == null) {
            return EMPTYLIST;
        }
        JSONArray arr = val.isArray();
        if (arr == null) {
            throw new RuntimeException("unrecognized payload" + val.toString());
        }
        ArrayList<MarshalledMessage> list = new ArrayList<MarshalledMessage>(arr.size());
        for (int i = 0; i < arr.size(); i++) {
            list.add(new MarshalledMessageImpl((JSONObject) arr.get(i)));
        }

        return list;
    } catch (Exception e) {
        System.out.println("JSONUtilCli.decodePayload=" + value);
        e.printStackTrace();
        return EMPTYLIST;
    }

}

From source file:org.jboss.errai.enterprise.client.jaxrs.JacksonTransformer.java

License:Apache License

/**
 * Transforms Errai JSON into a Jackson compatible JSON.
 * /*from ww  w.  j a  v a 2  s  .com*/
 * @param erraiJson
 *          JSON generated by Errai
 * @return jackson compatible JSON
 */
public static String toJackson(String erraiJson) {
    JSONValue val = JSONParser.parseStrict(erraiJson);
    val = toJackson(val, null, null, new HashMap<String, JSONValue>());

    return val.toString();
}

From source file:org.jboss.errai.enterprise.client.jaxrs.JacksonTransformer.java

License:Apache License

/**
 * The transformation from Errai JSON to Jackson's JSON contains the following steps:
 * <ul>/*www .  j  av a  2 s  . c om*/
 * <li>For all JSON objects, recursively remove the Errai specific OBJECT_ID and ENCODED_TYPE
 * values</li>
 * <li>Keep a reference to the removed OBJECT_IDs, so back-references can be resolved</li>
 * <li>If an array is encountered, process all its elements, then remove the Errai specific
 * QUALIFIED_VALUE key, by associating its actual value with the object's key directly: "list":
 * {"^Value": ["e1","e2"]} becomes "list": ["e1","e2"]</li>
 * <li>If an enum is encountered, remove the Errai specific ENUM_STRING_VALUE key, by associating
 * its actual value with the object's key directly: "gender": {"^EnumStringValue": "MALE"} becomes
 * "gender": "MALE"</li>
 * <li>If a number is encountered, remove the Errai specific NUMERIC_VALUE key, by associating its
 * actual value with the object's key directly: "id": {"^NumValue": "1"} becomes "id": "1"</li>
 * <li>If a date is encountered, remove the Errai specific QUALIFIED_VALUE key, by associating its
 * actual value with the object's key directly and turning it into a JSON number</li>
 * <li>If EMBEDDED_JSON is encountered, turn in into standard json</li>
 * </ul>
 * 
 * @param val
 *          the JSON value to transform
 * @param key
 *          the key of the JSON value to transform
 * @param parent
 *          the parent object of the current value
 * @param objectCache
 *          a cache for removed objects, that is used to resolve backreferences
 * @return the modified JSON value
 */
private static JSONValue toJackson(JSONValue val, String key, JSONObject parent,
        Map<String, JSONValue> objectCache) {
    JSONObject obj;
    if ((obj = val.isObject()) != null) {
        JSONValue objectIdVal = obj.get(OBJECT_ID);
        if (objectIdVal != null) {
            String objectId = objectIdVal.isString().stringValue();
            if (!objectId.equals("-1")) {
                JSONValue backRef = objectCache.get(objectId);
                if (backRef != null) {
                    if (parent != null) {
                        parent.put(key, backRef);
                    } else {
                        return backRef.isObject();
                    }
                } else {
                    objectCache.put(objectId, obj);
                }
            }
        }

        JSONValue encType = obj.get(ENCODED_TYPE);
        obj.put(OBJECT_ID, null);
        obj.put(ENCODED_TYPE, null);

        for (String k : obj.keySet()) {
            JSONArray arr;
            if ((arr = obj.get(k).isArray()) != null) {
                for (int i = 0; i < arr.size(); i++) {
                    if (arr.get(i).isObject() != null && arr.get(i).isObject().get(NUMERIC_VALUE) != null) {
                        arr.set(i, arr.get(i).isObject().get(NUMERIC_VALUE));
                    } else if (arr.get(i).isObject() != null) {
                        arr.set(i, toJackson(arr.get(i), null, null, objectCache));
                    }
                }

                if (k.equals(QUALIFIED_VALUE)) {
                    if (parent != null) {
                        parent.put(key, arr);
                    } else {
                        return arr;
                    }
                }
            } else if (k.equals(ENUM_STRING_VALUE) || k.equals(NUMERIC_VALUE)) {
                if (parent != null) {
                    parent.put(key, obj.get(k));
                }
            } else if (k.equals(QUALIFIED_VALUE)) {
                if (parent != null) {
                    if (encType.isString().stringValue().equals("java.util.Date")) {
                        String dateValue = obj.get(k).isString().stringValue();
                        parent.put(key, new JSONNumber(Double.parseDouble(dateValue)));
                    } else {
                        parent.put(key, obj.get(k));
                    }
                }
            } else if (k.startsWith(SerializationParts.EMBEDDED_JSON)) {
                final JSONValue newKey = JSONParser
                        .parseStrict((k.substring(SerializationParts.EMBEDDED_JSON.length())));
                JSONValue value = obj.get(k);
                JSONObject tmpObject = new JSONObject();
                toJackson(newKey, QUALIFIED_VALUE, tmpObject, objectCache);

                String embeddedKey = null;
                JSONValue qualVal = tmpObject.get(QUALIFIED_VALUE);
                if (qualVal.isString() != null) {
                    embeddedKey = qualVal.isString().stringValue();
                } else {
                    embeddedKey = qualVal.toString();
                }
                obj.put(embeddedKey, value);
            }

            toJackson(obj.get(k), k, obj, objectCache);
        }
    }

    return cleanUpEmbeddedJson(obj);
}