@@ -38,6 +38,7 @@ of this software and associated documentation files (the "Software"), to deal
38
38
import java .lang .reflect .Modifier ;
39
39
import java .math .BigDecimal ;
40
40
import java .math .BigInteger ;
41
+ import java .util .ArrayList ;
41
42
import java .util .Collection ;
42
43
import java .util .Enumeration ;
43
44
import java .util .HashMap ;
@@ -1892,35 +1893,44 @@ public JSONObject updateOrRemove(JSONObject jo) throws JSONException {
1892
1893
}
1893
1894
1894
1895
private JSONObject updateOrRemove (JSONObject jo , boolean remove ) throws JSONException {
1895
- final HashMap <String , Map .Entry <Object , Object >> entries = new HashMap <String , Map .Entry <Object , Object >>();
1896
+ final HashMap <String , Object > oldValues = new HashMap <String , Object >();
1897
+ final HashMap <String , Object > newValues = new HashMap <String , Object >();
1898
+ final ArrayList <String > delValues = new ArrayList <String >();
1896
1899
1897
- jo .forEach ((key , newValue ) -> {
1898
- this .merge (key , newValue , ( v1 , v2 ) -> {
1899
- if (Objects .equals (v1 , v2 )) {
1900
+ jo .forEach ((key , v2 ) -> {
1901
+ this .compute (key , ( k , v1 ) -> {
1902
+ if (Objects .equals (v1 , v2 ) && Objects . equals ( v2 , v1 ) ) {
1900
1903
return v1 ;
1901
1904
} else {
1902
- entries .put (key , Map .entry (v1 , v2 ));
1903
- return v2 ;
1905
+ oldValues .put (key , JSONObject .NULL .equals (v1 ) ? null : v1 );
1906
+ newValues .put (key , JSONObject .NULL .equals (v2 ) ? null : v2 );
1907
+ return JSONObject .NULL .equals (v1 ) ? JSONObject .NULL : v1 ;
1904
1908
}
1905
1909
});
1906
1910
});
1907
1911
1908
1912
if (remove ) {
1909
- this . forEach (( key , oldValue ) -> {
1913
+ for ( String key : JSONObject . getNames ( this )) {
1910
1914
if (!jo .has (key )) {
1911
- this .remove (key );
1912
- final Object ret = entries .put (key , Map .entry (oldValue , null ));
1913
- if (ret != null ) {
1914
- System .err .println ("unexpected behavior!" );
1915
- }
1915
+ oldValues .put (key , this .remove (key ));
1916
+ delValues .add (key );
1916
1917
}
1917
- });
1918
+ }
1918
1919
}
1919
1920
1920
- entries .forEach ((key , entry ) -> {
1921
- final Object oldValue = entry .getKey ();
1922
- final Object newValue = entry .getValue ();
1923
- this .propertyChangeSupport .firePropertyChange (key , oldValue , newValue );
1921
+ oldValues .forEach ((key , oldValue ) -> {
1922
+ final Object newValue ;
1923
+ if (remove && delValues .contains (key )) {
1924
+ newValue = null ;
1925
+ } else {
1926
+ newValue = newValues .get (key );
1927
+ }
1928
+
1929
+ if (oldValue == null && newValue == null ) {
1930
+ this .propertyChangeSupport .firePropertyChange (key , JSONObject .NULL , newValue );
1931
+ } else {
1932
+ this .propertyChangeSupport .firePropertyChange (key , JSONObject .NULL , newValue );
1933
+ }
1924
1934
});
1925
1935
1926
1936
return this ;
0 commit comments