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

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

Introduction

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

Prototype

public JSONObject isObject() 

Source Link

Document

Returns non-null if this JSONValue is really a JSONObject.

Usage

From source file:com.dimdim.ui.common.client.json.ServerResponseReader.java

License:Open Source License

public ServerResponse readServerResponse(JSONValue value, ServerResponseObjectReader objectReader) {
    ServerResponse resp = null;//ww w . j  a v a2  s  .  c om
    JSONObject obj = value.isObject();
    //Window.alert("ResponseAndEventReader:readResponse value.isObject()?::"+obj);
    if (obj != null) {
        String dataType = "";
        String successStr = "false";
        String codeStr = "0";
        boolean success = false;
        int code = 0;
        String objClass = "";

        if (obj.containsKey(KEY_SUCCESS)) {
            JSONString v = obj.get(KEY_SUCCESS).isString();
            if (v != null) {
                successStr = v.stringValue();
                if (successStr.equals("true") || successStr.equals("false")) {
                    success = (new Boolean(successStr)).booleanValue();
                }
            }
        }
        if (obj.containsKey(KEY_CODE)) {
            JSONString v = obj.get(KEY_CODE).isString();
            if (v != null) {
                codeStr = v.stringValue();
                try {
                    code = (new Integer(codeStr)).intValue();
                } catch (Exception e) {
                    code = 0;
                }
            }
        }
        if (obj.containsKey(KEY_DATA_TYPE)) {
            JSONString v = obj.get(KEY_DATA_TYPE).isString();
            if (v != null) {
                dataType = v.stringValue();
            }
        }
        if (obj.containsKey(KEY_DATA_OBJECT_CLASS)) {
            JSONString v = obj.get(KEY_DATA_OBJECT_CLASS).isString();
            if (v != null) {
                objClass = v.stringValue();
            }
        }
        if (dataType != null && objClass != null) {
            //   Read the object.
            if (obj.containsKey(KEY_DATA)) {
                if (dataType.equals(KEY_VALUE_DATA_TYPE_ARRAY)) {
                    JSONValue v = obj.get(KEY_DATA);
                    JSONArray ary = v.isArray();
                    if (ary != null) {
                        ArrayList vec = readArray(ary, objClass, objectReader);
                        resp = new ServerResponse(success, code, vec);
                    } else {
                        //                     Window.alert("Received null array");
                    }
                } else if (dataType.equals(KEY_VALUE_DATA_TYPE_OBJECT)) {
                    JSONValue v = obj.get(KEY_DATA);
                    JSONObject o = v.isObject();
                    if (o != null) {
                        UIObject uo = objectReader.readServerResponseData(objClass, o);
                        resp = new ServerResponse(success, code, uo);
                    }
                }
            }
        }
        if (resp == null) {
            resp = new ServerResponse(false, -1, "Server response reading failure");
        }
    }
    return resp;
}

From source file:com.eduworks.gwt.client.net.packet.AjaxPacket.java

License:Apache License

public JSONObject getObject(String key) {
    JSONValue jv;
    try {//from  ww  w .j a v a 2s .co m
        jv = super.get(key);
    } catch (NullPointerException e) {
        return null;
    }
    if (jv != null && jv.isObject() != null)
        return jv.isObject();
    return null;
}

From source file:com.eduworks.gwt.client.net.packet.ESBPacket.java

License:Apache License

@Override
public ESBPacket getObject(String key) {
    JSONValue jv;
    try {/*from   w ww  .j a v a 2 s  . c om*/
        jv = super.get(key);
    } catch (NullPointerException e) {
        return null;
    }
    if (jv != null && jv.isObject() != null)
        return new ESBPacket(jv.isObject());
    return null;
}

From source file:com.emitrom.lienzo.client.core.shape.json.JSONDeserializer.java

License:Open Source License

/**
 * Parses the JSON string and returns the IJSONSerializable.
 * Use this method if you need to parse JSON that may contain one or more errors.
 * <pre>//from  ww  w.  jav a  2  s .c  om
 * ValidationContext ctx = new ValidationContext();
 * ctx.setValidate(true);
 * ctx.setStopOnError(false); // find all errors
 * IJSONSerializable<?> node = JSONDeserializer.getInstance().fromString(jsonString, ctx);
 * if (ctx.getErrorCount() > 0)
 * {
 *   Console.log(ctx.getDebugString());
 * }
 * </pre>
 * 
 * @param string JSON string as produced by {@link IJSONSerializable#toJSONString()}
 * @param ctx ValidationContext
 * @return IJSONSerializable
 */
