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:
parent
fbd2be7431
commit
4f5bf16676
7 changed files with 95 additions and 88 deletions
|
@ -1,5 +1,7 @@
|
||||||
package org.json;
|
package org.json;
|
||||||
|
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2002 JSON.org
|
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.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a web browser cookie list string to a JSONObject and back.
|
* Convert a web browser cookie list string to a JSONObject and back.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
|
@ -69,18 +69,17 @@ public class CookieList {
|
||||||
*/
|
*/
|
||||||
public static String toString(JSONObject jo) throws JSONException {
|
public static String toString(JSONObject jo) throws JSONException {
|
||||||
boolean b = false;
|
boolean b = false;
|
||||||
Iterator<String> keys = jo.keys();
|
|
||||||
String string;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
while (keys.hasNext()) {
|
for (final Entry<String,?> entry : jo.entrySet()) {
|
||||||
string = keys.next();
|
final String key = entry.getKey();
|
||||||
if (!jo.isNull(string)) {
|
final Object value = entry.getValue();
|
||||||
|
if (!JSONObject.NULL.equals(value)) {
|
||||||
if (b) {
|
if (b) {
|
||||||
sb.append(';');
|
sb.append(';');
|
||||||
}
|
}
|
||||||
sb.append(Cookie.escape(string));
|
sb.append(Cookie.escape(key));
|
||||||
sb.append("=");
|
sb.append("=");
|
||||||
sb.append(Cookie.escape(jo.getString(string)));
|
sb.append(Cookie.escape(value.toString()));
|
||||||
b = true;
|
b = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
18
HTTP.java
18
HTTP.java
|
@ -24,8 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an HTTP header to a JSONObject and back.
|
* Convert an HTTP header to a JSONObject and back.
|
||||||
|
@ -126,8 +126,6 @@ public class HTTP {
|
||||||
* information.
|
* information.
|
||||||
*/
|
*/
|
||||||
public static String toString(JSONObject jo) throws JSONException {
|
public static String toString(JSONObject jo) throws JSONException {
|
||||||
Iterator<String> keys = jo.keys();
|
|
||||||
String string;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||||
sb.append(jo.getString("HTTP-Version"));
|
sb.append(jo.getString("HTTP-Version"));
|
||||||
|
@ -147,14 +145,14 @@ public class HTTP {
|
||||||
throw new JSONException("Not enough material for an HTTP header.");
|
throw new JSONException("Not enough material for an HTTP header.");
|
||||||
}
|
}
|
||||||
sb.append(CRLF);
|
sb.append(CRLF);
|
||||||
while (keys.hasNext()) {
|
for (final Entry<String,?> entry : jo.entrySet()) {
|
||||||
string = keys.next();
|
final String key = entry.getKey();
|
||||||
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&
|
if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
|
||||||
!"Reason-Phrase".equals(string) && !"Method".equals(string) &&
|
!"Reason-Phrase".equals(key) && !"Method".equals(key) &&
|
||||||
!"Request-URI".equals(string) && !jo.isNull(string)) {
|
!"Request-URI".equals(key) && !JSONObject.NULL.equals(entry.getValue())) {
|
||||||
sb.append(string);
|
sb.append(key);
|
||||||
sb.append(": ");
|
sb.append(": ");
|
||||||
sb.append(jo.getString(string));
|
sb.append(jo.optString(key));
|
||||||
sb.append(CRLF);
|
sb.append(CRLF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1263,8 +1263,14 @@ public class JSONArray implements Iterable<Object> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < len; i += 1) {
|
for (int i = 0; i < len; i += 1) {
|
||||||
Object valueThis = this.get(i);
|
Object valueThis = this.myArrayList.get(i);
|
||||||
Object valueOther = ((JSONArray)other).get(i);
|
Object valueOther = ((JSONArray)other).myArrayList.get(i);
|
||||||
|
if(valueThis == valueOther) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(valueThis == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (valueThis instanceof JSONObject) {
|
if (valueThis instanceof JSONObject) {
|
||||||
if (!((JSONObject)valueThis).similar(valueOther)) {
|
if (!((JSONObject)valueThis).similar(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
|
|
30
JSONML.java
30
JSONML.java
|
@ -1,5 +1,7 @@
|
||||||
package org.json;
|
package org.json;
|
||||||
|
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2008 JSON.org
|
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.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides static methods to convert an XML text into a JSONArray or
|
* 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
|
* 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 {
|
public static String toString(JSONArray ja) throws JSONException {
|
||||||
int i;
|
int i;
|
||||||
JSONObject jo;
|
JSONObject jo;
|
||||||
String key;
|
|
||||||
Iterator<String> keys;
|
|
||||||
int length;
|
int length;
|
||||||
Object object;
|
Object object;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String tagName;
|
String tagName;
|
||||||
String value;
|
|
||||||
|
|
||||||
// Emit <tagName
|
// Emit <tagName
|
||||||
|
|
||||||
|
@ -420,17 +416,16 @@ public class JSONML {
|
||||||
|
|
||||||
// Emit the attributes
|
// Emit the attributes
|
||||||
|
|
||||||
keys = jo.keys();
|
for (final Entry<String, ?> entry : jo.entrySet()) {
|
||||||
while (keys.hasNext()) {
|
final String key = entry.getKey();
|
||||||
key = keys.next();
|
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.optString(key);
|
final Object value = entry.getValue();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(XML.escape(key));
|
sb.append(XML.escape(key));
|
||||||
sb.append('=');
|
sb.append('=');
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
sb.append(XML.escape(value));
|
sb.append(XML.escape(value.toString()));
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -482,12 +477,10 @@ public class JSONML {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
int i;
|
int i;
|
||||||
JSONArray ja;
|
JSONArray ja;
|
||||||
String key;
|
|
||||||
Iterator<String> keys;
|
|
||||||
int length;
|
int length;
|
||||||
Object object;
|
Object object;
|
||||||
String tagName;
|
String tagName;
|
||||||
String value;
|
Object value;
|
||||||
|
|
||||||
//Emit <tagName
|
//Emit <tagName
|
||||||
|
|
||||||
|
@ -502,18 +495,17 @@ public class JSONML {
|
||||||
|
|
||||||
//Emit the attributes
|
//Emit the attributes
|
||||||
|
|
||||||
keys = jo.keys();
|
for (final Entry<String, ?> entry : jo.entrySet()) {
|
||||||
while (keys.hasNext()) {
|
final String key = entry.getKey();
|
||||||
key = keys.next();
|
|
||||||
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
|
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.optString(key);
|
value = entry.getValue();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(XML.escape(key));
|
sb.append(XML.escape(key));
|
||||||
sb.append('=');
|
sb.append('=');
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
sb.append(XML.escape(value));
|
sb.append(XML.escape(value.toString()));
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,14 +245,14 @@ public class JSONObject {
|
||||||
/**
|
/**
|
||||||
* Construct a JSONObject from a Map.
|
* Construct a JSONObject from a Map.
|
||||||
*
|
*
|
||||||
* @param map
|
* @param m
|
||||||
* A map object that can be used to initialize the contents of
|
* A map object that can be used to initialize the contents of
|
||||||
* the JSONObject.
|
* the JSONObject.
|
||||||
*/
|
*/
|
||||||
public JSONObject(Map<?, ?> map) {
|
public JSONObject(Map<?, ?> m) {
|
||||||
this.map = new HashMap<String, Object>();
|
this.map = new HashMap<String, Object>();
|
||||||
if (map != null) {
|
if (m != null) {
|
||||||
for (final Entry<?, ?> e : map.entrySet()) {
|
for (final Entry<?, ?> e : m.entrySet()) {
|
||||||
final Object value = e.getValue();
|
final Object value = e.getValue();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
this.map.put(String.valueOf(e.getKey()), wrap(value));
|
this.map.put(String.valueOf(e.getKey()), wrap(value));
|
||||||
|
@ -729,14 +729,7 @@ public class JSONObject {
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Iterator<String> iterator = jo.keys();
|
return jo.keySet().toArray(new String[length]);
|
||||||
String[] names = new String[length];
|
|
||||||
int i = 0;
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
names[i] = iterator.next();
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return names;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -837,8 +830,11 @@ 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.
|
* @return An iterator of the keys.
|
||||||
*/
|
*/
|
||||||
public Iterator<String> keys() {
|
public Iterator<String> 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.
|
* @return A keySet.
|
||||||
*/
|
*/
|
||||||
|
@ -854,6 +853,22 @@ public class JSONObject {
|
||||||
return this.map.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 A keySet.
|
||||||
|
*/
|
||||||
|
protected Set<Entry<String, Object>> entrySet() {
|
||||||
|
return this.map.entrySet();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of keys stored in the JSONObject.
|
* Get the number of keys stored in the JSONObject.
|
||||||
*
|
*
|
||||||
|
@ -871,12 +886,10 @@ public class JSONObject {
|
||||||
* is empty.
|
* is empty.
|
||||||
*/
|
*/
|
||||||
public JSONArray names() {
|
public JSONArray names() {
|
||||||
JSONArray ja = new JSONArray();
|
if(this.map.isEmpty()) {
|
||||||
Iterator<String> keys = this.keys();
|
return null;
|
||||||
while (keys.hasNext()) {
|
}
|
||||||
ja.put(keys.next());
|
return new JSONArray(this.map.keySet());
|
||||||
}
|
|
||||||
return ja.length() == 0 ? null : ja;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1762,15 +1775,19 @@ public class JSONObject {
|
||||||
if (!(other instanceof JSONObject)) {
|
if (!(other instanceof JSONObject)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Set<String> set = this.keySet();
|
if (!this.keySet().equals(((JSONObject)other).keySet())) {
|
||||||
if (!set.equals(((JSONObject)other).keySet())) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Iterator<String> iterator = set.iterator();
|
for (final Entry<String,?> entry : this.entrySet()) {
|
||||||
while (iterator.hasNext()) {
|
String name = entry.getKey();
|
||||||
String name = iterator.next();
|
Object valueThis = entry.getValue();
|
||||||
Object valueThis = this.get(name);
|
|
||||||
Object valueOther = ((JSONObject)other).get(name);
|
Object valueOther = ((JSONObject)other).get(name);
|
||||||
|
if(valueThis == valueOther) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(valueThis == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (valueThis instanceof JSONObject) {
|
if (valueThis instanceof JSONObject) {
|
||||||
if (!((JSONObject)valueThis).similar(valueOther)) {
|
if (!((JSONObject)valueThis).similar(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -2220,21 +2237,19 @@ public class JSONObject {
|
||||||
try {
|
try {
|
||||||
boolean commanate = false;
|
boolean commanate = false;
|
||||||
final int length = this.length();
|
final int length = this.length();
|
||||||
Iterator<String> keys = this.keys();
|
|
||||||
writer.write('{');
|
writer.write('{');
|
||||||
|
|
||||||
if (length == 1) {
|
if (length == 1) {
|
||||||
Object key = keys.next();
|
final Entry<String,?> entry = this.entrySet().iterator().next();
|
||||||
writer.write(quote(key.toString()));
|
writer.write(quote(entry.getKey()));
|
||||||
writer.write(':');
|
writer.write(':');
|
||||||
if (indentFactor > 0) {
|
if (indentFactor > 0) {
|
||||||
writer.write(' ');
|
writer.write(' ');
|
||||||
}
|
}
|
||||||
writeValue(writer, this.map.get(key), indentFactor, indent);
|
writeValue(writer, entry.getValue(), indentFactor, indent);
|
||||||
} else if (length != 0) {
|
} else if (length != 0) {
|
||||||
final int newindent = indent + indentFactor;
|
final int newindent = indent + indentFactor;
|
||||||
while (keys.hasNext()) {
|
for (final Entry<String,?> entry : this.entrySet()) {
|
||||||
Object key = keys.next();
|
|
||||||
if (commanate) {
|
if (commanate) {
|
||||||
writer.write(',');
|
writer.write(',');
|
||||||
}
|
}
|
||||||
|
@ -2242,12 +2257,12 @@ public class JSONObject {
|
||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
indent(writer, newindent);
|
indent(writer, newindent);
|
||||||
writer.write(quote(key.toString()));
|
writer.write(quote(entry.getKey()));
|
||||||
writer.write(':');
|
writer.write(':');
|
||||||
if (indentFactor > 0) {
|
if (indentFactor > 0) {
|
||||||
writer.write(' ');
|
writer.write(' ');
|
||||||
}
|
}
|
||||||
writeValue(writer, this.map.get(key), indentFactor, newindent);
|
writeValue(writer, entry.getValue(), indentFactor, newindent);
|
||||||
commanate = true;
|
commanate = true;
|
||||||
}
|
}
|
||||||
if (indentFactor > 0) {
|
if (indentFactor > 0) {
|
||||||
|
@ -2273,7 +2288,7 @@ public class JSONObject {
|
||||||
*/
|
*/
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
Map<String, Object> results = new HashMap<String, Object>();
|
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;
|
Object value;
|
||||||
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
|
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
|
||||||
value = null;
|
value = null;
|
||||||
|
|
|
@ -25,7 +25,7 @@ SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Map.Entry;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,10 +61,11 @@ public class Property {
|
||||||
public static Properties toProperties(JSONObject jo) throws JSONException {
|
public static Properties toProperties(JSONObject jo) throws JSONException {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
if (jo != null) {
|
if (jo != null) {
|
||||||
Iterator<String> keys = jo.keys();
|
for (final Entry<String, ?> entry : jo.entrySet()) {
|
||||||
while (keys.hasNext()) {
|
Object value = entry.getValue();
|
||||||
String name = keys.next();
|
if (!JSONObject.NULL.equals(value)) {
|
||||||
properties.put(name, jo.getString(name));
|
properties.put(entry.getKey(), value.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return properties;
|
return properties;
|
||||||
|
|
12
XML.java
12
XML.java
|
@ -25,6 +25,7 @@ SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides static methods to convert an XML text into a JSONObject, and to
|
* 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();
|
StringBuilder sb = new StringBuilder();
|
||||||
JSONArray ja;
|
JSONArray ja;
|
||||||
JSONObject jo;
|
JSONObject jo;
|
||||||
String key;
|
|
||||||
Iterator<String> keys;
|
|
||||||
String string;
|
String string;
|
||||||
Object value;
|
|
||||||
|
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
|
|
||||||
|
@ -529,16 +527,14 @@ public class XML {
|
||||||
|
|
||||||
// Loop thru the keys.
|
// Loop thru the keys.
|
||||||
jo = (JSONObject) object;
|
jo = (JSONObject) object;
|
||||||
keys = jo.keys();
|
for (final Entry<String, ?> entry : jo.entrySet()) {
|
||||||
while (keys.hasNext()) {
|
final String key = entry.getKey();
|
||||||
key = keys.next();
|
Object value = entry.getValue();
|
||||||
value = jo.opt(key);
|
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
value = "";
|
value = "";
|
||||||
} else if (value.getClass().isArray()) {
|
} else if (value.getClass().isArray()) {
|
||||||
value = new JSONArray(value);
|
value = new JSONArray(value);
|
||||||
}
|
}
|
||||||
string = value instanceof String ? (String) value : null;
|
|
||||||
|
|
||||||
// Emit content in body
|
// Emit content in body
|
||||||
if ("content".equals(key)) {
|
if ("content".equals(key)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue