@@ -38,6 +38,7 @@ of this software and associated documentation files (the "Software"), to deal
3838import java .lang .reflect .Modifier ;
3939import java .math .BigDecimal ;
4040import java .math .BigInteger ;
41+ import java .util .ArrayList ;
4142import java .util .Collection ;
4243import java .util .Enumeration ;
4344import java .util .HashMap ;
@@ -1892,35 +1893,44 @@ public JSONObject updateOrRemove(JSONObject jo) throws JSONException {
18921893 }
18931894
18941895 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 >();
18961899
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 ) ) {
19001903 return v1 ;
19011904 } 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 ;
19041908 }
19051909 });
19061910 });
19071911
19081912 if (remove ) {
1909- this . forEach (( key , oldValue ) -> {
1913+ for ( String key : JSONObject . getNames ( this )) {
19101914 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 );
19161917 }
1917- });
1918+ }
19181919 }
19191920
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+ }
19241934 });
19251935
19261936 return this ;
0 commit comments