Skip to content

Commit

Permalink
fix update always had global listener
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardofel committed Nov 30, 2021
1 parent 62935b4 commit 6134019
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 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.42.unsafe</version>
<version>3.0.43.unsafe</version>
<packaging>jar</packaging>

<name>JSON in Java WITH WORKING .put(null)</name>
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1845,15 +1845,14 @@ public void addUpdateListener(String key, PropertyChangeListener listener) {
* If updateListener not initialized.
*/
public JSONObject update(String key, Object newValue) throws JSONException {
if (this.propertyChangeSupport.hasListeners(key)) {
final JSONObject oldThis = new JSONObject(this.toString());
final Object oldValue = this.opt(key);
this.put(key, newValue);
final JSONObject oldThis = new JSONObject(this.toString());
final Object oldValue = this.opt(key);
this.put(key, newValue);

this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);

if (this.propertyChangeSupport.hasListeners(key)) {
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
} else {
throw new JSONException("updateListener on \"" + key + "\" not initialized");
}

return this;
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/org/json/junit/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -12,7 +11,6 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

Expand Down Expand Up @@ -425,7 +423,7 @@ public void computeTest() {
public void updateNotEqualsTest() {
final JSONObject j = new JSONObject();

assertThrows(JSONException.class, () -> j.update("myMapListener", "propertyChange"));
//assertThrows(JSONException.class, () -> j.update("myMapListener", "propertyChange"));

j.addUpdateListenerGlobal(evt -> {
final Object oldValue = evt.getOldValue();
Expand Down

0 comments on commit 6134019

Please sign in to comment.