Skip to content

Commit 757b6ed

Browse files
author
John J. Aylward
committed
Merge branch 'master' of github.com:stleary/JSON-java into OptimizeOpt
2 parents 04d6e83 + f2b642a commit 757b6ed

File tree

8 files changed

+83
-55
lines changed

8 files changed

+83
-55
lines changed

CookieList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class CookieList {
3939
* The pairs are separated by ';'. The names and the values
4040
* will be unescaped, possibly converting '+' and '%' sequences.
4141
*
42-
* To add a cookie to a cooklist,
42+
* To add a cookie to a cookie list,
4343
* cookielistJSONObject.put(cookieJSONObject.getString("name"),
4444
* cookieJSONObject.getString("value"));
4545
* @param string A cookie list string

JSONArray.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public JSONArray(Object array) throws JSONException {
183183

184184
@Override
185185
public Iterator<Object> iterator() {
186-
return myArrayList.iterator();
186+
return this.myArrayList.iterator();
187187
}
188188

189189
/**
@@ -1314,6 +1314,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
13141314
* @return a printable, displayable, transmittable representation of the
13151315
* array.
13161316
*/
1317+
@Override
13171318
public String toString() {
13181319
try {
13191320
return this.toString(0);
@@ -1323,7 +1324,7 @@ public String toString() {
13231324
}
13241325

13251326
/**
1326-
* Make a prettyprinted JSON text of this JSONArray. Warning: This method
1327+
* Make a pretty-printed JSON text of this JSONArray. Warning: This method
13271328
* assumes that the data structure is acyclical.
13281329
*
13291330
* @param indentFactor
@@ -1365,7 +1366,7 @@ public Writer write(Writer writer) throws JSONException {
13651366
* @param indentFactor
13661367
* The number of spaces to add to each level of indentation.
13671368
* @param indent
1368-
* The indention of the top level.
1369+
* The indentation of the top level.
13691370
* @return The writer.
13701371
* @throws JSONException
13711372
*/

JSONML.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class JSONML {
4242
* @param arrayForm true if array form, false if object form.
4343
* @param ja The JSONArray that is containing the current tag or null
4444
* if we are at the outermost level.
45-
* @param keepStrings Don't type-convert text nodes and attibute values
45+
* @param keepStrings Don't type-convert text nodes and attribute values
4646
* @return A JSONArray if the value is the outermost tag, otherwise null.
4747
* @throws JSONException
4848
*/

JSONObject.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ protected final Object clone() {
126126
public boolean equals(Object object) {
127127
return object == null || object == this;
128128
}
129+
/**
130+
* A Null object is equal to the null value and to itself.
131+
*
132+
* @return always returns 0.
133+
*/
134+
@Override
135+
public int hashCode() {
136+
return 0;
137+
}
129138

130139
/**
131140
* Get the "null" string value.
@@ -801,13 +810,13 @@ public JSONObject increment(String key) throws JSONException {
801810
} else if (value instanceof BigDecimal) {
802811
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
803812
} else if (value instanceof Integer) {
804-
this.put(key, (Integer) value + 1);
813+
this.put(key, ((Integer) value).intValue() + 1);
805814
} else if (value instanceof Long) {
806-
this.put(key, (Long) value + 1);
815+
this.put(key, ((Long) value).longValue() + 1L);
807816
} else if (value instanceof Double) {
808-
this.put(key, (Double) value + 1);
817+
this.put(key, ((Double) value).doubleValue() + 1.0d);
809818
} else if (value instanceof Float) {
810-
this.put(key, (Float) value + 1);
819+
this.put(key, ((Float) value).floatValue() + 1.0f);
811820
} else {
812821
throw new JSONException("Unable to increment [" + quote(key) + "].");
813822
}
@@ -934,7 +943,7 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, String key) {
934943
* @param defaultValue
935944
* The default in case the value is not found
936945
* @return The enum value associated with the key or defaultValue
937-
* if the value is not found or cannot be assigned to clazz
946+
* if the value is not found or cannot be assigned to <code>clazz</code>
938947
*/
939948
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) {
940949
try {
@@ -1437,7 +1446,23 @@ public JSONObject put(String key, Collection<?> value) throws JSONException {
14371446
* If the key is null or if the number is invalid.
14381447
*/
14391448
public JSONObject put(String key, double value) throws JSONException {
1440-
this.put(key, new Double(value));
1449+
this.put(key, Double.valueOf(value));
1450+
return this;
1451+
}
1452+
1453+
/**
1454+
* Put a key/float pair in the JSONObject.
1455+
*
1456+
* @param key
1457+
* A key string.
1458+
* @param value
1459+
* A float which is the value.
1460+
* @return this.
1461+
* @throws JSONException
1462+
* If the key is null or if the number is invalid.
1463+
*/
1464+
public JSONObject put(String key, float value) throws JSONException {
1465+
this.put(key, Float.valueOf(value));
14411466
return this;
14421467
}
14431468

@@ -1453,7 +1478,7 @@ public JSONObject put(String key, double value) throws JSONException {
14531478
* If the key is null.
14541479
*/
14551480
public JSONObject put(String key, int value) throws JSONException {
1456-
this.put(key, new Integer(value));
1481+
this.put(key, Integer.valueOf(value));
14571482
return this;
14581483
}
14591484

@@ -1469,7 +1494,7 @@ public JSONObject put(String key, int value) throws JSONException {
14691494
* If the key is null.
14701495
*/
14711496
public JSONObject put(String key, long value) throws JSONException {
1472-
this.put(key, new Long(value));
1497+
this.put(key, Long.valueOf(value));
14731498
return this;
14741499
}
14751500

@@ -1559,7 +1584,7 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
15591584
}
15601585

15611586
/**
1562-
* Creates a JSONPointer using an intialization string and tries to
1587+
* Creates a JSONPointer using an initialization string and tries to
15631588
* match it to an item within this JSONObject. For example, given a
15641589
* JSONObject initialized with this document:
15651590
* <pre>
@@ -1581,7 +1606,7 @@ public Object query(String jsonPointer) {
15811606
return query(new JSONPointer(jsonPointer));
15821607
}
15831608
/**
1584-
* Uses a uaer initialized JSONPointer and tries to
1609+
* Uses a user initialized JSONPointer and tries to
15851610
* match it to an item within this JSONObject. For example, given a
15861611
* JSONObject initialized with this document:
15871612
* <pre>
@@ -1959,7 +1984,7 @@ public String toString() {
19591984
}
19601985

19611986
/**
1962-
* Make a prettyprinted JSON text of this JSONObject.
1987+
* Make a pretty-printed JSON text of this JSONObject.
19631988
* <p>
19641989
* Warning: This method assumes that the data structure is acyclical.
19651990
*
@@ -2024,7 +2049,8 @@ public static String valueToString(Object value) throws JSONException {
20242049
final String numberAsString = numberToString((Number) value);
20252050
try {
20262051
// Use the BigDecimal constructor for it's parser to validate the format.
2027-
new BigDecimal(numberAsString);
2052+
@SuppressWarnings("unused")
2053+
BigDecimal unused = new BigDecimal(numberAsString);
20282054
// Close enough to a JSON number that we will return it unquoted
20292055
return numberAsString;
20302056
} catch (NumberFormatException ex){
@@ -2185,7 +2211,7 @@ static final void indent(Writer writer, int indent) throws IOException {
21852211
* @param indentFactor
21862212
* The number of spaces to add to each level of indentation.
21872213
* @param indent
2188-
* The indention of the top level.
2214+
* The indentation of the top level.
21892215
* @return The writer.
21902216
* @throws JSONException
21912217
*/

JSONPointer.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public static class Builder {
6868
* {@link #append(String)} method calls.
6969
*/
7070
public JSONPointer build() {
71-
return new JSONPointer(refTokens);
71+
return new JSONPointer(this.refTokens);
7272
}
7373

7474
/**
75-
* Adds an arbitary token to the list of reference tokens. It can be any non-null value.
75+
* Adds an arbitrary token to the list of reference tokens. It can be any non-null value.
7676
*
7777
* Unlike in the case of JSON string or URI fragment representation of JSON pointers, the
7878
* argument of this method MUST NOT be escaped. If you want to query the property called
@@ -87,7 +87,7 @@ public Builder append(String token) {
8787
if (token == null) {
8888
throw new NullPointerException("token cannot be null");
8989
}
90-
refTokens.add(token);
90+
this.refTokens.add(token);
9191
return this;
9292
}
9393

@@ -99,7 +99,7 @@ public Builder append(String token) {
9999
* @return {@code this}
100100
*/
101101
public Builder append(int arrayIndex) {
102-
refTokens.add(String.valueOf(arrayIndex));
102+
this.refTokens.add(String.valueOf(arrayIndex));
103103
return this;
104104
}
105105
}
@@ -134,29 +134,30 @@ public static Builder builder() {
134134
* @param pointer the JSON String or URI Fragment representation of the JSON pointer.
135135
* @throws IllegalArgumentException if {@code pointer} is not a valid JSON pointer
136136
*/
137-
public JSONPointer(String pointer) {
137+
public JSONPointer(final String pointer) {
138138
if (pointer == null) {
139139
throw new NullPointerException("pointer cannot be null");
140140
}
141141
if (pointer.isEmpty() || pointer.equals("#")) {
142-
refTokens = Collections.emptyList();
142+
this.refTokens = Collections.emptyList();
143143
return;
144144
}
145+
String refs;
145146
if (pointer.startsWith("#/")) {
146-
pointer = pointer.substring(2);
147+
refs = pointer.substring(2);
147148
try {
148-
pointer = URLDecoder.decode(pointer, ENCODING);
149+
refs = URLDecoder.decode(refs, ENCODING);
149150
} catch (UnsupportedEncodingException e) {
150151
throw new RuntimeException(e);
151152
}
152153
} else if (pointer.startsWith("/")) {
153-
pointer = pointer.substring(1);
154+
refs = pointer.substring(1);
154155
} else {
155156
throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
156157
}
157-
refTokens = new ArrayList<String>();
158-
for (String token : pointer.split("/")) {
159-
refTokens.add(unescape(token));
158+
this.refTokens = new ArrayList<String>();
159+
for (String token : refs.split("/")) {
160+
this.refTokens.add(unescape(token));
160161
}
161162
}
162163

@@ -181,11 +182,11 @@ private String unescape(String token) {
181182
* @throws JSONPointerException if an error occurs during evaluation
182183
*/
183184
public Object queryFrom(Object document) {
184-
if (refTokens.isEmpty()) {
185+
if (this.refTokens.isEmpty()) {
185186
return document;
186187
}
187188
Object current = document;
188-
for (String token : refTokens) {
189+
for (String token : this.refTokens) {
189190
if (current instanceof JSONObject) {
190191
current = ((JSONObject) current).opt(unescape(token));
191192
} else if (current instanceof JSONArray) {
@@ -206,6 +207,7 @@ public Object queryFrom(Object document) {
206207
* @return the matched object. If no matching item is found a
207208
* JSONPointerException is thrown
208209
*/
210+
@SuppressWarnings("boxing")
209211
private Object readByIndexToken(Object current, String indexToken) {
210212
try {
211213
int index = Integer.parseInt(indexToken);
@@ -227,7 +229,7 @@ private Object readByIndexToken(Object current, String indexToken) {
227229
@Override
228230
public String toString() {
229231
StringBuilder rval = new StringBuilder("");
230-
for (String token: refTokens) {
232+
for (String token: this.refTokens) {
231233
rval.append('/').append(escape(token));
232234
}
233235
return rval.toString();
@@ -255,7 +257,7 @@ private String escape(String token) {
255257
public String toURIFragment() {
256258
try {
257259
StringBuilder rval = new StringBuilder("#");
258-
for (String token : refTokens) {
260+
for (String token : this.refTokens) {
259261
rval.append('/').append(URLEncoder.encode(token, ENCODING));
260262
}
261263
return rval.toString();

JSONStringer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public JSONStringer() {
7272
* <code>endArray</code>).
7373
* @return The JSON text.
7474
*/
75+
@Override
7576
public String toString() {
7677
return this.mode == 'd' ? this.writer.toString() : null;
7778
}

JSONWriter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,18 @@ public JSONWriter array() throws JSONException {
149149

150150
/**
151151
* End something.
152-
* @param mode Mode
152+
* @param m Mode
153153
* @param c Closing character
154154
* @return this
155155
* @throws JSONException If unbalanced.
156156
*/
157-
private JSONWriter end(char mode, char c) throws JSONException {
158-
if (this.mode != mode) {
159-
throw new JSONException(mode == 'a'
157+
private JSONWriter end(char m, char c) throws JSONException {
158+
if (this.mode != m) {
159+
throw new JSONException(m == 'a'
160160
? "Misplaced endArray."
161161
: "Misplaced endObject.");
162162
}
163-
this.pop(mode);
163+
this.pop(m);
164164
try {
165165
this.writer.append(c);
166166
} catch (IOException e) {

XML.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ public static String unescape(String string) {
211211
sb.append('<');
212212
} else if ("gt".equalsIgnoreCase(entity)) {
213213
sb.append('>');
214-
} else {
214+
} else {// unsupported xml entity. leave encoded
215215
sb.append('&').append(entity).append(';');
216216
}
217217
}
218218
// skip past the entity we just parsed.
219219
i += entity.length() + 1;
220220
} else {
221221
// this shouldn't happen in most cases since the parser
222-
// errors on unclosed enties.
222+
// errors on unclosed entries.
223223
sb.append(c);
224224
}
225225
} else {
@@ -508,7 +508,7 @@ public static String toString(Object object) throws JSONException {
508508
* @return A string.
509509
* @throws JSONException Thrown if there is an error parsing the string
510510
*/
511-
public static String toString(Object object, String tagName)
511+
public static String toString(final Object object, final String tagName)
512512
throws JSONException {
513513
StringBuilder sb = new StringBuilder();
514514
JSONArray ja;
@@ -595,21 +595,19 @@ public static String toString(Object object, String tagName)
595595

596596
}
597597

598-
if (object != null) {
599-
if (object.getClass().isArray()) {
600-
object = new JSONArray(object);
601-
}
602-
603-
if (object instanceof JSONArray) {
598+
if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
599+
if(object.getClass().isArray()) {
600+
ja = new JSONArray(object);
601+
} else {
604602
ja = (JSONArray) object;
605-
for (Object val : ja) {
606-
// XML does not have good support for arrays. If an array
607-
// appears in a place where XML is lacking, synthesize an
608-
// <array> element.
609-
sb.append(toString(val, tagName == null ? "array" : tagName));
610-
}
611-
return sb.toString();
612603
}
604+
for (Object val : ja) {
605+
// XML does not have good support for arrays. If an array
606+
// appears in a place where XML is lacking, synthesize an
607+
// <array> element.
608+
sb.append(toString(val, tagName == null ? "array" : tagName));
609+
}
610+
return sb.toString();
613611
}
614612

615613
string = (object == null) ? "null" : escape(object.toString());

0 commit comments

Comments
 (0)