From 835ec2cfcc00d72d0dc0ff3ff2bc6b3bd2c5293c Mon Sep 17 00:00:00 2001 From: uklimaschewski Date: Fri, 3 Jan 2014 12:42:16 +0100 Subject: [PATCH] Retain sort order if object is an instance of SortedMap --- JSONObject.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 5ca5a45bc..a30e66d0f 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -38,6 +38,8 @@ of this software and associated documentation files (the "Software"), to deal import java.util.Map; import java.util.ResourceBundle; import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; /** * A JSONObject is an unordered collection of name/value pairs. Its external @@ -240,7 +242,11 @@ public JSONObject(JSONTokener x) throws JSONException { * @throws JSONException */ public JSONObject(Map map) { - this.map = new HashMap(); + if (map instanceof SortedMap) { + this.map = new TreeMap(map); + } else { + this.map = new HashMap(); + } if (map != null) { Iterator i = map.entrySet().iterator(); while (i.hasNext()) { @@ -252,7 +258,7 @@ public JSONObject(Map map) { } } } - + /** * Construct a JSONObject from an Object using bean getters. It reflects on * all of the public methods of the object. For each of the methods with no