Skip to content

Commit 62935b4

Browse files
committed
new methods mix and mixOrRemove
1 parent 89cfe43 commit 62935b4

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
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.41.unsafe</version>
6+
<version>3.0.42.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: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ public JSONObject update(String key, Object newValue) throws JSONException {
18761876
* If updateListener not initialized.
18771877
*/
18781878
public JSONObject update(JSONObject jo) throws JSONException {
1879-
return this.updateOrRemove(jo, false);
1879+
return this.updateOrRemove(jo, false, true);
18801880
}
18811881

18821882
/**
@@ -1896,10 +1896,18 @@ public JSONObject update(JSONObject jo) throws JSONException {
18961896
* If updateListener not initialized.
18971897
*/
18981898
public JSONObject updateOrRemove(JSONObject jo) throws JSONException {
1899-
return this.updateOrRemove(jo, true);
1899+
return this.updateOrRemove(jo, true, true);
19001900
}
19011901

1902-
private JSONObject updateOrRemove(JSONObject jo, boolean remove) throws JSONException {
1902+
public JSONObject mix(JSONObject jo) throws JSONException {
1903+
return this.updateOrRemove(jo, false, false);
1904+
}
1905+
1906+
public JSONObject mixOrRemove(JSONObject jo) throws JSONException {
1907+
return this.updateOrRemove(jo, true, false);
1908+
}
1909+
1910+
private JSONObject updateOrRemove(JSONObject jo, boolean remove, boolean triggerEvent) throws JSONException {
19031911
final JSONObject oldThis = new JSONObject(this.toString());
19041912

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

1933-
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1941+
if (triggerEvent) {
1942+
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
19341943

1935-
oldValues.forEach((key, oldValue) -> {
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-
}
1944+
oldValues.forEach((key, oldValue) -> {
1945+
if (this.propertyChangeSupport.hasListeners(key)) {
1946+
final Object newValue;
1947+
if (remove && delValues.contains(key)) {
1948+
newValue = null;
1949+
} else {
1950+
newValue = newValues.get(key);
1951+
}
19431952

1944-
if (oldValue == null && newValue == null) {
1945-
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1946-
} else {
1947-
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1953+
if (oldValue == null && newValue == null) {
1954+
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1955+
} else {
1956+
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1957+
}
19481958
}
1949-
}
1950-
});
1959+
});
1960+
}
19511961

19521962
return this;
19531963
}

0 commit comments

Comments
 (0)