Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize loops #341

Merged
merged 3 commits into from
Jun 1, 2017
Merged
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
17 changes: 8 additions & 9 deletions CookieList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.json;

import java.util.Map.Entry;

/*
Copyright (c) 2002 JSON.org

Expand All @@ -24,8 +26,6 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/

import java.util.Iterator;

/**
* Convert a web browser cookie list string to a JSONObject and back.
* @author JSON.org
Expand Down Expand Up @@ -69,18 +69,17 @@ public static JSONObject toJSONObject(String string) throws JSONException {
*/
public static String toString(JSONObject jo) throws JSONException {
boolean b = false;
Iterator<String> keys = jo.keys();
String string;
StringBuilder sb = new StringBuilder();
while (keys.hasNext()) {
string = keys.next();
if (!jo.isNull(string)) {
for (final Entry<String,?> entry : jo.entrySet()) {
final String key = entry.getKey();
final Object value = entry.getValue();
if (!JSONObject.NULL.equals(value)) {
if (b) {
sb.append(';');
}
sb.append(Cookie.escape(string));
sb.append(Cookie.escape(key));
sb.append("=");
sb.append(Cookie.escape(jo.getString(string)));
sb.append(Cookie.escape(value.toString()));
b = true;
}
}
Expand Down
18 changes: 8 additions & 10 deletions HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/

import java.util.Iterator;
import java.util.Locale;
import java.util.Map.Entry;

/**
* Convert an HTTP header to a JSONObject and back.
Expand Down Expand Up @@ -126,8 +126,6 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* information.
*/
public static String toString(JSONObject jo) throws JSONException {
Iterator<String> keys = jo.keys();
String string;
StringBuilder sb = new StringBuilder();
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
sb.append(jo.getString("HTTP-Version"));
Expand All @@ -147,14 +145,14 @@ public static String toString(JSONObject jo) throws JSONException {
throw new JSONException("Not enough material for an HTTP header.");
}
sb.append(CRLF);
while (keys.hasNext()) {
string = keys.next();
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&
!"Reason-Phrase".equals(string) && !"Method".equals(string) &&
!"Request-URI".equals(string) && !jo.isNull(string)) {
sb.append(string);
for (final Entry<String,?> entry : jo.entrySet()) {
final String key = entry.getKey();
if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
!"Reason-Phrase".equals(key) && !"Method".equals(key) &&
!"Request-URI".equals(key) && !JSONObject.NULL.equals(entry.getValue())) {
sb.append(key);
sb.append(": ");
sb.append(jo.getString(string));
sb.append(jo.optString(key));
sb.append(CRLF);
}
}
Expand Down
10 changes: 8 additions & 2 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1263,8 +1263,14 @@ public boolean similar(Object other) {
return false;
}
for (int i = 0; i < len; i += 1) {
Object valueThis = this.get(i);
Object valueOther = ((JSONArray)other).get(i);
Object valueThis = this.myArrayList.get(i);
Object valueOther = ((JSONArray)other).myArrayList.get(i);
if(valueThis == valueOther) {
return true;
}
if(valueThis == null) {
return false;
}
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
Expand Down
30 changes: 11 additions & 19 deletions JSONML.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.json;

import java.util.Map.Entry;

/*
Copyright (c) 2008 JSON.org

Expand All @@ -24,9 +26,6 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/

import java.util.Iterator;


/**
* This provides static methods to convert an XML text into a JSONArray or
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
Expand Down Expand Up @@ -397,13 +396,10 @@ public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings) throws
public static String toString(JSONArray ja) throws JSONException {
int i;
JSONObject jo;
String key;
Iterator<String> keys;
int length;
Object object;
StringBuilder sb = new StringBuilder();
String tagName;
String value;

// Emit <tagName

Expand All @@ -420,17 +416,16 @@ public static String toString(JSONArray ja) throws JSONException {

// Emit the attributes

keys = jo.keys();
while (keys.hasNext()) {
key = keys.next();
for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
XML.noSpace(key);
value = jo.optString(key);
final Object value = entry.getValue();
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
sb.append('=');
sb.append('"');
sb.append(XML.escape(value));
sb.append(XML.escape(value.toString()));
sb.append('"');
}
}
Expand Down Expand Up @@ -482,12 +477,10 @@ public static String toString(JSONObject jo) throws JSONException {
StringBuilder sb = new StringBuilder();
int i;
JSONArray ja;
String key;
Iterator<String> keys;
int length;
Object object;
String tagName;
String value;
Object value;

//Emit <tagName

Expand All @@ -502,18 +495,17 @@ public static String toString(JSONObject jo) throws JSONException {

//Emit the attributes

keys = jo.keys();
while (keys.hasNext()) {
key = keys.next();
for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
XML.noSpace(key);
value = jo.optString(key);
value = entry.getValue();
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
sb.append('=');
sb.append('"');
sb.append(XML.escape(value));
sb.append(XML.escape(value.toString()));
sb.append('"');
}
}
Expand Down
88 changes: 51 additions & 37 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ public JSONObject(JSONTokener x) throws JSONException {
/**
* Construct a JSONObject from a Map.
*
* @param map
* @param m
* A map object that can be used to initialize the contents of
* the JSONObject.
*/
public JSONObject(Map<?, ?> map) {
public JSONObject(Map<?, ?> m) {
this.map = new HashMap<String, Object>();
if (map != null) {
for (final Entry<?, ?> e : map.entrySet()) {
if (m != null) {
for (final Entry<?, ?> e : m.entrySet()) {
final Object value = e.getValue();
if (value != null) {
this.map.put(String.valueOf(e.getKey()), wrap(value));
Expand Down Expand Up @@ -729,14 +729,7 @@ public static String[] getNames(JSONObject jo) {
if (length == 0) {
return null;
}
Iterator<String> iterator = jo.keys();
String[] names = new String[length];
int i = 0;
while (iterator.hasNext()) {
names[i] = iterator.next();
i++;
}
return names;
return jo.keySet().toArray(new String[length]);
}

/**
Expand Down Expand Up @@ -837,23 +830,45 @@ public boolean isNull(String key) {
}

/**
* Get an enumeration of the keys of the JSONObject.
* Get an enumeration of the keys of the JSONObject. Modifying this key Set will also
* modify the JSONObject. Use with caution.
*
* @see Set#iterator()
*
* @return An iterator of the keys.
*/
public Iterator<String> keys() {
return this.keySet().iterator();
}

/**
* Get a set of keys of the JSONObject.
* Get a set of keys of the JSONObject. Modifying this key Set will also modify the
* JSONObject. Use with caution.
*
* @see Map#keySet()
*
* @return A keySet.
*/
public Set<String> keySet() {
return this.map.keySet();
}

/**
* Get a set of entries of the JSONObject. These are raw values and may not
* match what is returned by the JSONObject get* and opt* functions. Modifying
* the returned EntrySet or the Entry objects contained therein will modify the
* backing JSONObject. This does not return a clone or a read-only view.
*
* Use with caution.
*
* @see Map#entrySet()
*
* @return An Entry Set
*/
protected Set<Entry<String, Object>> entrySet() {
return this.map.entrySet();
}

/**
* Get the number of keys stored in the JSONObject.
*
Expand All @@ -871,12 +886,10 @@ public int length() {
* is empty.
*/
public JSONArray names() {
JSONArray ja = new JSONArray();
Iterator<String> keys = this.keys();
while (keys.hasNext()) {
ja.put(keys.next());
}
return ja.length() == 0 ? null : ja;
if(this.map.isEmpty()) {
return null;
}
return new JSONArray(this.map.keySet());
}

/**
Expand Down Expand Up @@ -1762,15 +1775,19 @@ public boolean similar(Object other) {
if (!(other instanceof JSONObject)) {
return false;
}
Set<String> set = this.keySet();
if (!set.equals(((JSONObject)other).keySet())) {
if (!this.keySet().equals(((JSONObject)other).keySet())) {
return false;
}
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
Object valueThis = this.get(name);
for (final Entry<String,?> entry : this.entrySet()) {
String name = entry.getKey();
Object valueThis = entry.getValue();
Object valueOther = ((JSONObject)other).get(name);
if(valueThis == valueOther) {
return true;
}
if(valueThis == null) {
return false;
}
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
Expand Down Expand Up @@ -2201,8 +2218,7 @@ static final void indent(Writer writer, int indent) throws IOException {
}

/**
* Write the contents of the JSONObject as JSON text to a writer. For
* compactness, no whitespace is added.
* Write the contents of the JSONObject as JSON text to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
Expand All @@ -2220,34 +2236,32 @@ public Writer write(Writer writer, int indentFactor, int indent)
try {
boolean commanate = false;
final int length = this.length();
Iterator<String> keys = this.keys();
writer.write('{');

if (length == 1) {
Object key = keys.next();
writer.write(quote(key.toString()));
final Entry<String,?> entry = this.entrySet().iterator().next();
writer.write(quote(entry.getKey()));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
writeValue(writer, this.map.get(key), indentFactor, indent);
writeValue(writer, entry.getValue(), indentFactor, indent);
} else if (length != 0) {
final int newindent = indent + indentFactor;
while (keys.hasNext()) {
Object key = keys.next();
for (final Entry<String,?> entry : this.entrySet()) {
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
indent(writer, newindent);
writer.write(quote(key.toString()));
writer.write(quote(entry.getKey()));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
writeValue(writer, this.map.get(key), indentFactor, newindent);
writeValue(writer, entry.getValue(), indentFactor, newindent);
commanate = true;
}
if (indentFactor > 0) {
Expand All @@ -2273,7 +2287,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
*/
public Map<String, Object> toMap() {
Map<String, Object> results = new HashMap<String, Object>();
for (Entry<String, Object> entry : this.map.entrySet()) {
for (Entry<String, Object> entry : this.entrySet()) {
Object value;
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
value = null;
Expand Down
Loading