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

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

Introduction

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

Prototype

public JSONString isString() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONString.

Usage

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());//  w ww .j  a  v  a2s .  co  m
        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 ava2  s  . c o  m*/
        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.idwebmail.client.MainEntryPoint.java

/**
* Retorna el string de un elemento del tipo JSONValue
* @param jsonValue Objeto del cual se quiere el String
* @return String que se obtuvo del objeto
*//*  w  ww  .j a va 2s .c om*/
public static String getString(JSONValue jsonValue) {
    if (jsonValue == null || jsonValue.isString() == null
            || jsonValue.getClass().getName().equals("com.google.gwt.json.client.JSONNull"))
        return "null";
    return ((JSONString) jsonValue).stringValue();
}

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);/*from ww  w .j av  a2s  .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  .c  o  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
        }/*from  w w  w.  ja v a2 s  . c om*/
        JSONString item = jsonValue.isString();
        result.add(item.stringValue());
    }

    return result;
}

From source file:org.jboss.bpm.console.client.process.JSONTree.java

License:Open Source License

private void parseValue(TreeItem root, String key, JSONValue jsonValue) {
    if (jsonValue.isBoolean() != null) {
        TreeItem treeItem = root.addItem(key);
        treeItem.addItem(jsonValue.isBoolean().toString());
    } else if (jsonValue.isNumber() != null) {
        TreeItem fastTreeItem = root.addItem(key);
        fastTreeItem.addItem(jsonValue.isNumber().toString());
    } else if (jsonValue.isString() != null) {
        TreeItem treeItem = root.addItem(key);
        treeItem.addItem(jsonValue.isString().toString());
    } else {/*ww w  .j  ava 2  s  . c  o m*/
        ConsoleLog.warn("Unexpected JSON value: " + jsonValue);
    }

}

From source file:org.jboss.dmr.client.dispatch.impl.UploadResponse.java

License:Open Source License

private String extractFailure(final JSONObject response) {
    String failure = "n/a";
    JSONValue failureValue = response.get(FAILURE_DESCRIPTION);
    if (failureValue.isString() != null) {
        failure = failureValue.isString().stringValue();
    } else if (failureValue.isObject() != null) {
        JSONObject failureObject = failureValue.isObject();
        for (String key : failureObject.keySet()) {
            if (key.contains("failure") && failureObject.get(key).isString() != null) {
                failure = failureObject.get(key).isString().stringValue();
                break;
            }//from   w  w w  . j a  v  a  2s  .  c om
        }
    }
    return failure;
}

From source file:org.jboss.errai.common.client.json.JSONDecoderCli.java

License:Apache License

private static Object _decode(JSONValue v, DecodingContext ctx) {
    if (v.isString() != null) {
        return v.isString().stringValue();
    } else if (v.isNumber() != null) {
        return v.isNumber().doubleValue();
    } else if (v.isBoolean() != null) {
        return v.isBoolean().booleanValue();
    } else if (v.isNull() != null) {
        return null;
    } else if (v instanceof JSONObject) {
        return decodeObject(v.isObject(), ctx);
    } else if (v instanceof JSONArray) {
        return decodeList(v.isArray(), ctx);
    } else {//from ww w .  j a va  2 s.  c o m
        throw new RuntimeException("unknown encoding");
    }
}

From source file:org.jboss.errai.common.client.types.JSONTypeHelper.java

License:Apache License

public static Object convert(JSONValue value, Class to, DecodingContext ctx) {
    JSONValue v;/*from  w ww.  j  ava2s . c  o  m*/
    if ((v = value.isString()) != null) {
        return TypeHandlerFactory.convert(String.class, to, ((JSONString) v).stringValue(), ctx);
    } else if ((v = value.isNumber()) != null) {
        return TypeHandlerFactory.convert(Number.class, to, ((JSONNumber) v).doubleValue(), ctx);
    } else if ((v = value.isBoolean()) != null) {
        return TypeHandlerFactory.convert(Boolean.class, to, ((JSONBoolean) v).booleanValue(), ctx);
    } else if ((v = value.isArray()) != null) {
        List list = new ArrayList();
        JSONArray arr = (JSONArray) v;

        Class cType = to.getComponentType();

        while (cType != null && cType.getComponentType() != null)
            cType = cType.getComponentType();

        if (cType == null)
            cType = to;

        Object o;
        for (int i = 0; i < arr.size(); i++) {
            if ((o = convert(arr.get(i), cType, ctx)) instanceof UnsatisfiedForwardLookup) {
                ctx.addUnsatisfiedDependency(list, (UnsatisfiedForwardLookup) o);
            } else {
                list.add(convert(arr.get(i), cType, ctx));
            }
        }

        Object t = TypeHandlerFactory.convert(Collection.class, to, list, ctx);

        ctx.swapDepReference(list, t);

        return t;
    } else if ((v = value.isObject()) != null) {
        JSONObject eMap = (JSONObject) v;

        Map<Object, Object> m = new UHashMap<Object, Object>();
        Object o;
        Object val;

        for (String key : eMap.keySet()) {
            o = key;
            if (key.startsWith(SerializationParts.EMBEDDED_JSON)) {
                o = JSONDecoderCli.decode(key.substring(SerializationParts.EMBEDDED_JSON.length()), ctx);
            } else if (SerializationParts.ENCODED_TYPE.equals(key)) {
                String className = eMap.get(key).isString().stringValue();
                String objId = null;
                if ((v = eMap.get(SerializationParts.OBJECT_ID)) != null) {
                    objId = v.isString().stringValue();
                }

                boolean ref = false;
                if (objId != null) {
                    if (objId.charAt(0) == '$') {
                        ref = true;
                        objId = objId.substring(1);
                    }

                    if (ctx.hasObject(objId)) {
                        return ctx.getObject(objId);
                    } else if (ref) {
                        return new UnsatisfiedForwardLookup(objId);
                    }
                }

                if (TypeDemarshallers.hasDemarshaller(className)) {
                    o = TypeDemarshallers.getDemarshaller(className).demarshall(eMap, ctx);
                    if (objId != null)
                        ctx.putObject(objId, o);
                    return o;
                } else {
                    throw new RuntimeException("no available demarshaller: " + className);
                }
            }

            val = JSONDecoderCli.decode(eMap.get(key), ctx);
            boolean commit = true;

            if (o instanceof UnsatisfiedForwardLookup) {
                ctx.addUnsatisfiedDependency(m, (UnsatisfiedForwardLookup) o);
                if (!(val instanceof UnsatisfiedForwardLookup)) {
                    ((UnsatisfiedForwardLookup) o).setVal(val);
                }
                commit = false;
            }
            if (val instanceof UnsatisfiedForwardLookup) {
                ((UnsatisfiedForwardLookup) val).setKey(o);
                ctx.addUnsatisfiedDependency(m, (UnsatisfiedForwardLookup) val);
                commit = false;
            }

            if (commit) {
                m.put(o, JSONDecoderCli.decode(eMap.get(key), ctx));
            }

        }

        return TypeHandlerFactory.convert(Map.class, to, m, ctx);
    }

    return null;
}