/** * Wraps the options and query {@link JSONObject}s together into a new object. */ private JSONObject mergeQueryConfigOptions(JSONObject options, JSONObject query) { JSONObject result = JsonUtilities.deepClone( options ); try { // all client configurable properties exist under an unseen 'public' node, // create this node before overlay query parameters onto server config if ( query != null ) { JSONObject publicNode = new JSONObject(); publicNode.put( "public", query ); result = JsonUtilities.overlayInPlace( result, publicNode ); } } catch (Exception e) { LOGGER.error("Couldn't merge query options with main options.", e); } return result; }
/** * Wraps the options and query {@link JSONObject}s together into a new object. */ private JSONObject mergeQueryConfigOptions(JSONObject options, JSONObject query) { JSONObject result = JsonUtilities.deepClone( options ); try { // all client configurable properties exist under an unseen 'public' node, // create this node before overlay query parameters onto server config if ( query != null ) { JSONObject publicNode = new JSONObject(); publicNode.put( "public", query ); result = JsonUtilities.overlayInPlace( result, publicNode ); } } catch (Exception e) { LOGGER.error("Couldn't merge query options with main options.", e); } return result; }
/** * Clone a JSON object and all its child objects */ public static JSONObject deepClone (JSONObject source) { if (null == source) return null; try { JSONObject clone = new JSONObject(); String[] keys = JSONObject.getNames(source); if (null != keys) { for (String key: keys) { Object value = source.opt(key); if (value instanceof JSONObject) { JSONObject valueClone = deepClone((JSONObject) value); clone.put(key, valueClone); } else if (value instanceof JSONArray) { JSONArray valueClone = deepClone((JSONArray) value); clone.put(key, valueClone); } else { clone.put(key, value); } } } return clone; } catch (JSONException e) { LOGGER.error("Weird JSON exception cloning object", e); return null; } }
/** * Clone a JSON array and all its child objects */ public static JSONArray deepClone (JSONArray source) { if (null == source) return null; try { JSONArray clone = new JSONArray(); for (int i=0; i<source.length(); ++i) { if (!source.isNull(i)) { Object value = source.opt(i); if (value instanceof JSONObject) { JSONObject valueClone = deepClone((JSONObject) value); clone.put(i, valueClone); } else if (value instanceof JSONArray) { JSONArray valueClone = deepClone((JSONArray) value); clone.put(i, valueClone); } else { clone.put(i, value); } } } return clone; } catch (JSONException e) { LOGGER.error("Weird JSON exception cloning object", e); return null; } }
if (null == base) return deepClone(overlay); result.put(i, deepClone((JSONObject) baseValue)); } else if (baseValue instanceof JSONArray) { result.put(i, deepClone((JSONArray) baseValue)); } else { result.put(i, baseValue); result.put(i, overlayInPlace((JSONObject) baseValue, (JSONObject) value)); } else { result.put(i, deepClone((JSONObject) value)); result.put(i, overlay((JSONArray) baseValue, (JSONArray) value)); } else { result.put(i, deepClone((JSONArray) value));
@Test public void testObjectCloning () throws JSONException { JSONObject source = new JSONObject("{name: 'abc', int: 3, double: 3.3, object: {a: 'a', b: 'b'}, array: [1, 2, 3]}"); JSONObject clone = JsonUtilities.deepClone(source); Assert.assertFalse(source == clone); Assert.assertEquals(source.toString(), clone.toString()); Assert.assertEquals("abc", source.getString("name")); Assert.assertEquals(3, source.getInt("int")); Assert.assertEquals(3.3, source.getDouble("double"), 1E-12); Assert.assertTrue(source.getJSONObject("object") == source.getJSONObject("object")); Assert.assertFalse(source.getJSONObject("object") == clone.getJSONObject("object")); Assert.assertEquals(source.getJSONObject("object").toString(), clone.getJSONObject("object").toString()); Assert.assertTrue(source.getJSONArray("array") == source.getJSONArray("array")); Assert.assertFalse(source.getJSONArray("array") == clone.getJSONArray("array")); Assert.assertEquals(source.getJSONArray("array").toString(), clone.getJSONArray("array").toString()); }
@Test public void testArrayCloning () throws JSONException { JSONArray source = new JSONArray("['abc', 3, 3.3, {a: 'a', b: 'b'}, [1, 2, 3]]"); JSONArray clone = JsonUtilities.deepClone(source); Assert.assertFalse(source == clone); Assert.assertEquals(source.toString(), clone.toString()); Assert.assertEquals("abc", source.getString(0)); Assert.assertEquals(3, source.getInt(1)); Assert.assertEquals(3.3, source.getDouble(2), 1E-12); Assert.assertTrue(source.getJSONObject(3) == source.getJSONObject(3)); Assert.assertFalse(source.getJSONObject(3) == clone.getJSONObject(3)); Assert.assertEquals(source.getJSONObject(3).toString(), clone.getJSONObject(3).toString()); Assert.assertTrue(source.getJSONArray(4) == source.getJSONArray(4)); Assert.assertFalse(source.getJSONArray(4) == clone.getJSONArray(4)); Assert.assertEquals(source.getJSONArray(4).toString(), clone.getJSONArray(4).toString()); }