private static void setFile(JSONObject o,String file) throws JSONException { o.put("_codeFile",file); for (String key : EwJson.getKeys(o)) if (o.get(key) instanceof JSONObject) setFile(o.getJSONObject(key),file); } private static void setMethod(JSONObject o,String method) throws JSONException
if (!EwJson.isNull(from)) final Object wrapped = EwJson.wrap(from, true); if (EwJson.isJson(wrapped)) return EwJsonObject.merge(converted, EwJson.tryConvert(wrapped), ref);
/*** * Deep copies all JSON elements, shallow copies anything else. * * @param cl Object to clone. * @return Cloned object. * @throws JSONException Malformed object. */ public static JSONObject clone(JSONObject cl) throws JSONException { JSONObject jo = new JSONObject(); Iterator<String> it = cl.keys(); while (it.hasNext()) { String next = it.next(); Object o = cl.get(next); if (o == null) continue; if (o instanceof JSONObject) jo.put(next, clone((JSONObject) o)); else if (o instanceof JSONArray) jo.put(next, clone((JSONArray) o)); else jo.put(next, o); } return jo; }
@Deprecated public static Object tryParseJson(Object object, boolean nullify, boolean reduce) { if (isNull(object)) return null; final JSONArray ja = getInstanceOfJsonArray(object); if (ja != null) return (reduce) ? tryReduce(ja, nullify) : ja; final JSONObject jo = getInstanceOfJsonObject(object); if (jo != null) return (reduce) ? tryReduce(jo, nullify) : jo; return (nullify) ? null : object; }
@Deprecated public static boolean hasComplexKey(Object object, String key) throws JSONException { final Object json = tryParseJson(object, true); if (!isComplexKey(key) || !isJson(json)) return false; return (processComplexKey(json, KEY_PATTERN.split(key)).size() > 0); }
/** * Compares this with object, after it has been wrapped by * {@link EwJson#wrap(Object)}. */ @Override public boolean equals(Object object) { if (super.equals(object)) return true; final Object wrapped = EwJson.wrap(object); if (this == wrapped) return true; if (EwJson.isNull(wrapped)) return false; if (wrapped instanceof EwJsonCollection) return EwJson.equals(this, (EwJsonCollection) wrapped); return false; }
@Deprecated private static Object derefObjectIndexKey(Object object, String key) throws JSONException object = tryParseJson(object, true); return derefComplexKey(object, key); else if (isObjectIndexKey(key)) deref = object; else deref = (isBracketedKey(outerKey)) ? derefBracketedKey(object, outerKey) : getElement(object, outerKey); return derefObjectIndexKey(deref, innerKey); else if (isValidIndex(key)) final List<String> keys = EwJson.getKeys(jo); final int index = keyToIndex(key); return getElement(object, key);
object = tryParseJson(object, false, reduce); else if (isBracketedKey(key)) return derefBracketedKey(object, key); else if (isObjectIndexKey(key)) return derefObjectIndexKey(object, key); if (isValidIndex(key)) return ((JSONArray) object).opt(keyToIndex(key)); else if (isBracketedKey(key)) return derefBracketedKey(object, key); else if (isObjectIndexKey(key)) return derefObjectIndexKey(object, key); return (reduce) ? null : derefCompositeKey(object, key, true);
@Deprecated private static Object derefBracketedKey(Object object, String key) throws JSONException { final Object jsonObject = tryParseJson(object, true); if (jsonObject == null || key == null) return null; if (key.indexOf(KEY_DELIM) != -1) return derefComplexKey(jsonObject, key); if (!isBracketedKey(key)) return getElement(jsonObject, key); /* * Dereference key or index from array or object -- null if non-existent */ // For key "arr[0][1]": outer is "arr", inner is "0", next is "[1]" final int keyDelim = key.indexOf(ARR_OPEN); final int keyLength = key.indexOf(ARR_CLOSE) + 1; final String outerKey = key.substring(0, keyDelim); final String innerKey = key.substring(keyDelim + 1, keyLength - 1); if (isBracketedKey(innerKey)) throw new JSONException("Invalid inner key: " + key); final String nextKey = (keyLength < key.length()) ? key.substring(keyLength) : ""; if (nextKey.length() > 0 && nextKey.charAt(0) != ARR_OPEN) throw new JSONException("Invalid next key: " + key); final Object nextObject = (outerKey.isEmpty()) ? getElement(jsonObject, innerKey) : derefCompositeKey(getElement(jsonObject, outerKey), innerKey); return (nextKey.isEmpty()) ? nextObject : derefCompositeKey(nextObject, nextKey); }
@Deprecated public static EwJsonCollection tryMerge(Object into, Object from, Object ref) { if (isJson(into)) { final EwJsonCollection converted = tryConvert(into); try { if (converted instanceof JSONArray) return EwJsonArray.tryMergeAny((JSONArray) converted, from, ref); if (converted instanceof JSONObject) return EwJsonObject.tryMergeAny((JSONObject) converted, from, ref); } catch (JSONException je) { } } return null; }
private static EwList<Object> processComplexKey(Object object, String[] innerKeys) throws JSONException if (isNull(object) || innerKeys == null) return new EwList<Object>(); results.add(derefCompositeKey(object, lastKey)); results.add(tryParseJson(object, false)); final Object value = derefCompositeKey(o, innerKey); for (int j = 0; j < ja.length(); j++) final Object innerValue = derefCompositeKey(ja, (new Integer(j).toString())); if (isJson(innerValue)) results.add(innerValue); final Object value = derefCompositeKey(o, lastKey); if (!isNull(value)) finalResults.add(value);
@Override public EwJsonCollection optJSONCollection(Object ref, EwJsonCollection defaultValue) { return EwJson.tryConvert(this.opt(ref)); }
return into; final int index = EwJson.keyToIndex(ref); final int putDex = (index >= 0) ? index : into.length(); EwJson.merge(into, (JSONArray) from, 0, putDex, from.length()); else if (!EwJson.isJson(from) || EwJson.getElements(from) > 0) into.put(putDex, from);
if (isNull(object)) return null; else if (isWrapped(object)) return object; return tryParseJson(object, false); final Object json = tryParseJson(object, true); return (isNull(json)) ? populateMap(new EwJsonObject(), object) : json;
@Deprecated public static Object wrap(Object object) { return wrap(object, false); }
/** * Overridden to first try wrapping as a valid JSON value. All index-based * puts in parent call this method. All index-based puts also pad the * underlying {@link ArrayList} with null values when index is beyond the * current range of the array. */ @Override public EwJsonArray put(int index, Object value) throws JSONException { Object wrapped = EwJson.wrap(value); if (EwJson.isNull(wrapped)) super.put(index, (Object) null); else super.put(index, wrapped); return this; }
/** Cast or parse object as JSON object, or return the object */ private static Object cast(Object o) throws JSONException { JSONArray ja = EwJson.getInstanceOfJsonArray(o); if (ja != null) return create(ja); JSONObject jo = EwJson.getInstanceOfJsonObject(o); if (jo != null) return create(jo); if (o instanceof Cruncher) return create((Cruncher) o); return o; }
final Object inner = wrap(array.opt(i), true); nodes.addAll(getNodes((EwJsonCollection) inner)); else if (isNode(inner)) nodes.add(inner.toString()); final Object inner = wrap(object.opt(keys.next()), true); nodes.addAll(getNodes((EwJsonCollection) inner)); else if (isNode(inner)) nodes.add(inner.toString());
final Object inner = wrap(array.opt(i), true); nodes.addAll(getNodesByKey((EwJsonCollection) inner, key)); final Object inner = wrap(coll.opt(key), true); nodes.addAll(getNodesByKey((EwJsonCollection) inner, key)); else if (isNode(inner)) nodes.add(inner.toString());