JSONHelper { public static boolean isEqual(final JSONObject jsonObject1, final JSONObject jsonObject2) { if (jsonObject1 == null || JSONObject.getNames(jsonObject1) == null) { return (jsonObject2 == null || JSONObject.getNames(jsonObject2) == null); } else if(jsonObject2 == null || JSONObject.getNames(jsonObject2) == null) { return false; } final List<String> objectProperties1 = Arrays.asList(JSONObject.getNames(jsonObject1)); Collections.sort(objectProperties1); final List<String> objectProperties2 = Arrays.asList(JSONObject.getNames(jsonObject2)); Collections.sort(objectProperties2); if (!objectProperties1.equals(objectProperties2)) { log.debug("Object:\n", objectProperties1, "didn't have same properties as:\n", objectProperties2); return false; } try { for (String key : objectProperties1) { final Object value1 = jsonObject1.get(key); final Object value2 = jsonObject2.get(key); if (value1 instanceof JSONObject) { if (!(value2 instanceof JSONObject)) { log.debug(value1, "was a JSONObject unlike", value2); return false; } else if (!isEqual((JSONObject) value1, (JSONObject) value2)) { log.debug("JSONObject recursion was not equal"); return false; } } else if (value1 instanceof JSONArray) { if (!(value2 instanceof JSONArray)) { log.debug(value1, "was a JSONArray unlike", value2); return false; } if (!isEqual((JSONArray) value1, (JSONArray) value2)) { log.debug("JSONArrays were not equal"); return false; } } else if (value1 == null) { if (value2 != null) { log.debug("value1 was <null>, but value2 was:" + value2); return false; } } else if (value1 instanceof Number && value2 instanceof Number) { double v1 = ((Number) value1).doubleValue(); double v2 = ((Number) value2).doubleValue(); if (Math.abs(v1-v2) > 1e-6) { log.debug("Values were not equal:", value1, "!=", value2); return false; } } else if (!value1.equals(value2)) { log.debug("Values were not equal:", value1, "!=", value2); return false; } } } catch (Exception ex) { log.warn(ex, "Error comparing JSONObjects"); return false; } return true; }  static final JSONObject createJSONObject(final String key, final String value); static final JSONObject createJSONObject(final String key, final Object value); static final JSONObject createJSONObject(final String key, final JSONObject value); static final JSONObject createJSONObject(final String key, final int value); static final JSONObject createJSONObject(final String key, final boolean value); static final JSONObject createJSONObject(final String key, final JSONArray value); static final JSONObject createJSONObject(final String content); static final JSONObject createJSONObject4Tokener(final JSONTokener content); static final JSONObject getJSONObject(final JSONObject content, String key); static final Object get(final JSONObject content, String key); static final JSONObject getJSONObject(final JSONArray content, int key); static final JSONArray getJSONArray(final JSONObject content, String key); static final Map<String, T> getObjectAsMap(final JSONObject obj); static final List<T> getArrayAsList(final JSONArray array); static JSONArray getEmptyIfNull(final JSONArray array); static boolean getBooleanFromJSON(final JSONObject data, final String key, final boolean defaultValue); static final String getStringFromJSON(final JSONObject data, final String key, final String defaultValue); static final String getStringFromJSON(final JSONObject data, final String defaultValue); static final String getStringFromJSON(final JSONArray data, final String defaultValue); static final boolean accumulateValue(final JSONObject json, final String key, final Object value); static final boolean putValue(final JSONObject json, final String key, final Object value); static final boolean put(final JSONObject json, final String key, final JSONArray value); static final Boolean putValue(final JSONObject json, final String key, final boolean value); static final boolean putValue(final JSONObject json, final String key, final long value); static final boolean putValue(final JSONObject json, final String key, final Date value); static final boolean putValue(final JSONObject json, final String key, final double value); static final boolean putAll(final JSONArray toArray, JSONArray fromArray); static JSONArray createJSONArray(final String content, boolean emptyIfNull); static JSONArray createJSONArray(final String content); static JSONArray createJSONArray(final Object content); static JSONArray createJSONArrayJsonKeys(final JSONObject json); static boolean isEqual(final JSONObject jsonObject1, final JSONObject jsonObject2); static boolean isEqual(JSONArray jsonArray1, JSONArray jsonArray2); static JSONObject merge(final JSONObject baseData, final JSONObject overrides); static String optString(JSONObject obj, String key); static String optString(JSONObject obj, String key, String defaultValue); static String getString(JSONObject obj, String key);  }
@Test public void testIsEqual() throws Exception { }