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.kk_electronic.kkportal.core.rpc.jsonformat.JsonMap.java

License:Open Source License

@Override
public Map<String, V> fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject == null)
        throw new UnableToDeserialize("Expected Json Object");
    if (subtypes.get(0) != String.class)
        throw new UnableToDeserialize("Json Maps only supports Strings as keys");
    subtypes = subtypes.subList(1, subtypes.size());
    Map<String, V> map = new HashMap<String, V>();
    for (String key : jsonObject.keySet()) {
        V result = null;//from   www  . j  a  va 2  s  .  c o  m
        result = encoder.validate(jsonObject.get(key), result, subtypes.toArray(new Class<?>[subtypes.size()]));
        map.put(key, result);
    }
    return map;
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonModuleInfo.java

License:Open Source License

@Override
public ModuleInfo fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject == null)
        throw new UnableToDeserialize("Expected json Object");
    Integer id = null;/*from   www. j  av a  2s.  c o m*/
    id = encoder.validate(jsonObject.get("module_id"), id, new Class<?>[] { Integer.class });
    Integer type = null;
    type = encoder.validate(jsonObject.get("type_id"), type, new Class<?>[] { Integer.class });
    Integer height = null;
    height = encoder.validate(jsonObject.get("height"), height, new Class<?>[] { Integer.class });
    return new ModuleInfoDTO(id, type, height);
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonRpcEnvelope.java

License:Open Source License

@Override
public RpcEnvelope fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject == null)
        throw new UnableToDeserialize("Identity must be an Json Object");
    if (jsonObject.containsKey("result"))
        return responseFromJson(jsonObject, encoder);
    if (jsonObject.containsKey("method"))
        return requestFromJson(jsonObject, encoder);
    if (jsonObject.containsKey("error"))
        return errorFromJson(jsonObject, encoder);
    throw new UnableToDeserialize("Json Rpc Envelope must contain either result,error or method");
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonTabInfo.java

License:Open Source License

@Override
public TabInfo fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject == null)
        throw new UnableToDeserialize("Expected json Object");

    Integer id = null;/*from  w w  w.  j ava 2s .com*/
    String name = null;
    List<List<Integer>> moduleIds = null;

    id = encoder.validate(jsonObject.get("tab_id"), id, new Class<?>[] { Integer.class });
    name = encoder.validate(jsonObject.get("name"), name, new Class<?>[] { String.class });
    moduleIds = encoder.validate(jsonObject.get("module_ids"), moduleIds,
            new Class<?>[] { List.class, List.class, Integer.class });

    return new TabInfo(id, name, moduleIds);
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonTimeEntry.java

License:Open Source License

@Override
public TimeEntry fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    if (jsonValue.isNull() != null)
        return null;
    if (jsonValue.isObject() == null)
        throw new UnableToDeserialize("Expected Json Object");
    JSONObject o = jsonValue.isObject();

    Long checkin = null;//  w w w . ja va2s  . c  o m
    Long checkout = null;
    Integer taskid = null;
    Integer id = null;
    checkin = encoder.validate(o.get("checkin"), checkin, new Class<?>[] { Long.class });
    checkout = encoder.validate(o.get("checkout"), checkout, new Class<?>[] { Long.class });
    taskid = encoder.validate(o.get("taskId"), taskid, new Class<?>[] { Integer.class });
    id = encoder.validate(o.get("id"), id, new Class<?>[] { Integer.class });
    TimeEntry t = new TimeEntry(checkin, checkout, taskid);
    t.setId(id);
    return t;
}

From source file:com.kk_electronic.kkportal.core.rpc.SimpleEncoder.java

License:Open Source License

private static Object convert(JSONValue y, ObjectHelper objectHelper) {
    if (y.isArray() != null)
        return convert(y.isArray(), objectHelper);
    if (y.isObject() != null)
        return convert(y.isObject(), objectHelper);
    if (y.isBoolean() != null)
        return y.isBoolean().booleanValue();
    if (y.isString() != null)
        return y.isString().stringValue();
    if (y.isNumber() != null)
        return y.isNumber().doubleValue();
    if (y.isNull() != null)
        return null;
    throw new RuntimeException("Could not convert");
}

