|
0 |
/**
|
|
1 |
* Copyright (C) 2010 Google Inc.
|
|
2 |
*
|
|
3 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
* you may not use this file except in compliance with the License.
|
|
5 |
* You may obtain a copy of the License at
|
|
6 |
*
|
|
7 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
*
|
|
9 |
* Unless required by applicable law or agreed to in writing, software
|
|
10 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
* See the License for the specific language governing permissions and
|
|
13 |
* limitations under the License.
|
|
14 |
*/
|
|
15 |
|
|
16 |
|
|
17 |
package org.json;
|
|
18 |
|
|
19 |
import junit.framework.TestCase;
|
|
20 |
|
|
21 |
import java.math.BigDecimal;
|
|
22 |
import java.math.BigInteger;
|
|
23 |
import java.util.*;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* This black box test was written without inspecting the non-free org.json sourcecode.
|
|
27 |
*/
|
|
28 |
public class JSONObjectTest extends TestCase {
|
|
29 |
|
|
30 |
public void testEmptyObject() throws JSONException {
|
|
31 |
JSONObject object = new JSONObject();
|
|
32 |
assertEquals(0, object.length());
|
|
33 |
|
|
34 |
// bogus (but documented) behaviour: returns null rather than the empty object
|
|
35 |
assertNull(object.names());
|
|
36 |
|
|
37 |
// bogus behaviour: returns null rather than an empty array
|
|
38 |
assertNull(object.toJSONArray(new JSONArray()));
|
|
39 |
assertEquals("{}", object.toString());
|
|
40 |
assertEquals("{}", object.toString(5));
|
|
41 |
try {
|
|
42 |
object.get("foo");
|
|
43 |
fail();
|
|
44 |
} catch (JSONException e) {
|
|
45 |
}
|
|
46 |
try {
|
|
47 |
object.getBoolean("foo");
|
|
48 |
fail();
|
|
49 |
} catch (JSONException e) {
|
|
50 |
}
|
|
51 |
try {
|
|
52 |
object.getDouble("foo");
|
|
53 |
fail();
|
|
54 |
} catch (JSONException e) {
|
|
55 |
}
|
|
56 |
try {
|
|
57 |
object.getInt("foo");
|
|
58 |
fail();
|
|
59 |
} catch (JSONException e) {
|
|
60 |
}
|
|
61 |
try {
|
|
62 |
object.getJSONArray("foo");
|
|
63 |
fail();
|
|
64 |
} catch (JSONException e) {
|
|
65 |
}
|
|
66 |
try {
|
|
67 |
object.getJSONObject("foo");
|
|
68 |
fail();
|
|
69 |
} catch (JSONException e) {
|
|
70 |
}
|
|
71 |
try {
|
|
72 |
object.getLong("foo");
|
|
73 |
fail();
|
|
74 |
} catch (JSONException e) {
|
|
75 |
}
|
|
76 |
try {
|
|
77 |
object.getString("foo");
|
|
78 |
fail();
|
|
79 |
} catch (JSONException e) {
|
|
80 |
}
|
|
81 |
assertFalse(object.has("foo"));
|
|
82 |
assertTrue(object.isNull("foo")); // isNull also means "is not present"
|
|
83 |
assertNull(object.opt("foo"));
|
|
84 |
assertEquals(false, object.optBoolean("foo"));
|
|
85 |
assertEquals(true, object.optBoolean("foo", true));
|
|
86 |
assertEquals(Double.NaN, object.optDouble("foo"));
|
|
87 |
assertEquals(5.0, object.optDouble("foo", 5.0));
|
|
88 |
assertEquals(0, object.optInt("foo"));
|
|
89 |
assertEquals(5, object.optInt("foo", 5));
|
|
90 |
assertEquals(null, object.optJSONArray("foo"));
|
|
91 |
assertEquals(null, object.optJSONObject("foo"));
|
|
92 |
assertEquals(0, object.optLong("foo"));
|
|
93 |
assertEquals(Long.MAX_VALUE-1, object.optLong("foo", Long.MAX_VALUE-1));
|
|
94 |
assertEquals("", object.optString("foo")); // empty string is default!
|
|
95 |
assertEquals("bar", object.optString("foo", "bar"));
|
|
96 |
assertNull(object.remove("foo"));
|
|
97 |
}
|
|
98 |
|
|
99 |
public void testEqualsAndHashCode() throws JSONException {
|
|
100 |
JSONObject a = new JSONObject();
|
|
101 |
JSONObject b = new JSONObject();
|
|
102 |
|
|
103 |
// JSON object doesn't override either equals or hashCode (!)
|
|
104 |
assertFalse(a.equals(b));
|
|
105 |
assertEquals(a.hashCode(), System.identityHashCode(a));
|
|
106 |
}
|
|
107 |
|
|
108 |
public void testGet() throws JSONException {
|
|
109 |
JSONObject object = new JSONObject();
|
|
110 |
Object value = new Object();
|
|
111 |
object.put("foo", value);
|
|
112 |
object.put("bar", new Object());
|
|
113 |
object.put("baz", new Object());
|
|
114 |
assertSame(value, object.get("foo"));
|
|
115 |
try {
|
|
116 |
object.get("FOO");
|
|
117 |
fail();
|
|
118 |
} catch (JSONException e) {
|
|
119 |
}
|
|
120 |
try {
|
|
121 |
object.put(null, value);
|
|
122 |
fail();
|
|
123 |
} catch (JSONException e) {
|
|
124 |
}
|
|
125 |
try {
|
|
126 |
object.get(null);
|
|
127 |
fail();
|
|
128 |
} catch (JSONException e) {
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
public void testPut() throws JSONException {
|
|
133 |
JSONObject object = new JSONObject();
|
|
134 |
assertSame(object, object.put("foo", true));
|
|
135 |
object.put("foo", false);
|
|
136 |
assertEquals(false, object.get("foo"));
|
|
137 |
|
|
138 |
object.put("foo", 5.0d);
|
|
139 |
assertEquals(5.0d, object.get("foo"));
|
|
140 |
object.put("foo", 0);
|
|
141 |
assertEquals(0, object.get("foo"));
|
|
142 |
object.put("bar", Long.MAX_VALUE - 1);
|
|
143 |
assertEquals(Long.MAX_VALUE - 1, object.get("bar"));
|
|
144 |
object.put("baz", "x");
|
|
145 |
assertEquals("x", object.get("baz"));
|
|
146 |
object.put("bar", JSONObject.NULL);
|
|
147 |
assertSame(JSONObject.NULL, object.get("bar"));
|
|
148 |
}
|
|
149 |
|
|
150 |
public void testPutNullRemoves() throws JSONException {
|
|
151 |
JSONObject object = new JSONObject();
|
|
152 |
object.put("foo", "bar");
|
|
153 |
object.put("foo", null);
|
|
154 |
assertEquals(0, object.length());
|
|
155 |
assertFalse(object.has("foo"));
|
|
156 |
try {
|
|
157 |
object.get("foo");
|
|
158 |
fail();
|
|
159 |
} catch (JSONException e) {
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
public void testPutOpt() throws JSONException {
|
|
164 |
JSONObject object = new JSONObject();
|
|
165 |
object.put("foo", "bar");
|
|
166 |
object.putOpt("foo", null);
|
|
167 |
assertEquals("bar", object.get("foo"));
|
|
168 |
object.putOpt(null, null);
|
|
169 |
assertEquals(1, object.length());
|
|
170 |
object.putOpt(null, "bar");
|
|
171 |
assertEquals(1, object.length());
|
|
172 |
}
|
|
173 |
|
|
174 |
public void testPutOptUnsupportedNumbers() throws JSONException {
|
|
175 |
JSONObject object = new JSONObject();
|
|
176 |
try {
|
|
177 |
object.putOpt("foo", Double.NaN);
|
|
178 |
fail();
|
|
179 |
} catch (JSONException e) {
|
|
180 |
}
|
|
181 |
try {
|
|
182 |
object.putOpt("foo", Double.NEGATIVE_INFINITY);
|
|
183 |
fail();
|
|
184 |
} catch (JSONException e) {
|
|
185 |
}
|
|
186 |
try {
|
|
187 |
object.putOpt("foo", Double.POSITIVE_INFINITY);
|
|
188 |
fail();
|
|
189 |
} catch (JSONException e) {
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
public void testRemove() throws JSONException {
|
|
194 |
JSONObject object = new JSONObject();
|
|
195 |
object.put("foo", "bar");
|
|
196 |
assertEquals(null, object.remove(null));
|
|
197 |
assertEquals(null, object.remove(""));
|
|
198 |
assertEquals(null, object.remove("bar"));
|
|
199 |
assertEquals("bar", object.remove("foo"));
|
|
200 |
assertEquals(null, object.remove("foo"));
|
|
201 |
}
|
|
202 |
|
|
203 |
public void testBooleans() throws JSONException {
|
|
204 |
JSONObject object = new JSONObject();
|
|
205 |
object.put("foo", true);
|
|
206 |
object.put("bar", false);
|
|
207 |
object.put("baz", "true");
|
|
208 |
object.put("quux", "false");
|
|
209 |
assertEquals(4, object.length());
|
|
210 |
assertEquals(true, object.getBoolean("foo"));
|
|
211 |
assertEquals(false, object.getBoolean("bar"));
|
|
212 |
assertEquals(true, object.getBoolean("baz"));
|
|
213 |
assertEquals(false, object.getBoolean("quux"));
|
|
214 |
assertFalse(object.isNull("foo"));
|
|
215 |
assertFalse(object.isNull("quux"));
|
|
216 |
assertTrue(object.has("foo"));
|
|
217 |
assertTrue(object.has("quux"));
|
|
218 |
assertFalse(object.has("missing"));
|
|
219 |
assertEquals(true, object.optBoolean("foo"));
|
|
220 |
assertEquals(false, object.optBoolean("bar"));
|
|
221 |
assertEquals(true, object.optBoolean("baz"));
|
|
222 |
assertEquals(false, object.optBoolean("quux"));
|
|
223 |
assertEquals(false, object.optBoolean("missing"));
|
|
224 |
assertEquals(true, object.optBoolean("foo", true));
|
|
225 |
assertEquals(false, object.optBoolean("bar", true));
|
|
226 |
assertEquals(true, object.optBoolean("baz", true));
|
|
227 |
assertEquals(false, object.optBoolean("quux", true));
|
|
228 |
assertEquals(true, object.optBoolean("missing", true));
|
|
229 |
|
|
230 |
object.put("foo", "truE");
|
|
231 |
object.put("bar", "FALSE");
|
|
232 |
assertEquals(true, object.getBoolean("foo"));
|
|
233 |
assertEquals(false, object.getBoolean("bar"));
|
|
234 |
assertEquals(true, object.optBoolean("foo"));
|
|
235 |
assertEquals(false, object.optBoolean("bar"));
|
|
236 |
assertEquals(true, object.optBoolean("foo", false));
|
|
237 |
assertEquals(false, object.optBoolean("bar", false));
|
|
238 |
}
|
|
239 |
|
|
240 |
public void testNumbers() throws JSONException {
|
|
241 |
JSONObject object = new JSONObject();
|
|
242 |
object.put("foo", Double.MIN_VALUE);
|
|
243 |
object.put("bar", 9223372036854775806L);
|
|
244 |
object.put("baz", Double.MAX_VALUE);
|
|
245 |
object.put("quux", -0d);
|
|
246 |
assertEquals(4, object.length());
|
|
247 |
|
|
248 |
assertTrue(object.toString().contains("\"foo\":4.9E-324"));
|
|
249 |
assertTrue(object.toString().contains("\"bar\":9223372036854775806"));
|
|
250 |
assertTrue(object.toString().contains("\"baz\":1.7976931348623157E308"));
|
|
251 |
|
|
252 |
// bogus behaviour: toString() and getString() return different values for -0d
|
|
253 |
assertTrue(object.toString().contains("\"quux\":-0}") // no trailing decimal point
|
|
254 |
|| object.toString().contains("\"quux\":-0,"));
|
|
255 |
|
|
256 |
assertEquals(Double.MIN_VALUE, object.get("foo"));
|
|
257 |
assertEquals(9223372036854775806L, object.get("bar"));
|
|
258 |
assertEquals(Double.MAX_VALUE, object.get("baz"));
|
|
259 |
assertEquals(-0d, object.get("quux"));
|
|
260 |
assertEquals(Double.MIN_VALUE, object.getDouble("foo"));
|
|
261 |
assertEquals(9.223372036854776E18, object.getDouble("bar"));
|
|
262 |
assertEquals(Double.MAX_VALUE, object.getDouble("baz"));
|
|
263 |
assertEquals(-0d, object.getDouble("quux"));
|
|
264 |
assertEquals(0, object.getLong("foo"));
|
|
265 |
assertEquals(9223372036854775806L, object.getLong("bar"));
|
|
266 |
assertEquals(Long.MAX_VALUE, object.getLong("baz"));
|
|
267 |
assertEquals(0, object.getLong("quux"));
|
|
268 |
assertEquals(0, object.getInt("foo"));
|
|
269 |
assertEquals(-2, object.getInt("bar"));
|
|
270 |
assertEquals(Integer.MAX_VALUE, object.getInt("baz"));
|
|
271 |
assertEquals(0, object.getInt("quux"));
|
|
272 |
assertEquals(Double.MIN_VALUE, object.opt("foo"));
|
|
273 |
assertEquals(9223372036854775806L, object.optLong("bar"));
|
|
274 |
assertEquals(Double.MAX_VALUE, object.optDouble("baz"));
|
|
275 |
assertEquals(0, object.optInt("quux"));
|
|
276 |
assertEquals(Double.MIN_VALUE, object.opt("foo"));
|
|
277 |
assertEquals(9223372036854775806L, object.optLong("bar"));
|
|
278 |
assertEquals(Double.MAX_VALUE, object.optDouble("baz"));
|
|
279 |
assertEquals(0, object.optInt("quux"));
|
|
280 |
assertEquals(Double.MIN_VALUE, object.optDouble("foo", 5.0d));
|
|
281 |
assertEquals(9223372036854775806L, object.optLong("bar", 1L));
|
|
282 |
assertEquals(Long.MAX_VALUE, object.optLong("baz", 1L));
|
|
283 |
assertEquals(0, object.optInt("quux", -1));
|
|
284 |
assertEquals("4.9E-324", object.getString("foo"));
|
|
285 |
assertEquals("9223372036854775806", object.getString("bar"));
|
|
286 |
assertEquals("1.7976931348623157E308", object.getString("baz"));
|
|
287 |
assertEquals("-0.0", object.getString("quux"));
|
|
288 |
}
|
|
289 |
|
|
290 |
public void testFloats() throws JSONException {
|
|
291 |
JSONObject object = new JSONObject();
|
|
292 |
try {
|
|
293 |
object.put("foo", (Float) Float.NaN);
|
|
294 |
fail();
|
|
295 |
} catch (JSONException e) {
|
|
296 |
}
|
|
297 |
try {
|
|
298 |
object.put("foo", (Float) Float.NEGATIVE_INFINITY);
|
|
299 |
fail();
|
|
300 |
} catch (JSONException e) {
|
|
301 |
}
|
|
302 |
try {
|
|
303 |
object.put("foo", (Float) Float.POSITIVE_INFINITY);
|
|
304 |
fail();
|
|
305 |
} catch (JSONException e) {
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
public void testOtherNumbers() throws JSONException {
|
|
310 |
Number nan = new Number() {
|
|
311 |
public int intValue() {
|
|
312 |
return 0;
|
|
313 |
}
|
|
314 |
public long longValue() {
|
|
315 |
return 1L;
|
|
316 |
}
|
|
317 |
public float floatValue() {
|
|
318 |
return 2;
|
|
319 |
}
|
|
320 |
public double doubleValue() {
|
|
321 |
return Double.NaN;
|
|
322 |
}
|
|
323 |
@Override public String toString() {
|
|
324 |
return "x";
|
|
325 |
}
|
|
326 |
};
|
|
327 |
|
|
328 |
// bogus behaviour: foreign object types should be rejected!
|
|
329 |
JSONObject object = new JSONObject();
|
|
330 |
object.put("foo", nan);
|
|
331 |
assertEquals("{\"foo\":x}", object.toString());
|
|
332 |
}
|
|
333 |
|
|
334 |
public void testForeignObjects() throws JSONException {
|
|
335 |
Object foreign = new Object() {
|
|
336 |
@Override public String toString() {
|
|
337 |
return "x";
|
|
338 |
}
|
|
339 |
};
|
|
340 |
|
|
341 |
// bogus behaviour: foreign object types should be rejected and not treated as Strings
|
|
342 |
JSONObject object = new JSONObject();
|
|
343 |
object.put("foo", foreign);
|
|
344 |
assertEquals("{\"foo\":\"x\"}", object.toString());
|
|
345 |
}
|
|
346 |
|
|
347 |
public void testNullKeys() {
|
|
348 |
try {
|
|
349 |
new JSONObject().put(null, false);
|
|
350 |
fail();
|
|
351 |
} catch (JSONException e) {
|
|
352 |
}
|
|
353 |
try {
|
|
354 |
new JSONObject().put(null, 0.0d);
|
|
355 |
fail();
|
|
356 |
} catch (JSONException e) {
|
|
357 |
}
|
|
358 |
try {
|
|
359 |
new JSONObject().put(null, 5);
|
|
360 |
fail();
|
|
361 |
} catch (JSONException e) {
|
|
362 |
}
|
|
363 |
try {
|
|
364 |
new JSONObject().put(null, 5L);
|
|
365 |
fail();
|
|
366 |
} catch (JSONException e) {
|
|
367 |
}
|
|
368 |
try {
|
|
369 |
new JSONObject().put(null, "foo");
|
|
370 |
fail();
|
|
371 |
} catch (JSONException e) {
|
|
372 |
}
|
|
373 |
}
|
|
374 |
|
|
375 |
public void testStrings() throws JSONException {
|
|
376 |
JSONObject object = new JSONObject();
|
|
377 |
object.put("foo", "true");
|
|
378 |
object.put("bar", "5.5");
|
|
379 |
object.put("baz", "9223372036854775806");
|
|
380 |
object.put("quux", "null");
|
|
381 |
object.put("height", "5\"8' tall");
|
|
382 |
|
|
383 |
assertTrue(object.toString().contains("\"foo\":\"true\""));
|
|
384 |
assertTrue(object.toString().contains("\"bar\":\"5.5\""));
|
|
385 |
assertTrue(object.toString().contains("\"baz\":\"9223372036854775806\""));
|
|
386 |
assertTrue(object.toString().contains("\"quux\":\"null\""));
|
|
387 |
assertTrue(object.toString().contains("\"height\":\"5\\\"8' tall\""));
|
|
388 |
|
|
389 |
assertEquals("true", object.get("foo"));
|
|
390 |
assertEquals("null", object.getString("quux"));
|
|
391 |
assertEquals("5\"8' tall", object.getString("height"));
|
|
392 |
assertEquals("true", object.opt("foo"));
|
|
393 |
assertEquals("5.5", object.optString("bar"));
|
|
394 |
assertEquals("true", object.optString("foo", "x"));
|
|
395 |
assertFalse(object.isNull("foo"));
|
|
396 |
|
|
397 |
assertEquals(true, object.getBoolean("foo"));
|
|
398 |
assertEquals(true, object.optBoolean("foo"));
|
|
399 |
assertEquals(true, object.optBoolean("foo", false));
|
|
400 |
assertEquals(0, object.optInt("foo"));
|
|
401 |
assertEquals(-2, object.optInt("foo", -2));
|
|
402 |
|
|
403 |
assertEquals(5.5d, object.getDouble("bar"));
|
|
404 |
assertEquals(5L, object.getLong("bar"));
|
|
405 |
assertEquals(5, object.getInt("bar"));
|
|
406 |
assertEquals(5, object.optInt("bar", 3));
|
|
407 |
|
|
408 |
// The last digit of the string is a 6 but getLong returns a 7. It's probably parsing as a
|
|
409 |
// double and then converting that to a long. This is consistent with JavaScript.
|
|
410 |
assertEquals(9223372036854775807L, object.getLong("baz"));
|
|
411 |
assertEquals(9.223372036854776E18, object.getDouble("baz"));
|
|
412 |
assertEquals(Integer.MAX_VALUE, object.getInt("baz"));
|
|
413 |
|
|
414 |
assertFalse(object.isNull("quux"));
|
|
415 |
try {
|
|
416 |
object.getDouble("quux");
|
|
417 |
fail();
|
|
418 |
} catch (JSONException e) {
|
|
419 |
}
|
|
420 |
assertEquals(Double.NaN, object.optDouble("quux"));
|
|
421 |
assertEquals(-1.0d, object.optDouble("quux", -1.0d));
|
|
422 |
|
|
423 |
object.put("foo", "TRUE");
|
|
424 |
assertEquals(true, object.getBoolean("foo"));
|
|
425 |
}
|
|
426 |
|
|
427 |
public void testJSONObjects() throws JSONException {
|
|
428 |
JSONObject object = new JSONObject();
|
|
429 |
|
|
430 |
JSONArray a = new JSONArray();
|
|
431 |
JSONObject b = new JSONObject();
|
|
432 |
object.put("foo", a);
|
|
433 |
object.put("bar", b);
|
|
434 |
|
|
435 |
assertSame(a, object.getJSONArray("foo"));
|
|
436 |
assertSame(b, object.getJSONObject("bar"));
|
|
437 |
try {
|
|
438 |
object.getJSONObject("foo");
|
|
439 |
fail();
|
|
440 |
} catch (JSONException e) {
|
|
441 |
}
|
|
442 |
try {
|
|
443 |
object.getJSONArray("bar");
|
|
444 |
fail();
|
|
445 |
} catch (JSONException e) {
|
|
446 |
}
|
|
447 |
assertEquals(a, object.optJSONArray("foo"));
|
|
448 |
assertEquals(b, object.optJSONObject("bar"));
|
|
449 |
assertEquals(null, object.optJSONArray("bar"));
|
|
450 |
assertEquals(null, object.optJSONObject("foo"));
|
|
451 |
}
|
|
452 |
|
|
453 |
public void testToJSONArray() throws JSONException {
|
|
454 |
JSONObject object = new JSONObject();
|
|
455 |
Object value = new Object();
|
|
456 |
object.put("foo", true);
|
|
457 |
object.put("bar", 5.0d);
|
|
458 |
object.put("baz", -0.0d);
|
|
459 |
object.put("quux", value);
|
|
460 |
|
|
461 |
JSONArray names = new JSONArray();
|
|
462 |
names.put("baz");
|
|
463 |
names.put("quux");
|
|
464 |
names.put("foo");
|
|
465 |
|
|
466 |
JSONArray array = object.toJSONArray(names);
|
|
467 |
assertEquals(-0.0d, array.get(0));
|
|
468 |
assertEquals(value, array.get(1));
|
|
469 |
assertEquals(true, array.get(2));
|
|
470 |
|
|
471 |
object.put("foo", false);
|
|
472 |
assertEquals(true, array.get(2));
|
|
473 |
}
|
|
474 |
|
|
475 |
public void testToJSONArrayMissingNames() throws JSONException {
|
|
476 |
JSONObject object = new JSONObject();
|
|
477 |
object.put("foo", true);
|
|
478 |
object.put("bar", 5.0d);
|
|
479 |
object.put("baz", JSONObject.NULL);
|
|
480 |
|
|
481 |
JSONArray names = new JSONArray();
|
|
482 |
names.put("bar");
|
|
483 |
names.put("foo");
|
|
484 |
names.put("quux");
|
|
485 |
names.put("baz");
|
|
486 |
|
|
487 |
JSONArray array = object.toJSONArray(names);
|
|
488 |
assertEquals(4, array.length());
|
|
489 |
|
|
490 |
assertEquals(5.0d, array.get(0));
|
|
491 |
assertEquals(true, array.get(1));
|
|
492 |
try {
|
|
493 |
array.get(2);
|
|
494 |
fail();
|
|
495 |
} catch (JSONException e) {
|
|
496 |
}
|
|
497 |
assertEquals(JSONObject.NULL, array.get(3));
|
|
498 |
}
|
|
499 |
|
|
500 |
public void testToJSONArrayNull() throws JSONException {
|
|
501 |
JSONObject object = new JSONObject();
|
|
502 |
assertEquals(null, object.toJSONArray(null));
|
|
503 |
object.put("foo", 5);
|
|
504 |
try {
|
|
505 |
object.toJSONArray(null);
|
|
506 |
} catch (JSONException e) {
|
|
507 |
}
|
|
508 |
}
|
|
509 |
|
|
510 |
public void testToJSONArrayEndsUpEmpty() throws JSONException {
|
|
511 |
JSONObject object = new JSONObject();
|
|
512 |
object.put("foo", 5);
|
|
513 |
JSONArray array = new JSONArray();
|
|
514 |
array.put("bar");
|
|
515 |
assertEquals(1, object.toJSONArray(array).length());
|
|
516 |
}
|
|
517 |
|
|
518 |
public void testToJSONArrayNonString() throws JSONException {
|
|
519 |
JSONObject object = new JSONObject();
|
|
520 |
object.put("foo", 5);
|
|
521 |
object.put("null", 10);
|
|
522 |
object.put("false", 15);
|
|
523 |
|
|
524 |
JSONArray names = new JSONArray();
|
|
525 |
names.put(JSONObject.NULL);
|
|
526 |
names.put(false);
|
|
527 |
names.put("foo");
|
|
528 |
|
|
529 |
// bogus behaviour: array elements are converted to Strings
|
|
530 |
JSONArray array = object.toJSONArray(names);
|
|
531 |
assertEquals(3, array.length());
|
|
532 |
assertEquals(10, array.get(0));
|
|
533 |
assertEquals(15, array.get(1));
|
|
534 |
assertEquals(5, array.get(2));
|
|
535 |
}
|
|
536 |
|
|
537 |
public void testPutUnsupportedNumbers() throws JSONException {
|
|
538 |
JSONObject object = new JSONObject();
|
|
539 |
try {
|
|
540 |
object.put("foo", Double.NaN);
|
|
541 |
fail();
|
|
542 |
} catch (JSONException e) {
|
|
543 |
}
|
|
544 |
try {
|
|
545 |
object.put("foo", Double.NEGATIVE_INFINITY);
|
|
546 |
fail();
|
|
547 |
} catch (JSONException e) {
|
|
548 |
}
|
|
549 |
try {
|
|
550 |
object.put("foo", Double.POSITIVE_INFINITY);
|
|
551 |
fail();
|
|
552 |
} catch (JSONException e) {
|
|
553 |
}
|
|
554 |
}
|
|
555 |
|
|
556 |
public void testPutUnsupportedNumbersAsObjects() throws JSONException {
|
|
557 |
JSONObject object = new JSONObject();
|
|
558 |
try {
|
|
559 |
object.put("foo", (Double) Double.NaN);
|
|
560 |
fail();
|
|
561 |
} catch (JSONException e) {
|
|
562 |
}
|
|
563 |
try {
|
|
564 |
object.put("foo", (Double) Double.NEGATIVE_INFINITY);
|
|
565 |
fail();
|
|
566 |
} catch (JSONException e) {
|
|
567 |
}
|
|
568 |
try {
|
|
569 |
object.put("foo", (Double) Double.POSITIVE_INFINITY);
|
|
570 |
fail();
|
|
571 |
} catch (JSONException e) {
|
|
572 |
}
|
|
573 |
}
|
|
574 |
|
|
575 |
/**
|
|
576 |
* Although JSONObject is usually defensive about which numbers it accepts,
|
|
577 |
* it doesn't check inputs in its constructor.
|
|
578 |
*/
|
|
579 |
public void testCreateWithUnsupportedNumbers() throws JSONException {
|
|
580 |
Map<String, Object> contents = new HashMap<String, Object>();
|
|
581 |
contents.put("foo", Double.NaN);
|
|
582 |
contents.put("bar", Double.NEGATIVE_INFINITY);
|
|
583 |
contents.put("baz", Double.POSITIVE_INFINITY);
|
|
584 |
|
|
585 |
JSONObject object = new JSONObject(contents);
|
|
586 |
assertEquals(Double.NaN, object.get("foo"));
|
|
587 |
assertEquals(Double.NEGATIVE_INFINITY, object.get("bar"));
|
|
588 |
assertEquals(Double.POSITIVE_INFINITY, object.get("baz"));
|
|
589 |
}
|
|
590 |
|
|
591 |
public void testToStringWithUnsupportedNumbers() {
|
|
592 |
// bogus behaviour: when the object contains an unsupported number, toString returns null
|
|
593 |
JSONObject object = new JSONObject(Collections.singletonMap("foo", Double.NaN));
|
|
594 |
assertEquals(null, object.toString());
|
|
595 |
}
|
|
596 |
|
|
597 |
public void testMapConstructorCopiesContents() throws JSONException {
|
|
598 |
Map<String, Object> contents = new HashMap<String, Object>();
|
|
599 |
contents.put("foo", 5);
|
|
600 |
JSONObject object = new JSONObject(contents);
|
|
601 |
contents.put("foo", 10);
|
|
602 |
assertEquals(5, object.get("foo"));
|
|
603 |
}
|
|
604 |
|
|
605 |
public void testMapConstructorWithBogusEntries() {
|
|
606 |
Map<Object, Object> contents = new HashMap<Object, Object>();
|
|
607 |
contents.put(5, 5);
|
|
608 |
|
|
609 |
// bogus behaviour: the constructor doesn't validate its input
|
|
610 |
new JSONObject(contents);
|
|
611 |
}
|
|
612 |
|
|
613 |
public void testAccumulateMutatesInPlace() throws JSONException {
|
|
614 |
JSONObject object = new JSONObject();
|
|
615 |
object.put("foo", 5);
|
|
616 |
object.accumulate("foo", 6);
|
|
617 |
JSONArray array = object.getJSONArray("foo");
|
|
618 |
assertEquals("[5,6]", array.toString());
|
|
619 |
object.accumulate("foo", 7);
|
|
620 |
assertEquals("[5,6,7]", array.toString());
|
|
621 |
}
|
|
622 |
|
|
623 |
public void testAccumulateExistingArray() throws JSONException {
|
|
624 |
JSONArray array = new JSONArray();
|
|
625 |
JSONObject object = new JSONObject();
|
|
626 |
object.put("foo", array);
|
|
627 |
object.accumulate("foo", 5);
|
|
628 |
assertEquals("[5]", array.toString());
|
|
629 |
}
|
|
630 |
|
|
631 |
public void testAccumulatePutArray() throws JSONException {
|
|
632 |
JSONObject object = new JSONObject();
|
|
633 |
object.accumulate("foo", 5);
|
|
634 |
assertEquals("{\"foo\":5}", object.toString());
|
|
635 |
object.accumulate("foo", new JSONArray());
|
|
636 |
assertEquals("{\"foo\":[5,[]]}", object.toString());
|
|
637 |
}
|
|
638 |
|
|
639 |
public void testAccumulateNull() {
|
|
640 |
JSONObject object = new JSONObject();
|
|
641 |
try {
|
|
642 |
object.accumulate(null, 5);
|
|
643 |
fail();
|
|
644 |
} catch (JSONException e) {
|
|
645 |
}
|
|
646 |
}
|
|
647 |
|
|
648 |
public void testEmptyStringKey() throws JSONException {
|
|
649 |
JSONObject object = new JSONObject();
|
|
650 |
object.put("", 5);
|
|
651 |
assertEquals(5, object.get(""));
|
|
652 |
assertEquals("{\"\":5}", object.toString());
|
|
653 |
}
|
|
654 |
|
|
655 |
public void testNullValue() throws JSONException {
|
|
656 |
JSONObject object = new JSONObject();
|
|
657 |
object.put("foo", JSONObject.NULL);
|
|
658 |
object.put("bar", null);
|
|
659 |
|
|
660 |
// bogus behaviour: there's 2 ways to represent null; each behaves differently!
|
|
661 |
assertTrue(object.has("foo"));
|
|
662 |
assertFalse(object.has("bar"));
|
|
663 |
assertTrue(object.isNull("foo"));
|
|
664 |
assertTrue(object.isNull("bar"));
|
|
665 |
}
|
|
666 |
|
|
667 |
public void testHas() throws JSONException {
|
|
668 |
JSONObject object = new JSONObject();
|
|
669 |
object.put("foo", 5);
|
|
670 |
assertTrue(object.has("foo"));
|
|
671 |
assertFalse(object.has("bar"));
|
|
672 |
assertFalse(object.has(null));
|
|
673 |
}
|
|
674 |
|
|
675 |
public void testOptNull() throws JSONException {
|
|
676 |
JSONObject object = new JSONObject();
|
|
677 |
object.put("foo", "bar");
|
|
678 |
assertEquals(null, object.opt(null));
|
|
679 |
assertEquals(false, object.optBoolean(null));
|
|
680 |
assertEquals(Double.NaN, object.optDouble(null));
|
|
681 |
assertEquals(0, object.optInt(null));
|
|
682 |
assertEquals(0L, object.optLong(null));
|
|
683 |
assertEquals(null, object.optJSONArray(null));
|
|
684 |
assertEquals(null, object.optJSONObject(null));
|
|
685 |
assertEquals("", object.optString(null));
|
|
686 |
assertEquals(true, object.optBoolean(null, true));
|
|
687 |
assertEquals(0.0d, object.optDouble(null, 0.0d));
|
|
688 |
assertEquals(1, object.optInt(null, 1));
|
|
689 |
assertEquals(1L, object.optLong(null, 1L));
|
|
690 |
assertEquals("baz", object.optString(null, "baz"));
|
|
691 |
}
|
|
692 |
|
|
693 |
public void testToStringWithIndentFactor() throws JSONException {
|
|
694 |
JSONObject object = new JSONObject();
|
|
695 |
object.put("foo", new JSONArray(Arrays.asList(5, 6)));
|
|
696 |
object.put("bar", new JSONObject());
|
|
697 |
String foobar = "{\n" +
|
|
698 |
" \"foo\": [\n" +
|
|
699 |
" 5,\n" +
|
|
700 |
" 6\n" +
|
|
701 |
" ],\n" +
|
|
702 |
" \"bar\": {}\n" +
|
|
703 |
"}";
|
|
704 |
String barfoo = "{\n" +
|
|
705 |
" \"bar\": {},\n" +
|
|
706 |
" \"foo\": [\n" +
|
|
707 |
" 5,\n" +
|
|
708 |
" 6\n" +
|
|
709 |
" ]\n" +
|
|
710 |
"}";
|
|
711 |
String string = object.toString(5);
|
|
712 |
assertTrue(string, foobar.equals(string) || barfoo.equals(string));
|
|
713 |
}
|
|
714 |
|
|
715 |
public void testNames() throws JSONException {
|
|
716 |
JSONObject object = new JSONObject();
|
|
717 |
object.put("foo", 5);
|
|
718 |
object.put("bar", 6);
|
|
719 |
object.put("baz", 7);
|
|
720 |
JSONArray array = object.names();
|
|
721 |
assertTrue(array.toString().contains("foo"));
|
|
722 |
assertTrue(array.toString().contains("bar"));
|
|
723 |
assertTrue(array.toString().contains("baz"));
|
|
724 |
}
|
|
725 |
|
|
726 |
public void testKeysEmptyObject() {
|
|
727 |
JSONObject object = new JSONObject();
|
|
728 |
assertFalse(object.keys().hasNext());
|
|
729 |
try {
|
|
730 |
object.keys().next();
|
|
731 |
fail();
|
|
732 |
} catch (NoSuchElementException e) {
|
|
733 |
}
|
|
734 |
}
|
|
735 |
|
|
736 |
public void testKeys() throws JSONException {
|
|
737 |
JSONObject object = new JSONObject();
|
|
738 |
object.put("foo", 5);
|
|
739 |
object.put("bar", 6);
|
|
740 |
object.put("foo", 7);
|
|
741 |
|
|
742 |
@SuppressWarnings("unchecked")
|
|
743 |
Iterator<String> keys = (Iterator<String>) object.keys();
|
|
744 |
Set<String> result = new HashSet<String>();
|
|
745 |
assertTrue(keys.hasNext());
|
|
746 |
result.add(keys.next());
|
|
747 |
assertTrue(keys.hasNext());
|
|
748 |
result.add(keys.next());
|
|
749 |
assertFalse(keys.hasNext());
|
|
750 |
assertEquals(new HashSet<String>(Arrays.asList("foo", "bar")), result);
|
|
751 |
|
|
752 |
try {
|
|
753 |
keys.next();
|
|
754 |
fail();
|
|
755 |
} catch (NoSuchElementException e) {
|
|
756 |
}
|
|
757 |
}
|
|
758 |
|
|
759 |
public void testMutatingKeysMutatesObject() throws JSONException {
|
|
760 |
JSONObject object = new JSONObject();
|
|
761 |
object.put("foo", 5);
|
|
762 |
Iterator keys = object.keys();
|
|
763 |
keys.next();
|
|
764 |
keys.remove();
|
|
765 |
assertEquals(0, object.length());
|
|
766 |
}
|
|
767 |
|
|
768 |
public void testQuote() {
|
|
769 |
// covered by JSONStringerTest.testEscaping()
|
|
770 |
}
|
|
771 |
|
|
772 |
public void testNumberToString() throws JSONException {
|
|
773 |
assertEquals("5", JSONObject.numberToString(5));
|
|
774 |
assertEquals("-0", JSONObject.numberToString(-0.0d));
|
|
775 |
assertEquals("9223372036854775806", JSONObject.numberToString(9223372036854775806L));
|
|
776 |
assertEquals("4.9E-324", JSONObject.numberToString(Double.MIN_VALUE));
|
|
777 |
assertEquals("1.7976931348623157E308", JSONObject.numberToString(Double.MAX_VALUE));
|
|
778 |
try {
|
|
779 |
JSONObject.numberToString(Double.NaN);
|
|
780 |
fail();
|
|
781 |
} catch (JSONException e) {
|
|
782 |
}
|
|
783 |
try {
|
|
784 |
JSONObject.numberToString(Double.NEGATIVE_INFINITY);
|
|
785 |
fail();
|
|
786 |
} catch (JSONException e) {
|
|
787 |
}
|
|
788 |
try {
|
|
789 |
JSONObject.numberToString(Double.POSITIVE_INFINITY);
|
|
790 |
fail();
|
|
791 |
} catch (JSONException e) {
|
|
792 |
}
|
|
793 |
assertEquals("0.001", JSONObject.numberToString(new BigDecimal("0.001")));
|
|
794 |
assertEquals("9223372036854775806",
|
|
795 |
JSONObject.numberToString(new BigInteger("9223372036854775806")));
|
|
796 |
try {
|
|
797 |
JSONObject.numberToString(null);
|
|
798 |
fail();
|
|
799 |
} catch (JSONException e) {
|
|
800 |
}
|
|
801 |
}
|
|
802 |
}
|