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

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

Introduction

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

Prototype

public JSONNull isNull() 

Source Link

Document

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

Usage

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 w  w.j  a v  a 2s  . 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.parabay.client.utils.JSONCodec.java

License:Apache License

/**
 * Converts a JSONValue to a Java object.
 * /*from  w  w w .ja v  a2s.  com*/
 * @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.smartgwt.mobile.client.data.DataSource.java

License:Open Source License

private static RecordList extractRecordList(JSONArray dataArr, Map<String, DataSourceField> fields) {
    final DataSourceField pkField = getPrimaryKeyField(fields);
    final String pkFieldName = pkField == null ? "id" : pkField.getName();

    final RecordList records = new RecordList();
    for (int i = 0; i < dataArr.size(); ++i) {
        final JSONValue datumVal = dataArr.get(i);
        if (datumVal == null)
            continue;
        JSONObject datumObj = datumVal.isObject();
        if (datumObj != null) {
            records.add(extractRecord(datumObj, fields));
        } else {//from  ww  w .  j a va2  s .com
            if (datumVal.isNull() != null)
                continue;

            String idValue;
            JSONString datumStr = datumVal.isString();
            if (datumStr != null)
                idValue = datumStr.stringValue();
            else
                idValue = datumVal.toString();

            idValue = idValue.trim();
            final Record record = new Record();
            record.setAttribute(pkFieldName, idValue);
            records.add(record);
        }
    }

    return records;
}

From source file:com.smartgwt.mobile.client.data.DSResponse.java

License:Open Source License

DSResponse(DSRequest dsRequest, int status, JSONObject responseObj) {
    init(dsRequest);//from   w  w w. j  av  a  2  s.  c o  m
    Map<String, String> errors = null;

    if (responseObj != null) {
        JSONValue val = responseObj.get("status");
        JSONNumber num = (val == null ? null : val.isNumber());
        if (num != null) {
            status = (int) num.doubleValue();
        }
        val = responseObj.get("startRow");
        num = (val == null ? null : val.isNumber());
        if (num != null) {
            setStartRow(Integer.valueOf((int) num.doubleValue()));
        }
        val = responseObj.get("endRow");
        num = (val == null ? null : val.isNumber());
        if (num != null) {
            setEndRow(Integer.valueOf((int) num.doubleValue()));
        }
        val = responseObj.get("totalRows");
        num = (val == null ? null : val.isNumber());
        if (num != null) {
            setTotalRows(Integer.valueOf((int) num.doubleValue()));
        }
        val = responseObj.get("errors");
        JSONObject errorsObj = (val == null ? null : val.isObject());
        if (errorsObj != null) {
            errors = new HashMap<String, String>();
            for (final String key : errorsObj.keySet()) {
                val = errorsObj.get(key);
                if (val == null || val.isNull() != null)
                    continue;
                final String errorMessage;
                JSONString str = val.isString();
                if (str != null)
                    errorMessage = str.stringValue();
                else
                    errorMessage = val.toString();
                errors.put(key, errorMessage);
            }
        }
    }

    setStatus(status);
    if (errors != null)
        setErrors(errors);

    // No need to handle the response data here.
}

From source file:com.vaadin.client.communication.JsonDecoder.java

License:Apache License

/**
 * Decode a JSON array with two elements (type and value) into a client-side
 * type, recursively if necessary.// w  w w.  ja va2 s.  c om
 * 
 * @param jsonValue
 *            JSON value with encoded data
 * @param connection
 *            reference to the current ApplicationConnection
 * @return decoded value (does not contain JSON types)
 */
public static Object decodeValue(Type type, JSONValue jsonValue, Object target,
        ApplicationConnection connection) {

    // Null is null, regardless of type
    if (jsonValue.isNull() != null) {
        return null;
    }

    String baseTypeName = type.getBaseTypeName();
    if (Map.class.getName().equals(baseTypeName) || HashMap.class.getName().equals(baseTypeName)) {
        return decodeMap(type, jsonValue, connection);
    } else if (List.class.getName().equals(baseTypeName) || ArrayList.class.getName().equals(baseTypeName)) {
        return decodeList(type, (JSONArray) jsonValue, connection);
    } else if (Set.class.getName().equals(baseTypeName)) {
        return decodeSet(type, (JSONArray) jsonValue, connection);
    } else if (String.class.getName().equals(baseTypeName)) {
        return ((JSONString) jsonValue).stringValue();
    } else if (Integer.class.getName().equals(baseTypeName)) {
        return Integer.valueOf(String.valueOf(jsonValue));
    } else if (Long.class.getName().equals(baseTypeName)) {
        // TODO handle properly
        return Long.valueOf(String.valueOf(jsonValue));
    } else if (Float.class.getName().equals(baseTypeName)) {
        // TODO handle properly
        return Float.valueOf(String.valueOf(jsonValue));
    } else if (Double.class.getName().equals(baseTypeName)) {
        // TODO handle properly
        return Double.valueOf(String.valueOf(jsonValue));
    } else if (Boolean.class.getName().equals(baseTypeName)) {
        // TODO handle properly
        return Boolean.valueOf(String.valueOf(jsonValue));
    } else if (Byte.class.getName().equals(baseTypeName)) {
        // TODO handle properly
        return Byte.valueOf(String.valueOf(jsonValue));
    } else if (Character.class.getName().equals(baseTypeName)) {
        // TODO handle properly
        return Character.valueOf(((JSONString) jsonValue).stringValue().charAt(0));
    } else if (Connector.class.getName().equals(baseTypeName)) {
        return ConnectorMap.get(connection).getConnector(((JSONString) jsonValue).stringValue());
    } else {
        return decodeObject(type, jsonValue, target, connection);
    }
}

