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

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

Introduction

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

Prototype

public JSONArray isArray() 

Source Link

Document

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

Usage

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)));
        }//  www  .  j  a v a 2 s. c o  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 inputObject, String fieldName, StatValues statValues) {
    JSONValue fieldValue = notNull(inputObject.get(fieldName));
    JSONArray fieldArray = notNull(fieldValue.isArray());
    if (fieldArray.size() != ZithiaStat.getNumStats()) {
        throw new JSONBuildException();
    }//from  w  ww.  java  2  s.com
    for (ZithiaStat zithiaStat : ZithiaStat.values()) {
        update(fieldArray.get(zithiaStat.ordinal()), statValues.getStat(zithiaStat));
    }
}

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

License:Apache License

protected void updateFromField(JSONObject inputObject, String fieldName, SkillList skillList) {
    JSONValue fieldValue = notNull(inputObject.get(fieldName));
    JSONArray fieldArray = notNull(fieldValue.isArray());
    skillList.clear();//  w w  w. jav a 2  s .com
    for (int i = 0; i < fieldArray.size(); i++) {
        JSONValue skillDataValue = fieldArray.get(i);
        JSONObject skillDataObj = notNull(skillDataValue.isObject());
        JSONValue zithiaSkillValue = notNull(skillDataObj.get("skill"));
        ZithiaSkill zithiaSkill = lookupSkill(zithiaSkillValue);
        SkillValue result = skillList.addNewSkill(zithiaSkill);
        updateFromField(skillDataObj, "levels", result.getLevels());
        if (zithiaSkill.hasRoll()) {
            updateFromField(skillDataObj, "roll", result.getRoll());
        }
        updateFromField(skillDataObj, "cost", result.getCost());
    }
}

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

License:Apache License

/**
 * This is used for reading the top-level WeaponTraining. The input must
 * specify a weaponSkill that matches the skill in wt. The WeaponTraining
 * passed must be newly created or have had .clean() called on it before
 * calling this.//from   w w  w .ja va 2  s.c  o  m
 */
protected void update(JSONValue input, WeaponTraining wt) {
    JSONObject inputObject = notNull(input.isObject());
    WeaponSkill weaponSkillFound = lookupWeaponSkill(notNull(inputObject.get("weaponSkill")));
    if (wt.getWeaponSkill() != weaponSkillFound) {
        throw new JSONBuildException();
    }
    updateFromField(inputObject, "basicTrainingDesired", wt.getBasicTrainingDesired());
    updateFromField(inputObject, "levelsPurchased", wt.getLevelsPurchased());
    updateFromField(inputObject, "levels", wt.getLevels());
    updateFromField(inputObject, "thisCost", wt.getThisCost());
    updateFromField(inputObject, "totalCost", wt.getTotalCost());
    JSONValue childrenValue = inputObject.get("children");
    if (childrenValue != null) {
        JSONArray childrenArray = notNull(childrenValue.isArray());
        for (int i = 0; i < childrenArray.size(); i++) {
            JSONValue childValue = childrenArray.get(i);
            JSONObject childObject = notNull(childValue.isObject());
            JSONValue childWeaponSkillValue = notNull(childObject.get("weaponSkill"));
            WeaponSkill childWeaponSkill = lookupWeaponSkill(childWeaponSkillValue);
            WeaponTraining newChild = wt.createChild(childWeaponSkill);
            update(childValue, newChild);
        }
    }
}

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

License:Apache License

protected void updateFromField(JSONObject inputObject, String fieldName, TalentList talentList) {
    JSONValue fieldValue = inputObject.get(fieldName);
    talentList.clear();/*from w  ww  .ja va2  s .  c o m*/
    if (fieldValue != null) {
        JSONArray fieldArray = notNull(fieldValue.isArray());
        for (int i = 0; i < fieldArray.size(); i++) {
            JSONObject talentDataObj = notNull(fieldArray.get(i).isObject());
            TalentValue talentValue = new TalentValue();
            updateFromField(talentDataObj, "description", talentValue.getDescription());
            updateFromField(talentDataObj, "cost", talentValue.getCost());
            talentList.add(talentValue);
        }
    }
}

