Skip to content

Commit

Permalink
Update hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardofel committed Feb 15, 2022
1 parent 98c673c commit 0e84420
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.github.leonardofel</groupId>
<artifactId>json-java-put-null-fix</artifactId>
<version>3.0.44.unsafe</version>
<version>3.0.45.unsafe</version>
<packaging>jar</packaging>

<name>JSON in Java WITH WORKING .put(null)</name>
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,24 @@ public void onNotify(String key, PropertyChangeListener listener) {
public JSONObject update(String key, Object newValue) throws JSONException {
final JSONObject oldThis = new JSONObject(this.toString());
final Object oldValue = this.opt(key);
this.put(key, newValue);


if (newValue instanceof JSONObject) {
this.put(key, newValue);
final JSONObject __oldThis = new JSONObject(this.toString());

JSONObject newValueJson = (JSONObject) newValue;

newValueJson.onUpdateGlobal(evt -> {
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, __oldThis, this);
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);

this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
});
} else {
this.put(key, newValue);
}


this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/org/json/junit/JSONTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.json.junit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -613,6 +615,67 @@ public void updateListener2Test() {
assertEquals(jsonObject2.toString(), oldJsonObject2.toString());
}

@Test
public void updateChildTest() {
final JSONObject jsonParent = new JSONObject();
final JSONObject jsonChild = new JSONObject();

jsonParent.onUpdateGlobal(evt -> {
final Object oldValue = evt.getOldValue();
final Object newValue = evt.getNewValue();

assertNotEquals(oldValue.toString(), newValue.toString());

assertTrue(oldValue instanceof JSONObject);
assertTrue(newValue instanceof JSONObject);

final JSONObject oldValueJson = (JSONObject) oldValue;
final JSONObject newValueJson = (JSONObject) newValue;

if (oldValueJson.has("jsonChild")) {
var oldValueJsonChild = oldValueJson.optJSONObject("jsonChild");
var newValueJsonChild = newValueJson.optJSONObject("jsonChild");

assertNotEquals(oldValueJsonChild.toString(), newValueJsonChild.toString());
assertNotEquals(oldValueJson.toString(), newValueJson.toString());

assertFalse(newValueJsonChild.has("test4"));
assertTrue(newValueJsonChild.isNull("test4"));
assertFalse(newValueJsonChild.has("test5"));
assertTrue(newValueJsonChild.isNull("test5"));

if (newValueJsonChild.has("test3")) {
final JSONObject test3 = newValueJsonChild.getJSONObject("test3");
if (test3.has("test4")) {
assertEquals(test3.optString("test4"), "value4");
}

if (test3.has("test5")) {
assertEquals(test3.optString("test5"), "value5");
}
} else if (newValueJsonChild.has("test2")) {
assertNull(oldValueJsonChild.optJSONObject("test2"));
assertNotNull(newValueJsonChild.optJSONObject("test2"));
} else {
assertTrue(oldValueJsonChild.isNull("test1"));
assertFalse(oldValueJsonChild.optBoolean("test1"));

assertFalse(newValueJsonChild.isNull("test1"));
assertTrue(newValueJsonChild.optBoolean("test1"));
}
}
});

jsonParent.update("jsonChild", jsonChild);

jsonChild.update("test1", true);
jsonChild.update("test2", new JSONObject());
final JSONObject test3 = new JSONObject();
test3.update("test4", "value4");
jsonChild.update("test3", test3);
test3.update("test5", "value5");
}

@Test
public void updateOrRemoveSrcEmptyTest() {
try {
Expand Down

0 comments on commit 0e84420

Please sign in to comment.