1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #348 from johnjaylward/ArrayPerformance

Capacity improvements for internal structures
This commit is contained in:
Sean Leary 2017-06-12 02:05:26 -05:00 committed by GitHub
commit 1add1247fa
3 changed files with 31 additions and 9 deletions

View file

@ -154,8 +154,10 @@ public class JSONArray implements Iterable<Object> {
* A Collection. * A Collection.
*/ */
public JSONArray(Collection<?> collection) { public JSONArray(Collection<?> collection) {
this.myArrayList = new ArrayList<Object>(); if (collection == null) {
if (collection != null) { this.myArrayList = new ArrayList<Object>();
} else {
this.myArrayList = new ArrayList<Object>(collection.size());
for (Object o: collection){ for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o)); this.myArrayList.add(JSONObject.wrap(o));
} }
@ -172,6 +174,7 @@ public class JSONArray implements Iterable<Object> {
this(); this();
if (array.getClass().isArray()) { if (array.getClass().isArray()) {
int length = Array.getLength(array); int length = Array.getLength(array);
this.myArrayList.ensureCapacity(length);
for (int i = 0; i < length; i += 1) { for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i))); this.put(JSONObject.wrap(Array.get(array, i)));
} }
@ -495,7 +498,7 @@ public class JSONArray implements Iterable<Object> {
* Get the optional object value associated with an index. * Get the optional object value associated with an index.
* *
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1. If not, null is returned.
* @return An object value, or null if there is no object at that index. * @return An object value, or null if there is no object at that index.
*/ */
public Object opt(int index) { public Object opt(int index) {
@ -1150,7 +1153,13 @@ public class JSONArray implements Iterable<Object> {
} }
if (index < this.length()) { if (index < this.length()) {
this.myArrayList.set(index, value); this.myArrayList.set(index, value);
} else if(index == this.length()){
// simple append
this.put(value);
} else { } else {
// if we are inserting past the length, we want to grow the array all at once
// instead of incrementally.
this.myArrayList.ensureCapacity(index + 1);
while (index != this.length()) { while (index != this.length()) {
this.put(JSONObject.NULL); this.put(JSONObject.NULL);
} }
@ -1302,7 +1311,7 @@ public class JSONArray implements Iterable<Object> {
if (names == null || names.length() == 0 || this.length() == 0) { if (names == null || names.length() == 0 || this.length() == 0) {
return null; return null;
} }
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject(names.length());
for (int i = 0; i < names.length(); i += 1) { for (int i = 0; i < names.length(); i += 1) {
jo.put(names.getString(i), this.opt(i)); jo.put(names.getString(i), this.opt(i));
} }

View file

@ -184,7 +184,7 @@ public class JSONObject {
* An array of strings. * An array of strings.
*/ */
public JSONObject(JSONObject jo, String[] names) { public JSONObject(JSONObject jo, String[] names) {
this(); this(names.length);
for (int i = 0; i < names.length; i += 1) { for (int i = 0; i < names.length; i += 1) {
try { try {
this.putOnce(names[i], jo.opt(names[i])); this.putOnce(names[i], jo.opt(names[i]));
@ -256,8 +256,10 @@ public class JSONObject {
* the JSONObject. * the JSONObject.
*/ */
public JSONObject(Map<?, ?> m) { public JSONObject(Map<?, ?> m) {
this.map = new HashMap<String, Object>(); if (m == null) {
if (m != null) { this.map = new HashMap<String, Object>();
} else {
this.map = new HashMap<String, Object>(m.size());
for (final Entry<?, ?> e : m.entrySet()) { for (final Entry<?, ?> e : m.entrySet()) {
final Object value = e.getValue(); final Object value = e.getValue();
if (value != null) { if (value != null) {
@ -308,7 +310,7 @@ public class JSONObject {
* from the object. * from the object.
*/ */
public JSONObject(Object object, String names[]) { public JSONObject(Object object, String names[]) {
this(); this(names.length);
Class<?> c = object.getClass(); Class<?> c = object.getClass();
for (int i = 0; i < names.length; i += 1) { for (int i = 0; i < names.length; i += 1) {
String name = names[i]; String name = names[i];
@ -377,6 +379,17 @@ public class JSONObject {
} }
} }
} }
/**
* Constructor to specify an initial capacity of the internal map. Useful for library
* internal calls where we know, or at least can best guess, how big this JSONObject
* will be.
*
* @param initialCapacity initial capacity of the internal map.
*/
protected JSONObject(int initialCapacity){
this.map = new HashMap<String, Object>(initialCapacity);
}
/** /**
* Accumulate values under a key. It is similar to the put method except * Accumulate values under a key. It is similar to the put method except

View file

@ -41,7 +41,7 @@ public class Property {
* @throws JSONException * @throws JSONException
*/ */
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException { public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
if (properties != null && !properties.isEmpty()) { if (properties != null && !properties.isEmpty()) {
Enumeration<?> enumProperties = properties.propertyNames(); Enumeration<?> enumProperties = properties.propertyNames();
while(enumProperties.hasMoreElements()) { while(enumProperties.hasMoreElements()) {