Skip to content

Commit

Permalink
new methods mix and mixOrRemove
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardofel committed Nov 30, 2021
1 parent 89cfe43 commit 62935b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 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.41.unsafe</version>
<version>3.0.42.unsafe</version>
<packaging>jar</packaging>

<name>JSON in Java WITH WORKING .put(null)</name>
Expand Down
46 changes: 28 additions & 18 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ public JSONObject update(String key, Object newValue) throws JSONException {
* If updateListener not initialized.
*/
public JSONObject update(JSONObject jo) throws JSONException {
return this.updateOrRemove(jo, false);
return this.updateOrRemove(jo, false, true);
}

/**
Expand All @@ -1896,10 +1896,18 @@ public JSONObject update(JSONObject jo) throws JSONException {
* If updateListener not initialized.
*/
public JSONObject updateOrRemove(JSONObject jo) throws JSONException {
return this.updateOrRemove(jo, true);
return this.updateOrRemove(jo, true, true);
}

private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONException {
public JSONObject mix(JSONObject jo) throws JSONException {
return this.updateOrRemove(jo, false, false);
}

public JSONObject mixOrRemove(JSONObject jo) throws JSONException {
return this.updateOrRemove(jo, true, false);
}

private JSONObject updateOrRemove(JSONObject jo, boolean remove, boolean triggerEvent) throws JSONException {
final JSONObject oldThis = new JSONObject(this.toString());

final HashMap<String, Object> oldValues = new HashMap<String, Object>();
Expand Down Expand Up @@ -1930,24 +1938,26 @@ private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONExce
}
}

this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
if (triggerEvent) {
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);

oldValues.forEach((key, oldValue) -> {
if (this.propertyChangeSupport.hasListeners(key)) {
final Object newValue;
if (remove && delValues.contains(key)) {
newValue = null;
} else {
newValue = newValues.get(key);
}
oldValues.forEach((key, oldValue) -> {
if (this.propertyChangeSupport.hasListeners(key)) {
final Object newValue;
if (remove && delValues.contains(key)) {
newValue = null;
} else {
newValue = newValues.get(key);
}

if (oldValue == null && newValue == null) {
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
} else {
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
if (oldValue == null && newValue == null) {
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
} else {
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
}
}
}
});
});
}

return this;
}
Expand Down

0 comments on commit 62935b4

Please sign in to comment.