Skip to content

Commit 56898e8

Browse files
committed
Fix updateOrRemove with both this and jo empty
1 parent 950ff8d commit 56898e8

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
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.38.unsafe</version>
6+
<version>3.0.39.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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,10 +1910,13 @@ private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONExce
19101910
});
19111911

19121912
if (remove) {
1913-
for (String key : JSONObject.getNames(this)) {
1914-
if (!jo.has(key)) {
1915-
oldValues.put(key, this.remove(key));
1916-
delValues.add(key);
1913+
final String[] names = JSONObject.getNames(this);
1914+
if (names != null) {
1915+
for (String key : names) {
1916+
if (!jo.has(key)) {
1917+
oldValues.put(key, this.remove(key));
1918+
delValues.add(key);
1919+
}
19171920
}
19181921
}
19191922
}

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ public void updateListener2Test() {
526526
.put("intKey", Long.valueOf(42));
527527
//.put("doubleKey", Double.valueOf(-23.45e67)); PROBLEM WITH DOUBLE CONVERTING TO BIGDECIMAL AFTER JSONOBJECT.TOSTRING
528528

529+
final JSONObject jsonObject3 = new JSONObject(jsonObject2.toString());
530+
529531
final JSONObject oldJsonObject2 = new JSONObject(jsonObject2.toString());
530532

531533
jsonObject1.addUpdateListenerGlobal(evt -> {
@@ -565,8 +567,83 @@ public void updateListener2Test() {
565567
assertEquals(jsonObject2.toString(), oldJsonObject2.toString());
566568

567569
jsonObject1.update(jsonObject2);
570+
jsonObject3.updateOrRemove(jsonObject2);
568571

569572
assertNotEquals(jsonObject1.toString(), oldJsonObject1.toString());
570573
assertEquals(jsonObject2.toString(), oldJsonObject2.toString());
571574
}
575+
576+
@Test
577+
public void updateOrRemoveSrcEmptyTest() {
578+
try {
579+
final JSONObject jsonObject1 = new JSONObject();
580+
final JSONObject jsonObject2 = new JSONObject()
581+
.put("trueKey", Boolean.valueOf(true))
582+
.put("falseKey", Boolean.valueOf(false))
583+
.put("stringKey", "hello world!")
584+
.put("nullKey", null);
585+
586+
jsonObject1.addUpdateListenerGlobal(evt -> {
587+
final Object oldValue = evt.getOldValue();
588+
final Object newValue = evt.getNewValue();
589+
590+
if (!(oldValue == JSONObject.NULL && newValue == null)) {
591+
assertNotEquals(oldValue, newValue);
592+
}
593+
});
594+
595+
jsonObject1.updateOrRemove(jsonObject2);
596+
} catch (Exception ex) {
597+
ex.printStackTrace();
598+
fail(ex.getMessage());
599+
}
600+
}
601+
602+
@Test
603+
public void updateOrRemoveDstEmptyTest() {
604+
try {
605+
final JSONObject jsonObject1 = new JSONObject()
606+
.put("trueKey", Boolean.valueOf(true))
607+
.put("falseKey", Boolean.valueOf(false))
608+
.put("stringKey", "hello world!")
609+
.put("nullKey", null);
610+
final JSONObject jsonObject2 = new JSONObject();
611+
612+
jsonObject1.addUpdateListenerGlobal(evt -> {
613+
final Object oldValue = evt.getOldValue();
614+
final Object newValue = evt.getNewValue();
615+
616+
if (!(oldValue == JSONObject.NULL && newValue == null)) {
617+
assertNotEquals(oldValue, newValue);
618+
}
619+
});
620+
621+
jsonObject1.updateOrRemove(jsonObject2);
622+
} catch (Exception ex) {
623+
ex.printStackTrace();
624+
fail(ex.getMessage());
625+
}
626+
}
627+
628+
@Test
629+
public void updateOrRemoveAlltEmptyTest() {
630+
try {
631+
final JSONObject jsonObject1 = new JSONObject();
632+
final JSONObject jsonObject2 = new JSONObject();
633+
634+
jsonObject1.addUpdateListenerGlobal(evt -> {
635+
final Object oldValue = evt.getOldValue();
636+
final Object newValue = evt.getNewValue();
637+
638+
if (!(oldValue == JSONObject.NULL && newValue == null)) {
639+
assertNotEquals(oldValue, newValue);
640+
}
641+
});
642+
643+
jsonObject1.updateOrRemove(jsonObject2);
644+
} catch (Exception ex) {
645+
ex.printStackTrace();
646+
fail(ex.getMessage());
647+
}
648+
}
572649
}

0 commit comments

Comments
 (0)