List of usage examples for com.google.gwt.json.client JSONValue isObject
public JSONObject isObject()
From source file:cl.uai.client.data.AjaxRequest.java
License:Open Source License
/** * Assuming a json string (PHP json_encode() format), it transforms the string to a JSONObject. * @param Json string to parse.// ww w . j av a 2s .c o m * @param Header of the string (this fix the json_encode() PHP format). * @param Numeric key of the json collection to get the values. * @param * @return a string value of defined key */ public static List<Map<String, String>> getValuesFromJsonString(String json, String header) { //Defining output List<Map<String, String>> output = new ArrayList<Map<String, String>>(); //Define keys to get from the markers json array List<String> markersKeys = Arrays.asList("id", "username", "firstname", "lastname"); //fix the json format for JSONObject to understand it String jsonString = "{\"" + header + "\":" + json + "}"; //Set json string as a JSONValue JSONValue markersValue = JSONParser.parseStrict(jsonString); JSONObject markersObject; JSONArray markersArray; JSONString jsonStringOutput; //Object checking if ((markersObject = markersValue.isObject()) == null) { logger.severe("Error parsing the JSONObject"); } //Array checking markersValue = markersObject.get(header); if ((markersArray = markersValue.isArray()) == null) { logger.severe("Error parsing the JSONArray"); } //Loop for getting each marker info, creating markers info array for (int i = 0; i < markersArray.size(); i++) { Map<String, String> obj = new HashMap<String, String>(); //Object checking for defined numeric key of the array markersValue = markersArray.get(i); if ((markersObject = markersValue.isObject()) == null) { logger.severe("Error parsing the JSONValue"); } //String checking for "id", "username", "firstname", "lastname" //Loop for getting the info values for each marker for (int j = 0; j < markersKeys.size(); j++) { markersValue = markersObject.get(markersKeys.get(j).toString()); if ((jsonStringOutput = markersValue.isString()) == null) { logger.severe("Error parsing the JSONString"); } obj.put(markersKeys.get(j).toString(), jsonStringOutput.stringValue()); } output.add(obj); } //Return markers array output return output; }
From source file:colt.json.gwt.client.JsonClient.java
License:Apache License
public void invoke(final String _url, final String _serviceName, String requestData, final IAsyncJSON result) { try {//from w w w . j a v a 2s . c o m RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, URL.encode(_url + _serviceName)); requestBuilder.setHeader("content-type", "application/x-www-form-urlencoded"); Request request = requestBuilder.sendRequest(requestData, new RequestCallback() { public void onError(Request request, Throwable exception) { result.error(exception); } public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { String text = response.getText(); JSONValue parser = JSONParser.parse(text); JSONObject jobj = parser.isObject(); result.done(jobj); } else { result.error(new RuntimeException(_url + _serviceName + " :(")); } } }); } catch (RequestException e) { Window.alert(e.getMessage()); result.error(e); } }
From source file:com.ait.lienzo.client.core.image.filter.AbstractImageDataFilter.java
License:Open Source License
protected AbstractImageDataFilter(JSONObject node, ValidationContext ctx) throws ValidationException { m_type = ClassUtils.getSimpleName(getClass()); if (null == node) { m_attr = new Attributes(this); m_meta = new MetaData(); return;// ww w .j a v a 2 s. c o m } JSONValue aval = node.get("attributes"); if (null == aval) { m_attr = new Attributes(this); } else { JSONObject aobj = aval.isObject(); if (null == aobj) { m_attr = new Attributes(this); } else { JavaScriptObject ajso = aobj.getJavaScriptObject(); if (null == ajso) { m_attr = new Attributes(this); } else { m_attr = new Attributes(ajso, this); } } } JSONValue mval = node.get("meta"); if (null == mval) { m_meta = new MetaData(); } else { JSONObject mobj = mval.isObject(); if (null == mobj) { m_meta = new MetaData(); } else { JavaScriptObject mjso = mobj.getJavaScriptObject(); if (null == mjso) { m_meta = new MetaData(); } else { NFastStringMapMixedJSO jso = mjso.cast(); m_meta = new MetaData(jso); } } } }
From source file:com.ait.lienzo.client.core.palette.AbstractPaletteBase.java
License:Open Source License
protected AbstractPaletteBase(final PaletteType type, final JSONObject node, final ValidationContext ctx) throws ValidationException { m_type = type;//w w w. j a va2s. c om if (null == node) { m_attr = new Attributes(this); m_meta = new MetaData(); return; } JSONValue aval = node.get("attributes"); if (null == aval) { m_attr = new Attributes(this); } else { JSONObject aobj = aval.isObject(); if (null == aobj) { m_attr = new Attributes(this); } else { JavaScriptObject ajso = aobj.getJavaScriptObject(); if (null == ajso) { m_attr = new Attributes(this); } else { m_attr = new Attributes(ajso, this); } } } JSONValue mval = node.get("meta"); if (null == mval) { m_meta = new MetaData(); } else { JSONObject mobj = mval.isObject(); if (null == mobj) { m_meta = new MetaData(); } else { JavaScriptObject mjso = mobj.getJavaScriptObject(); if (null == mjso) { m_meta = new MetaData(); } else { NObjectJSO jso = mjso.cast(); m_meta = new MetaData(jso); } } } }
From source file:com.ait.lienzo.client.core.shape.json.JSONDeserializer.java
License:Open Source License
/** * Parses the JSON string and returns the IJSONSerializable. * If validate is true, it will attempt to validate the attributes and types of child nodes etc. * If validate is false, it assumes the JSON string is correct * (this is a little faster.)//from ww w.jav a 2 s .c o m * * @param string JSON string as produced by {@link IJSONSerializable#toJSONString()} * @param validate Whether to validate the attributes and child node types * @return IJSONSerializable */ public final IJSONSerializable<?> fromString(String string, boolean validate) { if ((null == string) || (string = string.trim()).isEmpty()) { return null; } JSONValue value = JSONParser.parseStrict(string); if (null == value) { return null; } JSONObject json = value.isObject(); if (null == json) { return null; } try { ValidationContext ctx = new ValidationContext(); ctx.setValidate(validate); ctx.setStopOnError(true); // bail if an error is encountered return fromJSON(json, ctx); } catch (ValidationException e) { return null; } }
From source file:com.ait.lienzo.client.core.shape.json.JSONDeserializer.java
License:Open Source License
/** * Parses the JSON string and returns the IJSONSerializable. * Use this method if you need to parse JSON that may contain one or more errors. * <pre>//from w ww .ja v a 2 s .co m * ValidationContext ctx = new ValidationContext(); * ctx.setValidate(true); * ctx.setStopOnError(false); // find all errors * IJSONSerializable<?> node = JSONDeserializer.getInstance().fromString(jsonString, ctx); * if (ctx.getErrorCount() > 0) * { * Console.log(ctx.getDebugString()); * } * </pre> * * @param string JSON string as produced by {@link IJSONSerializable#toJSONString()} * @param ctx ValidationContext * @return IJSONSerializable */ public final IJSONSerializable<?> fromString(String string, ValidationContext ctx) { try { ctx.push("fromString"); if ((null == string) || (string = string.trim()).isEmpty()) { ctx.addError("NULL JSON String"); return null; } JSONValue value = JSONParser.parseStrict(string); if (null == value) { ctx.addError("NULL from JSONParser"); return null; } JSONObject json = value.isObject(); if (null == json) { ctx.addError("Result is not a JSONObject"); return null; } return fromJSON(json, ctx); } catch (ValidationException e) { return null; } }
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 }/*from w w w . ja va2 s.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.JSONDeserializer.java
License:Open Source License
/** * Creates the child nodes for a {@link IJSONSerializable} that implements * {@link IContainer} from a JSONObject node. * <p>/*from ww w .ja va 2s . c om*/ * You should only call this when you're writing your own {@link IContainer} class * and you're building a custom {@link IFactory}. * * @param container IContainer * @param node parent JSONObject * @param factory IContainerFactory * @param ctx ValidationContext * @throws ValidationException */ public final void deserializeChildren(IContainer<?, ?> container, JSONObject node, IContainerFactory factory, ValidationContext ctx) throws ValidationException { JSONValue jsonvalu = node.get("children"); if (null == jsonvalu) { return; // OK - 'children' is optional } ctx.push("children"); JSONArray array = jsonvalu.isArray(); if (null == array) { ctx.addBadTypeError("Array"); } else { final int size = array.size(); for (int i = 0; i < size; i++) { ctx.pushIndex(i); jsonvalu = array.get(i); JSONObject object = jsonvalu.isObject(); if (null == object) { ctx.addBadTypeError("Object"); } else { IJSONSerializable<?> serial = fromJSON(object, ctx); if (null != serial) { if (serial instanceof Node) { if (false == factory.addNodeForContainer(container, (Node<?>) serial, ctx)) { ; } } else { ctx.addBadTypeError("Node"); } } } ctx.pop(); // index } } ctx.pop(); // children }
From source file:com.ait.lienzo.client.core.shape.json.JSONDeserializer.java
License:Open Source License
public final void deserializeFilters(ImageDataFilterable<?> filterable, JSONObject node, ValidationContext ctx) throws ValidationException { JSONValue jsonvalu = node.get("filters"); if (null == jsonvalu) { return; // OK - 'children' is optional }//from w w w . j a va 2 s .co m ctx.push("filters"); JSONArray array = jsonvalu.isArray(); if (null == array) { ctx.addBadTypeError("Array"); } else { final int size = array.size(); ArrayList<ImageDataFilter<?>> list = new ArrayList<ImageDataFilter<?>>(size); for (int i = 0; i < size; i++) { ctx.pushIndex(i); jsonvalu = array.get(i); JSONObject object = jsonvalu.isObject(); if (null == object) { ctx.addBadTypeError("Object"); } else { IJSONSerializable<?> serial = fromJSON(object, ctx); if (null != serial) { if (serial instanceof ImageDataFilter) { list.add((ImageDataFilter<?>) serial); } else { ctx.addBadTypeError("ImageDataFilter"); } } } ctx.pop(); // index } filterable.setFilters(list); } ctx.pop(); // children }
From source file:com.ait.lienzo.client.core.shape.json.validators.BehaviorMapValidator.java
License:Open Source License
@Override public void validate(JSONValue jval, ValidationContext ctx) throws ValidationException { if (null == jval) { ctx.addBadTypeError("BoundingBox"); return;//ww w.j a v a 2 s.c o m } JSONObject jobj = jval.isObject(); if (null == jobj) { ctx.addBadTypeError("BoundingBox"); } else { Set<String> keys = jobj.keySet(); if (keys.isEmpty()) { ctx.addBadTypeError("BoundingBox no keys"); return; } for (String ikey : keys) { if ((null == ikey) || (ikey.trim().isEmpty())) { ctx.addBadTypeError("BoundingBox bad key"); return; } jval = jobj.get(ikey); if (null == jval) { ctx.addBadTypeError("BoundingBox no array"); return; } JSONArray jarr = jval.isArray(); if (null == jarr) { ctx.addBadTypeError("BoundingBox not array"); return; } if (jarr.size() < 2) { ctx.addBadArraySizeError(2, jarr.size()); return; } BoundingBoxArrayValidator.INSTANCE.validate(jarr, ctx); } } }