Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ public String toString() {
*/
private final Map<String, Object> map;

/**
* An internal factory method that can be overridden if the
* user desires JSONObject to use a specific map type.
* <p>
* The default is HashMap to ensure that elements are unordered per the specification.
* JSON tends to be a portable transfer format that allows container
* implementations to rearrange their items for faster element
* retrieval based on associative access.
* Therefore, an implementation ought not rely on the order of items.
* @param capacity starting capacity. If less than 0, then use the default capacity/constructor
* @return a new Map
*/
protected Map<String, Object> newInternalMap(int capacity) {
if (capacity < 0)
return new HashMap<String, Object>();
return new HashMap<String, Object>(capacity);
}

/**
* Retrieves the type of the underlying Map in this class.
*
Expand All @@ -156,13 +174,7 @@ public Class<? extends Map> getMapType() {
* Construct an empty JSONObject.
*/
public JSONObject() {
// HashMap is used on purpose to ensure that elements are unordered by
// the specification.
// JSON tends to be a portable transfer format to allows the container
// implementations to rearrange their items for a faster element
// retrieval based on associative access.
// Therefore, an implementation mustn't rely on the order of the item.
this.map = new HashMap<String, Object>();
this.map = newInternalMap(-1);
}

/**
Expand Down Expand Up @@ -324,9 +336,9 @@ private JSONObject(Map<?, ?> m, int recursionDepth, JSONParserConfiguration json
throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth());
}
if (m == null) {
this.map = new HashMap<String, Object>();
this.map = newInternalMap(-1);
} else {
this.map = new HashMap<String, Object>(m.size());
this.map = newInternalMap(m.size());
for (final Entry<?, ?> e : m.entrySet()) {
if(e.getKey() == null) {
throw new NullPointerException("Null key.");
Expand Down Expand Up @@ -525,7 +537,7 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
* @param initialCapacity initial capacity of the internal map.
*/
protected JSONObject(int initialCapacity){
this.map = new HashMap<String, Object>(initialCapacity);
this.map = newInternalMap(initialCapacity);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4044,4 +4044,21 @@ public void jsonObjectParseNullFieldsWithoutParserConfiguration() {
assertTrue("JSONObject should be empty", jsonObject.isEmpty());
}

/**
* Testing that a custom map extension works.
*/
@Test
public void jsonObjectOrderedTest() {
JSONObject jsonObject = new JSONObject() {
@Override
public Map<String, Object> newInternalMap(int capacity) {
return new LinkedHashMap<String, Object>();
}
};
jsonObject.put("a", 1)
.put("c", 2)
.put("b", 3)
.put("d", 4);
assertEquals("{\"a\":1,\"c\":2,\"b\":3,\"d\":4}", jsonObject.toString());
}
}