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

* Adds protected entrySet accessor to JSONObject

* Updates loops that request key/value pairs to use the new entrySet accessor
This commit is contained in:
John J. Aylward 2017-05-22 00:50:39 -04:00
parent fbd2be7431
commit 4f5bf16676
7 changed files with 95 additions and 88 deletions

View file

@ -1,5 +1,7 @@
package org.json;
import java.util.Map.Entry;
/*
Copyright (c) 2002 JSON.org
@ -24,8 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.util.Iterator;
/**
* Convert a web browser cookie list string to a JSONObject and back.
* @author JSON.org
@ -69,18 +69,17 @@ public class CookieList {
*/
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;
}
}

View file

@ -24,8 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.util.Iterator;
import java.util.Locale;
import java.util.Map.Entry;
/**
* Convert an HTTP header to a JSONObject and back.
@ -126,8 +126,6 @@ public class HTTP {
* 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"));
@ -147,14 +145,14 @@ public class HTTP {
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);
}
}

View file

@ -1263,8 +1263,14 @@ public class JSONArray implements Iterable<Object> {
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;

View file

@ -1,5 +1,7 @@
package org.json;
import java.util.Map.Entry;
/*
Copyright (c) 2008 JSON.org
@ -24,9 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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
@ -397,13 +396,10 @@ public class JSONML {
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
@ -420,17 +416,16 @@ public class JSONML {
// 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('"');
}
}
@ -482,12 +477,10 @@ public class JSONML {
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
@ -502,18 +495,17 @@ public class JSONML {
//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('"');
}
}

View file

@ -245,14 +245,14 @@ public class JSONObject {
/**
* 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));
@ -729,14 +729,7 @@ public class JSONObject {
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]);
}
/**
@ -837,7 +830,10 @@ public class JSONObject {
}
/**
* 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.
*/
@ -846,7 +842,10 @@ public class JSONObject {
}
/**
* 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.
*/
@ -854,6 +853,22 @@ public class JSONObject {
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 A keySet.
*/
protected Set<Entry<String, Object>> entrySet() {
return this.map.entrySet();
}
/**
* Get the number of keys stored in the JSONObject.
*
@ -871,12 +886,10 @@ public class JSONObject {
* 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());
}
/**
@ -1762,15 +1775,19 @@ public class JSONObject {
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;
@ -2220,21 +2237,19 @@ public class JSONObject {
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(',');
}
@ -2242,12 +2257,12 @@ public class JSONObject {
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) {
@ -2273,7 +2288,7 @@ public class JSONObject {
*/
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;

View file

@ -25,7 +25,7 @@ SOFTWARE.
*/
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
/**
@ -61,10 +61,11 @@ public class Property {
public static Properties toProperties(JSONObject jo) throws JSONException {
Properties properties = new Properties();
if (jo != null) {
Iterator<String> keys = jo.keys();
while (keys.hasNext()) {
String name = keys.next();
properties.put(name, jo.getString(name));
for (final Entry<String, ?> entry : jo.entrySet()) {
Object value = entry.getValue();
if (!JSONObject.NULL.equals(value)) {
properties.put(entry.getKey(), value.toString());
}
}
}
return properties;

View file

@ -25,6 +25,7 @@ SOFTWARE.
*/
import java.util.Iterator;
import java.util.Map.Entry;
/**
* This provides static methods to convert an XML text into a JSONObject, and to
@ -513,10 +514,7 @@ public class XML {
StringBuilder sb = new StringBuilder();
JSONArray ja;
JSONObject jo;
String key;
Iterator<String> keys;
String string;
Object value;
if (object instanceof JSONObject) {
@ -529,16 +527,14 @@ public class XML {
// Loop thru the keys.
jo = (JSONObject) object;
keys = jo.keys();
while (keys.hasNext()) {
key = keys.next();
value = jo.opt(key);
for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
value = "";
} else if (value.getClass().isArray()) {
value = new JSONArray(value);
}
string = value instanceof String ? (String) value : null;
// Emit content in body
if ("content".equals(key)) {