forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestJSONObject.java
More file actions
2507 lines (2337 loc) · 76.1 KB
/
TestJSONObject.java
File metadata and controls
2507 lines (2337 loc) · 76.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: TestJSONObject.java Author: JSON.org
*/
package org.json.tests;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import org.json.XML;
import junit.framework.TestCase;
/**
* The Class TestJSONObject.
*/
public class TestJSONObject extends TestCase
{
/**
* The Class GoodJsonString.
*/
public class GoodJsonString implements JSONString
{
/*
* (non-Javadoc)
*
* @see org.json.JSONString#toJSONString()
*/
@Override
public String toJSONString()
{
return "jsonstring";
}
}
/**
* The Class NullJsonString.
*/
public class NullJsonString implements JSONString
{
/*
* (non-Javadoc)
*
* @see org.json.JSONString#toJSONString()
*/
@Override
public String toJSONString()
{
return null;
}
}
/**
* The Class BadJsonString.
*/
public class BadJsonString implements JSONString
{
/*
* (non-Javadoc)
*
* @see org.json.JSONString#toJSONString()
*/
@Override
public String toJSONString()
{
String[] arString = new String[]
{
"abc"
};
return arString[1];
}
}
/**
* The Class ObjectWithPrimatives.
*/
public class ObjectWithPrimatives
{
/** The i. */
public int i;
/** The null string. */
private String nullString;
/** The j. */
private String j;
/** The k. */
private double k;
/** The l. */
private long l;
/** The m. */
public boolean m;
/**
* Instantiates a new object with primatives.
*/
public ObjectWithPrimatives()
{
i = 3;
j = "3";
k = 10.03;
l = 5748548957230984584L;
m = true;
nullString = null;
}
/**
* Gets the i.
*
* @return the i
*/
public int getI()
{
return i;
}
/**
* Gets the j.
*
* @return the j
*/
public String getJ()
{
return j;
}
/**
* Gets the k.
*
* @return the k
*/
public double getK()
{
return k;
}
/**
* Gets the l.
*
* @return the l
*/
public long getL()
{
return l;
}
/**
* Gets the m.
*
* @return the m
*/
public boolean getM()
{
return m;
}
/**
* Gets the m.
*
* @param test
* the test
* @return the m
*/
public boolean getM(Boolean test)
{
return m;
}
/**
* Gets the null string.
*
* @return the null string
*/
public String getNullString()
{
return nullString;
}
/**
* Gets the zERO.
*
* @return the zERO
*/
public int getZERO()
{
return 0;
}
/**
* Gets the one.
*
* @return the one
*/
public int getone()
{
return 1;
}
/**
* Checks if is big.
*
* @return true, if is big
*/
public boolean isBig()
{
return false;
}
/**
* Checks if is small.
*
* @return true, if is small
*/
@SuppressWarnings("unused")
private boolean isSmall()
{
return true;
}
}
/**
* The Class ObjectWithPrimativesExtension.
*/
public class ObjectWithPrimativesExtension extends ObjectWithPrimatives
{
// Same Object
}
/** The jsonobject. */
JSONObject jsonobject = new JSONObject();
/** The iterator. */
Iterator<String> iterator;
/** The jsonarray. */
JSONArray jsonarray;
/** The object. */
Object object;
/** The string. */
String string;
/** The eps. */
double eps = 2.220446049250313e-16;
/**
* Tests the null method.
*
* @throws Exception
* the exception
*/
public void testNull() throws Exception
{
jsonobject = new JSONObject("{\"message\":\"null\"}");
assertFalse(jsonobject.isNull("message"));
assertEquals("null", jsonobject.getString("message"));
jsonobject = new JSONObject("{\"message\":null}");
assertTrue(jsonobject.isNull("message"));
}
/**
* Tests the constructor method using duplicate key.
*/
public void testConstructor_DuplicateKey()
{
try
{
string = "{\"koda\": true, \"koda\": true}";
jsonobject = new JSONObject(string);
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("Duplicate key \"koda\"", jsone.getMessage());
}
}
/**
* Tests the constructor method using null key.
*/
public void testConstructor_NullKey()
{
try
{
jsonobject.put(null, "howard");
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("Null key.", jsone.getMessage());
}
}
/**
* Tests the getDouble method using invalid key howard.
*/
public void testGetDouble_InvalidKeyHoward()
{
try
{
jsonobject.getDouble("howard");
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("JSONObject[\"howard\"] not found.",
jsone.getMessage());
}
}
/**
* Tests the getDouble method using invalid key stooge.
*/
public void testGetDouble_InvalidKeyStooge()
{
jsonobject = new JSONObject();
try
{
jsonobject.getDouble("stooge");
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("JSONObject[\"stooge\"] not found.",
jsone.getMessage());
}
}
/**
* Tests the isNull method.
*/
public void testIsNull()
{
try
{
jsonobject = new JSONObject();
object = null;
jsonobject.put("booga", object);
jsonobject.put("wooga", JSONObject.NULL);
assertEquals("{\"wooga\":null}", jsonobject.toString());
assertTrue(jsonobject.isNull("booga"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the increment method.
*/
public void testIncrement_NewKey()
{
try
{
jsonobject = new JSONObject();
jsonobject.increment("two");
jsonobject.increment("two");
assertEquals("{\"two\":2}", jsonobject.toString());
assertEquals(2, jsonobject.getInt("two"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using listof lists.
*/
public void testToString_ListofLists()
{
try
{
string = "{ \"list of lists\" : [ [1, 2, 3], [4, 5, 6], ] }";
jsonobject = new JSONObject(string);
assertEquals("{\"list of lists\": [\n" + " [\n" + " 1,\n"
+ " 2,\n" + " 3\n" + " ],\n" + " [\n"
+ " 4,\n" + " 5,\n" + " 6\n"
+ " ]\n" + "]}", jsonobject.toString(4));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using indentation.
*/
public void testToString_Indentation()
{
try
{
string = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
jsonobject = new JSONObject(string);
assertEquals(
"{\"entity\": {\n \"id\": 12336,\n \"averageRating\": null,\n \"ratingCount\": null,\n \"name\": \"IXXXXXXXXXXXXX\",\n \"imageURL\": \"\"\n}}",
jsonobject.toString(2));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the multipleThings method.
*/
public void testMultipleThings()
{
try
{
jsonobject = new JSONObject(
"{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001,"
+ " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], "
+ " to : null, op : 'Good',"
+ "ten:10} postfix comment");
jsonobject.put("String", "98.6");
jsonobject.put("JSONObject", new JSONObject());
jsonobject.put("JSONArray", new JSONArray());
jsonobject.put("int", 57);
jsonobject.put("double", 123456789012345678901234567890.);
jsonobject.put("true", true);
jsonobject.put("false", false);
jsonobject.put("null", JSONObject.NULL);
jsonobject.put("bool", "true");
jsonobject.put("zero", -0.0);
jsonobject.put("\\u2028", "\u2028");
jsonobject.put("\\u2029", "\u2029");
jsonarray = jsonobject.getJSONArray("foo");
jsonarray.put(666);
jsonarray.put(2001.99);
jsonarray.put("so \"fine\".");
jsonarray.put("so <fine>.");
jsonarray.put(true);
jsonarray.put(false);
jsonarray.put(new JSONArray());
jsonarray.put(new JSONObject());
jsonobject.put("keys", JSONObject.getNames(jsonobject));
assertEquals(
"{\n \"to\": null,\n \"ten\": 10,\n \"JSONObject\": {},\n \"JSONArray\": [],\n \"op\": \"Good\",\n \"keys\": [\n \"to\",\n \"ten\",\n \"JSONObject\",\n \"JSONArray\",\n \"op\",\n \"int\",\n \"true\",\n \"foo\",\n \"zero\",\n \"double\",\n \"String\",\n \"false\",\n \"bool\",\n \"\\\\u2028\",\n \"\\\\u2029\",\n \"null\"\n ],\n \"int\": 57,\n \"true\": true,\n \"foo\": [\n true,\n false,\n 9876543210,\n 0,\n 1.00000001,\n 1.000000000001,\n 1,\n 1.0E-17,\n 2,\n 0.1,\n 2.0E100,\n -32,\n [],\n {},\n \"string\",\n 666,\n 2001.99,\n \"so \\\"fine\\\".\",\n \"so <fine>.\",\n true,\n false,\n [],\n {}\n ],\n \"zero\": -0,\n \"double\": 1.2345678901234568E29,\n \"String\": \"98.6\",\n \"false\": false,\n \"bool\": \"true\",\n \"\\\\u2028\": \"\\u2028\",\n \"\\\\u2029\": \"\\u2029\",\n \"null\": null\n}",
jsonobject.toString(4));
assertEquals(
"<to>null</to><ten>10</ten><JSONObject></JSONObject><op>Good</op><keys>to</keys><keys>ten</keys><keys>JSONObject</keys><keys>JSONArray</keys><keys>op</keys><keys>int</keys><keys>true</keys><keys>foo</keys><keys>zero</keys><keys>double</keys><keys>String</keys><keys>false</keys><keys>bool</keys><keys>\\u2028</keys><keys>\\u2029</keys><keys>null</keys><int>57</int><true>true</true><foo>true</foo><foo>false</foo><foo>9876543210</foo><foo>0.0</foo><foo>1.00000001</foo><foo>1.000000000001</foo><foo>1.0</foo><foo>1.0E-17</foo><foo>2.0</foo><foo>0.1</foo><foo>2.0E100</foo><foo>-32</foo><foo></foo><foo></foo><foo>string</foo><foo>666</foo><foo>2001.99</foo><foo>so "fine".</foo><foo>so <fine>.</foo><foo>true</foo><foo>false</foo><foo></foo><foo></foo><zero>-0.0</zero><double>1.2345678901234568E29</double><String>98.6</String><false>false</false><bool>true</bool><\\u2028>\u2028</\\u2028><\\u2029>\u2029</\\u2029><null>null</null>",
XML.toString(jsonobject));
assertEquals(98.6d, jsonobject.getDouble("String"), eps);
assertTrue(jsonobject.getBoolean("bool"));
assertEquals(
"[true,false,9876543210,0,1.00000001,1.000000000001,1,1.0E-17,2,0.1,2.0E100,-32,[],{},\"string\",666,2001.99,\"so \\\"fine\\\".\",\"so <fine>.\",true,false,[],{}]",
jsonobject.getJSONArray("foo").toString());
assertEquals("Good", jsonobject.getString("op"));
assertEquals(10, jsonobject.getInt("ten"));
assertFalse(jsonobject.optBoolean("oops"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the multipleThings2 method.
*/
public void testMultipleThings2()
{
try
{
jsonobject = new JSONObject(
"{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
assertEquals(
"{\n \"int\": 2147483647,\n \"string\": \"98.6\",\n \"longer\": 9223372036854775807,\n \"double\": \"9223372036854775808\",\n \"long\": 2147483648\n}",
jsonobject.toString(1));
// getInt
assertEquals(2147483647, jsonobject.getInt("int"));
assertEquals(-2147483648, jsonobject.getInt("long"));
assertEquals(-1, jsonobject.getInt("longer"));
try
{
jsonobject.getInt("double");
fail("should fail with - JSONObject[\"double\"] is not an int.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"double\"] is not an int.",
expected.getMessage());
}
try
{
jsonobject.getInt("string");
fail("should fail with - JSONObject[\"string\"] is not an int.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"string\"] is not an int.",
expected.getMessage());
}
// getLong
assertEquals(2147483647, jsonobject.getLong("int"));
assertEquals(2147483648l, jsonobject.getLong("long"));
assertEquals(9223372036854775807l, jsonobject.getLong("longer"));
try
{
jsonobject.getLong("double");
fail("should fail with - JSONObject[\"double\"] is not a long.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"double\"] is not a long.",
expected.getMessage());
}
try
{
jsonobject.getLong("string");
fail("should fail with - JSONObject[\"string\"] is not a long.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"string\"] is not a long.",
expected.getMessage());
}
// getDouble
assertEquals(2.147483647E9, jsonobject.getDouble("int"), eps);
assertEquals(2.147483648E9, jsonobject.getDouble("long"), eps);
assertEquals(9.223372036854776E18, jsonobject.getDouble("longer"),
eps);
assertEquals(9223372036854775808d, jsonobject.getDouble("double"),
eps);
assertEquals(98.6, jsonobject.getDouble("string"), eps);
jsonobject.put("good sized", 9223372036854775807L);
assertEquals(
"{\n \"int\": 2147483647,\n \"string\": \"98.6\",\n \"longer\": 9223372036854775807,\n \"good sized\": 9223372036854775807,\n \"double\": \"9223372036854775808\",\n \"long\": 2147483648\n}",
jsonobject.toString(1));
jsonarray = new JSONArray(
"[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
assertEquals(
"[\n 2147483647,\n 2147483648,\n 9223372036854775807,\n \"9223372036854775808\"\n]",
jsonarray.toString(1));
List<String> expectedKeys = new ArrayList<String>(6);
expectedKeys.add("int");
expectedKeys.add("string");
expectedKeys.add("longer");
expectedKeys.add("good sized");
expectedKeys.add("double");
expectedKeys.add("long");
iterator = jsonobject.keys();
while (iterator.hasNext())
{
string = iterator.next();
assertTrue(expectedKeys.remove(string));
}
assertEquals(0, expectedKeys.size());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the put method using collection and map.
*/
public void testPut_CollectionAndMap()
{
try
{
string = "{plist=Apple; AnimalSmells = { pig = piggish; lamb = lambish; worm = wormy; }; AnimalSounds = { pig = oink; lamb = baa; worm = baa; Lisa = \"Why is the worm talking like a lamb?\" } ; AnimalColors = { pig = pink; lamb = black; worm = pink; } } ";
jsonobject = new JSONObject(string);
assertEquals(
"{\"AnimalColors\":{\"worm\":\"pink\",\"lamb\":\"black\",\"pig\":\"pink\"},\"plist\":\"Apple\",\"AnimalSounds\":{\"worm\":\"baa\",\"Lisa\":\"Why is the worm talking like a lamb?\",\"lamb\":\"baa\",\"pig\":\"oink\"},\"AnimalSmells\":{\"worm\":\"wormy\",\"lamb\":\"lambish\",\"pig\":\"piggish\"}}",
jsonobject.toString());
Collection<Object> collection = null;
Map<String, Object> map = null;
jsonobject = new JSONObject(map);
jsonarray = new JSONArray(collection);
jsonobject.append("stooge", "Joe DeRita");
jsonobject.append("stooge", "Shemp");
jsonobject.accumulate("stooges", "Curly");
jsonobject.accumulate("stooges", "Larry");
jsonobject.accumulate("stooges", "Moe");
jsonobject.accumulate("stoogearray", jsonobject.get("stooges"));
jsonobject.put("map", map);
jsonobject.put("collection", collection);
jsonobject.put("array", jsonarray);
jsonarray.put(map);
jsonarray.put(collection);
assertEquals(
"{\"stooge\":[\"Joe DeRita\",\"Shemp\"],\"map\":{},\"stooges\":[\"Curly\",\"Larry\",\"Moe\"],\"collection\":[],\"stoogearray\":[[\"Curly\",\"Larry\",\"Moe\"]],\"array\":[{},[]]}",
jsonobject.toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the accumulate method.
*/
public void testAccumulate()
{
try
{
jsonobject = new JSONObject();
jsonobject.accumulate("stooge", "Curly");
jsonobject.accumulate("stooge", "Larry");
jsonobject.accumulate("stooge", "Moe");
jsonarray = jsonobject.getJSONArray("stooge");
jsonarray.put(5, "Shemp");
assertEquals("{\"stooge\": [\n" + " \"Curly\",\n"
+ " \"Larry\",\n" + " \"Moe\",\n" + " null,\n"
+ " null,\n" + " \"Shemp\"\n" + "]}",
jsonobject.toString(4));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the write method.
*/
public void testWrite()
{
try
{
jsonobject = new JSONObject();
jsonobject.accumulate("stooge", "Curly");
jsonobject.accumulate("stooge", "Larry");
jsonobject.accumulate("stooge", "Moe");
jsonarray = jsonobject.getJSONArray("stooge");
jsonarray.put(5, "Shemp");
assertEquals(
"{\"stooge\":[\"Curly\",\"Larry\",\"Moe\",null,null,\"Shemp\"]}",
jsonobject.write(new StringWriter()).toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using html.
*/
public void testToString_Html()
{
try
{
jsonobject = new JSONObject(
"{script: 'It is not allowed in HTML to send a close script tag in a string<script>because it confuses browsers</script>so we insert a backslash before the /'}");
assertEquals(
"{\"script\":\"It is not allowed in HTML to send a close script tag in a string<script>because it confuses browsers<\\/script>so we insert a backslash before the /\"}",
jsonobject.toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using multiple test cases.
*/
public void testToString_MultipleTestCases()
{
try
{
jsonobject = new JSONObject(
"{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=> o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
assertEquals(
"{\n \"noh\": \"0x0x\",\n \"one\": [[1]],\n \"o\": 999,\n \"+\": 6.0E66,\n \"true\": true,\n \"forgiving\": \"This package can be used to parse formats that are similar to but not stricting conforming to JSON\",\n \"fun\": \"with non-standard forms\",\n \"double\": 0.666,\n \"uno\": [[{\"1\": 1}]],\n \"dec\": 666,\n \"oct\": 666,\n \"hex\": \"0x666\",\n \"string\": \"o. k.\",\n \"empty\": \"\",\n \"false\": false,\n \"[true]\": [[\n \"!\",\n \"@\",\n \"*\"\n ]],\n \"pluses\": \"+++\",\n \"why\": \"To make it easier to migrate existing data to JSON\",\n \"null\": null\n}",
jsonobject.toString(1));
assertTrue(jsonobject.getBoolean("true"));
assertFalse(jsonobject.getBoolean("false"));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the populateMap method using priative class.
*/
public void testConstructor_PriativeClass()
{
jsonobject = new JSONObject(new ObjectWithPrimatives());
assertEquals(
"{\"l\":5748548957230984584,\"m\":true,\"big\":false,\"j\":\"3\",\"k\":10.03,\"ZERO\":0,\"i\":3}",
jsonobject.toString());
}
/**
* Tests the populateMap method using sub class.
*/
public void testConstructor_SubClass()
{
ObjectWithPrimatives ob = new ObjectWithPrimativesExtension();
jsonobject = new JSONObject(ob);
assertEquals(
"{\"l\":5748548957230984584,\"m\":true,\"big\":false,\"j\":\"3\",\"k\":10.03,\"ZERO\":0,\"i\":3}",
jsonobject.toString());
}
/**
* Tests the populateMap method using private class.
*/
public void testConstructor_PrivateClass()
{
class PrivateObject
{
private int i;
public PrivateObject()
{
i = 3;
}
@SuppressWarnings("unused")
public int getI()
{
return i;
}
}
jsonobject = new JSONObject(new PrivateObject());
assertEquals("{}", jsonobject.toString());
}
/**
* Tests the populateMap method using array list.
*/
public void testConstructor_ArrayList()
{
List<String> ar = new ArrayList<String>();
ar.add("test1");
ar.add("test2");
jsonobject = new JSONObject(ar);
assertEquals("{\"empty\":false}", jsonobject.toString());
}
/**
* Tests the populateMap method using class class.
*/
public void testConstructor_ClassClass()
{
try
{
jsonobject = new JSONObject(this.getClass());
assertEquals("class junit.framework.TestCase",
jsonobject.get("genericSuperclass").toString());
} catch (Exception e)
{
fail(e.getMessage());
}
}
/**
* Tests the constructor method using french resource bundle.
*/
public static void testConstructor_FrenchResourceBundle()
{
try
{
Locale currentLocale = new Locale("fr", "CA", "UNIX");
assertEquals(
"{\"ASCII\":\"Number that represent chraracters\",\"JSON\":\"What are we testing?\",\"JAVA\":\"The language you are running to see this\"}",
new JSONObject("org.json.tests.SampleResourceBundle",
currentLocale).toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the constructor method using us resource bundle.
*/
public static void testConstructor_UsResourceBundle()
{
try
{
Locale currentLocale = new Locale("en");
assertEquals(
"{\"ASCII\":\"American Standard Code for Information Interchange\",\"JSON\":\"JavaScript Object Notation\",\"JAVA\":{\"desc\":\"Just Another Vague Acronym\",\"data\":\"Sweet language\"}}",
new JSONObject("org.json.tests.SampleResourceBundle",
currentLocale).toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the constructor method using object with string array.
*/
public void testConstructor_ObjectWithStringArray()
{
assertEquals("{\"m\":true,\"i\":3}", new JSONObject(
new ObjectWithPrimatives(), new String[]
{
"i", "m", "k"
}).toString());
}
/**
* Tests the opt method.
*/
public void testOpt()
{
try
{
jsonobject = new JSONObject("{\"a\":2}");
assertEquals(2, jsonobject.opt("a"));
assertEquals(null, jsonobject.opt("b"));
assertEquals(null, jsonobject.opt(null));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the stringToValue method.
*/
public static void testStringToValue()
{
assertEquals("", JSONObject.stringToValue(""));
assertEquals(true, JSONObject.stringToValue("true"));
assertEquals(false, JSONObject.stringToValue("false"));
assertEquals(JSONObject.NULL, JSONObject.stringToValue("null"));
assertEquals(10, JSONObject.stringToValue("10"));
assertEquals(10000.0, JSONObject.stringToValue("10e3"));
assertEquals(10000.0, JSONObject.stringToValue("10E3"));
assertEquals("10E3000000000", JSONObject.stringToValue("10E3000000000"));
}
/**
* Tests the quote method.
*/
public static void testQuote()
{
assertEquals("\"\"", JSONObject.quote(""));
assertEquals("\"\"", JSONObject.quote(null));
assertEquals("\"true\"", JSONObject.quote("true"));
assertEquals("\"10\"", JSONObject.quote("10"));
assertEquals("\"\\b\\t\\n\\f\\r\"", JSONObject.quote("\b\t\n\f\r"));
assertEquals("\"\\u0012\\u0085\\u2086\u2286\"",
JSONObject.quote("\u0012\u0085\u2086\u2286"));
}
/**
* Tests the getNames method.
*/
public void testGetNames()
{
try
{
jsonobject = new JSONObject();
jsonobject.put("a", "123");
jsonobject.put("b", "123");
jsonobject.put("c", "123");
String[] names = JSONObject.getNames(jsonobject);
assertEquals(3, names.length);
assertEquals("b", names[0]);
assertEquals("c", names[1]);
assertEquals("a", names[2]);
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the getNames method using empty json object.
*/
public void testGetNames_EmptyJsonObject()
{
jsonobject = new JSONObject();
assertEquals(null, JSONObject.getNames(jsonobject));
}
/**
* Tests the getNames method using object with primatives.
*/
public void testGetNames_ObjectWithPrimatives()
{
String[] names = JSONObject.getNames(new ObjectWithPrimatives());
assertEquals(2, names.length);
assertEquals("i", names[0]);
assertEquals("m", names[1]);
}
/**
* Tests the getNames method using empty object.
*/
public static void testGetNames_EmptyObject()
{
class EmptyObject
{
// Empty Object
}
assertEquals(null, JSONObject.getNames(new EmptyObject()));
}
/**
* Tests the getNames method using null.
*/
public static void testGetNames_Null()
{
ObjectWithPrimatives owp = null;
assertEquals(null, JSONObject.getNames(owp));
}
/**
* Tests the getLong method.
*/
public void testGetLong_Long()
{
try
{
jsonobject = new JSONObject();
jsonobject.put("abc", "98765432");
jsonobject.put("123", 98765432);
assertEquals(98765432, jsonobject.getLong("abc"));
assertEquals(98765432, jsonobject.getLong("123"));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the getJsonObject method using json object.
*/
public void testGetJsonObject_JsonObject()
{
try
{
jsonobject = new JSONObject();
jsonobject.put("abc", new JSONObject());
assertEquals("{}", jsonobject.getJSONObject("abc").toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the getJsonObject method using int.
*/
public void testGetJsonObject_Int()
{
try
{
jsonobject = new JSONObject();
jsonobject.put("abc", 45);
jsonobject.getJSONObject("abc");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("JSONObject[\"abc\"] is not a JSONObject.",
e.getMessage());
}
}
/**
* Tests the getJsonObject method using invalid key.
*/
public void testGetJsonObject_InvalidKey()
{
try
{
jsonobject = new JSONObject();
jsonobject.getJSONObject("abc");
fail("Should have thrown exception.");
} catch (JSONException e)
{