|
71 | 71 | * @author JSON.org |
72 | 72 | * @version 2016-08-15 |
73 | 73 | */ |
74 | | -public class JSONObject { |
| 74 | +public class JSONObject implements JSONSimilar { |
75 | 75 | /** |
76 | 76 | * JSONObject.NULL is equivalent to the value that JavaScript calls null, |
77 | 77 | * whilst Java's null is equivalent to the value that JavaScript calls |
@@ -2358,45 +2358,21 @@ public Object remove(String key) { |
2358 | 2358 | * Determine if two JSONObjects are similar. |
2359 | 2359 | * They must contain the same set of names which must be associated with |
2360 | 2360 | * similar values. |
2361 | | - * |
2362 | 2361 | * @param other The other JSONObject |
2363 | 2362 | * @return true if they are equal |
2364 | 2363 | */ |
| 2364 | + @Override |
2365 | 2365 | public boolean similar(Object other) { |
2366 | 2366 | try { |
2367 | 2367 | if (!(other instanceof JSONObject)) { |
2368 | 2368 | return false; |
2369 | 2369 | } |
2370 | | - if (!this.keySet().equals(((JSONObject)other).keySet())) { |
| 2370 | + JSONObject otherObj = (JSONObject)other; |
| 2371 | + if (!this.keySet().equals(otherObj.keySet())) { |
2371 | 2372 | return false; |
2372 | 2373 | } |
2373 | | - for (final Entry<String,?> entry : this.entrySet()) { |
2374 | | - String name = entry.getKey(); |
2375 | | - Object valueThis = entry.getValue(); |
2376 | | - Object valueOther = ((JSONObject)other).get(name); |
2377 | | - if(valueThis == valueOther) { |
2378 | | - continue; |
2379 | | - } |
2380 | | - if(valueThis == null) { |
2381 | | - return false; |
2382 | | - } |
2383 | | - if (valueThis instanceof JSONObject) { |
2384 | | - if (!((JSONObject)valueThis).similar(valueOther)) { |
2385 | | - return false; |
2386 | | - } |
2387 | | - } else if (valueThis instanceof JSONArray) { |
2388 | | - if (!((JSONArray)valueThis).similar(valueOther)) { |
2389 | | - return false; |
2390 | | - } |
2391 | | - } else if (valueThis instanceof Number && valueOther instanceof Number) { |
2392 | | - if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) { |
2393 | | - return false; |
2394 | | - } |
2395 | | - } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) { |
2396 | | - if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) { |
2397 | | - return false; |
2398 | | - } |
2399 | | - } else if (!valueThis.equals(valueOther)) { |
| 2374 | + for (Map.Entry<String,?> entry : this.entrySet()) { |
| 2375 | + if (!JSONSimilar.compare(entry.getValue(), otherObj.get(entry.getKey()))) { |
2400 | 2376 | return false; |
2401 | 2377 | } |
2402 | 2378 | } |
|
0 commit comments