public final IJSONSerializable<?> fromString(String string, ValidationContext ctx) {
    if ((null == string) || (string = string.trim()).isEmpty()) {
        return null;
    }
    JSONValue value = JSONParser.parseStrict(string);

    if (null == value) {
        return null;
    }
    JSONObject json = value.isObject();

    if (null == json) {
        return null;
    }
    try {
        return fromJSON(json, ctx);
    } catch (ValidationException e) {
        return null;
    }
}

From source file:com.emitrom.lienzo.client.core.shape.json.JSONDeserializer.java

License:Open Source License

/**
 * Creates the child nodes for a {@link IJSONSerializable} that implements 
 * {@link IContainer} from a JSONObject node.
 * <p>/*from  w  ww . j  a  va 2  s. c o m*/
 * You should only call this when you're writing your own {@link IContainer} class
 * and you're building a custom {@link IFactory}.
 * 
 * @param g IContainer
 * @param node parent JSONObject
 * @param containerFactory IContainerFactory
 * @param ctx ValidationContext
 * @throws ValidationException
 */
@SuppressWarnings("unchecked")
public final void deserializeChildren(@SuppressWarnings("rawtypes") IContainer g, JSONObject node,
        IContainerFactory containerFactory, ValidationContext ctx) throws ValidationException {
    // TODO I couldn't get the template parameters of IContainer to work with
    // GroupFactory and LayerFactory, so I'll use @SuppressWarnings

    JSONValue kidsVal = node.get("children");

    if (kidsVal == null) {
        return; // OK - 'children' is optional
    }
    ctx.push("children");

    JSONArray arr = kidsVal.isArray();

    if (arr == null) {
        ctx.addBadTypeError("Array");
    } else {
        final int size = arr.size();

        for (int i = 0, n = size; i < n; i++) {
            ctx.pushIndex(i);

            JSONValue kidVal = arr.get(i);

            JSONObject kidObj = kidVal.isObject();

            if (kidObj == null) {
                ctx.addBadTypeError("Object");
            } else {
                IJSONSerializable<?> kidNode = fromJSON(kidObj, ctx);

                if (kidNode != null) {
                    if (containerFactory.isValidForContainer(g, kidNode)) {
                        g.add(kidNode);
                    }
                }
            }
            ctx.pop(); // index
        }
    }
    ctx.pop(); // children
}

From source file:com.emitrom.lienzo.client.core.shape.json.validators.ObjectValidator.java

License:Open Source License

@Override
public void validate(JSONValue jval, ValidationContext ctx) throws ValidationException {
    if (null == jval) {
        ctx.addBadTypeError("Object");

        return;/*w  w w.  j  a v  a 2  s  .  c  o m*/
    }
    JSONObject jobj = jval.isObject();

    if (null == jobj) {
        ctx.addBadTypeError("Object");
    } else {
        Set<String> keys = jobj.keySet();

        // Check required attributes

        for (String attrName : m_requiredAttributes) {
            ctx.push(attrName);

            if (false == keys.contains(attrName)) {
                ctx.addRequiredError(); // value is missing
            } else {
                JSONValue aval = jobj.get(attrName);

                if ((null == aval) || (null != aval.isNull())) {
                    ctx.addRequiredError(); // value is null
                }
            }
            ctx.pop(); // attrName
        }
        // Now check the attribute values

        for (String attrName : keys) {
            ctx.push(attrName);

            AttributeTypeValidator validator = m_attributes.get(attrName);

            if (null == validator) {
                ctx.addInvalidAttributeError(m_typeName);
            } else {
                JSONValue aval = jobj.get(attrName);

                validator.validate(aval, ctx);
            }
            ctx.pop(); // attrName
        }
    }
}

From source file:com.emitrom.lienzo.client.core.shape.Node.java

License:Open Source License

/**
 * Constructor used by deserialization code.
 * /*from w  w w  .  ja v  a 2s  .  c  o m*/
 * @param type
 * @param node
 */
