mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 16:00:51 -07:00
hygiene
This commit is contained in:
parent
667813de3c
commit
68de4feaec
2 changed files with 55 additions and 59 deletions
100
JSONObject.java
100
JSONObject.java
|
@ -163,10 +163,10 @@ public class JSONObject {
|
||||||
public JSONObject(JSONObject jo, String[] names) {
|
public JSONObject(JSONObject jo, String[] names) {
|
||||||
this();
|
this();
|
||||||
for (int i = 0; i < names.length; i += 1) {
|
for (int i = 0; i < names.length; i += 1) {
|
||||||
try {
|
try {
|
||||||
putOnce(names[i], jo.opt(names[i]));
|
putOnce(names[i], jo.opt(names[i]));
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,9 +197,7 @@ public class JSONObject {
|
||||||
key = x.nextValue().toString();
|
key = x.nextValue().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// The key is followed by ':'. We will also tolerate '=' or '=>'.
|
||||||
* The key is followed by ':'. We will also tolerate '=' or '=>'.
|
|
||||||
*/
|
|
||||||
|
|
||||||
c = x.nextClean();
|
c = x.nextClean();
|
||||||
if (c == '=') {
|
if (c == '=') {
|
||||||
|
@ -211,9 +209,7 @@ public class JSONObject {
|
||||||
}
|
}
|
||||||
putOnce(key, x.nextValue());
|
putOnce(key, x.nextValue());
|
||||||
|
|
||||||
/*
|
// Pairs are separated by ','. We will also tolerate ';'.
|
||||||
* Pairs are separated by ','. We will also tolerate ';'.
|
|
||||||
*/
|
|
||||||
|
|
||||||
switch (x.nextClean()) {
|
switch (x.nextClean()) {
|
||||||
case ';':
|
case ';':
|
||||||
|
@ -247,7 +243,7 @@ public class JSONObject {
|
||||||
Map.Entry e = (Map.Entry)i.next();
|
Map.Entry e = (Map.Entry)i.next();
|
||||||
Object value = e.getValue();
|
Object value = e.getValue();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
this.map.put(e.getKey(), wrap(value));
|
this.map.put(e.getKey(), wrap(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -326,33 +322,33 @@ public class JSONObject {
|
||||||
public JSONObject(String baseName, Locale locale) throws JSONException {
|
public JSONObject(String baseName, Locale locale) throws JSONException {
|
||||||
this();
|
this();
|
||||||
ResourceBundle r = ResourceBundle.getBundle(baseName, locale,
|
ResourceBundle r = ResourceBundle.getBundle(baseName, locale,
|
||||||
Thread.currentThread().getContextClassLoader());
|
Thread.currentThread().getContextClassLoader());
|
||||||
|
|
||||||
// Iterate through the keys in the bundle.
|
// Iterate through the keys in the bundle.
|
||||||
|
|
||||||
Enumeration keys = r.getKeys();
|
Enumeration keys = r.getKeys();
|
||||||
while (keys.hasMoreElements()) {
|
while (keys.hasMoreElements()) {
|
||||||
Object key = keys.nextElement();
|
Object key = keys.nextElement();
|
||||||
if (key instanceof String) {
|
if (key instanceof String) {
|
||||||
|
|
||||||
// Go through the path, ensuring that there is a nested JSONObject for each
|
// Go through the path, ensuring that there is a nested JSONObject for each
|
||||||
// segment except the last. Add the value using the last segment's name into
|
// segment except the last. Add the value using the last segment's name into
|
||||||
// the deepest nested JSONObject.
|
// the deepest nested JSONObject.
|
||||||
|
|
||||||
String[] path = ((String)key).split("\\.");
|
String[] path = ((String)key).split("\\.");
|
||||||
int last = path.length - 1;
|
int last = path.length - 1;
|
||||||
JSONObject target = this;
|
JSONObject target = this;
|
||||||
for (int i = 0; i < last; i += 1) {
|
for (int i = 0; i < last; i += 1) {
|
||||||
String segment = path[i];
|
String segment = path[i];
|
||||||
JSONObject nextTarget = target.optJSONObject(segment);
|
JSONObject nextTarget = target.optJSONObject(segment);
|
||||||
if (nextTarget == null) {
|
if (nextTarget == null) {
|
||||||
nextTarget = new JSONObject();
|
nextTarget = new JSONObject();
|
||||||
target.put(segment, nextTarget);
|
target.put(segment, nextTarget);
|
||||||
}
|
}
|
||||||
target = nextTarget;
|
target = nextTarget;
|
||||||
}
|
}
|
||||||
target.put(path[last], r.getString((String)key));
|
target.put(path[last], r.getString((String)key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -653,23 +649,23 @@ public class JSONObject {
|
||||||
* that is not an Integer, Long, Double, or Float.
|
* that is not an Integer, Long, Double, or Float.
|
||||||
*/
|
*/
|
||||||
public JSONObject increment(String key) throws JSONException {
|
public JSONObject increment(String key) throws JSONException {
|
||||||
Object value = opt(key);
|
Object value = opt(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
put(key, 1);
|
put(key, 1);
|
||||||
} else {
|
} else {
|
||||||
if (value instanceof Integer) {
|
if (value instanceof Integer) {
|
||||||
put(key, ((Integer)value).intValue() + 1);
|
put(key, ((Integer)value).intValue() + 1);
|
||||||
} else if (value instanceof Long) {
|
} else if (value instanceof Long) {
|
||||||
put(key, ((Long)value).longValue() + 1);
|
put(key, ((Long)value).longValue() + 1);
|
||||||
} else if (value instanceof Double) {
|
} else if (value instanceof Double) {
|
||||||
put(key, ((Double)value).doubleValue() + 1);
|
put(key, ((Double)value).doubleValue() + 1);
|
||||||
} else if (value instanceof Float) {
|
} else if (value instanceof Float) {
|
||||||
put(key, ((Float)value).floatValue() + 1);
|
put(key, ((Float)value).floatValue() + 1);
|
||||||
} else {
|
} else {
|
||||||
throw new JSONException("Unable to increment [" + key + "].");
|
throw new JSONException("Unable to increment [" + key + "].");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -961,12 +957,12 @@ public class JSONObject {
|
||||||
String name = method.getName();
|
String name = method.getName();
|
||||||
String key = "";
|
String key = "";
|
||||||
if (name.startsWith("get")) {
|
if (name.startsWith("get")) {
|
||||||
if (name.equals("getClass") ||
|
if (name.equals("getClass") ||
|
||||||
name.equals("getDeclaringClass")) {
|
name.equals("getDeclaringClass")) {
|
||||||
key = "";
|
key = "";
|
||||||
} else {
|
} else {
|
||||||
key = name.substring(3);
|
key = name.substring(3);
|
||||||
}
|
}
|
||||||
} else if (name.startsWith("is")) {
|
} else if (name.startsWith("is")) {
|
||||||
key = name.substring(2);
|
key = name.substring(2);
|
||||||
}
|
}
|
||||||
|
@ -982,7 +978,7 @@ public class JSONObject {
|
||||||
|
|
||||||
Object result = method.invoke(bean, (Object[])null);
|
Object result = method.invoke(bean, (Object[])null);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
map.put(key, wrap(result));
|
map.put(key, wrap(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1230,7 +1226,7 @@ public class JSONObject {
|
||||||
* @param s A String.
|
* @param s A String.
|
||||||
* @return A simple JSON value.
|
* @return A simple JSON value.
|
||||||
*/
|
*/
|
||||||
static public Object stringToValue(String s) {
|
public static Object stringToValue(String s) {
|
||||||
if (s.equals("")) {
|
if (s.equals("")) {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -1264,7 +1260,7 @@ public class JSONObject {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (s.indexOf('.') > -1 ||
|
if (s.indexOf('.') > -1 ||
|
||||||
s.indexOf('e') > -1 || s.indexOf('E') > -1) {
|
s.indexOf('e') > -1 || s.indexOf('E') > -1) {
|
||||||
return Double.valueOf(s);
|
return Double.valueOf(s);
|
||||||
} else {
|
} else {
|
||||||
Long myLong = new Long(s);
|
Long myLong = new Long(s);
|
||||||
|
@ -1558,8 +1554,8 @@ public class JSONObject {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (object instanceof JSONObject || object instanceof JSONArray ||
|
if (object instanceof JSONObject || object instanceof JSONArray ||
|
||||||
NULL.equals(object) || object instanceof JSONString ||
|
NULL.equals(object) || object instanceof JSONString ||
|
||||||
object instanceof Byte || object instanceof Character ||
|
object instanceof Byte || object instanceof Character ||
|
||||||
object instanceof Short || object instanceof Integer ||
|
object instanceof Short || object instanceof Integer ||
|
||||||
object instanceof Long || object instanceof Boolean ||
|
object instanceof Long || object instanceof Boolean ||
|
||||||
object instanceof Float || object instanceof Double ||
|
object instanceof Float || object instanceof Double ||
|
||||||
|
@ -1579,8 +1575,8 @@ public class JSONObject {
|
||||||
Package objectPackage = object.getClass().getPackage();
|
Package objectPackage = object.getClass().getPackage();
|
||||||
String objectPackageName = ( objectPackage != null ? objectPackage.getName() : "" );
|
String objectPackageName = ( objectPackage != null ? objectPackage.getName() : "" );
|
||||||
if (objectPackageName.startsWith("java.") ||
|
if (objectPackageName.startsWith("java.") ||
|
||||||
objectPackageName.startsWith("javax.") ||
|
objectPackageName.startsWith("javax.") ||
|
||||||
object.getClass().getClassLoader() == null) {
|
object.getClass().getClassLoader() == null) {
|
||||||
return object.toString();
|
return object.toString();
|
||||||
}
|
}
|
||||||
return new JSONObject(object);
|
return new JSONObject(object);
|
||||||
|
|
10
README
10
README
|
@ -25,15 +25,15 @@ The package compiles on Java 1.2 thru Java 1.4.
|
||||||
|
|
||||||
|
|
||||||
JSONObject.java: The JSONObject can parse text from a String or a JSONTokener
|
JSONObject.java: The JSONObject can parse text from a String or a JSONTokener
|
||||||
to produce a map-like object. The object provides methods of manipulating its
|
to produce a map-like object. The object provides methods for manipulating its
|
||||||
contents, and for producing a JSON compliant serialization.
|
contents, and for producing a JSON compliant object serialization.
|
||||||
|
|
||||||
JSONArray.java: The JSONObject can parse text from a String or a JSONTokener
|
JSONArray.java: The JSONObject can parse text from a String or a JSONTokener
|
||||||
to produce a vector-like object. The object provides methods of manipulating
|
to produce a vector-like object. The object provides methods for manipulating
|
||||||
its contents, and for producing a JSON compliant serialization.
|
its contents, and for producing a JSON compliant array serialization.
|
||||||
|
|
||||||
JSONTokenizer.java: The JSONTokener breaks a text into a sequence of individual
|
JSONTokenizer.java: The JSONTokener breaks a text into a sequence of individual
|
||||||
tokens. It can be constructed from a String,Reader, or InputStream.
|
tokens. It can be constructed from a String, Reader, or InputStream.
|
||||||
|
|
||||||
JSONException.java: The JSONException is the standard exception type thrown
|
JSONException.java: The JSONException is the standard exception type thrown
|
||||||
by this package.
|
by this package.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue