/** If "ref" is not a String, the default value is returned regardless. */ @Override public Object opt(Object ref, Object defaultValue) { if (ref instanceof String) { // Try it the way the parent would do it first final Object object = super.opt((String) ref); // If that fails, try parsing ref as an array key if (object == null) try { if (EwJson.isComplexKey(ref)) return EwJson.derefComplexKey(this, (String) ref); } catch (JSONException e) { } return object; } return defaultValue; }
@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); }
@Override public Object opt(Object ref, Object defaultValue) { // Try it the way the parent would do it first final Object object = super.opt(EwJson.keyToIndex(ref)); // If that fails, try parsing ref as a composite key if (object == null && ref instanceof String) try { if (EwJson.isComplexKey(ref)) return EwJson.derefComplexKey(this, (String) ref); } catch (JSONException e) { } return (object == null) ? defaultValue : object; }
return derefComplexKey(object, key);