protected Node(NodeType type, JSONObject node) {
    m_type = type;

    if (null == node) {
        m_attr = Attributes.make();

        return;
    }
    JSONValue aval = node.get("attributes");

    if (null == aval) {
        m_attr = Attributes.make();

        return;
    }
    JSONObject aobj = aval.isObject();

    if (null == aobj) {
        m_attr = Attributes.make();

        return;
    }
    JavaScriptObject ajso = aobj.getJavaScriptObject();

    if (null == ajso) {
        m_attr = Attributes.make();

        return;
    }
    m_attr = ajso.cast();

    if (NativeInternalType.BOOLEAN != m_attr.typeOf(Attribute.VISIBLE)) {
        setVisible(true);
    }
    if (NativeInternalType.BOOLEAN != m_attr.typeOf(Attribute.LISTENING)) {
        setListening(true);
    }
}

From source file:com.extjs.gxt.ui.client.data.JsonReader.java

License:sencha.com license

@SuppressWarnings({ "unchecked", "rawtypes" })
public D read(Object loadConfig, Object data) {
    JSONObject jsonRoot = null;/*from w w w . j a v  a 2  s .  c o m*/
    if (data instanceof JavaScriptObject) {
        jsonRoot = new JSONObject((JavaScriptObject) data);
    } else {
        jsonRoot = (JSONObject) JSONParser.parse((String) data);
    }
    JSONArray root = (JSONArray) jsonRoot.get(modelType.getRoot());
    int size = root.size();
    ArrayList<ModelData> models = new ArrayList<ModelData>();
    for (int i = 0; i < size; i++) {
        JSONObject obj = (JSONObject) root.get(i);
        ModelData model = newModelInstance();
        for (int j = 0; j < modelType.getFieldCount(); j++) {
            DataField field = modelType.getField(j);
            String name = field.getName();
            Class type = field.getType();
            String map = field.getMap() != null ? field.getMap() : field.getName();
            JSONValue value = obj.get(map);

            if (value == null)
                continue;
            if (value.isArray() != null) {
                // nothing
            } else if (value.isBoolean() != null) {
                model.set(name, value.isBoolean().booleanValue());
            } else if (value.isNumber() != null) {
                if (type != null) {
                    Double d = value.isNumber().doubleValue();
                    if (type.equals(Integer.class)) {
                        model.set(name, d.intValue());
                    } else if (type.equals(Long.class)) {
                        model.set(name, d.longValue());
                    } else if (type.equals(Float.class)) {
                        model.set(name, d.floatValue());
                    } else {
                        model.set(name, d);
                    }
                } else {
                    model.set(name, value.isNumber().doubleValue());
                }
            } else if (value.isObject() != null) {
                // nothing
            } else if (value.isString() != null) {
                String s = value.isString().stringValue();
                if (type != null) {
                    if (type.equals(Date.class)) {
                        if ("timestamp".equals(field.getFormat())) {
                            Date d = new Date(Long.parseLong(s) * 1000);
                            model.set(name, d);
                        } else {
                            DateTimeFormat format = DateTimeFormat.getFormat(field.getFormat());
                            Date d = format.parse(s);
                            model.set(name, d);
                        }
                    }
                } else {
                    model.set(name, s);
                }
            } else if (value.isNull() != null) {
                model.set(name, null);
            }
        }
        models.add(model);
    }
    int totalCount = models.size();
    if (modelType.getTotalName() != null) {
        totalCount = getTotalCount(jsonRoot);
    }
    return (D) createReturnData(loadConfig, models, totalCount);
}

From source file:com.extjs.gxt.ui.client.js.JsonConverter.java

License:sencha.com license

/**
 * Decodes a Json string into a map.//  w w  w . j a  va  2  s .c o  m
 * 
 * @param jsonString the Json string
 * @return the map
 */
public static Map<String, Object> decode(String jsonString) {
    JSONValue v = JSONParser.parse(jsonString);
    if (v.isObject() != null) {
        return decode(v.isObject());
    } else {
        return null;
    }
}

From source file:com.extjs.gxt.ui.client.js.JsonConverter.java

License:sencha.com license

/**
 * Decodes a JSONObject to a map./* w w w.j  a  v  a2  s . co  m*/
 * 
 * @param jso the JSONObject
 * @return the map
 */
public static Map<String, Object> decode(JSONObject jso) {
    Map<String, Object> map = new FastMap<Object>();
    for (String key : jso.keySet()) {
        JSONValue j = jso.get(key);
        if (j.isObject() != null) {
            map.put(key, decode(j.isObject()));
        } else if (j.isArray() != null) {
            map.put(key, decodeToList(j.isArray()));
        } else if (j.isBoolean() != null) {
            map.put(key, j.isBoolean().booleanValue());
        } else if (j.isString() != null) {
            map.put(key, decodeValue(j.isString().stringValue()));
        }
    }
    return map;
}