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:com.data2semantics.yasgui.client.helpers.JsonHelper.java

License:Open Source License

/**
 * Gets string from json object/*from   w w w.ja  v  a 2  s  .c om*/
 * 
 * @param key
 * @return null if string is empty, or key is not found
 */
protected String getString(String key) {
    String result = null;
    if (containsKey(key)) {
        JSONValue resultValue = get(key);
        JSONString resultString = resultValue.isString();
        if (resultString != null && resultString.stringValue().length() > 0) {
            result = resultString.stringValue();
        }
    }
    return result;
}

From source file:com.data2semantics.yasgui.client.tab.optionbar.endpoints.EndpointDataSource.java

License:Open Source License

private void addEndpointFromJson(JSONObject endpoint) throws Exception {
    ListGridRecord record = new ListGridRecord();
    Set<String> keys = endpoint.keySet();
    for (String key : keys) {
        JSONValue value = endpoint.get(key);
        if (value != null) {
            JSONString stringJsonVal = value.isString();
            if (stringJsonVal != null) {
                record.setAttribute(key, stringJsonVal.stringValue());
            }//from  w w w  .  j  a v a 2  s.  c om
        }
    }

    records.add(record);
}

From source file:com.data2semantics.yasgui.client.tab.results.input.JsonResults.java

License:Open Source License

/**
 * Gets JSON value as string, and throws exception when value is null
 * /*w  ww  .  jav  a2 s . co  m*/
 * @param jsonValue
 * @param message
 * @return
 * @throws SparqlParseException
 */
public String getAsString(JSONValue jsonValue) throws SparqlParseException {
    JSONString jsonString = jsonValue.isString();
    if (jsonString == null) {
        throw new SparqlParseException("Cannot format value as string");
    }
    return jsonString.stringValue();
}

From source file:com.dawg6.web.dhcalc.client.JsonUtil.java

License:Open Source License

public static Map<String, String> parseMap(JSONValue jsonValue) {
    JSONObject obj = jsonValue.isObject();
    Map<String, String> map = new TreeMap<String, String>();

    if (obj != null) {

        for (String key : obj.keySet()) {
            JSONValue value = obj.get(key);

            if (value != null) {
                JSONString str = value.isString();

                if (str != null) {
                    map.put(key, str.stringValue());
                }/*from   w ww . j a v  a  2  s  . c  o  m*/
            }
        }
    }

    return map;
}

From source file:com.dawg6.web.dhcalc.client.JsonUtil.java

License:Open Source License

public static Version parseVersion(JSONValue jsonValue) {
    JSONObject obj = jsonValue.isObject();

    Version version = new Version();

    if (obj != null) {
        JSONValue value = obj.get("version");

        if (value != null) {
            JSONString str = value.isString();

            if (str != null) {
                version.version = str.stringValue();
            }/*  w  ww .  j  a v a  2 s . c o m*/
        }

    }

    return version;
}

From source file:com.dawg6.web.dhcalc.client.JsonUtil.java

License:Open Source License

public static <T extends Enum<T>> Set<T> parseSet(Class<T> clazz, String text) {
    if ((text == null) || (text.trim().length() == 0))
        return null;

    Set<T> set = new TreeSet<T>();

    JSONValue value = JSONParser.parseLenient(text);

    JSONArray array = value.isArray();/*from   ww w. j a va 2s  . co  m*/

    if (array == null)
        return null;

    for (int i = 0; i < array.size(); i++) {
        JSONValue e = array.get(i);

        if (e != null) {
            JSONString str = e.isString();

            if (str != null) {
                String name = str.stringValue();

                if (name != null) {
                    T elem = Enum.valueOf(clazz, name);

                    if (elem != null) {
                        set.add(elem);
                    }
                }
            }
        }
    }

    return set;
}

From source file:com.dimdim.ui.common.client.data.SelectListOption.java

License:Open Source License

private String getProperty(String propName) {
    JSONValue v = jsonObject.get(propName);
    if (v != null) {
        JSONString s = v.isString();
        if (s != null) {
            return s.stringValue();
        }/*from  w  w  w.j  av  a 2 s.c om*/
    }
    return "";
}

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

License:Apache License

public String getString(String key) {
    JSONValue jv;
    try {//from  w  w w.  j  a  v a 2s.  com
        jv = super.get(key);
    } catch (NullPointerException e) {
        return null;
    }
    if (jv != null && jv.isString() != null)
        return jv.isString().stringValue();
    return null;
}

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

License:Open Source License

/**
 * Creates a IJSONSerializable from the JSONObject, using the ValidationContext.
 * <p>//  w w w.  j a v  a 2s .  c  om
 * You should only call this when you're writing your own node class
 * and you're building a custom {@link IFactory}.
 * 
 * @param json JSONObject
 * @param ctx ValidationContext
 * @return IJSONSerializable
 * @throws ValidationException
 */
public final IJSONSerializable<?> fromJSON(JSONObject json, ValidationContext ctx) throws ValidationException {
    if (null == json) {
        return null;
    }
    String type = null;

    IFactory<?> factory = null;

    JSONValue tval = json.get("type");

    ctx.push("type");

    if (null == tval) {
        ctx.addRequiredError();
    } else {
        JSONString styp = tval.isString();

        if (null == styp) {
            ctx.addBadTypeError("String");
        } else {
            type = styp.stringValue();

            factory = FactoryRegistry.getInstance().getFactory(type);

            if (null == factory) {
                ctx.addMissingNodeFactoryError(type);
            }
        }
    }
    ctx.pop(); // type

    if (null == factory) {
        return null;
    } else {
        if (ctx.isValidate()) {
            // we don't need to validate during a copy operation!

            validateAttributes(json, factory, type, ctx);
        }
        if (factory instanceof PostProcessNodeFactory) {
            IJSONSerializable<?> node = factory.create(json, ctx);

            if (null != node) {
                ((PostProcessNodeFactory) factory).process(node);
            }
            return node;
        } else {
            return factory.create(json, ctx);
        }
    }
}

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;/*w  ww  . 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);
}