public static JSONObject newJSONObject(String json) { try { return new JSONObject(json); } catch (JSONException e) { logger.warning("Assert Can't Happen in newJSONObject: " + e); return new JSONObject(); } }
/** * Put a value in the JSONArray, where the value will be a JSONObject which is * produced from a Map. * * @param value * A Map value. * @return this. */ public JSONArray put(Map<?, ?> value) { put(new JSONObject(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a JSONObject which is * produced from a Map. * * @param value * A Map value. * @return this. */ public JSONArray put(Map<?, ?> value) { put(new JSONObject(value)); return this; }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONObject which is produced from a Map. * * @param key * A key string. * @param value * A Map value. * @return this. * @throws JSONException */ public JSONObject put(String key, Map<?, ?> value) throws JSONException { put(key, new JSONObject(value)); return this; }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONObject which is produced from a Map. * * @param key * A key string. * @param value * A Map value. * @return this. * @throws JSONException */ public JSONObject put(String key, Map<?, ?> value) throws JSONException { put(key, new JSONObject(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a JSONObject which is * produced from a Map. * * @param index * The subscript. * @param value * The Map value. * @return this. * @throws JSONException * If the index is negative or if the the value is an invalid * number. */ public JSONArray put(int index, Map<?, ?> value) throws JSONException { put(index, new JSONObject(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a JSONObject which is * produced from a Map. * * @param index * The subscript. * @param value * The Map value. * @return this. * @throws JSONException * If the index is negative or if the the value is an invalid * number. */ public JSONArray put(int index, Map<?, ?> value) throws JSONException { put(index, new JSONObject(value)); return this; }
JSONObject invokeCommand(String command, String payload) { Content c; try { c = Resty.content(new JSONObject(payload)); } catch (JSONException ex) { Tesla.logger.severe("Can't Happen - JSON Syntax Error: " + payload); return new JSONObject(); } return call(command, c); }
private JSONObject produce() { if (streamReader == null) { return null; } String line = null; try { line = streamReader.readLine(); } catch (IOException ex) { } if (line == null) { // End of stream or timeout, shut it down... streamReader = null; httpConnection = null; return null; } JSONObject jo = new JSONObject(); String vals[] = line.split(","); for (int i = 0; i < keyList.length; i++) { try { jo.put(keyList[i], vals[i]); } catch (JSONException ex) { Tesla.logger.severe("Malformed data: " + ex); return null; } } return jo; }
private JSONObject call(String command, Content payload) { JSONObject rawResponse = null; try { if (payload == null) rawResponse = api.json(command).object(); else rawResponse = api.json(command, payload).object(); if (rawResponse == null) return new JSONObject(); return rawResponse.getJSONObject("response"); } catch (IOException | JSONException ex) { String error = ex.toString().replace("\n", " -- "); Tesla.logger.finer( "Failed invoking (" + StringUtils.substringAfterLast(command, "/") + "): [" + StringUtils.substringAfter(error, "[") ); return (rawResponse == null) ? new JSONObject() : rawResponse; } }
java.io.StringReader r = new java.io.StringReader(args [0]); JSONPathCompiler app = new JSONPathCompiler(r); JSONPathExpr x = JSONPathExpr.class.cast(app.expr()); x.dump(" "); JSONObject json = new JSONObject("{ \u005c"a\u005c": [" + "{\u005c"Hello\u005c": \u005c"World\u005c"},{\u005c"Hello\u005c": \u005c"JB\u005c"}]" + ", \u005c"b\u005c": null, \u005c"c\u005c": { \u005c"d\u005c": 1234 }}"); Object o = x.eval(json); System.err.println("Compiling <code>" + args [0] + "</code>"); System.err.println("Eval <code>" + json + "</code>"); System.err.println("Result is:<code>" + o + "</code>"); } catch (Exception err) { err.printStackTrace(); } } final public JSONPathExpr json() throws ParseException { /*@bgen(jjtree) EXPRESSION */ JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTEXPRESSION); boolean jjtc000 = true;
java.io.StringReader r = new java.io.StringReader(args [0]); JSONPathCompiler app = new JSONPathCompiler(r); JSONPathExpr x = JSONPathExpr.class.cast(app.expr()); x.dump(" "); JSONObject json = new JSONObject("{ \u005c"a\u005c": [" + "{\u005c"Hello\u005c": \u005c"World\u005c"},{\u005c"Hello\u005c": \u005c"JB\u005c"}]" + ", \u005c"b\u005c": null, \u005c"c\u005c": { \u005c"d\u005c": 1234 }}"); Object o = x.eval(json); System.err.println("Compiling <code>" + args [0] + "</code>"); System.err.println("Eval <code>" + json + "</code>"); System.err.println("Result is:<code>" + o + "</code>"); } catch (Exception err) { err.printStackTrace(); } } final public JSONPathExpr json() throws ParseException { /*@bgen(jjtree) EXPRESSION */ JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTEXPRESSION); boolean jjtc000 = true;
/** * Begin appending a new object. All keys and values until the balancing * <code>endObject</code> will be appended to this object. The * <code>endObject</code> method must be called to mark the object's end. * @return this * @throws JSONException If the nesting is too deep, or if the object is * started in the wrong place (for example as a key or after the end of the * outermost array or object). */ public JSONWriter object() throws JSONException { if (this.mode == 'i') { this.mode = 'o'; } if (this.mode == 'o' || this.mode == 'a') { this.append("{"); this.push(new JSONObject()); this.comma = false; return this; } throw new JSONException("Misplaced object."); }
public void saveClassification(String branchPath, String classificationId) throws RestClientException, InterruptedException { String classifyURL = urlHelper.getClassificationsUrl(branchPath); try { logger.debug("Saving classification via {}", classifyURL); JSONObject jsonObj = new JSONObject().put("status", "SAVED"); resty.put(classifyURL, jsonObj, SNOWOWL_CONTENT_TYPE); //We'll wait the same time for saving as we do for the classification boolean savingCompleted = waitForStatus(classifyURL, getTimeoutDate(classificationTimeoutMinutes), ProcessingStatus.SAVED, "classifier result saving"); if (!savingCompleted) { throw new IOException("Classifier reported non-saved status when saving"); } } catch (Exception e) { throw new RestClientException("Failed to save classification via URL " + classifyURL, e); } }
/** * Begin appending a new object. All keys and values until the balancing * <code>endObject</code> will be appended to this object. The * <code>endObject</code> method must be called to mark the object's end. * @return this * @throws JSONException If the nesting is too deep, or if the object is * started in the wrong place (for example as a key or after the end of the * outermost array or object). */ public JSONWriter object() throws JSONException { if (this.mode == 'i') { this.mode = 'o'; } if (this.mode == 'o' || this.mode == 'a') { this.append("{"); this.push(new JSONObject()); this.comma = false; return this; } throw new JSONException("Misplaced object."); }
private void doMerge(String sourcePath, String targetPath) throws RestClientException { try { JSONObject params = new JSONObject(); params.put("source", sourcePath); params.put("target", targetPath); resty.put(urlHelper.getMergesUrl(), params, SNOWOWL_CONTENT_TYPE); } catch (Exception e) { throw new RestClientException("Failed to merge " + sourcePath + " to " + targetPath, e); } }
/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject. Some information may be lost in this transformation * because JSON is a data format and XML is a document format. XML uses * elements, attributes, and content text, while JSON uses unordered * collections of name/value pairs and arrays of values. JSON does not * does not like to distinguish between elements and attributes. * Sequences of similar elements are represented as JSONArrays. Content * text may be placed in a "content" member. Comments, prologs, DTDs, and * <code><[ [ ]]></code> are ignored. * @param string The source string. * @return A JSONObject containing the structured data from the XML string. * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); XMLTokener x = new XMLTokener(string); while (x.more() && x.skipPast("<")) { parse(x, o, null); } return o; }
private void createBranch(String parentBranch, String newBranchName) throws RestClientException { try { JSONObject jsonObject = new JSONObject(); jsonObject.put("parent", parentBranch); jsonObject.put("name", newBranchName); resty.json(urlHelper.getBranchesUrl(), RestyHelper.content((jsonObject), SNOWOWL_CONTENT_TYPE)); } catch (Exception e) { throw new RestClientException("Failed to create branch " + newBranchName + ", parent branch " + parentBranch, e); } }
/** * Produce a JSONObject by combining a JSONArray of names with the values of * this JSONArray. * * @param names * A JSONArray containing a list of key strings. These will be paired * with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException * If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/** * Convert a cookie list into a JSONObject. A cookie list is a sequence * of name/value pairs. The names are separated from the values by '='. * The pairs are separated by ';'. The names and the values * will be unescaped, possibly converting '+' and '%' sequences. * * To add a cookie to a cooklist, * cookielistJSONObject.put(cookieJSONObject.getString("name"), * cookieJSONObject.getString("value")); * @param string A cookie list string * @return A JSONObject * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); JSONTokener x = new JSONTokener(string); while (x.more()) { String name = Cookie.unescape(x.nextTo('=')); x.next('='); o.put(name, Cookie.unescape(x.nextTo(';'))); x.next(); } return o; }