Skip to content

Commit 0e84420

Browse files
committed
Update hierarchy
1 parent 98c673c commit 0e84420

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

pom.xml

+1-1
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.44.unsafe</version>
6+
<version>3.0.45.unsafe</version>
77
<packaging>jar</packaging>
88

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

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,24 @@ public void onNotify(String key, PropertyChangeListener listener) {
18521852
public JSONObject update(String key, Object newValue) throws JSONException {
18531853
final JSONObject oldThis = new JSONObject(this.toString());
18541854
final Object oldValue = this.opt(key);
1855-
this.put(key, newValue);
1855+
1856+
1857+
if (newValue instanceof JSONObject) {
1858+
this.put(key, newValue);
1859+
final JSONObject __oldThis = new JSONObject(this.toString());
1860+
1861+
JSONObject newValueJson = (JSONObject) newValue;
1862+
1863+
newValueJson.onUpdateGlobal(evt -> {
1864+
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, __oldThis, this);
1865+
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);
1866+
1867+
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
1868+
});
1869+
} else {
1870+
this.put(key, newValue);
1871+
}
1872+
18561873

18571874
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
18581875
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);

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

+63
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.json.junit;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertNotEquals;
6+
import static org.junit.Assert.assertNotNull;
57
import static org.junit.Assert.assertNull;
68
import static org.junit.Assert.assertTrue;
79
import static org.junit.Assert.fail;
@@ -613,6 +615,67 @@ public void updateListener2Test() {
613615
assertEquals(jsonObject2.toString(), oldJsonObject2.toString());
614616
}
615617

618+
@Test
619+
public void updateChildTest() {
620+
final JSONObject jsonParent = new JSONObject();
621+
final JSONObject jsonChild = new JSONObject();
622+
623+
jsonParent.onUpdateGlobal(evt -> {
624+
final Object oldValue = evt.getOldValue();
625+
final Object newValue = evt.getNewValue();
626+
627+
assertNotEquals(oldValue.toString(), newValue.toString());
628+
629+
assertTrue(oldValue instanceof JSONObject);
630+
assertTrue(newValue instanceof JSONObject);
631+
632+
final JSONObject oldValueJson = (JSONObject) oldValue;
633+
final JSONObject newValueJson = (JSONObject) newValue;
634+
635+
if (oldValueJson.has("jsonChild")) {
636+
var oldValueJsonChild = oldValueJson.optJSONObject("jsonChild");
637+
var newValueJsonChild = newValueJson.optJSONObject("jsonChild");
638+
639+
assertNotEquals(oldValueJsonChild.toString(), newValueJsonChild.toString());
640+
assertNotEquals(oldValueJson.toString(), newValueJson.toString());
641+
642+
assertFalse(newValueJsonChild.has("test4"));
643+
assertTrue(newValueJsonChild.isNull("test4"));
644+
assertFalse(newValueJsonChild.has("test5"));
645+
assertTrue(newValueJsonChild.isNull("test5"));
646+
647+
if (newValueJsonChild.has("test3")) {
648+
final JSONObject test3 = newValueJsonChild.getJSONObject("test3");
649+
if (test3.has("test4")) {
650+
assertEquals(test3.optString("test4"), "value4");
651+
}
652+
653+
if (test3.has("test5")) {
654+
assertEquals(test3.optString("test5"), "value5");
655+
}
656+
} else if (newValueJsonChild.has("test2")) {
657+
assertNull(oldValueJsonChild.optJSONObject("test2"));
658+
assertNotNull(newValueJsonChild.optJSONObject("test2"));
659+
} else {
660+
assertTrue(oldValueJsonChild.isNull("test1"));
661+
assertFalse(oldValueJsonChild.optBoolean("test1"));
662+
663+
assertFalse(newValueJsonChild.isNull("test1"));
664+
assertTrue(newValueJsonChild.optBoolean("test1"));
665+
}
666+
}
667+
});
668+
669+
jsonParent.update("jsonChild", jsonChild);
670+
671+
jsonChild.update("test1", true);
672+
jsonChild.update("test2", new JSONObject());
673+
final JSONObject test3 = new JSONObject();
674+
test3.update("test4", "value4");
675+
jsonChild.update("test3", test3);
676+
test3.update("test5", "value5");
677+
}
678+
616679
@Test
617680
public void updateOrRemoveSrcEmptyTest() {
618681
try {

0 commit comments

Comments
 (0)