From source file:com.moesol.gwt.maps.client.tms.FeatureReader.java

License:Open Source License

private List<Feature> parseFeatures(String text) {
    ArrayList<Feature> features = new ArrayList<Feature>();
    try {/*  w ww.  j  a v  a 2 s .  c o  m*/
        JSONArray array = null;
        JSONValue parsedValue = JSONParser.parseStrict(text);
        if (m_root != null) {
            // should be an object with an array with property m_root
            JSONObject rootObj = parsedValue.isObject();
            if (rootObj != null) {
                JSONValue arrayValue = rootObj.get(m_root);
                if (arrayValue != null) {
                    array = arrayValue.isArray();
                }
            }
        } else {
            array = parsedValue.isArray();
        }

        if (array != null) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = (JSONObject) array.get(i);

                String title = getStringProperty(m_titlePropertyName, obj);
                double lat = getDoubleProperty(m_latPropertyName, obj);
                double lng = getDoubleProperty(m_lngPropertyName, obj);
                String iconUrl = getStringProperty(m_iconPropertyName, obj);
                if (m_iconPropertyConverter != null) {
                    iconUrl = m_iconPropertyConverter.convert(iconUrl);
                }
                Feature feature = new Feature.Builder(title, lat, lng).iconUrl(iconUrl).build();
                features.add(feature);
            }
        }
    } catch (Throwable t) {
        Logger.getLogger(FeatureReader.class.getName()).log(Level.SEVERE, "Failed parsing features", t);
    }

    return features;
}

From source file:com.palmagroup.gwt.orders.client.gin.JsonConverter.java

License:Open Source License

/**
 * Decodes a JSONObject to a map./*from   w  w w . j a va  2  s.  c o 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.isNumber() != null) {
            map.put(key, j.isNumber().doubleValue());
        } else if (j.isString() != null) {
            map.put(key, decodeValue(j.isString().stringValue()));
        }
    }
    return map;
}

From source file:com.parabay.client.utils.JSONCodec.java

License:Apache License

/**
 * Converts a JSONValue to a Java object.
 * /*from   w  w w .  ja  v  a2s.co m*/
 * @param value
 * @return
 */
private Object buildJavaObjectFromJSONValue(JSONValue value) {
    if (value.isNull() != null) {
        return null;
    }
    if (value.isBoolean() != null) {
        return Boolean.valueOf(value.isBoolean().booleanValue());
    }
    if (value.isString() != null) {
        return value.isString().stringValue();
    }
    if (value.isNumber() != null) {
        return buildNumber(value.isNumber().toString());
    }
    if (value.isArray() != null) {
        return buildJavaArrayFromJSONArray(value.isArray());
    }
    if (value.isObject() != null) {
        return buildJavaMapFromJSONObject(value.isObject());
    }
    return null;
}

From source file:com.qualogy.qafe.gwt.client.util.JSNIUtil.java

License:Apache License

/**
 * Convert JavaScript objects to their Java representations.
 *///  ww w. j a v a 2  s .c om
static Object resolveJavaValue(JSONValue jsonValue) {
    if (jsonValue == null) {
        return null;
    }
    Object value = null;
    if (jsonValue.isArray() != null) {
        value = resolveJavaList(jsonValue.isArray());
    } else if (jsonValue.isObject() != null) {
        value = resolveJavaMap(jsonValue.isObject());
    } else if (jsonValue.isBoolean() != null) {
        value = jsonValue.isBoolean().booleanValue();
    } else if (jsonValue.isNumber() != null) {
        value = jsonValue.isNumber().doubleValue();
    } else if (jsonValue.isString() != null) {
        value = jsonValue.isString().stringValue();
    }
    return value;
}