Skip to content

Commit 89cfe43

Browse files
committed
fix ListenerGlobal call once
1 parent 897ec34 commit 89cfe43

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
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.40.unsafe</version>
6+
<version>3.0.41.unsafe</version>
77
<packaging>jar</packaging>
88

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

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

+26-13
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
17941794
}
17951795

17961796
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
1797+
private static final String propertyChangeGlobalKeyword = "__THIS__";
17971798
/**
17981799
* Add a PropertyChangeListener to the JSONObject.
17991800
* The listener object may be added more than once, and will be called
@@ -1804,7 +1805,7 @@ public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
18041805
* @param listener The PropertyChangeListener to be added
18051806
*/
18061807
public void addUpdateListenerGlobal(PropertyChangeListener listener) {
1807-
this.propertyChangeSupport.addPropertyChangeListener(listener);
1808+
this.propertyChangeSupport.addPropertyChangeListener(JSONObject.propertyChangeGlobalKeyword, listener);
18081809
}
18091810

18101811
/**
@@ -1820,7 +1821,11 @@ public void addUpdateListenerGlobal(PropertyChangeListener listener) {
18201821
* @param listener The PropertyChangeListener to be added
18211822
*/
18221823
public void addUpdateListener(String key, PropertyChangeListener listener) {
1823-
this.propertyChangeSupport.addPropertyChangeListener(key, listener);
1824+
if (JSONObject.propertyChangeGlobalKeyword.equals(key)) {
1825+
throw new JSONException("key \"" + JSONObject.propertyChangeGlobalKeyword + "\" is reserved");
1826+
} else {
1827+
this.propertyChangeSupport.addPropertyChangeListener(key, listener);
1828+
}
18241829
}
18251830

18261831
/**
@@ -1841,9 +1846,11 @@ public void addUpdateListener(String key, PropertyChangeListener listener) {
18411846
*/
18421847
public JSONObject update(String key, Object newValue) throws JSONException {
18431848
if (this.propertyChangeSupport.hasListeners(key)) {
1849+
final JSONObject oldThis = new JSONObject(this.toString());
18441850
final Object oldValue = this.opt(key);
18451851
this.put(key, newValue);
18461852

1853+
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
18471854
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
18481855
} else {
18491856
throw new JSONException("updateListener on \"" + key + "\" not initialized");
@@ -1893,6 +1900,8 @@ public JSONObject updateOrRemove(JSONObject jo) throws JSONException {
18931900
}
18941901

18951902
private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONException {
1903+
final JSONObject oldThis = new JSONObject(this.toString());
1904+
18961905
final HashMap<String, Object> oldValues = new HashMap<String, Object>();
18971906
final HashMap<String, Object> newValues = new HashMap<String, Object>();
18981907
final ArrayList<String> delValues = new ArrayList<String>();
@@ -1904,7 +1913,7 @@ private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONExce
19041913
} else {
19051914
oldValues.put(key, JSONObject.NULL.equals(v1) ? null : v1);
19061915
newValues.put(key, JSONObject.NULL.equals(v2) ? null : v2);
1907-
return JSONObject.NULL.equals(v1) ? JSONObject.NULL : v1;
1916+
return JSONObject.NULL.equals(v2) ? JSONObject.NULL : v2;
19081917
}
19091918
});
19101919
});
@@ -1921,18 +1930,22 @@ private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONExce
19211930
}
19221931
}
19231932