From source file:com.kk_electronic.kkportal.core.rpc.SimpleEncoder.java

License:Open Source License

@SuppressWarnings("unchecked")
private <T> T decodeFromJson(List<Class<?>> resultSubTypes, JSONValue jsonvalue) {
    if (jsonvalue == null || jsonvalue.isNull() != null)
        return null;
    Class<?> target = resultSubTypes.get(0);
    if (target == List.class) {
        List<T> list = new ArrayList<T>();
        JSONArray ja = jsonvalue.isArray();
        for (int i = 0, l = ja.size(); i < l; i++) {
            list.add((T) decodeFromJson(resultSubTypes.subList(1, resultSubTypes.size()), ja.get(i)));
        }//from w ww.j  av  a  2  s  .co  m
        return (T) list;
    }
    if (target == Map.class) {
        if (resultSubTypes.get(1) == String.class) {
            Map<String, Object> map = new HashMap<String, Object>();
            JSONObject jo = jsonvalue.isObject();
            for (String key : jo.keySet()) {
                map.put(key, decodeFromJson(resultSubTypes.subList(2, resultSubTypes.size()), jo.get(key)));
            }
            return (T) map;
        }
    }
    if (target == Response.class) {
        JSONObject jo = jsonvalue.isObject();
        return (T) new ResponseDTO(jo);
    }
    if (target == String.class) {
        return (T) jsonvalue.isString().stringValue();
    }
    if (target == TabInfo.class) {
        return (T) new TabInfoDTO(jsonvalue.isObject());
    }
    if (target == ModuleInfo.class) {
        return (T) new ModuleInfoDTO(jsonvalue.isObject());
    }
    if (target == Object.class) {
        return (T) jsonvalue;
    }
    GWT.log("DECODING-Can't convert type " + target.getName());
    return null;
}

From source file:com.mcherm.zithiacharsheet.client.model.JSONDeserializer.java

License:Apache License

protected void updateFromField(JSONObject parent, String fieldName, TweakableIntValue tweakableIntValue) {
    JSONValue value = parent.get(fieldName);
    if (value == null) {
        tweakableIntValue.setAdjustments(null, null);
    } else {// w  w w  .  j  a va2 s .co m
        JSONObject obj = notNull(value.isObject());
        final Integer overrideInt;
        final Integer modifierInt;
        JSONValue overrideValue = obj.get("override");
        if (overrideValue == null) {
            overrideInt = null;
        } else {
            JSONNumber overrideNum = notNull(overrideValue.isNumber());
            overrideInt = Integer.valueOf((int) overrideNum.doubleValue());
        }
        JSONValue modifierValue = obj.get("modifier");
        if (modifierValue == null) {
            modifierInt = null;
        } else {
            JSONNumber modifierNum = notNull(modifierValue.isNumber());
            modifierInt = Integer.valueOf((int) modifierNum.doubleValue());
        }
        tweakableIntValue.setAdjustments(overrideInt, modifierInt);
    }
}

From source file:com.mcherm.zithiacharsheet.client.model.JSONDeserializer.java

License:Apache License

protected void update(JSONValue input, StatValue statValue) {
    JSONObject inputObj = notNull(input.isObject());
    updateFromField(inputObj, "value", statValue.getValue());
    updateFromField(inputObj, "roll", statValue.getRoll());
    updateFromField(inputObj, "cost", statValue.getCost());
}

From source file:com.mcherm.zithiacharsheet.client.model.JSONDeserializer.java

License:Apache License

protected ZithiaSkill lookupSkill(JSONValue input) {
    JSONObject inputObj = notNull(input.isObject());
    JSONValue idValue = notNull(inputObj.get("id"));
    JSONString idString = notNull(idValue.isString());
    String id = idString.stringValue();
    return notNull(SkillCatalog.get(id));
}