List of usage examples for com.google.gwt.json.client JSONValue isNull
public JSONNull isNull()
From source file:com.ait.lienzo.client.core.shape.json.JSONDeserializer.java
License:Open Source License
protected final void validateAttributes(JSONObject json, IFactory<?> factory, String type, ValidationContext ctx) throws ValidationException { JSONValue aval = json.get("attributes"); if (null == aval) { return; // OK - 'attributes' is optional }// w ww . j a v a2s . c o m ctx.push("attributes"); JSONObject aobj = aval.isObject(); if (aobj == null) { ctx.addBadTypeError("Object"); return; } else { // Make sure all required attributes are defined (and not null) Set<String> keys = aobj.keySet(); for (Attribute attr : factory.getRequiredAttributes()) { String attrName = attr.getProperty(); ctx.push(attrName); if (false == keys.contains(attrName)) { ctx.addRequiredError(); // value is missing } else { JSONValue jval = aobj.get(attrName); if (((jval == null) || (jval.isNull() != null))) { ctx.addRequiredError(); // value is null } } ctx.pop(); // attrName } // Now check the attribute values for (String attrName : keys) { ctx.push(attrName); AttributeType atyp = factory.getAttributeType(attrName); if (atyp == null) { ctx.addInvalidAttributeError(type); } else { atyp.validate(aobj.get(attrName), ctx); } ctx.pop(); // attrName } } ctx.pop(); // attributes }
From source file:com.ait.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;/*ww w . ja va 2 s . com*/ } 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); IAttributeTypeValidator validator = m_attributes.get(attrName); if (null == validator) { ctx.addInvalidAttributeError(m_typeName); } else if (false == (validator instanceof IgnoreTypeValidator)) { JSONValue aval = jobj.get(attrName); validator.validate(aval, ctx); } ctx.pop(); // attrName } } }
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;//from w ww .j a v a 2s . c om } 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.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 ww.ja v a2 s . com 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
protected static List<Object> decodeToList(JSONArray array) { List<Object> list = new ArrayList<Object>(); for (int i = 0; i < array.size(); i++) { JSONValue v = array.get(i); if (v.isObject() != null) { list.add(decode(v.isObject())); } else if (v.isArray() != null) { list.add(decodeToList(v.isArray())); } else if (v.isNull() != null) { list.add(null);//from w w w . j a v a2s . c o m } else if (v.isNumber() != null) { list.add(v.isNumber().doubleValue()); } else if (v.isBoolean() != null) { list.add(v.isBoolean().booleanValue()); } else if (v.isString() != null) { list.add(decodeValue(v.isString().stringValue())); } } return list; }
From source file:com.github.ligangty.common.highconvert.BaseChart.java
License:Apache License
private JavaScriptObject createNativeOptions() { JSONObject options = getOptions();//from w w w .j a va2 s . c o m if (options == null) { options = new JSONObject(); } // #1: We need to merge the options they provided with the additional detail we know internally, // and first we need to set which DOM element the chart should be rendered within JSONValue chartValue = options.get("chart"); if (chartValue == null || chartValue.isObject() == null) { chartValue = new JSONObject(); options.put("chart", chartValue); } final JSONObject chartObject = (JSONObject) options.get("chart"); chartObject.put("renderTo", new JSONString(this.getElement().getId())); // #2: We need to setup whatever data series needs to be rendered initially in the chart if (seriesList.size() > 0) { final JSONValue seriesValue = options.get("series"); if (seriesValue == null || seriesValue.isArray() == null) { options.put("series", new JSONArray()); } final JSONArray seriesArray = (JSONArray) options.get("series"); for (int i = 0, seriesListSize = seriesList.size(); i < seriesListSize; i++) { Series series = seriesList.get(i); JSONObject seriesOptions = convertSeriesToJSON(series); seriesArray.set(i, seriesOptions); } } // In the case that they're going to wait to access the X/Y axis after the chart is rendered, // we need to call the 'get' methods once (at least for the primary X/Y axis) so that the ids // are set on the Highcharts options correctly this.getXAxis(); this.getYAxis(); // #3: We need to add references to our axis so that we can later lookup the // axis by id (as well as pass along any configuration options that were applied to the axis) final JSONValue xAxisJSONValue = convertToJSONValue(xAxes.toArray(new Configurable[xAxes.size()])); if (xAxisJSONValue != null && xAxisJSONValue.isNull() == null) { options.put("xAxis", xAxisJSONValue); } final JSONValue yAxisJSONValue = convertToJSONValue(yAxes.toArray(new Configurable[yAxes.size()])); if (yAxisJSONValue != null && yAxisJSONValue.isNull() == null) { options.put("yAxis", yAxisJSONValue); } // For debugging the raw options that we're passing to the chart on startup, uncomment the following line // com.google.gwt.user.client.Window.alert(options.toString()); return options.getJavaScriptObject(); }
From source file:com.google.api.explorer.client.parameter.schema.ObjectElement.java
License:Apache License
@Override public void setJSONValue(JSONValue value) { if (value.isNull() != null) { isNull = true;/*ww w .ja v a 2 s.c o m*/ } else { isNull = false; innerEditor.setJSONValue(value); } updateDisplay(); }
From source file:com.google.gerrit.client.rpc.RestApi.java
License:Apache License
@SuppressWarnings("unchecked") private static <T extends JavaScriptObject> T cast(JSONValue val) { if (val.isObject() != null) { return (T) val.isObject().getJavaScriptObject(); } else if (val.isArray() != null) { return (T) val.isArray().getJavaScriptObject(); } else if (val.isString() != null) { return (T) NativeString.wrap(val.isString().stringValue()); } else if (val.isNull() != null) { return null; } else {//from w w w. jav a 2 s . co m throw new JSONException("unsupported JSON type"); } }
From source file:com.google.gson.JsonElement.java
License:Apache License
static JsonElement wrap(JSONValue value) { if (value == null) return null; if (value.isNull() != null) return JsonNull.createJsonNull(); if (value.isBoolean() != null || value.isNumber() != null || value.isString() != null) return new JsonPrimitive(value); if (value.isObject() != null) return new JsonObject(value.isObject()); if (value.isArray() != null) return new JsonArray(value.isArray()); throw new IllegalArgumentException(); }
From source file:com.guit.client.jsorm.AbstractSerializer.java
License:Apache License
@Override public final T deserialize(JSONValue data) { if (data.isNull() == null) { return deserializeJson(data); }/* w ww.j a va 2s.c o m*/ return null; }