/** * Get an optional JSONObject associated with a key. * It returns null if there is no such key, or if its value is not a * JSONObject. * * @param key A key string. * @return A JSONObject which is the value. */ public JSONObject optJSONObject(String key) { Object o = opt(key); return o instanceof JSONObject ? (JSONObject)o : null; }
/** * Append a JSON Object * * @param o * @return * @throws JSONException */ public JSONWriter writeObject(JSONObject o) throws JSONException { Iterator<String> keys = o.keys(); this.object(); while (keys.hasNext()) { String key = keys.next(); this.key(key); JSONObject objVal = o.optJSONObject(key); if (objVal != null) { this.writeObject(objVal); continue; } JSONArray arrVal = o.optJSONArray(key); if (arrVal != null) { this.writeArray(arrVal); continue; } Object obj = o.opt(key); this.value(obj); } this.endObject(); return this; }
Object value = metadata.opt(metadataPropertyName); if (value != null) { resourceProps.put(metadataPropertyName, value);
/** * Get an optional JSONArray associated with a key. * It returns null if there is no such key, or if its value is not a * JSONArray. * * @param key A key string. * @return A JSONArray which is the value. */ public JSONArray optJSONArray(String key) { Object o = opt(key); return o instanceof JSONArray ? (JSONArray)o : null; }
/** * Determine if the value associated with the key is null or if there is * no value. * @param key A key string. * @return true if there is no value associated with the key or if * the value is the JSONObject.NULL object. */ public boolean isNull(String key) { return JSONObject.NULL.equals(opt(key)); }
/** * Get an optional string associated with a key. * It returns the defaultValue if there is no such key. * * @param key A key string. * @param defaultValue The default. * @return A string which is the value. */ public String optString(String key, String defaultValue) { Object o = opt(key); return o != null ? o.toString() : defaultValue; }
/** * Get an optional double associated with a key, or the * defaultValue if there is no such key or if its value is not a number. * If the value is a string, an attempt will be made to evaluate it as * a number. * * @param key A key string. * @param defaultValue The default. * @return An object which is the value. */ public double optDouble(String key, double defaultValue) { try { Object o = opt(key); return o instanceof Number ? ((Number)o).doubleValue() : new Double((String)o).doubleValue(); } catch (Exception e) { return defaultValue; } }
/** * Construct a JSONObject from a subset of another JSONObject. * An array of strings is used to identify the keys that should be copied. * Missing keys are ignored. * @param jo A JSONObject. * @param sa An array of strings. * @exception JSONException If a value is a non-finite number. */ public JSONObject(JSONObject jo, String[] sa) throws JSONException { this(); // basic setup for (int i = 0; i < sa.length; i += 1) { putOpt(sa[i], jo.opt(sa[i])); } }
/** Render the supplied JSONObject to a String, in * the simplest possible way. */ public String toString(JSONObject jo) { try { final Iterator<String> keys = jo.keys(); final StringBuffer sb = new StringBuffer("{"); while (keys.hasNext()) { if (sb.length() > 1) { sb.append(','); } String o = keys.next(); sb.append(quote(o)); sb.append(':'); sb.append(valueToString(jo.opt(o))); } sb.append('}'); return sb.toString(); } catch (Exception e) { return null; } }
/** * Get the value object associated with a key. * * @param key A key string. * @return The object associated with the key. * @throws JSONException if the key is not found. */ public Object get(String key) throws JSONException { Object o = opt(key); if (o == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } return o; }
Object primaryTypeObj = jsonObject.opt(JcrConstants.JCR_PRIMARYTYPE); String primaryType = null; if (primaryTypeObj != null) {
/** * Produce a JSONArray containing the values of the members of this * JSONObject. * @param names A JSONArray containing a list of key strings. This * determines the sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
/** * Support legacy data structures where link metadata is stored as JSON fragment in single HTML5 data attribute. * @param resourceProps ValueMap to write link metadata to * @param element Link element */ private boolean getAnchorLegacyMetadataFromSingleData(ValueMap resourceProps, Element element) { boolean foundAny = false; JSONObject metadata = null; Attribute dataAttribute = element.getAttribute("data"); if (dataAttribute != null) { String metadataString = dataAttribute.getValue(); if (StringUtils.isNotEmpty(metadataString)) { try { metadata = new JSONObject(metadataString); } catch (JSONException ex) { log.debug("Invalid link metadata: " + metadataString, ex); } } } if (metadata != null) { JSONArray names = metadata.names(); for (int i = 0; i < names.length(); i++) { String name = names.optString(i); resourceProps.put(name, metadata.opt(name)); foundAny = true; } } return foundAny; }
/** * Accumulate values under a key. It is similar to the put method except * that if there is already an object stored under the key then a * JSONArray is stored under the key to hold all of the accumulated values. * If there is already a JSONArray, then the new value is appended to it. * In contrast, the put method replaces the previous value. * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws JSONException If the value is an invalid number * or if the key is null. */ public JSONObject accumulate(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, value); } else if (o instanceof JSONArray) { ((JSONArray)o).put(value); } else { put(key, new JSONArray().put(o).put(value)); } return this; }
/** * Append values to the array under a key. If the key does not exist in the * JSONObject, then the key is put in the JSONObject with its value being a * JSONArray containing the value parameter. If the key was already * associated with a JSONArray, then the value parameter is appended to it. * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws JSONException If the key is null or if the current value * associated with the key is not a JSONArray. */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, new JSONArray().put(value)); } else if (o instanceof JSONArray) { put(key, new JSONArray().put(o).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); } return this; }