@@ -31,15 +31,8 @@ of this software and associated documentation files (the "Software"), to deal
3131import java .lang .reflect .Method ;
3232import java .lang .reflect .Modifier ;
3333import java .math .*;
34- import java .util .Collection ;
35- import java .util .Enumeration ;
36- import java .util .HashMap ;
37- import java .util .Iterator ;
38- import java .util .Locale ;
39- import java .util .Map ;
34+ import java .util .*;
4035import java .util .Map .Entry ;
41- import java .util .ResourceBundle ;
42- import java .util .Set ;
4336
4437/**
4538 * A JSONObject is an unordered collection of name/value pairs. Its external
@@ -153,7 +146,7 @@ public String toString() {
153146 * Construct an empty JSONObject.
154147 */
155148 public JSONObject () {
156- this .map = new HashMap <String , Object >();
149+ this .map = new TreeMap <String , Object >();
157150 }
158151
159152 /**
@@ -389,15 +382,29 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
389382 */
390383 public JSONObject accumulate (String key , Object value ) throws JSONException {
391384 testValidity (value );
392- Object object = this .opt (key );
385+
386+ Object object = this .opt (key +"s" );
387+
388+ if ( object == null )
389+ object = this .opt (key );
390+
393391 if (object == null ) {
394- this .put (key ,
395- value instanceof JSONArray ? new JSONArray ().put (value )
396- : value );
392+
393+ if ( value instanceof JSONArray )
394+ this .put ( key , new JSONArray ().put (value ) );
395+ else {
396+ this .put (key , value );
397+ }
398+
397399 } else if (object instanceof JSONArray ) {
398400 ((JSONArray ) object ).put (value );
399401 } else {
400- this .put (key , new JSONArray ().put (object ).put (value ));
402+ //pluralize
403+ boolean endsWithS = key .endsWith ("s" );
404+ if ( !endsWithS )
405+ this .remove (key );
406+
407+ this .put ( endsWithS ? key : key + "s" , new JSONArray ().put (object ).put (value ));
401408 }
402409 return this ;
403410 }
0 commit comments