Skip to content

Updates for supporting the Android API #350

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Oct 30, 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
14 changes: 6 additions & 8 deletions CookieList.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.json;

import java.util.Map.Entry;

/*
Copyright (c) 2002 JSON.org

Expand All @@ -24,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
*/

/**
* Convert a web browser cookie list string to a JSONObject and back.
Expand Down Expand Up @@ -69,17 +67,17 @@ public static JSONObject toJSONObject(String string) throws JSONException {
*/
public static String toString(JSONObject jo) throws JSONException {
boolean b = false;
StringBuilder sb = new StringBuilder();
for (final Entry<String,?> entry : jo.entrySet()) {
final String key = entry.getKey();
final Object value = entry.getValue();
final StringBuilder sb = new StringBuilder();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
final Object value = jo.opt(key);
if (!JSONObject.NULL.equals(value)) {
if (b) {
sb.append(';');
}
sb.append(Cookie.escape(key));
sb.append("=");
sb.append(Cookie.escape(value.toString()));
sb.append(Cookie.escape(value.toString()));
b = true;
}
}
Expand Down
8 changes: 4 additions & 4 deletions HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ of this software and associated documentation files (the "Software"), to deal
*/

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

/**
* Convert an HTTP header to a JSONObject and back.
Expand Down Expand Up @@ -145,11 +144,12 @@ public static String toString(JSONObject jo) throws JSONException {
throw new JSONException("Not enough material for an HTTP header.");
}
sb.append(CRLF);
for (final Entry<String,?> entry : jo.entrySet()) {
final String key = entry.getKey();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
String value = jo.optString(key);
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())) {
!"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) {
sb.append(key);
sb.append(": ");
sb.append(jo.optString(key));
Expand Down
14 changes: 6 additions & 8 deletions JSONML.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.json;

import java.util.Map.Entry;

/*
Copyright (c) 2008 JSON.org

Expand Down Expand Up @@ -416,10 +414,10 @@ public static String toString(JSONArray ja) throws JSONException {

// Emit the attributes

for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
final Object value = jo.opt(key);
XML.noSpace(key);
final Object value = entry.getValue();
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
Expand Down Expand Up @@ -495,11 +493,11 @@ public static String toString(JSONObject jo) throws JSONException {

//Emit the attributes

for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
XML.noSpace(key);
value = entry.getValue();
value = jo.opt(key);
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
Expand Down
56 changes: 7 additions & 49 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,8 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
* A String.
* @return A simple JSON value.
*/
// Changes to this method must be copied to the corresponding method in
// the XML class to keep full support for Android
public static Object stringToValue(String string) {
if (string.equals("")) {
return string;
Expand Down Expand Up @@ -2120,55 +2122,11 @@ public String toString(int indentFactor) throws JSONException {
* If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
if (value == null || value.equals(null)) {
return "null";
}
if (value instanceof JSONString) {
Object object;
try {
object = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
if (object instanceof String) {
return (String) object;
}
throw new JSONException("Bad value from toJSONString: " + object);
}
if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
final String numberAsString = numberToString((Number) value);
try {
// Use the BigDecimal constructor for it's parser to validate the format.
@SuppressWarnings("unused")
BigDecimal unused = new BigDecimal(numberAsString);
// Close enough to a JSON number that we will return it unquoted
return numberAsString;
} catch (NumberFormatException ex){
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
return quote(numberAsString);
}
}
if (value instanceof Boolean || value instanceof JSONObject
|| value instanceof JSONArray) {
return value.toString();
}
if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
return new JSONObject(map).toString();
}
if (value instanceof Collection) {
Collection<?> coll = (Collection<?>) value;
return new JSONArray(coll).toString();
}
if (value.getClass().isArray()) {
return new JSONArray(value).toString();
}
if(value instanceof Enum<?>){
return quote(((Enum<?>)value).name());
}
return quote(value.toString());
// moves the implementation to JSONWriter as:
// 1. It makes more sense to be part of the writer class
// 2. For Android support this method is not available. By implementing it in the Writer
// Android users can use the writer with the built in Android JSONObject implementation.
return JSONWriter.valueToString(value);
}

/**
Expand Down
17 changes: 11 additions & 6 deletions JSONPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
Copyright (c) 2002 JSON.org
Expand Down Expand Up @@ -181,7 +183,7 @@ private String unescape(String token) {
* @return the result of the evaluation
* @throws JSONPointerException if an error occurs during evaluation
*/
public Object queryFrom(Object document) {
public Object queryFrom(Object document) throws JSONPointerException {
if (this.refTokens.isEmpty()) {
return document;
}
Expand All @@ -205,18 +207,21 @@ public Object queryFrom(Object document) {
* @param current the JSONArray to be evaluated
* @param indexToken the array index in string form
* @return the matched object. If no matching item is found a
* JSONPointerException is thrown
* @throws JSONPointerException is thrown if the index is out of bounds
*/
@SuppressWarnings("boxing")
private Object readByIndexToken(Object current, String indexToken) {
private Object readByIndexToken(Object current, String indexToken) throws JSONPointerException {
try {
int index = Integer.parseInt(indexToken);
JSONArray currentArr = (JSONArray) current;
if (index >= currentArr.length()) {
throw new JSONPointerException(format("index %d is out of bounds - the array has %d elements", index,
currentArr.length()));
}
return currentArr.get(index);
try {
return currentArr.get(index);
} catch (JSONException e) {
throw new JSONPointerException("Error reading value at index position " + index, e);
}
} catch (NumberFormatException e) {
throw new JSONPointerException(format("%s is not an array index", indexToken), e);
}
Expand Down
96 changes: 94 additions & 2 deletions JSONWriter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.json;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;

/*
Copyright (c) 2006 JSON.org
Expand Down Expand Up @@ -117,6 +120,9 @@ private JSONWriter append(String string) throws JSONException {
}
this.writer.append(string);
} catch (IOException e) {
// Android as of API 25 does not support this exception constructor
// however we won't worry about it. If an exception is happening here
// it will just throw a "Method not found" exception instead.
throw new JSONException(e);
}
if (this.mode == 'o') {
Expand Down Expand Up @@ -164,6 +170,9 @@ private JSONWriter end(char m, char c) throws JSONException {
try {
this.writer.append(c);
} catch (IOException e) {
// Android as of API 25 does not support this exception constructor
// however we won't worry about it. If an exception is happening here
// it will just throw a "Method not found" exception instead.
throw new JSONException(e);
}
this.comma = true;
Expand Down Expand Up @@ -204,7 +213,12 @@ public JSONWriter key(String string) throws JSONException {
}
if (this.mode == 'k') {
try {
this.stack[this.top - 1].putOnce(string, Boolean.TRUE);
JSONObject topObject = this.stack[this.top - 1];
// don't use the built in putOnce method to maintain Android support
if(topObject.has(string)) {
throw new JSONException("Duplicate key \"" + string + "\"");
}
topObject.put(string, true);
if (this.comma) {
this.writer.append(',');
}
Expand All @@ -214,6 +228,9 @@ public JSONWriter key(String string) throws JSONException {
this.mode = 'o';
return this;
} catch (IOException e) {
// Android as of API 25 does not support this exception constructor
// however we won't worry about it. If an exception is happening here
// it will just throw a "Method not found" exception instead.
throw new JSONException(e);
}
}
Expand Down Expand Up @@ -280,6 +297,81 @@ private void push(JSONObject jo) throws JSONException {
this.top += 1;
}

/**
* Make a JSON text of an Object value. If the object has an
* value.toJSONString() method, then that method will be used to produce the
* JSON text. The method is required to produce a strictly conforming text.
* If the object does not contain a toJSONString method (which is the most
* common case), then a text will be produced by other means. If the value
* is an array or Collection, then a JSONArray will be made from it and its
* toJSONString method will be called. If the value is a MAP, then a
* JSONObject will be made from it and its toJSONString method will be
* called. Otherwise, the value's toString method will be called, and the
* result will be quoted.
*
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The value to be serialized.
* @return a printable, displayable, transmittable representation of the
* object, beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>.
* @throws JSONException
* If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
if (value == null || value.equals(null)) {
return "null";
}
if (value instanceof JSONString) {
Object object;
try {
object = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
if (object instanceof String) {
return (String) object;
}
throw new JSONException("Bad value from toJSONString: " + object);
}
if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
final String numberAsString = JSONObject.numberToString((Number) value);
try {
// Use the BigDecimal constructor for it's parser to validate the format.
@SuppressWarnings("unused")
BigDecimal unused = new BigDecimal(numberAsString);
// Close enough to a JSON number that we will return it unquoted
return numberAsString;
} catch (NumberFormatException ex){
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
return JSONObject.quote(numberAsString);
}
}
if (value instanceof Boolean || value instanceof JSONObject
|| value instanceof JSONArray) {
return value.toString();
}
if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
return new JSONObject(map).toString();
}
if (value instanceof Collection) {
Collection<?> coll = (Collection<?>) value;
return new JSONArray(coll).toString();
}
if (value.getClass().isArray()) {
return new JSONArray(value).toString();
}
if(value instanceof Enum<?>){
return JSONObject.quote(((Enum<?>)value).name());
}
return JSONObject.quote(value.toString());
}

/**
* Append either the value <code>true</code> or the value
Expand Down Expand Up @@ -321,6 +413,6 @@ public JSONWriter value(long l) throws JSONException {
* @throws JSONException If the value is out of sequence.
*/
public JSONWriter value(Object object) throws JSONException {
return this.append(JSONObject.valueToString(object));
return this.append(valueToString(object));
}
}
12 changes: 7 additions & 5 deletions Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ of this software and associated documentation files (the "Software"), to deal
*/

import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;

/**
Expand All @@ -41,7 +40,9 @@ public class Property {
* @throws JSONException
*/
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
// can't use the new constructor for Android support
// JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
JSONObject jo = new JSONObject();
if (properties != null && !properties.isEmpty()) {
Enumeration<?> enumProperties = properties.propertyNames();
while(enumProperties.hasMoreElements()) {
Expand All @@ -61,10 +62,11 @@ public static JSONObject toJSONObject(java.util.Properties properties) throws JS
public static Properties toProperties(JSONObject jo) throws JSONException {
Properties properties = new Properties();
if (jo != null) {
for (final Entry<String, ?> entry : jo.entrySet()) {
Object value = entry.getValue();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
Object value = jo.opt(key);
if (!JSONObject.NULL.equals(value)) {
properties.put(entry.getKey(), value.toString());
properties.put(key, value.toString());
}
}
}
Expand Down
Loading