1933+
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1934+
19241935
oldValues.forEach((key, oldValue) -> {
1925-
final Object newValue;
1926-
if (remove && delValues.contains(key)) {
1927-
newValue = null;
1928-
} else {
1929-
newValue = newValues.get(key);
1930-
}
1936+
if (this.propertyChangeSupport.hasListeners(key)) {
1937+
final Object newValue;
1938+
if (remove && delValues.contains(key)) {
1939+
newValue = null;
1940+
} else {
1941+
newValue = newValues.get(key);
1942+
}
19311943

1932-
if (oldValue == null && newValue == null) {
1933-
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1934-
} else {
1935-
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1944+
if (oldValue == null && newValue == null) {
1945+
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1946+
} else {
1947+
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1948+
}
19361949
}
19371950
});
19381951

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

+38-16
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,12 @@ public void updateNotEqualsTest() {
429429

430430
j.addUpdateListenerGlobal(evt -> {
431431
final Object oldValue = evt.getOldValue();
432-
assertEquals(oldValue, null);
432+
assertEquals("{}", oldValue.toString());
433433
});
434434

435435
j.addUpdateListenerGlobal(evt -> {
436436
final Object newValue = evt.getNewValue();
437-
assertTrue(newValue.toString().startsWith("propertyChange"));
437+
assertEquals("{\"myMapListener\":\"propertyChange\"}", newValue.toString());
438438
});
439439

440440
j.addUpdateListener("myMapListener", evt -> {
@@ -531,9 +531,7 @@ public void updateListener2Test() {
531531
final Object oldValue = evt.getOldValue();
532532
final Object newValue = evt.getNewValue();
533533

534-
if (!(oldValue == JSONObject.NULL && newValue == null)) {
535-
assertNotEquals(oldValue, newValue);
536-
}
534+
assertNotEquals(oldValue.toString(), newValue.toString());
537535
});
538536

539537
jsonObject1.addUpdateListener("trueKey", evt -> {
@@ -631,9 +629,7 @@ public void updateOrRemoveSrcEmptyTest() {
631629
final Object oldValue = evt.getOldValue();
632630
final Object newValue = evt.getNewValue();
633631

634-
if (!(oldValue == JSONObject.NULL && newValue == null)) {
635-
assertNotEquals(oldValue, newValue);
636-
}
632+
assertNotEquals(oldValue.toString(), newValue.toString());
637633
});
638634

639635
jsonObject1.updateOrRemove(jsonObject2);
@@ -657,9 +653,7 @@ public void updateOrRemoveDstEmptyTest() {
657653
final Object oldValue = evt.getOldValue();
658654
final Object newValue = evt.getNewValue();
659655

660-
if (!(oldValue == JSONObject.NULL && newValue == null)) {
661-
assertNotEquals(oldValue, newValue);
662-
}
656+
assertNotEquals(oldValue.toString(), newValue.toString());
663657
});
664658

665659
jsonObject1.updateOrRemove(jsonObject2);
@@ -679,9 +673,7 @@ public void updateOrRemoveAllEmptyTest() {
679673
final Object oldValue = evt.getOldValue();
680674
final Object newValue = evt.getNewValue();
681675

682-
if (!(oldValue == JSONObject.NULL && newValue == null)) {
683-
assertNotEquals(oldValue, newValue);
684-
}
676+
assertNotEquals(oldValue.toString(), newValue.toString());
685677
});
686678

687679
jsonObject1.updateOrRemove(jsonObject2);
@@ -702,8 +694,8 @@ public void updateOrRemoveSrcTest() {
702694
final Object oldValue = evt.getOldValue();
703695
final Object newValue = evt.getNewValue();
704696

705-
assertEquals("hello world!", oldValue);
706-
assertNull(newValue);
697+
assertEquals("{\"stringKey\":\"hello world!\"}", oldValue.toString());
698+
assertEquals("{}", newValue.toString());
707699
});
708700

709701
jsonObject1.addUpdateListener("stringKey", evt -> {
@@ -732,10 +724,40 @@ public void updateOrRemoveDstTest() {
732724
final Object oldValue = evt.getOldValue();
733725
final Object newValue = evt.getNewValue();
734726

727+
assertEquals("{}", oldValue.toString());
728+
assertEquals("{\"stringKey\":\"hello world!\"}", newValue.toString());
729+
});
730+
731+
jsonObject1.addUpdateListener("stringKey", evt -> {
732+
final Object oldValue = evt.getOldValue();
733+
final Object newValue = evt.getNewValue();
734+
735735
assertNull(oldValue);
736736
assertEquals("hello world!", newValue);
737737
});
738738

739+
jsonObject1.updateOrRemove(jsonObject2);
740+
} catch (Exception ex) {
741+
ex.printStackTrace();
742+
fail(ex.getMessage());
743+
}
744+
}
745+
746+
@Test
747+
public void newNullTest() {
748+
try {
749+
final JSONObject jsonObject1 = new JSONObject();
750+
final JSONObject jsonObject2 = new JSONObject()
751+
.put("stringKey", "hello world!");
752+
753+
jsonObject1.addUpdateListenerGlobal(evt -> {
754+
final Object oldValue = evt.getOldValue();
755+
final Object newValue = evt.getNewValue();
756+
757+
assertEquals("{}", oldValue.toString());
758+
assertEquals("{\"stringKey\":\"hello world!\"}", newValue.toString());
759+
});
760+
739761
jsonObject1.addUpdateListener("stringKey", evt -> {
740762
final Object oldValue = evt.getOldValue();
741763
final Object newValue = evt.getNewValue();

0 commit comments

Comments
 (0)