Skip to content

Commit 950ff8d

Browse files
committed
Fix update method using JSONObject
1 parent bd5981c commit 950ff8d

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.github.leonardofel</groupId>
55
<artifactId>json-java-put-null-fix</artifactId>
6-
<version>3.0.37.unsafe</version>
6+
<version>3.0.38.unsafe</version>
77
<packaging>jar</packaging>
88

99
<name>JSON in Java WITH WORKING .put(null)</name>

src/main/java/org/json/JSONObject.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ of this software and associated documentation files (the "Software"), to deal
3838
import java.lang.reflect.Modifier;
3939
import java.math.BigDecimal;
4040
import java.math.BigInteger;
41+
import java.util.ArrayList;
4142
import java.util.Collection;
4243
import java.util.Enumeration;
4344
import java.util.HashMap;
@@ -1892,35 +1893,44 @@ public JSONObject updateOrRemove(JSONObject jo) throws JSONException {
18921893
}
18931894

18941895
private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONException {
1895-
final HashMap<String, Map.Entry<Object, Object>> entries = new HashMap<String, Map.Entry<Object, Object>>();
1896+
final HashMap<String, Object> oldValues = new HashMap<String, Object>();
1897+
final HashMap<String, Object> newValues = new HashMap<String, Object>();
1898+
final ArrayList<String> delValues = new ArrayList<String>();
18961899

1897-
jo.forEach((key, newValue) -> {
1898-
this.merge(key, newValue, (v1, v2) -> {
1899-
if (Objects.equals(v1, v2)) {
1900+
jo.forEach((key, v2) -> {
1901+
this.compute(key, (k, v1) -> {
1902+
if (Objects.equals(v1, v2) && Objects.equals(v2, v1)) {
19001903
return v1;
19011904
} else {
1902-
entries.put(key, Map.entry(v1, v2));
1903-
return v2;
1905+
oldValues.put(key, JSONObject.NULL.equals(v1) ? null : v1);
1906+
newValues.put(key, JSONObject.NULL.equals(v2) ? null : v2);
1907+
return JSONObject.NULL.equals(v1) ? JSONObject.NULL : v1;
19041908
}
19051909
});
19061910
});
19071911

19081912
if (remove) {
1909-
this.forEach((key, oldValue) -> {
1913+
for (String key : JSONObject.getNames(this)) {
19101914
if (!jo.has(key)) {
1911-
this.remove(key);
1912-
final Object ret = entries.put(key, Map.entry(oldValue, null));
1913-
if (ret != null) {
1914-
System.err.println("unexpected behavior!");
1915-
}
1915+
oldValues.put(key, this.remove(key));
1916+
delValues.add(key);
19161917
}
1917-
});
1918+
}
19181919
}
19191920

1920-
entries.forEach((key, entry) -> {
1921-
final Object oldValue = entry.getKey();
1922-
final Object newValue = entry.getValue();
1923-
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1921+
oldValues.forEach((key, oldValue) -> {
1922+
final Object newValue;
1923+
if (remove && delValues.contains(key)) {
1924+
newValue = null;
1925+
} else {
1926+
newValue = newValues.get(key);
1927+
}
1928+
1929+
if (oldValue == null && newValue == null) {
1930+
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1931+
} else {
1932+
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1933+
}
19241934
});
19251935

19261936
return this;

src/test/java/org/json/junit/JSONTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,18 @@ public void updateListener2Test() {
510510
jsonObject1
511511
.put("trueKey", Boolean.valueOf(true))
512512
.put("falseKey", Boolean.valueOf(false))
513-
.put("stringKey", "CHANGE ME!!!");
514-
//.put("nullKey", "NOT NULL");
513+
.put("stringKey", "CHANGE ME!!!")
514+
.put("nullKey", null)
515+
.put("nullBefore", null)
516+
.put("nullAfter", "null");
515517

516518
final JSONObject oldJsonObject1 = new JSONObject(jsonObject1.toString());
517519

518520
jsonObject2
519-
.put("stringKey", "hello world!")
520521
.put("nullKey", null)
522+
.put("nullBefore", "null")
523+
.put("nullAfter", null)
524+
.put("stringKey", "hello world!")
521525
.put("escapeStringKey", "h\be\tllo w\u1234orld!")
522526
.put("intKey", Long.valueOf(42));
523527
//.put("doubleKey", Double.valueOf(-23.45e67)); PROBLEM WITH DOUBLE CONVERTING TO BIGDECIMAL AFTER JSONOBJECT.TOSTRING
@@ -528,7 +532,9 @@ public void updateListener2Test() {
528532
final Object oldValue = evt.getOldValue();
529533
final Object newValue = evt.getNewValue();
530534

531-
assertNotEquals(oldValue, newValue);
535+
if (!(oldValue == JSONObject.NULL && newValue == null)) {
536+
assertNotEquals(oldValue, newValue);
537+
}
532538
});
533539

534540
jsonObject1.addUpdateListener("trueKey", evt -> {

0 commit comments

Comments
 (0)