/*** * 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; }
/*** * Deep copies all JSON elements, shallow copies anything else. * * @param ar Array to clone. * @return Cloned array. * @throws JSONException Malformed object. */ private static JSONArray clone(JSONArray ar) throws JSONException { JSONArray result = new JSONArray(); for (int i = 0; i < ar.length(); i++) { if (ar.isNull(i)) continue; Object o = ar.get(i); if (o == null) continue; if (o instanceof JSONObject) result.put(clone((JSONObject) o)); else if (o instanceof JSONArray) result.put(clone((JSONArray) o)); else result.put(o); } return result; }
@Override public Object resolve(Context c, Map<String, String[]> parameters, Map<String, InputStream> dataStreams) throws JSONException { JSONObject jo = getObjAsJsonObject(c, parameters, dataStreams); if (jo == null) jo = new JSONObject(); else jo = EwJson.clone(jo); for (String key : keySet()) { if (isSetting(key)) continue; if (key.equals("obj")) continue; jo.put(key, get(key, c, parameters, dataStreams)); } String key = optAsString("_key", null, c, parameters, dataStreams); if (key != null) jo.put(key, get("_value", c, parameters, dataStreams)); return jo; }