From source file:edu.nrao.dss.client.widget.explorers.Explorer.java

License:Open Source License

protected void addRecord(HashMap<String, Object> fields) {
    JSONRequest.post(rootURL, fields, new JSONCallbackAdapter() {
        @Override//w w w  .j  av  a  2 s. c  o  m
        public void onSuccess(JSONObject json) {
            BaseModelData model = new BaseModelData();
            for (int i = 0; i < modelType.getFieldCount(); ++i) {
                DataField field = modelType.getField(i);
                String fName = field.getName();
                if (json.containsKey(fName)) {
                    // Set model value dependent on data type
                    JSONValue value = json.get(fName);
                    if (value.isNumber() != null) {
                        double numValue = value.isNumber().doubleValue();
                        // Note: we're treating even integer values like doubles here.  Doesn't appear
                        // to be an issue.  1.0 is displayed as 1 anyways.
                        model.set(fName, numValue);
                    } else if (value.isBoolean() != null) {
                        model.set(fName, value.isBoolean().booleanValue());
                    } else if (value.isString() != null) {
                        model.set(fName, value.isString().stringValue());
                    } else if (value.isNull() != null) {
                        // not raising an error seems to be fine.
                    } else {
                        Window.alert("unknown JSON value type");
                    }
                }
            }
            grid.stopEditing();
            store.insert(model, 0);
            //grid.getView().refresh(true);
            grid.getSelectionModel().select(model, false);
        }
    });
}

From source file:es.deusto.weblab.client.lab.comm.LabSerializerJSON.java

License:Open Source License

@Override
public ResponseCommand parseSendCommandResponse(String responseText)
        throws SerializationException, SessionNotFoundException, NoCurrentReservationException,
        UserProcessingException, WebLabServerException {
    // "{\"result\": {\"commandstring\": null}, \"is_exception\": false}"
    final JSONObject result = this.parseResultObject(responseText);
    final JSONValue value = result.get("commandstring");
    if (value.isNull() != null)
        return new EmptyResponseCommand();
    final String commandString = this.json2string(value);
    return new ResponseCommand(commandString);
}

From source file:org.cruxframework.crux.core.client.utils.JsUtils.java

License:Apache License

/**
 * Extract the associated native javascript object from the given json object
 * @param jsonValue/*from   w ww. ja va  2  s.com*/
 * @return
 */
public static JavaScriptObject fromJSONValue(JSONValue jsonValue) {
    if (jsonValue.isNull() != null) {
        return null;
    }
    JSONArray jsonArray = jsonValue.isArray();
    if (jsonArray != null) {
        return jsonArray.getJavaScriptObject();
    }
    return jsonValue.isObject().getJavaScriptObject();
}

From source file:org.geomajas.gwt2.client.service.JsonService.java

License:Open Source License

/**
 * Get a child JSON object from a parent JSON object.
 * /*from   ww  w  .ja  v a2s.com*/
 * @param jsonObject The parent JSON object.
 * @param key The name of the child object.
 * @return Returns the child JSON object if it could be found, or null if the value was null.
 * @throws JSONException In case something went wrong while searching for the child.
 */
public static JSONObject getChild(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    if (value != null) {
        if (value.isObject() != null) {
            return value.isObject();
        } else if (value.isNull() != null) {
            return null;
        }
        throw new JSONException("Child is not a JSONObject, but a: " + value.getClass());
    }
    return null;
}

From source file:org.geomajas.gwt2.client.service.JsonService.java

License:Open Source License

/**
 * Get a child JSON array from a parent JSON object.
 * /*from   w  w  w .  j  av  a  2 s. c o m*/
 * @param jsonObject The parent JSON object.
 * @param key The name of the child object.
 * @return Returns the child JSON array if it could be found, or null if the value was null.
 * @throws JSONException In case something went wrong while searching for the child.
 */
public static JSONArray getChildArray(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    if (value != null) {
        if (value.isArray() != null) {
            return value.isArray();
        } else if (value.isNull() != null) {
            return null;
        }
        throw new JSONException("Child is not a JSONArray, but a: " + value.getClass());
    }
    return null;
}