if (null == base) return deepClone(overlay); if (value instanceof JSONObject) { if (base.has(key) && base.get(key) instanceof JSONObject) { overlayInPlace((JSONObject) base.get(key), (JSONObject) value); } else { base.put(key, deepClone((JSONObject) value)); base.put(key, overlay((JSONArray) base.get(key), (JSONArray) value)); } else { base.put(key, deepClone((JSONArray) value));
public void setCustomMetaData (JSONObject metadata) throws JSONException { if (!_metaData.has("meta")) _metaData.put("meta", new JSONObject()); JSONObject metaInfo = _metaData.getJSONObject("meta"); JsonUtilities.overlayInPlace(metaInfo, JsonUtilities.expandKeysInPlace(metadata)); }
/** * Converts a map into a {@link FixedPoint}. * The map should contain a key labelled 'scale' or '0' for scale, * and 'value' or '1' for value. * If anything is missing, then it's considered 0. * * @param map * A string->object map to pull objects from. * @return * Returns a {@link FixedPoint} constructed from the elements of the map. */ protected static FixedPoint getFixedPointFromMap(Map<?, ?> map) { double scale = 0; double value = 0; if (map.containsKey("scale")) { scale = JsonUtilities.getNumber(map.get("scale")).doubleValue(); } else if (map.containsKey("value")) { value = JsonUtilities.getNumber(map.get("value")).doubleValue(); } else if (map.containsKey("0")) { scale = JsonUtilities.getNumber(map.get("0")).doubleValue(); } else if (map.containsKey("1")) { value = JsonUtilities.getNumber(map.get("1")).doubleValue(); } return new FixedPoint(scale, value); }
/** * 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; }
/** * Converts a {@link JSONArray} into a {@link List} of values. * @param jsonList * @return * Returns a list of values */ public static List<Object> jsonArrayToList(JSONArray jsonList) { int numItems = jsonList.length(); List<Object> list = new ArrayList<Object>(numItems); for (int i = 0; i < numItems; i++) { Object obj = jsonList.opt(i); if (obj instanceof JSONObject) { list.add(jsonObjToMap((JSONObject)obj)); } else if (obj instanceof JSONArray) { list.add(jsonArrayToList((JSONArray)obj)); } else { list.add(obj); } } return list; }
/** * 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; } }
@Test public void testOverlaying () throws JSONException { JSONObject base = new JSONObject("{a: 'a', a1: 'a1', b: 1, b1: 2, c: 1.1, c1: 2.2, d: {a: 'aa', a1: 'aa1'}, e: ['a', 'a1', 1, 2, 1.1, 2.2, {b: 'bb', b1: 'bb1'}]}"); JSONObject overlay = new JSONObject("{a1: 'a3', b1: 3, c1: 3.3, d: {a1: 'aa3'}, e:[null, 'a3', null, 3, null, 3.3, {b1: 'bb3'}]}"); JSONObject result = JsonUtilities.overlayInPlace(base, overlay); Assert.assertTrue(base == result); // Make sure overlay is unchanged Assert.assertEquals(5, overlay.length()); // Make sure unchanged entries remain unchanged Assert.assertEquals("a", result.getString("a")); Assert.assertEquals(1, result.getInt("b")); Assert.assertEquals(1.1, result.getDouble("c"), 1E-12); Assert.assertEquals("aa", result.getJSONObject("d").getString("a")); Assert.assertEquals("a", result.getJSONArray("e").getString(0)); Assert.assertEquals(1, result.getJSONArray("e").getInt(2)); Assert.assertEquals(1.1, result.getJSONArray("e").getDouble(4), 1E-12); Assert.assertEquals("bb", result.getJSONArray("e").getJSONObject(6).getString("b")); // Make sure changed entries have changed Assert.assertEquals("a3", result.getString("a1")); Assert.assertEquals(3, result.getInt("b1")); Assert.assertEquals(3.3, result.getDouble("c1"), 1E-12); Assert.assertEquals("aa3", result.getJSONObject("d").getString("a1")); Assert.assertEquals("a3", result.getJSONArray("e").getString(1)); Assert.assertEquals(3, result.getJSONArray("e").getInt(3)); Assert.assertEquals(3.3, result.getJSONArray("e").getDouble(5), 1E-12); Assert.assertEquals("bb3", result.getJSONArray("e").getJSONObject(6).getString("b1")); }
/** * Transform a string -> string map into a json object, nesting levels * based on a period. * * @param map The string -> string map to translate * * @return The same data, as a JSON object */ public static JSONObject mapToJSON (Map<String, String> map) { Properties properties = new Properties(); properties.putAll(map); return propertiesObjToJSON(properties); }
Object value = base.get(i); if (value instanceof JSONObject) expandKeysInPlace((JSONObject) value); if (value instanceof JSONArray) expandKeysInPlace((JSONArray) value);
public static void assertJsonEqual (JSONObject expected, JSONObject actual) { Map<String, Object> mapE = JsonUtilities.jsonObjToMap(expected); Map<String, Object> mapA = JsonUtilities.jsonObjToMap(actual); assertEquals(mapE, mapA); }
int width = config.getPropertyValue(LayerConfiguration.OUTPUT_WIDTH); int height = config.getPropertyValue(LayerConfiguration.OUTPUT_HEIGHT); Properties initProps = JsonUtilities.jsonObjToProperties(initJSON);
public static void assertJsonEqual (JSONArray expected, JSONArray actual) { List<Object> listE = JsonUtilities.jsonArrayToList(expected); List<Object> listA = JsonUtilities.jsonArrayToList(actual); assertEquals(listE, listA); }
@Test public void testArrayOverlayTruncation () throws Exception { JSONArray base = new JSONArray("['a', 'b', 'c', 'd', 'e', 'f']"); JSONArray overlay = new JSONArray("[1, 2, null, 4]"); JSONArray result = JsonUtilities.overlay(base, overlay); Assert.assertEquals(4, result.length()); Assert.assertEquals(1, result.getInt(0)); Assert.assertEquals(2, result.getInt(1)); Assert.assertEquals("c", result.getString(2)); Assert.assertEquals(4, result.getInt(3)); }
/** * 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 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; } }
/** * Converts a {@link JSONObject} into a {@link Map} of key-value pairs. * This iterates through the tree and converts all {@link JSONObject}s * into their equivalent map, and converts {@link JSONArray}s into * {@link List}s. * * @param jsonObj * @return * Returns a map with the same */ public static Map<String, Object> jsonObjToMap(JSONObject jsonObj) { Map<String, Object> map = new HashMap<String, Object>(); Iterator<?> keys = jsonObj.keys(); while (keys.hasNext()) { String key = keys.next().toString(); Object obj = jsonObj.opt(key); if (obj instanceof JSONObject) { map.put(key, jsonObjToMap((JSONObject)obj)); } else if (obj instanceof JSONArray) { map.put(key, jsonArrayToList((JSONArray)obj)); } else { map.put(key, obj); } } return map; }
@Test public void testPropertyToJSONConversion () throws Exception { Properties p = new Properties(); p.setProperty("a", "aval"); p.setProperty("b.0", "bval0"); p.setProperty("b.2", "bval2"); p.setProperty("c.1", "cval1"); p.setProperty("c.a", "cvala"); p.setProperty("c.b.a", "cbaval"); p.setProperty("c.b.b", "cbbval"); p.setProperty("d", "{\"x\":\"xval\", \"y\": 10}"); JSONObject expected = new JSONObject("{\"a\": \"aval\", \"b\": [\"bval0\", null, \"bval2\"], " + "\"c\": {\"1\": \"cval1\", \"a\": \"cvala\", \"b\": {\"a\": \"cbaval\", \"b\": \"cbbval\"}}, " + "\"d\" : { \"x\" : \"xval\", \"y\" : 10 }}"); JSONObject actual = JsonUtilities.propertiesObjToJSON(p); assertJsonEqual(expected, actual); }
if (value instanceof JSONObject) expandKeysInPlace((JSONObject) value); if (value instanceof JSONArray) expandKeysInPlace((JSONArray) value);
filterMap = JsonUtilities.jsonObjToMap(filterJSON); } catch (Exception e){ filterMap = null;
@Test public void testPropertyClass () throws Exception { JSONObject base = new JSONObject("{a: 'a', b: {c: 'c', d:['a', 'a1', 1, 2, 1.1, 2.2]}}"); Properties props = JsonUtilities.jsonObjToProperties(base); Assert.assertEquals("a", props.getProperty("a")); Assert.assertEquals("c", props.getProperty("b.c")); Assert.assertEquals("a", props.getProperty("b.d.0")); Assert.assertEquals("a1", props.getProperty("b.d.1")); Assert.assertEquals("1", props.getProperty("b.d.2")); Assert.assertEquals("2", props.getProperty("b.d.3")); Assert.assertEquals("1.1", props.getProperty("b.d.4")); Assert.assertEquals("2.2", props.getProperty("b.d.5")); Assert.assertEquals(Properties.class, props.getClass()); }