mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-24 00:03:17 -05:00
Compare commits
1013 Commits
20190722
...
pre-releas
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
418d5e9973 | ||
|
|
82a02d879e | ||
|
|
2184ef34d1 | ||
|
|
8e65eaa992 | ||
|
|
74439cf696 | ||
|
|
53da5ce2a9 | ||
|
|
2e9ad6ff5a | ||
|
|
8dbf03e76b | ||
|
|
4917e3579d | ||
|
|
45ec164faa | ||
|
|
d4c5136c21 | ||
|
|
fd0cca3586 | ||
|
|
50a5ce256b | ||
|
|
1afd7cd6bc | ||
|
|
7751b397bf | ||
|
|
5d1c789490 | ||
|
|
d1327c2da3 | ||
|
|
b2943b8fd0 | ||
|
|
628d8c42d9 | ||
|
|
76ee4312b3 | ||
|
|
4a662316f7 | ||
|
|
6452a6f38d | ||
|
|
ae4f4afcc7 | ||
|
|
8a86894c63 | ||
|
|
f30167e7c0 | ||
|
|
75e5a3d646 | ||
|
|
3919abd69a | ||
|
|
f112a091aa | ||
|
|
42afb34045 | ||
|
|
a746322e57 | ||
|
|
c524cd17a0 | ||
|
|
52f249c71e | ||
|
|
1689fc28cf | ||
|
|
22f8290840 | ||
|
|
8b857da467 | ||
|
|
07b1291448 | ||
|
|
1d81e8879a | ||
|
|
4c873a1db4 | ||
|
|
6631b80e8f | ||
|
|
9218f28db8 | ||
|
|
94341cd663 | ||
|
|
afd9a6fbb7 | ||
|
|
54470e6b56 | ||
|
|
dde9d7eceb | ||
|
|
8c427d9101 | ||
|
|
4bbbe77446 | ||
|
|
ad44a9274c | ||
|
|
3b7ba07531 | ||
|
|
215f4268bf | ||
|
|
ca1c6830c9 | ||
|
|
2e153737b1 | ||
|
|
391c86931b | ||
|
|
ed8c73964a | ||
|
|
324090a876 | ||
|
|
41c6e9e81e | ||
|
|
0e60592bdb | ||
|
|
4cd70cf9d9 | ||
|
|
ac40a6faa3 | ||
|
|
2dcef89a6f | ||
|
|
d3c7eaf17e | ||
|
|
09536cd6c8 | ||
|
|
1f0729cadb | ||
|
|
80b2672f77 | ||
|
|
1f308db7c4 | ||
|
|
2ee5bf13f4 | ||
|
|
e7e52dad82 | ||
|
|
39ee5e092f | ||
|
|
215ec9bb9c | ||
|
|
61dc2644d8 | ||
|
|
ab1b9a3459 | ||
|
|
14e9cdc485 | ||
|
|
0d71dcf713 | ||
|
|
14f71274f7 | ||
|
|
a8ab79e3f3 | ||
|
|
783577be19 | ||
|
|
054786e300 | ||
|
|
48dfeb84b0 | ||
|
|
1ae43bdb90 | ||
|
|
cf00ef3e8a | ||
|
|
0180bd90f0 | ||
|
|
fa2f3402d6 | ||
|
|
f4944fbf1e | ||
|
|
1881cbe91a | ||
|
|
209837357b | ||
|
|
d1fd901bdb | ||
|
|
6529a7e536 | ||
|
|
4319b71934 | ||
|
|
1e3f37be98 | ||
|
|
7a8c21621c | ||
|
|
9216a19366 | ||
|
|
b6e347d7f8 | ||
|
|
879579d3bb | ||
|
|
898dd5a39d | ||
|
|
ce13ebd5fe | ||
|
|
7cc19483fb | ||
|
|
0bace72ced | ||
|
|
03def9c7fc | ||
|
|
3dcd5b2fab | ||
|
|
8983ca6da1 | ||
|
|
ce074e9f9a | ||
|
|
fe597d296e | ||
|
|
5bd4257c54 | ||
|
|
d02ac0f2a3 | ||
|
|
cfd47615d0 | ||
|
|
3200275881 | ||
|
|
d92d62afc2 | ||
|
|
46534b56ad | ||
|
|
87406e4db1 | ||
|
|
c0918c2428 | ||
|
|
d2cb38dba7 | ||
|
|
4929fc99c1 | ||
|
|
372f5caac4 | ||
|
|
0fcf352848 | ||
|
|
78cdb3d0d6 | ||
|
|
7324cc0b26 | ||
|
|
75a34a246f | ||
|
|
78151beea2 | ||
|
|
ccb4a9b95f | ||
|
|
4e39d8ccf2 | ||
|
|
d69d5e284b | ||
|
|
f1c9d0787b | ||
|
|
49de92224d | ||
|
|
d335447ab4 | ||
|
|
6c160b7d1a | ||
|
|
30dc22790c | ||
|
|
8f66865e0a | ||
|
|
45dede448c | ||
|
|
6aed1cfeb6 | ||
|
|
3672b5e158 | ||
|
|
f3b3491f4d | ||
|
|
e2fe14d951 | ||
|
|
0ff368ca07 | ||
|
|
c51efe8b08 | ||
|
|
e67abb3842 | ||
|
|
c140e91bb8 | ||
|
|
63e8314deb | ||
|
|
dcbbccc76c | ||
|
|
b75da07545 | ||
|
|
6c35b08ad6 | ||
|
|
60090a7167 | ||
|
|
0c5cf18255 | ||
|
|
5974fc1a38 | ||
|
|
a3f15e5883 | ||
|
|
045324ab42 | ||
|
|
eda08415ca | ||
|
|
48c092acfb | ||
|
|
dab29ec1d5 | ||
|
|
712859d771 | ||
|
|
05b0897f41 | ||
|
|
c010033591 | ||
|
|
5407423e43 | ||
|
|
63625b3c62 | ||
|
|
f9b5587c87 | ||
|
|
3d69990ab5 | ||
|
|
ba05e1a98c | ||
|
|
390d442054 | ||
|
|
3eb8a62af6 | ||
|
|
989cdb61bc | ||
|
|
8de0628bd1 | ||
|
|
569be9d19e | ||
|
|
3f97826462 | ||
|
|
d520210ea2 | ||
|
|
f38452a00c | ||
|
|
4f456d9432 | ||
|
|
898288810f | ||
|
|
d878c38d40 | ||
|
|
e2194bc190 | ||
|
|
d672b44a25 | ||
|
|
06778bd2d9 | ||
|
|
771c82c4eb | ||
|
|
6660e40915 | ||
|
|
0ff635c456 | ||
|
|
7c7a98da71 | ||
|
|
d36066cf82 | ||
|
|
af8cb376c2 | ||
|
|
c1107fa987 | ||
|
|
cd631d970e | ||
|
|
f0289413d6 | ||
|
|
b4b39bb441 | ||
|
|
6dc1ed0a02 | ||
|
|
86253211c2 | ||
|
|
77c899d325 | ||
|
|
b314611230 | ||
|
|
cb2c8d3962 | ||
|
|
f164b8c597 | ||
|
|
6358b7f681 | ||
|
|
21a9fae7b0 | ||
|
|
8550175556 | ||
|
|
10514e48cb | ||
|
|
72214f1b43 | ||
|
|
99c84fdf3a | ||
|
|
010e83b925 | ||
|
|
9865dbbebe | ||
|
|
4548696c8d | ||
|
|
f0308a3475 | ||
|
|
19dec1bb5f | ||
|
|
f2d20988de | ||
|
|
7915d8518f | ||
|
|
55b824d4c4 | ||
|
|
ac7806d060 | ||
|
|
86bb0a1a02 | ||
|
|
5ddb8c3d35 | ||
|
|
23ac2e7bca | ||
|
|
d7819a4fa2 | ||
|
|
7701f21839 | ||
|
|
ffd48afa42 | ||
|
|
abea194120 | ||
|
|
dcac3bc18e | ||
|
|
6d811607dd | ||
|
|
4d6de8c00a | ||
|
|
6dba7220e1 | ||
|
|
d4521696a9 | ||
|
|
e430db40aa | ||
|
|
7cbeb35498 | ||
|
|
aba82d9cc4 | ||
|
|
9ee10fdfc8 | ||
|
|
4a468d163a | ||
|
|
92991770ca | ||
|
|
09f35372d4 | ||
|
|
30f5b2de79 | ||
|
|
7f1cb8bf62 | ||
|
|
75419e3f25 | ||
|
|
097a401f3f | ||
|
|
5c4a7a1b1f | ||
|
|
b5f9febfe9 | ||
|
|
1a61af8255 | ||
|
|
11c29c366d | ||
|
|
a3742acf74 | ||
|
|
783d298f99 | ||
|
|
6c1bc0660a | ||
|
|
8ec822c575 | ||
|
|
1ceb70b525 | ||
|
|
c05d7058ff | ||
|
|
07a358449a | ||
|
|
7fe2fd95a5 | ||
|
|
b5b9f636ff | ||
|
|
c6ec2f0e4c | ||
|
|
1a2108efa2 | ||
|
|
caadcba30e | ||
|
|
ea842b437c | ||
|
|
a2a8240d0d | ||
|
|
1ab11d0802 | ||
|
|
5539722c69 | ||
|
|
98b79ae7bf | ||
|
|
04a4c5a3ec | ||
|
|
6007165c17 | ||
|
|
411f71137b | ||
|
|
e9117dbe5c | ||
|
|
7a85b514a9 | ||
|
|
82c8f486c5 | ||
|
|
006b29bda3 | ||
|
|
996d3a5fad | ||
|
|
de745e9c81 | ||
|
|
2374766018 | ||
|
|
1d0775cce7 | ||
|
|
d677a99f4e | ||
|
|
e6d37c469d | ||
|
|
f074bed732 | ||
|
|
e8f125fb6e | ||
|
|
ed183e6142 | ||
|
|
a86786a5f5 | ||
|
|
7c4f98c42c | ||
|
|
3894483560 | ||
|
|
be115059e9 | ||
|
|
2b41cf44b5 | ||
|
|
9a9efac2af | ||
|
|
134074aeaa | ||
|
|
8540bb80c0 | ||
|
|
4dfd779b1c | ||
|
|
7b2677ac5a | ||
|
|
29a7f4622d | ||
|
|
f346203cd6 | ||
|
|
b180dbedbc | ||
|
|
cca6d1020f | ||
|
|
af5f780d5b | ||
|
|
495cec9037 | ||
|
|
e4aa7f1308 | ||
|
|
56cb5f84c4 | ||
|
|
0cdc38ac24 | ||
|
|
d5277b126b | ||
|
|
228598ca84 | ||
|
|
0a6fb1d578 | ||
|
|
c4cd526c53 | ||
|
|
776b5ccb85 | ||
|
|
fb99c06bad | ||
|
|
bc09f90e90 | ||
|
|
c93014cb53 | ||
|
|
0e4a94d91d | ||
|
|
1a38879c90 | ||
|
|
4c8cac22a8 | ||
|
|
fe45fa9cfb | ||
|
|
79af389f7a | ||
|
|
1726b6cf55 | ||
|
|
beb2fb5706 | ||
|
|
ff921db783 | ||
|
|
61bb60e752 | ||
|
|
ef68cdf810 | ||
|
|
eaa5611ba3 | ||
|
|
dbb113176b | ||
|
|
16967f322e | ||
|
|
284a316838 | ||
|
|
4e8231c512 | ||
|
|
db0fde2a56 | ||
|
|
661114c50d | ||
|
|
ca88454f1c | ||
|
|
3e688afc66 | ||
|
|
becc1631e6 | ||
|
|
01727fd0ed | ||
|
|
74cd73f97c | ||
|
|
c29d4881e0 | ||
|
|
7c1b6531e7 | ||
|
|
db122e5d3a | ||
|
|
a309931d20 | ||
|
|
e27da22e05 | ||
|
|
af6d07cecb | ||
|
|
64093366b3 | ||
|
|
9b69ec49ad | ||
|
|
2c674be1b6 | ||
|
|
be33deb7d5 | ||
|
|
48089a4da7 | ||
|
|
a4e152f4f0 | ||
|
|
3dd8f2ecd5 | ||
|
|
bae0b0dac9 | ||
|
|
e563dbcaaa | ||
|
|
50dfcc59b3 | ||
|
|
b2943eb395 | ||
|
|
60662e2f83 | ||
|
|
2a4bc3420a | ||
|
|
b6ff0db984 | ||
|
|
c8a9e15a57 | ||
|
|
402db6ad84 | ||
|
|
4951ec48c8 | ||
|
|
8ce0019a5d | ||
|
|
3d524349a1 | ||
|
|
a963115ac2 | ||
|
|
f959baa3cb | ||
|
|
c048b36516 | ||
|
|
f6e5bfa2db | ||
|
|
084b24cbe7 | ||
|
|
22ccf1b8e5 | ||
|
|
133c0cc75f | ||
|
|
0578285036 | ||
|
|
48fb5261fe | ||
|
|
8353b9c3f0 | ||
|
|
fe22b242b5 | ||
|
|
7eca507d13 | ||
|
|
e1eabc9c27 | ||
|
|
0d436d92e2 | ||
|
|
45bcba518f | ||
|
|
47fb49b6a8 | ||
|
|
0df034c9fd | ||
|
|
f0a05e6911 | ||
|
|
24093491a8 | ||
|
|
1275f6809d | ||
|
|
9234eab00a | ||
|
|
72f4c3e646 | ||
|
|
df2d6f8363 | ||
|
|
a6e412bded | ||
|
|
2391d248cc | ||
|
|
401495ae86 | ||
|
|
448e204186 | ||
|
|
eb56704e68 | ||
|
|
651511f500 | ||
|
|
a14cb12c85 | ||
|
|
f566a1d9ee | ||
|
|
5920eca2d7 | ||
|
|
3b097d051a | ||
|
|
4e630e58a4 | ||
|
|
b732188e4e | ||
|
|
5369442671 | ||
|
|
bb1138762a | ||
|
|
6a732ec99d | ||
|
|
c798c76ddd | ||
|
|
23d5e52a53 | ||
|
|
98df35449a | ||
|
|
1be6ee31a7 | ||
|
|
a2d3d3c9b5 | ||
|
|
bf9219386a | ||
|
|
85495facbd | ||
|
|
7aba3ac941 | ||
|
|
d51250f6b0 | ||
|
|
9cb8e153bf | ||
|
|
80c1479ad8 | ||
|
|
444335d12a | ||
|
|
a2c0562e04 | ||
|
|
153972afdf | ||
|
|
4a8ff28fd8 | ||
|
|
fa457a4113 | ||
|
|
b7f708b222 | ||
|
|
12411b7981 | ||
|
|
61801c623e | ||
|
|
1915aab7c4 | ||
|
|
8439039da7 | ||
|
|
a6bdd081eb | ||
|
|
a30d71fdca | ||
|
|
cdd67b0aef | ||
|
|
89f6e7f6a6 | ||
|
|
346fb26f2d | ||
|
|
6daabb43ab | ||
|
|
57f785c42e | ||
|
|
3eecd67a3b | ||
|
|
5a587bbdfd | ||
|
|
e0534b3ec7 | ||
|
|
7dd9e01dae | ||
|
|
88ca19042b | ||
|
|
b4036e6a8e | ||
|
|
89f16ad0af | ||
|
|
6f92a3ab4e | ||
|
|
beae279b21 | ||
|
|
a642329314 | ||
|
|
9abb35ad39 | ||
|
|
c0a1d5f741 | ||
|
|
04a765647e | ||
|
|
4f78ec666a | ||
|
|
7d6b76de37 | ||
|
|
5cfe216ffd | ||
|
|
c33ad9c062 | ||
|
|
7a124d857d | ||
|
|
f1b0210b8a | ||
|
|
8ef8e1463d | ||
|
|
48b6aa3e4c | ||
|
|
cff5cc6c74 | ||
|
|
04e8ea84dc | ||
|
|
812955e39d | ||
|
|
bc623e36d6 | ||
|
|
5dd78bc0b9 | ||
|
|
e638955034 | ||
|
|
3f9b53fee4 | ||
|
|
a0f90b776d | ||
|
|
fafaeb7aa6 | ||
|
|
e356739a2f | ||
|
|
fca7e17b38 | ||
|
|
fb96e870a9 | ||
|
|
638273af7a | ||
|
|
1ffcf3915c | ||
|
|
4565bddcbb | ||
|
|
b5bcb68968 | ||
|
|
7823d3a4f3 | ||
|
|
d6227c83d7 | ||
|
|
f54b5e4b0d | ||
|
|
30b3680050 | ||
|
|
3ed9154f63 | ||
|
|
669316d29e | ||
|
|
30a70c8886 | ||
|
|
93f4b34890 | ||
|
|
9c87d6e214 | ||
|
|
f27e5fe04d | ||
|
|
2528e60b09 | ||
|
|
4e601fd46e | ||
|
|
9f19c22b77 | ||
|
|
9000901a11 | ||
|
|
f03eb56071 | ||
|
|
b4bbc58644 | ||
|
|
cf43419015 | ||
|
|
e896497602 | ||
|
|
d284c81e16 | ||
|
|
a526b41b67 | ||
|
|
9f07853f19 | ||
|
|
e29c541353 | ||
|
|
de5f768cff | ||
|
|
8ca8a80753 | ||
|
|
fb71d9d40f | ||
|
|
b80141a19c | ||
|
|
c0e467c848 | ||
|
|
8680b10716 | ||
|
|
2559114dbb | ||
|
|
bb048e3ffb | ||
|
|
579784d73e | ||
|
|
c03054b1a6 | ||
|
|
4e0faebe62 | ||
|
|
c6089e53f5 | ||
|
|
cfbc306673 | ||
|
|
449ec8745e | ||
|
|
f91d0c8a52 | ||
|
|
4571978840 | ||
|
|
76ec2fe5a2 | ||
|
|
0200c984cc | ||
|
|
8f3e5ade18 | ||
|
|
20b4f85efe | ||
|
|
5bc8dae5d0 | ||
|
|
8fa9be742c | ||
|
|
b48abe6558 | ||
|
|
97023e1098 | ||
|
|
143db39d27 | ||
|
|
75894086e5 | ||
|
|
fa46da45f4 | ||
|
|
f912e5d1c4 | ||
|
|
6bf3d38889 | ||
|
|
c62df81b3e | ||
|
|
e1f69ff3fe | ||
|
|
50d619698f | ||
|
|
29103e3228 | ||
|
|
2630676f36 | ||
|
|
7299b201f4 | ||
|
|
8cc1e9830d | ||
|
|
c43e21ae73 | ||
|
|
d6ccc64c79 | ||
|
|
7844eb79cd | ||
|
|
5b531faa49 | ||
|
|
31ff8a2291 | ||
|
|
e33f463179 | ||
|
|
e77a77e841 | ||
|
|
26f48484fd | ||
|
|
efad1d73a7 | ||
|
|
c7130d577a | ||
|
|
d85eea53bb | ||
|
|
57ad94ef5e | ||
|
|
a57eff26d5 | ||
|
|
68883b9ff8 | ||
|
|
11e6b1af7e | ||
|
|
e4b76d6588 | ||
|
|
3a8193bea4 | ||
|
|
6bf2692a94 | ||
|
|
fdde43cd3b | ||
|
|
6b779f8ca7 | ||
|
|
59b7a7adff | ||
|
|
7abd89b4bc | ||
|
|
56d4130a86 | ||
|
|
ed9658d5cb | ||
|
|
310f18fcdc | ||
|
|
900a8cc945 | ||
|
|
0a8091c954 | ||
|
|
61c1a882d6 | ||
|
|
250f74ef4d | ||
|
|
5541a6d91d | ||
|
|
d30ecad7f8 | ||
|
|
f35194bc1d | ||
|
|
5d828d2c0b | ||
|
|
c175a9eb62 | ||
|
|
3c9573cc3d | ||
|
|
f37c2d67c5 | ||
|
|
6351fa63aa | ||
|
|
870fa03a19 | ||
|
|
555f712a8c | ||
|
|
db2d1714de | ||
|
|
650f52501a | ||
|
|
896674ae36 | ||
|
|
8f0c3b0bf8 | ||
|
|
0de8d0d2e0 | ||
|
|
46fe58e912 | ||
|
|
26cd17687f | ||
|
|
879fba3f7f | ||
|
|
98cd8ef8b2 | ||
|
|
78528102d0 | ||
|
|
0e34d8d383 | ||
|
|
880402120c | ||
|
|
d10725dc92 | ||
|
|
0d13e56064 | ||
|
|
f1d354ce7b | ||
|
|
86e136afc9 | ||
|
|
4f1c8b2d6f | ||
|
|
5a31f9ef5f | ||
|
|
c136668f23 | ||
|
|
e18f42becc | ||
|
|
e0a6c2ef34 | ||
|
|
c98da43184 | ||
|
|
734f182242 | ||
|
|
c63e78bbc7 | ||
|
|
db6ca7fbf3 | ||
|
|
9a3e7dd7c4 | ||
|
|
79ff79ed70 | ||
|
|
d14c7b9127 | ||
|
|
de4395dc5e | ||
|
|
6ddaa13c1e | ||
|
|
f9908a6adb | ||
|
|
ba6c4089ea | ||
|
|
aa0a5a7245 | ||
|
|
9de97438ac | ||
|
|
601114ee29 | ||
|
|
c11c006e88 | ||
|
|
96bf788515 | ||
|
|
f4261add91 | ||
|
|
dced1b4a44 | ||
|
|
cf00d2f265 | ||
|
|
95c6373122 | ||
|
|
b6ed0d4178 | ||
|
|
75f656fd07 | ||
|
|
4b84ba2f66 | ||
|
|
d088cf014e | ||
|
|
19bb6fd606 | ||
|
|
fee6ddb922 | ||
|
|
5a32114792 | ||
|
|
56d33b8061 | ||
|
|
6029dece41 | ||
|
|
d334b58f45 | ||
|
|
b4a75c7bf8 | ||
|
|
956bdfa5b7 | ||
|
|
8546e68e20 | ||
|
|
0832d1d873 | ||
|
|
2b0a8838ef | ||
|
|
1da2b984cd | ||
|
|
78901383a4 | ||
|
|
3737c987a2 | ||
|
|
8e5b516f2b | ||
|
|
82202dbf65 | ||
|
|
89d4681e41 | ||
|
|
1265897f4e | ||
|
|
74e4932cfc | ||
|
|
f07ddd93e5 | ||
|
|
08719d4b3a | ||
|
|
16da56eb34 | ||
|
|
6b6e8e85d8 | ||
|
|
6ecbeaa0d2 | ||
|
|
3ac647a16f | ||
|
|
6f06801296 | ||
|
|
dd7056cb6d | ||
|
|
e62d763294 | ||
|
|
4990c3a180 | ||
|
|
4b49bc94ce | ||
|
|
9afa90d9a9 | ||
|
|
065f9a94bc | ||
|
|
223e328161 | ||
|
|
18eddf75c3 | ||
|
|
044b035847 | ||
|
|
b8fd9d0082 | ||
|
|
5b62cf13ff | ||
|
|
a24db2cce2 | ||
|
|
67e59888a2 | ||
|
|
e9c27ab376 | ||
|
|
fb01575394 | ||
|
|
3e7a0b13d1 | ||
|
|
328e7d8944 | ||
|
|
2a6af29235 | ||
|
|
af6d3c63bd | ||
|
|
c3ea0249c9 | ||
|
|
b044b7db4d | ||
|
|
115f8b660b | ||
|
|
f63d21fd13 | ||
|
|
c8ae720caf | ||
|
|
4d451468fd | ||
|
|
a03a01531a | ||
|
|
6dcd82a72f | ||
|
|
fa173fa51a | ||
|
|
614e8359b9 | ||
|
|
437ce10ee3 | ||
|
|
cfec288fe8 | ||
|
|
e7f7d348cd | ||
|
|
e699abb1c6 | ||
|
|
d0ea807884 | ||
|
|
0d053a000d | ||
|
|
e4186e072a | ||
|
|
4c64ad3d8a | ||
|
|
3e6c0a51bd | ||
|
|
3b8b0a681c | ||
|
|
dedb044f67 | ||
|
|
d00501eabd | ||
|
|
20d90bfb0b | ||
|
|
3fe4a767e6 | ||
|
|
43f3f5e80b | ||
|
|
f4201cf318 | ||
|
|
770cb9c4e5 | ||
|
|
37f1f4c8ca | ||
|
|
193a3823b5 | ||
|
|
aa5e80bc8d | ||
|
|
fc881e2631 | ||
|
|
cc2ed79e57 | ||
|
|
77d142d494 | ||
|
|
dae88d7c5c | ||
|
|
08d93f3eb5 | ||
|
|
bf26eba0d2 | ||
|
|
936db93445 | ||
|
|
e0801befe5 | ||
|
|
ee3aa03da1 | ||
|
|
578a442ef7 | ||
|
|
dfa37a298f | ||
|
|
bde6ba1c52 | ||
|
|
52ecc89702 | ||
|
|
af3b7dc443 | ||
|
|
2713f2e2a4 | ||
|
|
cb61bbf720 | ||
|
|
d0e2cf41a3 | ||
|
|
b90bee0f22 | ||
|
|
68b262914d | ||
|
|
c365e2a774 | ||
|
|
df466db7b9 | ||
|
|
1acb18091a | ||
|
|
fefd616d73 | ||
|
|
44c3e321b5 | ||
|
|
f2f6ad3b1f | ||
|
|
cf411b3187 | ||
|
|
1aeadd1765 | ||
|
|
aa562b5ec3 | ||
|
|
38d11227ee | ||
|
|
e94783f91b | ||
|
|
7bc8f41023 | ||
|
|
49117f33dc | ||
|
|
0e3f23d7a1 | ||
|
|
974a5f7d5d | ||
|
|
899cf528df | ||
|
|
47ff774f5c | ||
|
|
af39376d92 | ||
|
|
0e612ba8a4 | ||
|
|
971614ac8b | ||
|
|
3081b4bd96 | ||
|
|
441fec7498 | ||
|
|
c5e4b91fa4 | ||
|
|
93ca7b176f | ||
|
|
f5d3086c55 | ||
|
|
04d76b638b | ||
|
|
cfe6851d8c | ||
|
|
1967bee236 | ||
|
|
0150639119 | ||
|
|
bdb1163445 | ||
|
|
2867aaa8c8 | ||
|
|
49d47e3ff2 | ||
|
|
1d040ec407 | ||
|
|
c945b53308 | ||
|
|
c233ae709e | ||
|
|
95da4246a2 | ||
|
|
9df5d34bbe | ||
|
|
f6ab6d7b27 | ||
|
|
d1a5f15f0c | ||
|
|
82ff14ed8d | ||
|
|
e41972a574 | ||
|
|
a66abf22a8 | ||
|
|
f41e1d012a | ||
|
|
928179a1f3 | ||
|
|
df9c27c53f | ||
|
|
97e3d6c7ce | ||
|
|
f6a00e94c7 | ||
|
|
2b87f334d0 | ||
|
|
5027a283c1 | ||
|
|
c8563ff93d | ||
|
|
474711c4ea | ||
|
|
58aebaa14f | ||
|
|
3570890be7 | ||
|
|
59761f6f64 | ||
|
|
0b1dbe9369 | ||
|
|
a66b97f60b | ||
|
|
cbd0418704 | ||
|
|
bbd3fd5571 | ||
|
|
5779400f26 | ||
|
|
c400de3cfe | ||
|
|
a418d07460 | ||
|
|
8bae09f81b | ||
|
|
80e36eb63c | ||
|
|
a2c311527b | ||
|
|
62524b531d | ||
|
|
e57881f8fa | ||
|
|
efe33a1e37 | ||
|
|
1246e12827 | ||
|
|
ffcfa66d77 | ||
|
|
5d8ea6fa4e | ||
|
|
cdfdaba95b | ||
|
|
2307f6f85e | ||
|
|
6b4edbd40c | ||
|
|
ae77b5cd83 | ||
|
|
72c2b911bf | ||
|
|
c3ba4bdbe5 | ||
|
|
215321cd28 | ||
|
|
71c1eba1e7 | ||
|
|
c2de224711 | ||
|
|
01af31718e | ||
|
|
1204ea9dcf | ||
|
|
c5deff636b | ||
|
|
46a1c9acf9 | ||
|
|
80f9e48e64 | ||
|
|
0e0f3f2167 | ||
|
|
40f170b508 | ||
|
|
c7fdada0fd | ||
|
|
51bcbebaa8 | ||
|
|
a1893ebc02 | ||
|
|
8ed0362683 | ||
|
|
15f48a0500 | ||
|
|
45cbc66f5b | ||
|
|
06ae87c456 | ||
|
|
7038ea884e | ||
|
|
0112d82755 | ||
|
|
46044bfc51 | ||
|
|
a9ff159c78 | ||
|
|
b843d67a92 | ||
|
|
691734f342 | ||
|
|
ccc7a7af29 | ||
|
|
adb3118d31 | ||
|
|
2eed4be5fc | ||
|
|
6edc093803 | ||
|
|
77d0873abd | ||
|
|
052ce94a34 | ||
|
|
4a3565afb3 | ||
|
|
e748c60eb1 | ||
|
|
f857dda5d8 | ||
|
|
66f740eb45 | ||
|
|
6211384f87 | ||
|
|
e00191798e | ||
|
|
9c47ba299d | ||
|
|
8f16e065c5 | ||
|
|
974c09b22a | ||
|
|
bd958e0830 | ||
|
|
67d888e9be | ||
|
|
d3b197b1b9 | ||
|
|
706d898648 | ||
|
|
ace08f1944 | ||
|
|
280ce71285 | ||
|
|
147343c51b | ||
|
|
dc8c9e382d | ||
|
|
871a3e46d7 | ||
|
|
4df6984233 | ||
|
|
c88d06eede | ||
|
|
0dc886d11b | ||
|
|
fc318a765c | ||
|
|
0990f340db | ||
|
|
4ddd6a19a7 | ||
|
|
7f83a51718 | ||
|
|
95cf86688d | ||
|
|
91fcd6092f | ||
|
|
54cd97ded1 | ||
|
|
633ab108e7 | ||
|
|
472439546a | ||
|
|
cfec741090 | ||
|
|
abe421e6bb | ||
|
|
48c872f66f | ||
|
|
7187006bae | ||
|
|
d329b6514c | ||
|
|
a5390a0685 | ||
|
|
c6204a9f01 | ||
|
|
c578216844 | ||
|
|
38cbc31624 | ||
|
|
6406c7a379 | ||
|
|
3850b5fd25 | ||
|
|
0dbd9be0f1 | ||
|
|
4a2f9b8cd3 | ||
|
|
cb63a968fa | ||
|
|
4b0db65877 | ||
|
|
1a5718dc39 | ||
|
|
3f78a85908 | ||
|
|
9dd0ca7b81 | ||
|
|
045fc74688 | ||
|
|
1f6e07c914 | ||
|
|
8f71e01ae3 | ||
|
|
ccbec8127c | ||
|
|
1081ae092b | ||
|
|
58d72fe20f | ||
|
|
8e48caeb3d | ||
|
|
4a3bbedc32 | ||
|
|
41bfdad91f | ||
|
|
6f5bcb32e5 | ||
|
|
9ce62b9540 | ||
|
|
f66cc8d5c4 | ||
|
|
a0108f3e8e | ||
|
|
ee0a53d494 | ||
|
|
f69466f4c2 | ||
|
|
1f4e836863 | ||
|
|
8ac8c34e9f | ||
|
|
ec7eb25565 | ||
|
|
4e38ed01e5 | ||
|
|
752f66746b | ||
|
|
0361cc58d6 | ||
|
|
b39c3df766 | ||
|
|
b9c6f335ee | ||
|
|
f48b6439f6 | ||
|
|
2c026eb5f8 | ||
|
|
0d2d0e3f6c | ||
|
|
5eadebb797 | ||
|
|
16fa69c0f6 | ||
|
|
b06182cb73 | ||
|
|
0056b1af94 | ||
|
|
6cca292020 | ||
|
|
ab143af146 | ||
|
|
27b22b4724 | ||
|
|
7ed1f78f5f | ||
|
|
6dd85ad5b6 | ||
|
|
99927c5516 | ||
|
|
3de0a0a70e | ||
|
|
e056fc0881 | ||
|
|
355e832337 | ||
|
|
6c48db010f | ||
|
|
8c1a0c47b7 | ||
|
|
d27bf852e6 | ||
|
|
c5173e7cc3 | ||
|
|
9a9973c9ca | ||
|
|
0640856462 | ||
|
|
cb7b602f35 | ||
|
|
44f98e6a13 | ||
|
|
994a19b831 | ||
|
|
56aa2f8607 | ||
|
|
9cf532828d | ||
|
|
a5b00a5244 | ||
|
|
969e2d4fd5 | ||
|
|
f6bdc908d8 | ||
|
|
67a0c734b6 | ||
|
|
86f4bda2d4 | ||
|
|
6b03f1bbe7 | ||
|
|
32ea7e0ba3 | ||
|
|
e9ea5ca98f | ||
|
|
fa79826f0c | ||
|
|
88756c0490 | ||
|
|
dc7c59b23b | ||
|
|
60e84bff92 | ||
|
|
c72ac516a0 | ||
|
|
327c0e177e | ||
|
|
a9dd8e7b1d | ||
|
|
481ecd7964 | ||
|
|
f2ef541c2d | ||
|
|
0a995318e7 | ||
|
|
fb36918d85 | ||
|
|
fcb8048038 | ||
|
|
49d4985828 | ||
|
|
ad440b4f11 | ||
|
|
95b8cd5b03 | ||
|
|
fbd07da05a | ||
|
|
6195bd248b | ||
|
|
43396cf603 | ||
|
|
31614fe826 | ||
|
|
31cadbd810 | ||
|
|
912350ec75 | ||
|
|
65ae3e663f | ||
|
|
f115d6a3b8 | ||
|
|
ee06176ab2 | ||
|
|
da2e548f56 | ||
|
|
d2e19cc78c | ||
|
|
571b1a79bb | ||
|
|
3112e32089 | ||
|
|
102d273be3 | ||
|
|
15b5306f48 | ||
|
|
9f500c242c | ||
|
|
9bca907208 | ||
|
|
890fd4a397 | ||
|
|
30c86811c0 | ||
|
|
5acbee2719 | ||
|
|
35a4fefd2e | ||
|
|
f0d175c5b2 | ||
|
|
9df929963f | ||
|
|
d9e04ec5b6 | ||
|
|
9e78cfc48d | ||
|
|
0409c9bfb2 | ||
|
|
d75a96ae59 | ||
|
|
e5c01e4ff8 | ||
|
|
98cdaf6a39 | ||
|
|
67fbfa12ea | ||
|
|
9bffd5b3ca | ||
|
|
51e8a2d1e5 | ||
|
|
76c30539cc | ||
|
|
46a43b7b0d | ||
|
|
6a2c974581 | ||
|
|
a857869522 | ||
|
|
9835285f0c | ||
|
|
c4d9a9c5f9 | ||
|
|
dcaf5fa23a | ||
|
|
2784c614d4 | ||
|
|
bef37079dc | ||
|
|
a9bce1d6b2 | ||
|
|
22d5fd3aed | ||
|
|
2219b5919b | ||
|
|
8168e6f52a | ||
|
|
b2e0a77ae0 | ||
|
|
2db11cd4db | ||
|
|
03d1f0af72 | ||
|
|
702a918271 | ||
|
|
76cb83643d | ||
|
|
400bbd7fbf | ||
|
|
837b7c3037 | ||
|
|
f7b51414b8 | ||
|
|
83ac581f3d | ||
|
|
970e7a45a9 | ||
|
|
519c21c8b0 | ||
|
|
3406acd0aa | ||
|
|
ac8b1b098a | ||
|
|
5ee4a3fc12 | ||
|
|
5fc222229f | ||
|
|
e80ded6ebe | ||
|
|
45d7503e24 | ||
|
|
de13c7de86 | ||
|
|
9a6215c3be | ||
|
|
ef7e0c7d08 | ||
|
|
bc07b5196b | ||
|
|
1ff945de69 | ||
|
|
210bb41ba1 | ||
|
|
89f359e4f8 | ||
|
|
a18e9f7a25 | ||
|
|
2df27fc6e7 | ||
|
|
4c6da0e6f9 | ||
|
|
4fbe651e57 | ||
|
|
8b9c3cbf47 | ||
|
|
964cb540fb | ||
|
|
2876b27ec5 | ||
|
|
407e7fb8d6 | ||
|
|
30c31db095 | ||
|
|
231c3de79e | ||
|
|
d0223c2d08 | ||
|
|
25596c9578 | ||
|
|
e4ef254d8f | ||
|
|
e4ebd8c443 | ||
|
|
f8c37b1e05 | ||
|
|
e150039261 | ||
|
|
8374382947 | ||
|
|
2b0a9cc7c8 | ||
|
|
42ad66b032 | ||
|
|
09b5562ad9 | ||
|
|
b0ce7f3bd9 | ||
|
|
1bde00ed4c | ||
|
|
dca3726bf1 | ||
|
|
01a2fad7fb | ||
|
|
68e01afd7e | ||
|
|
441a00afc9 | ||
|
|
b5b4961a9d | ||
|
|
fd56452f6b | ||
|
|
8d9783d574 | ||
|
|
e899a2970d | ||
|
|
fac377fc13 | ||
|
|
ab08db4ad6 | ||
|
|
b557180bca | ||
|
|
d75ad1f0c0 | ||
|
|
df68a7b593 | ||
|
|
3d3325aaca | ||
|
|
d613203eec | ||
|
|
4a5809910b | ||
|
|
a859e4f547 | ||
|
|
5fc0c4e1df | ||
|
|
6c5e25dcb9 | ||
|
|
5b56b57074 | ||
|
|
03192b0162 | ||
|
|
7559b574dd | ||
|
|
03bb6d08be |
43
.github/workflows/codeql-analysis.yml
vendored
Normal file
43
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master ]
|
||||
schedule:
|
||||
- cron: '18 18 * * 1'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'java' ]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
- run: "mvn clean compile -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true"
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
228
.github/workflows/pipeline.yml
vendored
Normal file
228
.github/workflows/pipeline.yml
vendored
Normal file
@@ -0,0 +1,228 @@
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://docs.github.com/en/actions/learn-github-actions or https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
|
||||
|
||||
name: Java CI with Maven
|
||||
|
||||
on:
|
||||
push:
|
||||
# branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
# old-school build and jar method. No tests run or compiled.
|
||||
build-1_6:
|
||||
name: Java 1.6
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup java
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.6
|
||||
- name: Compile Java 1.6
|
||||
run: |
|
||||
mkdir -p target/classes
|
||||
javac -version
|
||||
javac -source 1.6 -target 1.6 -d target/classes/ src/main/java/org/json/*.java
|
||||
- name: Create java 1.6 JAR
|
||||
run: |
|
||||
jar cvf target/org.json.jar -C target/classes .
|
||||
- name: Upload JAR 1.6
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Create java 1.6 JAR
|
||||
path: target/*.jar
|
||||
|
||||
build-8:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
# build against supported Java LTS versions:
|
||||
java: [ 8 ]
|
||||
name: Java ${{ matrix.java }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up JDK ${{ matrix.java }}
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: ${{ matrix.java }}
|
||||
cache: 'maven'
|
||||
- name: Compile Java ${{ matrix.java }}
|
||||
run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true
|
||||
- name: Run Tests ${{ matrix.java }}
|
||||
run: |
|
||||
mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Build Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
run: |
|
||||
mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Upload Test Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Results ${{ matrix.java }}
|
||||
path: target/surefire-reports/
|
||||
- name: Upload Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Report ${{ matrix.java }}
|
||||
path: target/site/
|
||||
- name: Package Jar ${{ matrix.java }}
|
||||
run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true
|
||||
- name: Upload Package Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Package Jar ${{ matrix.java }}
|
||||
path: target/*.jar
|
||||
|
||||
build-11:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
# build against supported Java LTS versions:
|
||||
java: [ 11 ]
|
||||
name: Java ${{ matrix.java }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up JDK ${{ matrix.java }}
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: ${{ matrix.java }}
|
||||
cache: 'maven'
|
||||
- name: Compile Java ${{ matrix.java }}
|
||||
run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true
|
||||
- name: Run Tests ${{ matrix.java }}
|
||||
run: |
|
||||
mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Build Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
run: |
|
||||
mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Upload Test Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Results ${{ matrix.java }}
|
||||
path: target/surefire-reports/
|
||||
- name: Upload Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Report ${{ matrix.java }}
|
||||
path: target/site/
|
||||
- name: Package Jar ${{ matrix.java }}
|
||||
run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true
|
||||
- name: Upload Package Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Package Jar ${{ matrix.java }}
|
||||
path: target/*.jar
|
||||
|
||||
build-17:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
# build against supported Java LTS versions:
|
||||
java: [ 17 ]
|
||||
name: Java ${{ matrix.java }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up JDK ${{ matrix.java }}
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: ${{ matrix.java }}
|
||||
cache: 'maven'
|
||||
- name: Compile Java ${{ matrix.java }}
|
||||
run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true
|
||||
- name: Run Tests ${{ matrix.java }}
|
||||
run: |
|
||||
mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Build Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
run: |
|
||||
mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Upload Test Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Results ${{ matrix.java }}
|
||||
path: target/surefire-reports/
|
||||
- name: Upload Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Report ${{ matrix.java }}
|
||||
path: target/site/
|
||||
- name: Package Jar ${{ matrix.java }}
|
||||
run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true
|
||||
- name: Upload Package Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Package Jar ${{ matrix.java }}
|
||||
path: target/*.jar
|
||||
|
||||
build-21:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
# build against supported Java LTS versions:
|
||||
java: [ 21 ]
|
||||
name: Java ${{ matrix.java }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up JDK ${{ matrix.java }}
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: ${{ matrix.java }}
|
||||
cache: 'maven'
|
||||
- name: Compile Java ${{ matrix.java }}
|
||||
run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true
|
||||
- name: Run Tests ${{ matrix.java }}
|
||||
run: |
|
||||
mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Build Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
run: |
|
||||
mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }}
|
||||
- name: Upload Test Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Results ${{ matrix.java }}
|
||||
path: target/surefire-reports/
|
||||
- name: Upload Test Report ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Test Report ${{ matrix.java }}
|
||||
path: target/site/
|
||||
- name: Package Jar ${{ matrix.java }}
|
||||
run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true
|
||||
- name: Upload Package Results ${{ matrix.java }}
|
||||
if: ${{ always() }}
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: Package Jar ${{ matrix.java }}
|
||||
path: target/*.jar
|
||||
12
.gitignore
vendored
12
.gitignore
vendored
@@ -1,6 +1,18 @@
|
||||
# ignore eclipse project files
|
||||
.project
|
||||
.classpath
|
||||
# ignore vscode files
|
||||
.vscode
|
||||
# ignore Intellij Idea project files
|
||||
.idea
|
||||
*.iml
|
||||
/target/
|
||||
|
||||
/bin/
|
||||
build
|
||||
.settings/
|
||||
/.gradle/
|
||||
/gradle/
|
||||
/gradlew
|
||||
/gradlew.bat
|
||||
.gitmodules
|
||||
|
||||
76
CODE_OF_CONDUCT.md
Normal file
76
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
||||
level of experience, education, socio-economic status, nationality, personal
|
||||
appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at jsonjava060@gmail.com. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see
|
||||
https://www.contributor-covenant.org/faq
|
||||
22
CONTRIBUTING.md
Normal file
22
CONTRIBUTING.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Contribution Guidelines
|
||||
|
||||
Feel free to work on any open issue, you don't need to ask permission first. This year, the hacktoberfest label will be added to any PR and associated issue during the month of October.
|
||||
|
||||
If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, it will be available to work on (if not, it will be closed after review).
|
||||
|
||||
# Who is allowed to submit pull requests for this project?
|
||||
|
||||
Anyone can submit pull requests for code, tests, or documentation.
|
||||
|
||||
# How do you decide which pull requests to accept?
|
||||
|
||||
* Does it call out a bug that needs to be fixed? If so, it goes to the top of the list.
|
||||
* Does it fix a major user inconvenience? These are given high priority as well.
|
||||
* Does it align with the specs? If not, it will probably not be accepted. It turns out there are gray areas in the specs. If this is in a gray area, it will likely be given the benefit of the doubt.
|
||||
* Does it break the existing behavior of the lib? If so, it will not be accepted, unless it fixes an egregious bug. This is happening less frequently now.
|
||||
|
||||
# For more guidance, see these links:
|
||||
|
||||
[README.md (includes build instructions)](https://github.com/stleary/JSON-java#readme)
|
||||
|
||||
[FAQ - all your questions answered](https://github.com/stleary/JSON-java/wiki/FAQ)
|
||||
169
Cookie.java
169
Cookie.java
@@ -1,169 +0,0 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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 specification to a JSONObject and back.
|
||||
* JSON and Cookies are both notations for name/value pairs.
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class Cookie {
|
||||
|
||||
/**
|
||||
* Produce a copy of a string in which the characters '+', '%', '=', ';'
|
||||
* and control characters are replaced with "%hh". This is a gentle form
|
||||
* of URL encoding, attempting to cause as little distortion to the
|
||||
* string as possible. The characters '=' and ';' are meta characters in
|
||||
* cookies. By convention, they are escaped using the URL-encoding. This is
|
||||
* only a convention, not a standard. Often, cookies are expected to have
|
||||
* encoded values. We encode '=' and ';' because we must. We encode '%' and
|
||||
* '+' because they are meta characters in URL encoding.
|
||||
* @param string The source string.
|
||||
* @return The escaped result.
|
||||
*/
|
||||
public static String escape(String string) {
|
||||
char c;
|
||||
String s = string.trim();
|
||||
int length = s.length();
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
c = s.charAt(i);
|
||||
if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
|
||||
sb.append('%');
|
||||
sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
|
||||
sb.append(Character.forDigit((char)(c & 0x0f), 16));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a cookie specification string into a JSONObject. The string
|
||||
* will contain a name value pair separated by '='. The name and the value
|
||||
* will be unescaped, possibly converting '+' and '%' sequences. The
|
||||
* cookie properties may follow, separated by ';', also represented as
|
||||
* name=value (except the secure property, which does not have a value).
|
||||
* The name will be stored under the key "name", and the value will be
|
||||
* stored under the key "value". This method does not do checking or
|
||||
* validation of the parameters. It only converts the cookie string into
|
||||
* a JSONObject.
|
||||
* @param string The cookie specification string.
|
||||
* @return A JSONObject containing "name", "value", and possibly other
|
||||
* members.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
String name;
|
||||
JSONObject jo = new JSONObject();
|
||||
Object value;
|
||||
JSONTokener x = new JSONTokener(string);
|
||||
jo.put("name", x.nextTo('='));
|
||||
x.next('=');
|
||||
jo.put("value", x.nextTo(';'));
|
||||
x.next();
|
||||
while (x.more()) {
|
||||
name = unescape(x.nextTo("=;"));
|
||||
if (x.next() != '=') {
|
||||
if (name.equals("secure")) {
|
||||
value = Boolean.TRUE;
|
||||
} else {
|
||||
throw x.syntaxError("Missing '=' in cookie parameter.");
|
||||
}
|
||||
} else {
|
||||
value = unescape(x.nextTo(';'));
|
||||
x.next();
|
||||
}
|
||||
jo.put(name, value);
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a cookie specification string. The JSONObject
|
||||
* must contain "name" and "value" members.
|
||||
* If the JSONObject contains "expires", "domain", "path", or "secure"
|
||||
* members, they will be appended to the cookie specification string.
|
||||
* All other members are ignored.
|
||||
* @param jo A JSONObject
|
||||
* @return A cookie specification string
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(escape(jo.getString("name")));
|
||||
sb.append("=");
|
||||
sb.append(escape(jo.getString("value")));
|
||||
if (jo.has("expires")) {
|
||||
sb.append(";expires=");
|
||||
sb.append(jo.getString("expires"));
|
||||
}
|
||||
if (jo.has("domain")) {
|
||||
sb.append(";domain=");
|
||||
sb.append(escape(jo.getString("domain")));
|
||||
}
|
||||
if (jo.has("path")) {
|
||||
sb.append(";path=");
|
||||
sb.append(escape(jo.getString("path")));
|
||||
}
|
||||
if (jo.optBoolean("secure")) {
|
||||
sb.append(";secure");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert <code>%</code><i>hh</i> sequences to single characters, and
|
||||
* convert plus to space.
|
||||
* @param string A string that may contain
|
||||
* <code>+</code> <small>(plus)</small> and
|
||||
* <code>%</code><i>hh</i> sequences.
|
||||
* @return The unescaped string.
|
||||
*/
|
||||
public static String unescape(String string) {
|
||||
int length = string.length();
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for (int i = 0; i < length; ++i) {
|
||||
char c = string.charAt(i);
|
||||
if (c == '+') {
|
||||
c = ' ';
|
||||
} else if (c == '%' && i + 2 < length) {
|
||||
int d = JSONTokener.dehexchar(string.charAt(i + 1));
|
||||
int e = JSONTokener.dehexchar(string.charAt(i + 2));
|
||||
if (d >= 0 && e >= 0) {
|
||||
c = (char)(d * 16 + e);
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
433
Examples.md
Normal file
433
Examples.md
Normal file
@@ -0,0 +1,433 @@
|
||||
<h1>Examples</h1>
|
||||
<p>Imports used in the examples: </p>
|
||||
|
||||
```java
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
```
|
||||
|
||||
<p>This document's intention is to explain to new-comers the basics of this project</p>
|
||||
|
||||
|
||||
<h2>Part 1: Creating a JSON document</h2>
|
||||
|
||||
<h3>Using JSONArray</h3>
|
||||
|
||||
```java
|
||||
private static void JSONExampleArray1() {
|
||||
//We create a JSONObject from a String containing an array using JSONArray
|
||||
//Firstly, we declare an Array in a String
|
||||
|
||||
String arrayStr =
|
||||
"["+"true,"+"false,"+ "\"true\","+ "\"false\","+"\"hello\","+"23.45e-4,"+
|
||||
"\"23.45\","+"42,"+"\"43\","+"["+"\"world\""+"],"+
|
||||
"{"+
|
||||
"\"key1\":\"value1\","+
|
||||
"\"key2\":\"value2\","+
|
||||
"\"key3\":\"value3\","+
|
||||
"\"key4\":\"value4\""+
|
||||
"},"+
|
||||
"0,"+"\"-1\""+
|
||||
"]";
|
||||
|
||||
//Then, we initializate the JSONArray thanks to its constructor
|
||||
|
||||
JSONArray array = new JSONArray(arrayStr);
|
||||
System.out.println("Values array: "+ array);
|
||||
|
||||
//We convert that array into a JSONObject, but first, we need the labels, so we need another JSONArray with the labels.
|
||||
//Here we will use an auxiliary function to get one for the example.
|
||||
|
||||
JSONArray list = listNumberArray(array.length());
|
||||
System.out.println("Label Array: "+ list.toString());
|
||||
//Now, we construct the JSONObject using both the value array and the label array.
|
||||
JSONObject object = array.toJSONObject(list);
|
||||
System.out.println("Final JSONOBject: " + object);
|
||||
}
|
||||
|
||||
//This method creates an JSONArray of labels in which those are generated by their positions
|
||||
|
||||
private static JSONArray listNumberArray(int max){
|
||||
JSONArray res = new JSONArray();
|
||||
for (int i=0; i<max;i++) {
|
||||
//The value of the labels must be an String in order to make it work
|
||||
res.put(String.valueOf(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void JSONExampleArray2() {
|
||||
|
||||
//We can also create an Array without a String by creating an empty array and adding elements to it
|
||||
|
||||
JSONArray array = new JSONArray();
|
||||
|
||||
//Adding elements with .put()
|
||||
|
||||
array.put("value");
|
||||
array.put(5);
|
||||
array.put(-23.45e67);
|
||||
array.put(true);
|
||||
|
||||
//We convert it to JSONObject providing a label arrray like last time
|
||||
|
||||
JSONArray list = listNumberArray(array.length());
|
||||
JSONObject object = array.toJSONObject(list);
|
||||
System.out.println("Final JSONOBject: " + object);
|
||||
}
|
||||
```
|
||||
|
||||
<h3>Using JSONStringer</h3>
|
||||
|
||||
```java
|
||||
private static void JSONExampleStringer() {
|
||||
|
||||
//We initializate the JSONStringer
|
||||
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
|
||||
//Now we start the process of adding elements with .object()
|
||||
|
||||
jsonStringer.object();
|
||||
|
||||
//We can now add elements as keys and values with .values () and .key()
|
||||
|
||||
jsonStringer.key("trueValue").value(true);
|
||||
jsonStringer.key("falseValue").value(false);
|
||||
jsonStringer.key("nullValue").value(null);
|
||||
jsonStringer.key("stringValue").value("hello world!");
|
||||
jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!");
|
||||
jsonStringer.key("intValue").value(42);
|
||||
jsonStringer.key("doubleValue").value(-23.45e67);
|
||||
|
||||
//We end this prcedure with .ednObject
|
||||
|
||||
jsonStringer.endObject();
|
||||
|
||||
//Once we have a JSONStringer, we convert it to JSONObject generating a String and using JSONObject's contructor.
|
||||
|
||||
String str = jsonStringer.toString();
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
|
||||
System.out.println("Final JSONOBject: " + jsonObject);
|
||||
}
|
||||
```
|
||||
<h3>Using JSONObject</h3>
|
||||
|
||||
```java
|
||||
private static void JSONExampleObject1() {
|
||||
|
||||
//We can create a JSONObject from a String with the class builder
|
||||
|
||||
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
|
||||
JSONObject example = new JSONObject(string);
|
||||
System.out.println("Final JSONObject: " + example);
|
||||
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void JSONExampleObject2() {
|
||||
|
||||
//We can also create a JSONObject directly without messing around with any of the other functions.
|
||||
|
||||
JSONObject example = new JSONObject();
|
||||
|
||||
|
||||
//Now we add the keys and values in a similar way as the Stringer method
|
||||
example.put("key", "value");
|
||||
|
||||
//As you can see, the first entry is the key and the second would be its associeted value.
|
||||
|
||||
example.put("key2", 5);
|
||||
example.put("key3", -23.45e67);
|
||||
example.put("trueValue", true);
|
||||
|
||||
//We can't add null values thougth
|
||||
|
||||
//example.put("nullValue", null); //This is not possible
|
||||
|
||||
System.out.println("Final JSONOBject: " + example);
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void JSONExampleObject3() {
|
||||
|
||||
//We can also create a JSONObject with a Java Map
|
||||
//YoU will need a Map whose keys are Strings. The values can be whatever you want
|
||||
|
||||
Map<String,Double> map = new HashMap<String, Double>();
|
||||
|
||||
map.put("key1", 1.0);
|
||||
map.put("key2", -23.45e67);
|
||||
|
||||
//We create the JSONObject with the map with its class builder
|
||||
|
||||
JSONObject example = new JSONObject(map);
|
||||
System.out.println("Final JSONOBject: " + example);
|
||||
}
|
||||
```
|
||||
<h3>Using JSONWriter</h3>
|
||||
|
||||
```java
|
||||
private static void JSONExamplWriter() {
|
||||
|
||||
//This method works in a very similar way to Object and Stringer in the construction of the JSON.
|
||||
//The difference is that it needs a Java object called "Appendable" like StringBuilder
|
||||
|
||||
StringBuilder write = new StringBuilder();
|
||||
JSONWriter jsonWriter = new JSONWriter(write);
|
||||
|
||||
//We behave now the same way as Stringer
|
||||
|
||||
jsonWriter.object();
|
||||
|
||||
jsonWriter.key("trueValue").value(true);
|
||||
jsonWriter.key("falseValue").value(false);
|
||||
jsonWriter.key("nullValue").value(null);
|
||||
jsonWriter.key("stringValue").value("hello world!");
|
||||
jsonWriter.key("complexStringValue").value("h\be\tllo w\u1234orld!");
|
||||
jsonWriter.key("intValue").value(42);
|
||||
jsonWriter.key("doubleValue").value(-23.45e67);
|
||||
|
||||
jsonWriter.endObject();
|
||||
|
||||
//The resoult should be in the "write" object
|
||||
|
||||
System.out.println("JSON: " + write.toString());
|
||||
|
||||
//The difference is that we don't get a JSONObject in this one.
|
||||
|
||||
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void JSONExampleTokener() {
|
||||
|
||||
//A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject
|
||||
|
||||
String string = "this is not a valid JSON string";
|
||||
JSONTokener token = new JSONTokener(string);
|
||||
|
||||
//Now you can use the token in JSONObject and Array the same way as a String
|
||||
|
||||
JSONObject object = new JSONObject(token);
|
||||
JSONArray array = new JSONArray(token);
|
||||
|
||||
}
|
||||
```
|
||||
<h2>Part 2: Conversion methods</h2>
|
||||
<p>We don't need to have a JSON document to work. This project also admits conversions from other type of files.</p>
|
||||
<p>Secondly, we can also convert from JSON to those type of files.</p>
|
||||
|
||||
<h3>Extra: Conversion to JSONArray</h3>
|
||||
|
||||
```java
|
||||
private static void JSONObjectToArray() {
|
||||
//We start with a JSONObject
|
||||
|
||||
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
|
||||
|
||||
JSONObject example = new JSONObject(string);
|
||||
|
||||
//We need a list of key strings like the reverse operation
|
||||
|
||||
JSONArray keyStrings = listNumberArray(example.length());
|
||||
|
||||
//Then we convert to the Array using both elelements
|
||||
|
||||
JSONArray array = example.toJSONArray(keyStrings);
|
||||
|
||||
System.out.println("Final JSONArray: " + array);
|
||||
}
|
||||
```
|
||||
<h3>XML Conversions</h3>
|
||||
|
||||
```java
|
||||
private static void XMLToExampleConversion() {
|
||||
|
||||
//We start with a JSONObject
|
||||
|
||||
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
|
||||
JSONObject example = new JSONObject(string);
|
||||
|
||||
//We obtain a String with XML format with toString()
|
||||
|
||||
String output = XML.toString(example);
|
||||
System.out.println("Final XML: " + output);
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void XMLFromExampleConversion() {
|
||||
|
||||
//We start with a string with the XML format
|
||||
|
||||
String string = "<0>value</0><1>5</1><2>-2.345E+68</2><3>true</3>";
|
||||
|
||||
//We obtain a JSONObject with toJSONOBject()
|
||||
|
||||
JSONObject output = XML.toJSONObject(string);
|
||||
|
||||
System.out.println("Final JSONObject: " + output);
|
||||
}
|
||||
```
|
||||
<h3>Cookie Conversions</h3>
|
||||
|
||||
```java
|
||||
private static void CookieToExampleConversion() {
|
||||
|
||||
//We start with a JSONObject
|
||||
//The JSONOBject needs to entries that gives the cookie a name and gives the field "name" a name too.
|
||||
//The Cokkie format doesn't support booleans
|
||||
|
||||
String string = "{\"name\":\"Cookie-Name\",\"value\":\"name\",\"1\":5,\"2\":-2.345E68,\"3\":'true'}";
|
||||
JSONObject example = new JSONObject(string);
|
||||
|
||||
//We obtain a String with Cookie format with toString()
|
||||
|
||||
String output = Cookie.toString(example);
|
||||
System.out.println("Final Cookie: " + output);
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void CookieFromExampleConversion() {
|
||||
|
||||
//We start with a string with the Cookie format
|
||||
|
||||
String string = "Cookie-Name=name;1=5;2=-2.345E%2b68;3=true";
|
||||
|
||||
//We obtain a JSONObject with toJSONOBject()
|
||||
|
||||
JSONObject output = Cookie.toJSONObject(string);
|
||||
System.out.println("Final JSONObject: " + output);
|
||||
}
|
||||
```
|
||||
|
||||
<h3>HTTP Conversions</h3>
|
||||
|
||||
```java
|
||||
private static void HTTPToExampleConversion() {
|
||||
|
||||
//We start with a JSONObject
|
||||
//The JSONObject must have the minimun header for a HTTP request or header
|
||||
|
||||
String string = "{\"Method\":\"POST\",\"Request-URI\":'/',\"HTTP-Version\":'HTTP/1.1',\"Value1\":true,\"Value2\":2,\"Value3\":-2.345E68}";
|
||||
|
||||
JSONObject example = new JSONObject(string);
|
||||
|
||||
//We obtain a String with HTTP format with toString()
|
||||
|
||||
String output = HTTP.toString(example);
|
||||
System.out.println("Final HTTP: " + output);
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void HTTPFromExampleConversion() {
|
||||
|
||||
//We start with a string with the HTTP format
|
||||
|
||||
String string = "Final HTTP: POST '/' HTTP/1.1 Value3: -2.345E+68 Value1: true Value2: 2";
|
||||
|
||||
//We obtain a JSONObject with toJSONOBject()
|
||||
|
||||
JSONObject output = HTTP.toJSONObject(string);
|
||||
System.out.println("Final JSONObject: " + output);
|
||||
}
|
||||
```
|
||||
<h3>CDL Conversions</h3>
|
||||
|
||||
```java
|
||||
private static void CDLToExampleConversion() {
|
||||
|
||||
//We start with some JSONObjects with the same values in the keys but different values in the "values"
|
||||
|
||||
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
|
||||
JSONObject example = new JSONObject(string);
|
||||
|
||||
String string2 = "{\"0\":\"value2\",\"1\":6,\"2\":-8.345E68,\"3\":false}";
|
||||
JSONObject example2 = new JSONObject(string2);
|
||||
|
||||
//We need now a JSONArray with those JSONObjects
|
||||
|
||||
JSONArray array = new JSONArray();
|
||||
array.put(example);
|
||||
array.put(example2);
|
||||
|
||||
//We obtain a String with XML format with toString()
|
||||
|
||||
String output = CDL.toString(array);
|
||||
System.out.println("Final CDL: \r\n" + output);
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void CDLFromExampleConversion() {
|
||||
|
||||
//We start wtih a String with the CDL format
|
||||
|
||||
String string = "0,1,2,3\n"
|
||||
+ "value,5,-2.345E+68,true\n"
|
||||
+ "value2,6,-8.345E+68,false";
|
||||
|
||||
//We obtain a JSONArray with toJSONOBject()
|
||||
|
||||
JSONArray output = CDL.toJSONArray(string);
|
||||
System.out.println("Final JSONArray: " + output);
|
||||
}
|
||||
```
|
||||
<h3>Properties Conversions</h3>
|
||||
|
||||
```java
|
||||
private static Properties PropertyToExampleConversion() {
|
||||
|
||||
//We start with a JSONObject
|
||||
|
||||
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
|
||||
JSONObject example = new JSONObject(string);
|
||||
|
||||
//We obtain a String with Properties format with toString()
|
||||
|
||||
Properties output = Property.toProperties(example);
|
||||
System.out.println("Final Properties: " + output);
|
||||
|
||||
return output;
|
||||
}
|
||||
```
|
||||
```java
|
||||
private static void PropertyFromExampleConversion() {
|
||||
|
||||
//We start with a Properties object
|
||||
|
||||
Properties input = PropertyToExampleConversion();
|
||||
|
||||
//We obtain a JSONObject with toJSONOBject()
|
||||
|
||||
JSONObject output = Property.toJSONObject(input);
|
||||
System.out.println("Final JSONObject: " + output);
|
||||
}
|
||||
```
|
||||
<h2>List of all examples methods</h2>
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
//JSONObjectToArray();
|
||||
//JSONExampleArray1();
|
||||
//JSONExampleArray2();
|
||||
//JSONExampleStringer();
|
||||
//JSONExampleObject1();
|
||||
//JSONExampleObject2();
|
||||
//JSONExampleObject3();
|
||||
//JSONExamplWriter();
|
||||
//XMLToExampleConversion();
|
||||
//XMLFromExampleConversion();
|
||||
//CookieToExampleConversion();
|
||||
//CookieFromExampleConversion();
|
||||
//HTTPToExampleConversion();
|
||||
//HTTPFromExampleConversion();
|
||||
//CDLToExampleConversion();
|
||||
//CDLFromExampleConversion();
|
||||
//PropertyToExampleConversion();
|
||||
//PropertyFromExampleConversion();
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The JSONPointerException is thrown by {@link JSONPointer} if an error occurs
|
||||
* during evaluating a pointer.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-13
|
||||
*/
|
||||
public class JSONPointerException extends JSONException {
|
||||
private static final long serialVersionUID = 8872944667561856751L;
|
||||
|
||||
public JSONPointerException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JSONPointerException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2018 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Documented
|
||||
@Retention(RUNTIME)
|
||||
@Target({METHOD})
|
||||
/**
|
||||
* Use this annotation on a getter method to override the Bean name
|
||||
* parser for Bean -> JSONObject mapping. If this annotation is
|
||||
* present at any level in the class hierarchy, then the method will
|
||||
* not be serialized from the bean into the JSONObject.
|
||||
*/
|
||||
public @interface JSONPropertyIgnore { }
|
||||
@@ -1,47 +0,0 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2018 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Documented
|
||||
@Retention(RUNTIME)
|
||||
@Target({METHOD})
|
||||
/**
|
||||
* Use this annotation on a getter method to override the Bean name
|
||||
* parser for Bean -> JSONObject mapping. A value set to empty string <code>""</code>
|
||||
* will have the Bean parser fall back to the default field name processing.
|
||||
*/
|
||||
public @interface JSONPropertyName {
|
||||
/**
|
||||
* @return The name of the property as to be used in the JSON Object.
|
||||
*/
|
||||
String value();
|
||||
}
|
||||
23
LICENSE
23
LICENSE
@@ -1,23 +1,2 @@
|
||||
============================================================================
|
||||
Public Domain.
|
||||
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
|
||||
190
README.md
190
README.md
@@ -1,138 +1,122 @@
|
||||

|
||||
|
||||
<sub><sup>image credit: Ismael Pérez Ortiz</sup></sub>
|
||||
|
||||
|
||||
JSON in Java [package org.json]
|
||||
===============================
|
||||
|
||||
[](https://mvnrepository.com/artifact/org.json/json)
|
||||
[](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml)
|
||||
[](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml)
|
||||
|
||||
**[Click here if you just want the latest release jar file.](http://central.maven.org/maven2/org/json/json/20180813/json-20180813.jar)**
|
||||
|
||||
JSON is a light-weight, language independent, data interchange format.
|
||||
See http://www.JSON.org/
|
||||
|
||||
The files in this package implement JSON encoders/decoders in Java.
|
||||
It also includes the capability to convert between JSON and XML, HTTP
|
||||
headers, Cookies, and CDL.
|
||||
|
||||
This is a reference implementation. There is a large number of JSON packages
|
||||
in Java. Perhaps someday the Java community will standardize on one. Until
|
||||
then, choose carefully.
|
||||
|
||||
The license includes this restriction: "The software shall be used for good,
|
||||
not evil." If your conscience cannot live with that, then choose a different
|
||||
package.
|
||||
|
||||
The package compiles on Java 1.6-1.8.
|
||||
**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20250517/json-20250517.jar)**
|
||||
|
||||
|
||||
**JSONObject.java**: The `JSONObject` can parse text from a `String` or a `JSONTokener`
|
||||
to produce a map-like object. The object provides methods for manipulating its
|
||||
contents, and for producing a JSON compliant object serialization.
|
||||
# Overview
|
||||
|
||||
**JSONArray.java**: The `JSONArray` can parse text from a String or a `JSONTokener`
|
||||
to produce a vector-like object. The object provides methods for manipulating
|
||||
its contents, and for producing a JSON compliant array serialization.
|
||||
[JSON](http://www.JSON.org/) is a light-weight language-independent data interchange format.
|
||||
|
||||
**JSONTokener.java**: The `JSONTokener` breaks a text into a sequence of individual
|
||||
tokens. It can be constructed from a `String`, `Reader`, or `InputStream`.
|
||||
The JSON-Java package is a reference implementation that demonstrates how to parse JSON documents into Java objects and how to generate new JSON documents from the Java classes.
|
||||
|
||||
**JSONException.java**: The `JSONException` is the standard exception type thrown
|
||||
by this package.
|
||||
|
||||
**JSONPointer.java**: Implementation of
|
||||
[JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901). Supports
|
||||
JSON Pointers both in the form of string representation and URI fragment
|
||||
representation.
|
||||
|
||||
**JSONPropertyIgnore.java**: Annotation class that can be used on Java Bean getter methods.
|
||||
When used on a bean method that would normally be serialized into a `JSONObject`, it
|
||||
overrides the getter-to-key-name logic and forces the property to be excluded from the
|
||||
resulting `JSONObject`.
|
||||
|
||||
**JSONPropertyName.java**: Annotation class that can be used on Java Bean getter methods.
|
||||
When used on a bean method that would normally be serialized into a `JSONObject`, it
|
||||
overrides the getter-to-key-name logic and uses the value of the annotation. The Bean
|
||||
processor will look through the class hierarchy. This means you can use the annotation on
|
||||
a base class or interface and the value of the annotation will be used even if the getter
|
||||
is overridden in a child class.
|
||||
|
||||
**JSONString.java**: The `JSONString` interface requires a `toJSONString` method,
|
||||
allowing an object to provide its own serialization.
|
||||
|
||||
**JSONStringer.java**: The `JSONStringer` provides a convenient facility for
|
||||
building JSON strings.
|
||||
|
||||
**JSONWriter.java**: The `JSONWriter` provides a convenient facility for building
|
||||
JSON text through a writer.
|
||||
Project goals include:
|
||||
* Reliable and consistent results
|
||||
* Adherence to the JSON specification
|
||||
* Easy to build, use, and include in other projects
|
||||
* No external dependencies
|
||||
* Fast execution and low memory footprint
|
||||
* Maintain backward compatibility
|
||||
* Designed and tested to use on Java versions 1.6 - 21
|
||||
|
||||
|
||||
**CDL.java**: `CDL` provides support for converting between JSON and comma
|
||||
delimited lists.
|
||||
The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL.
|
||||
|
||||
**Cookie.java**: `Cookie` provides support for converting between JSON and cookies.
|
||||
# If you would like to contribute to this project
|
||||
|
||||
**CookieList.java**: `CookieList` provides support for converting between JSON and
|
||||
cookie lists.
|
||||
For more information on contributions, please see [CONTRIBUTING.md](https://github.com/stleary/JSON-java/blob/master/docs/CONTRIBUTING.md)
|
||||
|
||||
**HTTP.java**: `HTTP` provides support for converting between JSON and HTTP headers.
|
||||
Bug fixes, code improvements, and unit test coverage changes are welcome! Because this project is currently in the maintenance phase, the kinds of changes that can be accepted are limited. For more information, please read the [FAQ](https://github.com/stleary/JSON-java/wiki/FAQ).
|
||||
|
||||
**HTTPTokener.java**: `HTTPTokener` extends `JSONTokener` for parsing HTTP headers.
|
||||
# Build Instructions
|
||||
|
||||
**XML.java**: `XML` provides support for converting between JSON and XML.
|
||||
The org.json package can be built from the command line, Maven, and Gradle. The unit tests can be executed from Maven, Gradle, or individually in an IDE e.g. Eclipse.
|
||||
|
||||
**Building from the command line**
|
||||
|
||||
**JSONML.java**: `JSONML` provides support for converting between JSONML and XML.
|
||||
*Build the class files from the package root directory src/main/java*
|
||||
```shell
|
||||
javac org/json/*.java
|
||||
```
|
||||
|
||||
**XMLTokener.java**: `XMLTokener` extends `JSONTokener` for parsing XML text.
|
||||
*Create the jar file in the current directory*
|
||||
```shell
|
||||
jar cf json-java.jar org/json/*.class
|
||||
```
|
||||
|
||||
Unit tests are maintained in a separate project. Contributing developers can test
|
||||
JSON-java pull requests with the code in this project:
|
||||
https://github.com/stleary/JSON-Java-unit-test
|
||||
*Compile a program that uses the jar (see example code below)*
|
||||
```shell
|
||||
javac -cp .;json-java.jar Test.java (Windows)
|
||||
javac -cp .:json-java.jar Test.java (Unix Systems)
|
||||
```
|
||||
|
||||
Numeric types in this package comply with
|
||||
[ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) and
|
||||
[RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259#section-6).
|
||||
This package fully supports `Integer`, `Long`, and `Double` Java types. Partial support
|
||||
for `BigInteger` and `BigDecimal` values in `JSONObject` and `JSONArray` objects is provided
|
||||
in the form of `get()`, `opt()`, and `put()` API methods.
|
||||
*Test file contents*
|
||||
|
||||
Although 1.6 compatibility is currently supported, it is not a project goal and may be
|
||||
removed in some future release.
|
||||
```java
|
||||
import org.json.JSONObject;
|
||||
public class Test {
|
||||
public static void main(String args[]){
|
||||
JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }");
|
||||
System.out.println(jo);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In compliance with RFC8259 page 10 section 9, the parser is more lax with what is valid
|
||||
JSON than the Generator. For Example, the tab character (U+0009) is allowed when reading
|
||||
JSON Text strings, but when output by the Generator, tab is properly converted to \t in
|
||||
the string. Other instances may occur where reading invalid JSON text does not cause an
|
||||
error to be generated. Malformed JSON Texts such as missing end " (quote) on strings or
|
||||
invalid number formats (1.2e6.3) will cause errors as such documents can not be read
|
||||
reliably.
|
||||
*Execute the Test file*
|
||||
```shell
|
||||
java -cp .;json-java.jar Test (Windows)
|
||||
java -cp .:json-java.jar Test (Unix Systems)
|
||||
```
|
||||
|
||||
Release history:
|
||||
*Expected output*
|
||||
|
||||
~~~
|
||||
20180813 POM change to include Automatic-Module-Name (#431)
|
||||
```json
|
||||
{"abc":"def"}
|
||||
```
|
||||
|
||||
20180130 Recent commits
|
||||
|
||||
**Tools to build the package and execute the unit tests**
|
||||
|
||||
20171018 Checkpoint for recent commits.
|
||||
Execute the test suite with Maven:
|
||||
```shell
|
||||
mvn clean test
|
||||
```
|
||||
|
||||
20170516 Roll up recent commits.
|
||||
Execute the test suite with Gradlew:
|
||||
|
||||
20160810 Revert code that was breaking opt*() methods.
|
||||
```shell
|
||||
gradlew clean build test
|
||||
```
|
||||
|
||||
20160807 This release contains a bug in the JSONObject.opt*() and JSONArray.opt*() methods,
|
||||
it is not recommended for use.
|
||||
Java 1.6 compatability fixed, JSONArray.toList() and JSONObject.toMap(),
|
||||
RFC4180 compatibility, JSONPointer, some exception fixes, optional XML type conversion.
|
||||
Contains the latest code as of 7 Aug, 2016
|
||||
*Optional* Execute the test suite in strict mode with Gradlew:
|
||||
|
||||
20160212 Java 1.6 compatibility, OSGi bundle. Contains the latest code as of 12 Feb, 2016.
|
||||
```shell
|
||||
gradlew testWithStrictMode
|
||||
```
|
||||
|
||||
20151123 JSONObject and JSONArray initialization with generics. Contains the
|
||||
latest code as of 23 Nov, 2015.
|
||||
*Optional* Execute the test suite in strict mode with Maven:
|
||||
|
||||
20150729 Checkpoint for Maven central repository release. Contains the latest code
|
||||
as of 29 July, 2015.
|
||||
~~~
|
||||
```shell
|
||||
mvn test -P test-strict-mode
|
||||
```
|
||||
|
||||
# Notes
|
||||
|
||||
JSON-java releases can be found by searching the Maven repository for groupId "org.json"
|
||||
and artifactId "json". For example:
|
||||
https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav
|
||||
For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md)
|
||||
|
||||
# Files
|
||||
|
||||
For more information on files, please see [FILES.md](https://github.com/stleary/JSON-java/blob/master/docs/FILES.md)
|
||||
|
||||
# Release history:
|
||||
|
||||
For the release history, please see [RELEASES.md](https://github.com/stleary/JSON-java/blob/master/docs/RELEASES.md)
|
||||
|
||||
5
SECURITY.md
Normal file
5
SECURITY.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Security Policy
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Please follow the instructions in the ["How are vulnerabilities and exploits handled?"](https://github.com/stleary/JSON-java/wiki/FAQ#how-are-vulnerabilities-and-exploits-handled) section in the FAQ.
|
||||
@@ -1,107 +0,0 @@
|
||||
package org.json;
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Configuration object for the XML parser.
|
||||
* @author AylwardJ
|
||||
*
|
||||
*/
|
||||
public class XMLParserConfiguration {
|
||||
/** Original Configuration of the XML Parser. */
|
||||
public static final XMLParserConfiguration ORIGINAL = new XMLParserConfiguration();
|
||||
/** Original configuration of the XML Parser except that values are kept as strings. */
|
||||
public static final XMLParserConfiguration KEEP_STRINGS = new XMLParserConfiguration(true);
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values should be kept as strings (true), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||
*/
|
||||
public final boolean keepStrings;
|
||||
/**
|
||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||
* processing.
|
||||
*/
|
||||
public final String cDataTagName;
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||
* should be kept as attribute(false), or they should be converted to null(true)
|
||||
*/
|
||||
public final boolean convertNilAttributeToNull;
|
||||
|
||||
/**
|
||||
* Default parser configuration. Does not keep strings, and the CDATA Tag Name is "content".
|
||||
*/
|
||||
public XMLParserConfiguration () {
|
||||
this(false, "content", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser string processing and use the default CDATA Tag Name as "content".
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
*/
|
||||
public XMLParserConfiguration (final boolean keepStrings) {
|
||||
this(keepStrings, "content", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser string processing to try and convert XML values to JSON values and
|
||||
* use the passed CDATA Tag Name the processing value. Pass <code>null</code> to
|
||||
* disable CDATA processing
|
||||
* @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
*/
|
||||
public XMLParserConfiguration (final String cDataTagName) {
|
||||
this(false, cDataTagName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser to use custom settings.
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
*/
|
||||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser to use custom settings.
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
* @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
|
||||
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
|
||||
*/
|
||||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
||||
}
|
||||
}
|
||||
127
build.gradle
Normal file
127
build.gradle
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
// apply plugin: 'jacoco'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
//plugins {
|
||||
// id 'java'
|
||||
//id 'maven-publish'
|
||||
// }
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
maven {
|
||||
url = uri('https://oss.sonatype.org/content/repositories/snapshots')
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'com.jayway.jsonpath:json-path:2.9.0'
|
||||
testImplementation 'org.mockito:mockito-core:4.2.0'
|
||||
}
|
||||
|
||||
subprojects {
|
||||
tasks.withType(Javadoc).all { enabled = false }
|
||||
}
|
||||
|
||||
group = 'org.json'
|
||||
version = 'v20250517-SNAPSHOT'
|
||||
description = 'JSON in Java'
|
||||
sourceCompatibility = '1.8'
|
||||
|
||||
configurations.all {
|
||||
}
|
||||
|
||||
java {
|
||||
withSourcesJar()
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
maven(MavenPublication) {
|
||||
from(components.java)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
options.encoding = 'UTF-8'
|
||||
}
|
||||
// Add these imports at the top of your build.gradle file
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.nio.file.StandardCopyOption
|
||||
|
||||
// Your existing build configurations...
|
||||
|
||||
// Add a new task to modify the file
|
||||
task modifyStrictMode {
|
||||
doLast {
|
||||
println "Modifying JSONParserConfiguration.java to enable strictMode..."
|
||||
|
||||
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
|
||||
|
||||
if (!filePath.exists()) {
|
||||
throw new GradleException("Could not find file: ${filePath.absolutePath}")
|
||||
}
|
||||
|
||||
// Create a backup of the original file
|
||||
def backupFile = new File(filePath.absolutePath + '.bak')
|
||||
Files.copy(filePath.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
|
||||
|
||||
// Read and modify the file content
|
||||
def content = filePath.text
|
||||
def modifiedContent = content.replace('// this.strictMode = true;', 'this.strictMode = true;')
|
||||
|
||||
// Write the modified content back to the file
|
||||
filePath.text = modifiedContent
|
||||
|
||||
println "File modified successfully at: ${filePath.absolutePath}"
|
||||
}
|
||||
}
|
||||
|
||||
// Add a task to restore the original file
|
||||
task restoreStrictMode {
|
||||
doLast {
|
||||
println "Restoring original JSONParserConfiguration.java..."
|
||||
|
||||
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
|
||||
def backupFile = new File(filePath.absolutePath + '.bak')
|
||||
|
||||
if (backupFile.exists()) {
|
||||
Files.copy(backupFile.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING)
|
||||
backupFile.delete()
|
||||
println "Original file restored successfully at: ${filePath.absolutePath}"
|
||||
} else {
|
||||
println "Backup file not found at: ${backupFile.absolutePath}. No restoration performed."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a task to run the workflow
|
||||
task testWithStrictMode {
|
||||
dependsOn modifyStrictMode
|
||||
finalizedBy restoreStrictMode
|
||||
|
||||
doLast {
|
||||
// This will trigger a clean build and run tests with strictMode enabled
|
||||
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
|
||||
exec {
|
||||
executable 'cmd'
|
||||
args '/c', 'gradlew.bat', 'clean', 'build'
|
||||
}
|
||||
} else {
|
||||
exec {
|
||||
executable './gradlew'
|
||||
args 'clean', 'build'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
docs/CONTRIBUTING.md
Normal file
22
docs/CONTRIBUTING.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Contribution Guidelines
|
||||
|
||||
Feel free to work on any issue with a #hacktoberfest label.
|
||||
|
||||
If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, a hacktoberfest label will be added.
|
||||
|
||||
# Who is allowed to submit pull requests for this project?
|
||||
|
||||
Anyone can submit pull requests for code, tests, or documentation.
|
||||
|
||||
# How do you decide which pull requests to accept?
|
||||
|
||||
* Does it call out a bug that needs to be fixed? If so, it goes to the top of the list.
|
||||
* Does it fix a major user inconvenience? These are given high priority as well.
|
||||
* Does it align with the specs? If not, it will probably not be accepted. It turns out there are gray areas in the specs. If this is in a gray area, it will likely be given the benefit of the doubt.
|
||||
* Does it break the existing behavior of the lib? If so, it will not be accepted, unless it fixes an egregious bug. This is happening less frequently now.
|
||||
|
||||
# For more guidance, see these links:
|
||||
|
||||
[README.md (includes build instructions)](https://github.com/stleary/JSON-java#readme)
|
||||
|
||||
[FAQ - all your questions answered](https://github.com/stleary/JSON-java/wiki/FAQ)
|
||||
62
docs/FILES.md
Normal file
62
docs/FILES.md
Normal file
@@ -0,0 +1,62 @@
|
||||
# Files
|
||||
|
||||
**JSONObject.java**: The `JSONObject` can parse text from a `String` or a `JSONTokener`
|
||||
to produce a map-like object. The object provides methods for manipulating its
|
||||
contents, and for producing a JSON compliant object serialization.
|
||||
|
||||
**JSONArray.java**: The `JSONArray` can parse text from a String or a `JSONTokener`
|
||||
to produce a vector-like object. The object provides methods for manipulating
|
||||
its contents, and for producing a JSON compliant array serialization.
|
||||
|
||||
**JSONTokener.java**: The `JSONTokener` breaks a text into a sequence of individual
|
||||
tokens. It can be constructed from a `String`, `Reader`, or `InputStream`. It also can
|
||||
parse text from a `String`, `Number`, `Boolean` or `null` like `"hello"`, `42`, `true`,
|
||||
`null` to produce a simple json object.
|
||||
|
||||
**JSONException.java**: The `JSONException` is the standard exception type thrown
|
||||
by this package.
|
||||
|
||||
**JSONPointer.java**: Implementation of
|
||||
[JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901). Supports
|
||||
JSON Pointers both in the form of string representation and URI fragment
|
||||
representation.
|
||||
|
||||
**JSONPropertyIgnore.java**: Annotation class that can be used on Java Bean getter methods.
|
||||
When used on a bean method that would normally be serialized into a `JSONObject`, it
|
||||
overrides the getter-to-key-name logic and forces the property to be excluded from the
|
||||
resulting `JSONObject`.
|
||||
|
||||
**JSONPropertyName.java**: Annotation class that can be used on Java Bean getter methods.
|
||||
When used on a bean method that would normally be serialized into a `JSONObject`, it
|
||||
overrides the getter-to-key-name logic and uses the value of the annotation. The Bean
|
||||
processor will look through the class hierarchy. This means you can use the annotation on
|
||||
a base class or interface and the value of the annotation will be used even if the getter
|
||||
is overridden in a child class.
|
||||
|
||||
**JSONString.java**: The `JSONString` interface requires a `toJSONString` method,
|
||||
allowing an object to provide its own serialization.
|
||||
|
||||
**JSONStringer.java**: The `JSONStringer` provides a convenient facility for
|
||||
building JSON strings.
|
||||
|
||||
**JSONWriter.java**: The `JSONWriter` provides a convenient facility for building
|
||||
JSON text through a writer.
|
||||
|
||||
|
||||
**CDL.java**: `CDL` provides support for converting between JSON and comma
|
||||
delimited lists.
|
||||
|
||||
**Cookie.java**: `Cookie` provides support for converting between JSON and cookies.
|
||||
|
||||
**CookieList.java**: `CookieList` provides support for converting between JSON and
|
||||
cookie lists.
|
||||
|
||||
**HTTP.java**: `HTTP` provides support for converting between JSON and HTTP headers.
|
||||
|
||||
**HTTPTokener.java**: `HTTPTokener` extends `JSONTokener` for parsing HTTP headers.
|
||||
|
||||
**XML.java**: `XML` provides support for converting between JSON and XML.
|
||||
|
||||
**JSONML.java**: `JSONML` provides support for converting between JSONML and XML.
|
||||
|
||||
**XMLTokener.java**: `XMLTokener` extends `JSONTokener` for parsing XML text.
|
||||
87
docs/NOTES.md
Normal file
87
docs/NOTES.md
Normal file
@@ -0,0 +1,87 @@
|
||||
# Notes
|
||||
|
||||
**Recent directory structure change**
|
||||
|
||||
_Due to a recent commit - [#515 Merge tests and pom and code](https://github.com/stleary/JSON-java/pull/515) - the structure of the project has changed from a flat directory containing all of the Java files to a directory structure that includes unit tests and several tools used to build the project jar and run the unit tests. If you have difficulty using the new structure, please open an issue so we can work through it._
|
||||
|
||||
**Implementation notes**
|
||||
|
||||
Numeric types in this package comply with
|
||||
[ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) and
|
||||
[RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259#section-6).
|
||||
This package fully supports `Integer`, `Long`, and `Double` Java types. Partial support
|
||||
for `BigInteger` and `BigDecimal` values in `JSONObject` and `JSONArray` objects is provided
|
||||
in the form of `get()`, `opt()`, and `put()` API methods.
|
||||
|
||||
Although 1.6 compatibility is currently supported, it is not a project goal and might be
|
||||
removed in some future release.
|
||||
|
||||
In compliance with RFC8259 page 10 section 9, the parser is more lax with what is valid
|
||||
JSON then the Generator. For Example, the tab character (U+0009) is allowed when reading
|
||||
JSON Text strings, but when output by the Generator, the tab is properly converted to \t in
|
||||
the string. Other instances may occur where reading invalid JSON text does not cause an
|
||||
error to be generated. Malformed JSON Texts such as missing end " (quote) on strings or
|
||||
invalid number formats (1.2e6.3) will cause errors as such documents can not be read
|
||||
reliably.
|
||||
|
||||
Some notable exceptions that the JSON Parser in this library accepts are:
|
||||
* Unquoted keys `{ key: "value" }`
|
||||
* Unquoted values `{ "key": value }`
|
||||
* Unescaped literals like "tab" in string values `{ "key": "value with an unescaped tab" }`
|
||||
* Numbers out of range for `Double` or `Long` are parsed as strings
|
||||
|
||||
Recent pull requests added a new method `putAll` on the JSONArray. The `putAll` method
|
||||
works similarly to other `put` methods in that it does not call `JSONObject.wrap` for items
|
||||
added. This can lead to inconsistent object representation in JSONArray structures.
|
||||
|
||||
For example, code like this will create a mixed JSONArray, some items wrapped, others
|
||||
not:
|
||||
|
||||
```java
|
||||
SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
|
||||
// these will be wrapped
|
||||
JSONArray jArr = new JSONArray(myArr);
|
||||
// these will not be wrapped
|
||||
jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
|
||||
```
|
||||
|
||||
For structure consistency, it would be recommended that the above code is changed
|
||||
to look like 1 of 2 ways.
|
||||
|
||||
Option 1:
|
||||
```Java
|
||||
SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
|
||||
JSONArray jArr = new JSONArray();
|
||||
// these will not be wrapped
|
||||
jArr.putAll(myArr);
|
||||
// these will not be wrapped
|
||||
jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
|
||||
// our jArr is now consistent.
|
||||
```
|
||||
|
||||
Option 2:
|
||||
```Java
|
||||
SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
|
||||
// these will be wrapped
|
||||
JSONArray jArr = new JSONArray(myArr);
|
||||
// these will be wrapped
|
||||
jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }));
|
||||
// our jArr is now consistent.
|
||||
```
|
||||
|
||||
**Unit Test Conventions**
|
||||
|
||||
Test filenames should consist of the name of the module being tested, with the suffix "Test".
|
||||
For example, <b>Cookie.java</b> is tested by <b>CookieTest.java</b>.
|
||||
|
||||
<b>The fundamental issues with JSON-Java testing are:</b><br>
|
||||
* <b>JSONObjects</b> are unordered, making simple string comparison ineffective.
|
||||
* Comparisons via **equals()** is not currently supported. Neither <b>JSONArray</b> nor <b>JSONObject</b> override <b>hashCode()</b> or <b>equals()</b>, so comparison defaults to the <b>Object</b> equals(), which is not useful.
|
||||
* Access to the <b>JSONArray</b> and <b>JSONObject</b> internal containers for comparison is not currently available.
|
||||
|
||||
<b>General issues with unit testing are:</b><br>
|
||||
* Just writing tests to make coverage goals tends to result in poor tests.
|
||||
* Unit tests are a form of documentation - how a given method works is demonstrated by the test. So for a code reviewer or future developer looking at code a good test helps explain how a function is supposed to work according to the original author. This can be difficult if you are not the original developer.
|
||||
* It is difficult to evaluate unit tests in a vacuum. You also need to see the code being tested to understand if a test is good.
|
||||
* Without unit tests, it is hard to feel confident about the quality of the code, especially when fixing bugs or refactoring. Good tests prevent regressions and keep the intent of the code correct.
|
||||
* If you have unit test results along with pull requests, the reviewer has an easier time understanding your code and determining if it works as intended.
|
||||
62
docs/RELEASES.md
Normal file
62
docs/RELEASES.md
Normal file
@@ -0,0 +1,62 @@
|
||||
# Release history:
|
||||
|
||||
JSON-java releases can be found by searching the Maven repository for groupId "org.json"
|
||||
and artifactId "json". For example:
|
||||
[https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav)
|
||||
|
||||
~~~
|
||||
20250517 Strict mode hardening and recent commits
|
||||
|
||||
20250107 Restore moditect in pom.xml
|
||||
|
||||
20241224 Strict mode opt-in feature, and recent commits. This release does not contain module-info.class.
|
||||
It is not recommended if you need this feature.
|
||||
|
||||
20240303 Revert optLong/getLong changes, and recent commits.
|
||||
|
||||
20240205 Recent commits.
|
||||
|
||||
20231013 First release with minimum Java version 1.8. Recent commits, including fixes for CVE-2023-5072.
|
||||
|
||||
20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater.
|
||||
|
||||
20230227 Fix for CVE-2022-45688 and recent commits
|
||||
|
||||
20220924 New License - public domain, and some minor updates
|
||||
|
||||
20220320 Wrap StackOverflow with JSONException
|
||||
|
||||
20211205 Recent commits and some bug fixes for similar()
|
||||
|
||||
20210307 Recent commits and potentially breaking fix to JSONPointer
|
||||
|
||||
20201115 Recent commits and first release after project structure change
|
||||
|
||||
20200518 Recent commits and snapshot before project structure change
|
||||
|
||||
20190722 Recent commits
|
||||
|
||||
20180813 POM change to include Automatic-Module-Name (#431)
|
||||
JSONObject(Map) now throws an exception if any of a map keys are null (#405)
|
||||
|
||||
20180130 Recent commits
|
||||
|
||||
20171018 Checkpoint for recent commits.
|
||||
|
||||
20170516 Roll up recent commits.
|
||||
|
||||
20160810 Revert code that was breaking opt*() methods.
|
||||
|
||||
20160807 This release contains a bug in the JSONObject.opt*() and JSONArray.opt*() methods,
|
||||
it is not recommended for use.
|
||||
Java 1.6 compatability fixed, JSONArray.toList() and JSONObject.toMap(),
|
||||
RFC4180 compatibility, JSONPointer, some exception fixes, optional XML type conversion.
|
||||
Contains the latest code as of 7 Aug 2016
|
||||
|
||||
20160212 Java 1.6 compatibility, OSGi bundle. Contains the latest code as of 12 Feb 2016.
|
||||
|
||||
20151123 JSONObject and JSONArray initialization with generics. Contains the latest code as of 23 Nov 2015.
|
||||
|
||||
20150729 Checkpoint for Maven central repository release. Contains the latest code
|
||||
as of 29 July 2015.
|
||||
~~~
|
||||
5
docs/SECURITY.md
Normal file
5
docs/SECURITY.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Security Policy
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Please follow the instructions in the ["How are vulnerabilities and exploits handled?"](https://github.com/stleary/JSON-java/wiki/FAQ#how-are-vulnerabilities-and-exploits-handled) section in the FAQ.
|
||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
183
gradlew
vendored
Executable file
183
gradlew
vendored
Executable file
@@ -0,0 +1,183 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
case $i in
|
||||
0) set -- ;;
|
||||
1) set -- "$args0" ;;
|
||||
2) set -- "$args0" "$args1" ;;
|
||||
3) set -- "$args0" "$args1" "$args2" ;;
|
||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=`save "$@"`
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
103
gradlew.bat
vendored
Normal file
103
gradlew.bat
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
BIN
images/JsonJava.png
Normal file
BIN
images/JsonJava.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
254
pom.xml
Normal file
254
pom.xml
Normal file
@@ -0,0 +1,254 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20250517</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<name>JSON in Java</name>
|
||||
<description>
|
||||
JSON is a light-weight, language independent, data interchange format.
|
||||
See http://www.JSON.org/
|
||||
|
||||
The files in this package implement JSON encoders/decoders in Java.
|
||||
It also includes the capability to convert between JSON and XML, HTTP
|
||||
headers, Cookies, and CDL.
|
||||
|
||||
This is a reference implementation. There are a large number of JSON packages
|
||||
in Java. Perhaps someday the Java community will standardize on one. Until
|
||||
then, choose carefully.
|
||||
</description>
|
||||
<url>https://github.com/douglascrockford/JSON-java</url>
|
||||
|
||||
<scm>
|
||||
<url>https://github.com/douglascrockford/JSON-java.git</url>
|
||||
<connection>scm:git:git://github.com/douglascrockford/JSON-java.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:douglascrockford/JSON-java.git</developerConnection>
|
||||
</scm>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Public Domain</name>
|
||||
<url>https://github.com/stleary/JSON-java/blob/master/LICENSE</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Douglas Crockford</name>
|
||||
<email>douglas@crockford.com</email>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<name>Central Repository OSSRH</name>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>2.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>4.2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>5.1.9</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Export-Package>
|
||||
org.json
|
||||
</Export-Package>
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<compilerArgs>
|
||||
<arg>-Xlint:unchecked</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<additionalparam>-Xdoclint:none</additionalparam>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<gpgArguments>
|
||||
<arg>--pinentry-mode</arg>
|
||||
<arg>loopback</arg>
|
||||
</gpgArguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<version>1.6.3</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<serverId>ossrh</serverId>
|
||||
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
|
||||
<autoReleaseAfterClose>false</autoReleaseAfterClose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.moditect</groupId>
|
||||
<artifactId>moditect-maven-plugin</artifactId>
|
||||
<version>1.0.0.Final</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add-module-infos</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>add-module-info</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<jvmVersion>9</jvmVersion>
|
||||
<module>
|
||||
<moduleInfoSource>
|
||||
module org.json {
|
||||
exports org.json;
|
||||
}
|
||||
</moduleInfoSource>
|
||||
</module>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>test-strict-mode</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.google.code.maven-replacer-plugin</groupId>
|
||||
<artifactId>replacer</artifactId>
|
||||
<version>1.5.3</version>
|
||||
<executions>
|
||||
<!-- Enable strict mode -->
|
||||
<execution>
|
||||
<id>enable-strict-mode</id>
|
||||
<phase>process-sources</phase>
|
||||
<goals>
|
||||
<goal>replace</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<file>src/main/java/org/json/JSONParserConfiguration.java</file>
|
||||
<replacements>
|
||||
<replacement>
|
||||
<token>// this.strictMode = true;</token>
|
||||
<value>this.strictMode = true;</value>
|
||||
</replacement>
|
||||
</replacements>
|
||||
</configuration>
|
||||
</execution>
|
||||
<!-- Restore original code after tests -->
|
||||
<execution>
|
||||
<id>restore-original</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>replace</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<file>src/main/java/org/json/JSONParserConfiguration.java</file>
|
||||
<replacements>
|
||||
<replacement>
|
||||
<token>this.strictMode = true;</token>
|
||||
<value>// this.strictMode = true;</value>
|
||||
</replacement>
|
||||
</replacements>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
||||
@@ -1,39 +1,19 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This provides static methods to convert comma delimited text into a
|
||||
* JSONArray, and to convert a JSONArray into comma delimited text. Comma
|
||||
* This provides static methods to convert comma (or otherwise) delimited text into a
|
||||
* JSONArray, and to convert a JSONArray into comma (or otherwise) delimited text. Comma
|
||||
* delimited text is a very popular format for data interchange. It is
|
||||
* understood by most database, spreadsheet, and organizer programs.
|
||||
* <p>
|
||||
* Each row of text represents a row in a table or a data record. Each row
|
||||
* ends with a NEWLINE character. Each row contains one or more values.
|
||||
* Values are separated by commas. A value can contain any character except
|
||||
* for comma, unless is is wrapped in single quotes or double quotes.
|
||||
* for comma, unless it is wrapped in single quotes or double quotes.
|
||||
* <p>
|
||||
* The first row usually contains the names of the columns.
|
||||
* <p>
|
||||
@@ -45,35 +25,40 @@ SOFTWARE.
|
||||
*/
|
||||
public class CDL {
|
||||
|
||||
/**
|
||||
* Constructs a new CDL object.
|
||||
*/
|
||||
public CDL() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next value. The value can be wrapped in quotes. The value can
|
||||
* be empty.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @param delimiter used in the file
|
||||
* @return The value string, or null if empty.
|
||||
* @throws JSONException if the quoted string is badly formed.
|
||||
*/
|
||||
private static String getValue(JSONTokener x) throws JSONException {
|
||||
private static String getValue(JSONTokener x, char delimiter) throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
StringBuffer sb;
|
||||
StringBuilder sb;
|
||||
do {
|
||||
c = x.next();
|
||||
} while (c == ' ' || c == '\t');
|
||||
switch (c) {
|
||||
case 0:
|
||||
if (c == 0) {
|
||||
return null;
|
||||
case '"':
|
||||
case '\'':
|
||||
} else if (c == '"' || c == '\'') {
|
||||
q = c;
|
||||
sb = new StringBuffer();
|
||||
sb = new StringBuilder();
|
||||
for (;;) {
|
||||
c = x.next();
|
||||
if (c == q) {
|
||||
//Handle escaped double-quote
|
||||
char nextC = x.next();
|
||||
if(nextC != '\"') {
|
||||
if (nextC != '\"') {
|
||||
// if our quote was the end of the file, don't step
|
||||
if(nextC > 0) {
|
||||
if (nextC > 0) {
|
||||
x.back();
|
||||
}
|
||||
break;
|
||||
@@ -85,33 +70,47 @@ public class CDL {
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
case ',':
|
||||
} else if (c == delimiter) {
|
||||
x.back();
|
||||
return "";
|
||||
default:
|
||||
x.back();
|
||||
return x.nextTo(',');
|
||||
}
|
||||
x.back();
|
||||
return x.nextTo(delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of strings from a row of comma delimited values.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @return A JSONArray of strings.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
|
||||
return rowToJSONArray(x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of strings from a row of comma delimited values.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A JSONArray of strings.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray rowToJSONArray(JSONTokener x, char delimiter) throws JSONException {
|
||||
JSONArray ja = new JSONArray();
|
||||
for (;;) {
|
||||
String value = getValue(x);
|
||||
String value = getValue(x,delimiter);
|
||||
char c = x.next();
|
||||
if (value == null ||
|
||||
(ja.length() == 0 && value.length() == 0 && c != ',')) {
|
||||
if (value != null) {
|
||||
ja.put(value);
|
||||
} else if (ja.length() == 0 && c != delimiter) {
|
||||
return null;
|
||||
} else {
|
||||
// This line accounts for CSV ending with no newline
|
||||
ja.put("");
|
||||
}
|
||||
ja.put(value);
|
||||
|
||||
for (;;) {
|
||||
if (c == ',') {
|
||||
if (c == delimiter) {
|
||||
break;
|
||||
}
|
||||
if (c != ' ') {
|
||||
@@ -134,11 +133,25 @@ public class CDL {
|
||||
* method.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @return A JSONObject combining the names and values.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
|
||||
throws JSONException {
|
||||
JSONArray ja = rowToJSONArray(x);
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws JSONException {
|
||||
return rowToJSONObject(names, x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONObject from a row of comma delimited text, using a
|
||||
* parallel JSONArray of strings to provides the names of the elements.
|
||||
* @param names A JSONArray of names. This is commonly obtained from the
|
||||
* first row of a comma delimited text file using the rowToJSONArray
|
||||
* method.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A JSONObject combining the names and values.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
|
||||
JSONArray ja = rowToJSONArray(x, delimiter);
|
||||
return ja != null ? ja.toJSONObject(names) : null;
|
||||
}
|
||||
|
||||
@@ -150,15 +163,27 @@ public class CDL {
|
||||
* @return A string ending in NEWLINE.
|
||||
*/
|
||||
public static String rowToString(JSONArray ja) {
|
||||
return rowToString(ja, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a comma delimited text row from a JSONArray. Values containing
|
||||
* the comma character will be quoted. Troublesome characters may be
|
||||
* removed.
|
||||
* @param ja A JSONArray of strings.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A string ending in NEWLINE.
|
||||
*/
|
||||
public static String rowToString(JSONArray ja, char delimiter) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < ja.length(); i += 1) {
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
sb.append(delimiter);
|
||||
}
|
||||
Object object = ja.opt(i);
|
||||
if (object != null) {
|
||||
String string = object.toString();
|
||||
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
|
||||
if (string.length() > 0 && (string.indexOf(delimiter) >= 0 ||
|
||||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
||||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
||||
sb.append('"');
|
||||
@@ -184,10 +209,22 @@ public class CDL {
|
||||
* using the first row as a source of names.
|
||||
* @param string The comma delimited text.
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
return toJSONArray(new JSONTokener(string));
|
||||
return toJSONArray(string, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of JSONObjects from a comma delimited text string,
|
||||
* using the first row as a source of names.
|
||||
* @param string The comma delimited text.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string, char delimiter) throws JSONException {
|
||||
return toJSONArray(new JSONTokener(string), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,10 +232,22 @@ public class CDL {
|
||||
* using the first row as a source of names.
|
||||
* @param x The JSONTokener containing the comma delimited text.
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
|
||||
return toJSONArray(rowToJSONArray(x), x);
|
||||
return toJSONArray(x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of JSONObjects from a comma delimited text string,
|
||||
* using the first row as a source of names.
|
||||
* @param x The JSONTokener containing the comma delimited text.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONTokener x, char delimiter) throws JSONException {
|
||||
return toJSONArray(rowToJSONArray(x, delimiter), x, delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,11 +256,23 @@ public class CDL {
|
||||
* @param names A JSONArray of strings.
|
||||
* @param string The comma delimited text.
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, String string)
|
||||
throws JSONException {
|
||||
return toJSONArray(names, new JSONTokener(string));
|
||||
public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException {
|
||||
return toJSONArray(names, string, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of JSONObjects from a comma delimited text string
|
||||
* using a supplied JSONArray as the source of element names.
|
||||
* @param names A JSONArray of strings.
|
||||
* @param string The comma delimited text.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, String string, char delimiter) throws JSONException {
|
||||
return toJSONArray(names, new JSONTokener(string), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,16 +281,28 @@ public class CDL {
|
||||
* @param names A JSONArray of strings.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
|
||||
throws JSONException {
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException {
|
||||
return toJSONArray(names, x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of JSONObjects from a comma delimited text string
|
||||
* using a supplied JSONArray as the source of element names.
|
||||
* @param names A JSONArray of strings.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray ja = new JSONArray();
|
||||
for (;;) {
|
||||
JSONObject jo = rowToJSONObject(names, x);
|
||||
JSONObject jo = rowToJSONObject(names, x, delimiter);
|
||||
if (jo == null) {
|
||||
break;
|
||||
}
|
||||
@@ -238,6 +311,17 @@ public class CDL {
|
||||
if (ja.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The following block accounts for empty datasets (no keys or vals)
|
||||
if (ja.length() == 1) {
|
||||
JSONObject j = ja.getJSONObject(0);
|
||||
if (j.length() == 1) {
|
||||
String key = j.keys().next();
|
||||
if ("".equals(key) && "".equals(j.get(key))) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
@@ -248,14 +332,27 @@ public class CDL {
|
||||
* JSONObject.
|
||||
* @param ja A JSONArray of JSONObjects.
|
||||
* @return A comma delimited text.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONArray ja) throws JSONException {
|
||||
return toString(ja, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a comma delimited text from a JSONArray of JSONObjects. The
|
||||
* first row will be a list of names obtained by inspecting the first
|
||||
* JSONObject.
|
||||
* @param ja A JSONArray of JSONObjects.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A comma delimited text.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONArray ja, char delimiter) throws JSONException {
|
||||
JSONObject jo = ja.optJSONObject(0);
|
||||
if (jo != null) {
|
||||
JSONArray names = jo.names();
|
||||
if (names != null) {
|
||||
return rowToString(names) + toString(names, ja);
|
||||
return rowToString(names, delimiter) + toString(names, ja, delimiter);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -268,18 +365,31 @@ public class CDL {
|
||||
* @param names A JSONArray of strings.
|
||||
* @param ja A JSONArray of JSONObjects.
|
||||
* @return A comma delimited text.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONArray names, JSONArray ja)
|
||||
throws JSONException {
|
||||
public static String toString(JSONArray names, JSONArray ja) throws JSONException {
|
||||
return toString(names, ja, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a comma delimited text from a JSONArray of JSONObjects using
|
||||
* a provided list of names. The list of names is not included in the
|
||||
* output.
|
||||
* @param names A JSONArray of strings.
|
||||
* @param ja A JSONArray of JSONObjects.
|
||||
* @param delimiter custom delimiter char
|
||||
* @return A comma delimited text.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONArray names, JSONArray ja, char delimiter) throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
StringBuffer sb = new StringBuffer();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < ja.length(); i += 1) {
|
||||
JSONObject jo = ja.optJSONObject(i);
|
||||
if (jo != null) {
|
||||
sb.append(rowToString(jo.toJSONArray(names)));
|
||||
sb.append(rowToString(jo.toJSONArray(names), delimiter));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
210
src/main/java/org/json/Cookie.java
Normal file
210
src/main/java/org/json/Cookie.java
Normal file
@@ -0,0 +1,210 @@
|
||||
package org.json;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Convert a web browser cookie specification to a JSONObject and back.
|
||||
* JSON and Cookies are both notations for name/value pairs.
|
||||
* See also: <a href="https://tools.ietf.org/html/rfc6265">https://tools.ietf.org/html/rfc6265</a>
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class Cookie {
|
||||
|
||||
/**
|
||||
* Constructs a new Cookie object.
|
||||
*/
|
||||
public Cookie() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a copy of a string in which the characters '+', '%', '=', ';'
|
||||
* and control characters are replaced with "%hh". This is a gentle form
|
||||
* of URL encoding, attempting to cause as little distortion to the
|
||||
* string as possible. The characters '=' and ';' are meta characters in
|
||||
* cookies. By convention, they are escaped using the URL-encoding. This is
|
||||
* only a convention, not a standard. Often, cookies are expected to have
|
||||
* encoded values. We encode '=' and ';' because we must. We encode '%' and
|
||||
* '+' because they are meta characters in URL encoding.
|
||||
* @param string The source string.
|
||||
* @return The escaped result.
|
||||
*/
|
||||
public static String escape(String string) {
|
||||
char c;
|
||||
String s = string.trim();
|
||||
int length = s.length();
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
c = s.charAt(i);
|
||||
if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
|
||||
sb.append('%');
|
||||
sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
|
||||
sb.append(Character.forDigit((char)(c & 0x0f), 16));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a cookie specification string into a JSONObject. The string
|
||||
* must contain a name value pair separated by '='. The name and the value
|
||||
* will be unescaped, possibly converting '+' and '%' sequences. The
|
||||
* cookie properties may follow, separated by ';', also represented as
|
||||
* name=value (except the Attribute properties like "Secure" or "HttpOnly",
|
||||
* which do not have a value. The value {@link Boolean#TRUE} will be used for these).
|
||||
* The name will be stored under the key "name", and the value will be
|
||||
* stored under the key "value". This method does not do checking or
|
||||
* validation of the parameters. It only converts the cookie string into
|
||||
* a JSONObject. All attribute names are converted to lower case keys in the
|
||||
* JSONObject (HttpOnly => httponly). If an attribute is specified more than
|
||||
* once, only the value found closer to the end of the cookie-string is kept.
|
||||
* @param string The cookie specification string.
|
||||
* @return A JSONObject containing "name", "value", and possibly other
|
||||
* members.
|
||||
* @throws JSONException If there is an error parsing the Cookie String.
|
||||
* Cookie strings must have at least one '=' character and the 'name'
|
||||
* portion of the cookie must not be blank.
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) {
|
||||
final JSONObject jo = new JSONObject();
|
||||
String name;
|
||||
Object value;
|
||||
|
||||
|
||||
JSONTokener x = new JSONTokener(string);
|
||||
|
||||
name = unescape(x.nextTo('=').trim());
|
||||
//per RFC6265, if the name is blank, the cookie should be ignored.
|
||||
if("".equals(name)) {
|
||||
throw new JSONException("Cookies must have a 'name'");
|
||||
}
|
||||
jo.put("name", name);
|
||||
// per RFC6265, if there is no '=', the cookie should be ignored.
|
||||
// the 'next' call here throws an exception if the '=' is not found.
|
||||
x.next('=');
|
||||
jo.put("value", unescape(x.nextTo(';')).trim());
|
||||
// discard the ';'
|
||||
x.next();
|
||||
// parse the remaining cookie attributes
|
||||
while (x.more()) {
|
||||
name = unescape(x.nextTo("=;")).trim().toLowerCase(Locale.ROOT);
|
||||
// don't allow a cookies attributes to overwrite its name or value.
|
||||
if("name".equalsIgnoreCase(name)) {
|
||||
throw new JSONException("Illegal attribute name: 'name'");
|
||||
}
|
||||
if("value".equalsIgnoreCase(name)) {
|
||||
throw new JSONException("Illegal attribute name: 'value'");
|
||||
}
|
||||
// check to see if it's a flag property
|
||||
if (x.next() != '=') {
|
||||
value = Boolean.TRUE;
|
||||
} else {
|
||||
value = unescape(x.nextTo(';')).trim();
|
||||
x.next();
|
||||
}
|
||||
// only store non-blank attributes
|
||||
if(!"".equals(name) && !"".equals(value)) {
|
||||
jo.put(name, value);
|
||||
}
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a cookie specification string. The JSONObject
|
||||
* must contain "name" and "value" members (case insensitive).
|
||||
* If the JSONObject contains other members, they will be appended to the cookie
|
||||
* specification string. User-Agents are instructed to ignore unknown attributes,
|
||||
* so ensure your JSONObject is using only known attributes.
|
||||
* See also: <a href="https://tools.ietf.org/html/rfc6265">https://tools.ietf.org/html/rfc6265</a>
|
||||
* @param jo A JSONObject
|
||||
* @return A cookie specification string
|
||||
* @throws JSONException thrown if the cookie has no name.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String name = null;
|
||||
Object value = null;
|
||||
for(String key : jo.keySet()){
|
||||
if("name".equalsIgnoreCase(key)) {
|
||||
name = jo.getString(key).trim();
|
||||
}
|
||||
if("value".equalsIgnoreCase(key)) {
|
||||
value=jo.getString(key).trim();
|
||||
}
|
||||
if(name != null && value != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(name == null || "".equals(name.trim())) {
|
||||
throw new JSONException("Cookie does not have a name");
|
||||
}
|
||||
if(value == null) {
|
||||
value = "";
|
||||
}
|
||||
|
||||
sb.append(escape(name));
|
||||
sb.append("=");
|
||||
sb.append(escape((String)value));
|
||||
|
||||
for(String key : jo.keySet()){
|
||||
if("name".equalsIgnoreCase(key)
|
||||
|| "value".equalsIgnoreCase(key)) {
|
||||
// already processed above
|
||||
continue;
|
||||
}
|
||||
value = jo.opt(key);
|
||||
if(value instanceof Boolean) {
|
||||
if(Boolean.TRUE.equals(value)) {
|
||||
sb.append(';').append(escape(key));
|
||||
}
|
||||
// don't emit false values
|
||||
} else {
|
||||
sb.append(';')
|
||||
.append(escape(key))
|
||||
.append('=')
|
||||
.append(escape(value.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert <code>%</code><i>hh</i> sequences to single characters, and
|
||||
* convert plus to space.
|
||||
* @param string A string that may contain
|
||||
* <code>+</code> <small>(plus)</small> and
|
||||
* <code>%</code><i>hh</i> sequences.
|
||||
* @return The unescaped string.
|
||||
*/
|
||||
public static String unescape(String string) {
|
||||
int length = string.length();
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for (int i = 0; i < length; ++i) {
|
||||
char c = string.charAt(i);
|
||||
if (c == '+') {
|
||||
c = ' ';
|
||||
} else if (c == '%' && i + 2 < length) {
|
||||
int d = JSONTokener.dehexchar(string.charAt(i + 1));
|
||||
int e = JSONTokener.dehexchar(string.charAt(i + 2));
|
||||
if (d >= 0 && e >= 0) {
|
||||
c = (char)(d * 16 + e);
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,7 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -31,6 +11,12 @@ SOFTWARE.
|
||||
*/
|
||||
public class CookieList {
|
||||
|
||||
/**
|
||||
* Constructs a new CookieList object.
|
||||
*/
|
||||
public CookieList() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a cookie list into a JSONObject. A cookie list is a sequence
|
||||
* of name/value pairs. The names are separated from the values by '='.
|
||||
@@ -42,7 +28,7 @@ public class CookieList {
|
||||
* cookieJSONObject.getString("value"));
|
||||
* @param string A cookie list string
|
||||
* @return A JSONObject
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
@@ -63,22 +49,22 @@ public class CookieList {
|
||||
* in the names and values are replaced by "%hh".
|
||||
* @param jo A JSONObject
|
||||
* @return A cookie list string
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
boolean b = false;
|
||||
boolean isEndOfPair = false;
|
||||
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) {
|
||||
if (isEndOfPair) {
|
||||
sb.append(';');
|
||||
}
|
||||
sb.append(Cookie.escape(key));
|
||||
sb.append("=");
|
||||
sb.append(Cookie.escape(value.toString()));
|
||||
b = true;
|
||||
isEndOfPair = true;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
@@ -1,162 +1,148 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Convert an HTTP header to a JSONObject and back.
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class HTTP {
|
||||
|
||||
/** Carriage return/line feed. */
|
||||
public static final String CRLF = "\r\n";
|
||||
|
||||
/**
|
||||
* Convert an HTTP header string into a JSONObject. It can be a request
|
||||
* header or a response header. A request header will contain
|
||||
* <pre>{
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }</pre>
|
||||
* A response header will contain
|
||||
* <pre>{
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }</pre>
|
||||
* In addition, the other parameters in the header will be captured, using
|
||||
* the HTTP field names as JSON names, so that <pre>
|
||||
* Date: Sun, 26 May 2002 18:06:04 GMT
|
||||
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
|
||||
* Cache-Control: no-cache</pre>
|
||||
* become
|
||||
* <pre>{...
|
||||
* Date: "Sun, 26 May 2002 18:06:04 GMT",
|
||||
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
|
||||
* "Cache-Control": "no-cache",
|
||||
* ...}</pre>
|
||||
* It does no further checking or conversion. It does not parse dates.
|
||||
* It does not do '%' transforms on URLs.
|
||||
* @param string An HTTP header string.
|
||||
* @return A JSONObject containing the elements and attributes
|
||||
* of the XML string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
HTTPTokener x = new HTTPTokener(string);
|
||||
String token;
|
||||
|
||||
token = x.nextToken();
|
||||
if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) {
|
||||
|
||||
// Response
|
||||
|
||||
jo.put("HTTP-Version", token);
|
||||
jo.put("Status-Code", x.nextToken());
|
||||
jo.put("Reason-Phrase", x.nextTo('\0'));
|
||||
x.next();
|
||||
|
||||
} else {
|
||||
|
||||
// Request
|
||||
|
||||
jo.put("Method", token);
|
||||
jo.put("Request-URI", x.nextToken());
|
||||
jo.put("HTTP-Version", x.nextToken());
|
||||
}
|
||||
|
||||
// Fields
|
||||
|
||||
while (x.more()) {
|
||||
String name = x.nextTo(':');
|
||||
x.next(':');
|
||||
jo.put(name, x.nextTo('\0'));
|
||||
x.next();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into an HTTP header. A request header must contain
|
||||
* <pre>{
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }</pre>
|
||||
* A response header must contain
|
||||
* <pre>{
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }</pre>
|
||||
* Any other members of the JSONObject will be output as HTTP fields.
|
||||
* The result will end with two CRLF pairs.
|
||||
* @param jo A JSONObject
|
||||
* @return An HTTP header string.
|
||||
* @throws JSONException if the object does not contain enough
|
||||
* information.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Status-Code"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Reason-Phrase"));
|
||||
} else if (jo.has("Method") && jo.has("Request-URI")) {
|
||||
sb.append(jo.getString("Method"));
|
||||
sb.append(' ');
|
||||
sb.append('"');
|
||||
sb.append(jo.getString("Request-URI"));
|
||||
sb.append('"');
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
} else {
|
||||
throw new JSONException("Not enough material for an HTTP header.");
|
||||
}
|
||||
sb.append(CRLF);
|
||||
// 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(value)) {
|
||||
sb.append(key);
|
||||
sb.append(": ");
|
||||
sb.append(jo.optString(key));
|
||||
sb.append(CRLF);
|
||||
}
|
||||
}
|
||||
sb.append(CRLF);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Convert an HTTP header to a JSONObject and back.
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class HTTP {
|
||||
|
||||
/**
|
||||
* Constructs a new HTTP object.
|
||||
*/
|
||||
public HTTP() {
|
||||
}
|
||||
|
||||
/** Carriage return/line feed. */
|
||||
public static final String CRLF = "\r\n";
|
||||
|
||||
/**
|
||||
* Convert an HTTP header string into a JSONObject. It can be a request
|
||||
* header or a response header. A request header will contain
|
||||
* <pre>{
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }</pre>
|
||||
* A response header will contain
|
||||
* <pre>{
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }</pre>
|
||||
* In addition, the other parameters in the header will be captured, using
|
||||
* the HTTP field names as JSON names, so that <pre>{@code
|
||||
* Date: Sun, 26 May 2002 18:06:04 GMT
|
||||
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
|
||||
* Cache-Control: no-cache}</pre>
|
||||
* become
|
||||
* <pre>{@code
|
||||
* Date: "Sun, 26 May 2002 18:06:04 GMT",
|
||||
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
|
||||
* "Cache-Control": "no-cache",
|
||||
* ...}</pre>
|
||||
* It does no further checking or conversion. It does not parse dates.
|
||||
* It does not do '%' transforms on URLs.
|
||||
* @param string An HTTP header string.
|
||||
* @return A JSONObject containing the elements and attributes
|
||||
* of the XML string.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
HTTPTokener x = new HTTPTokener(string);
|
||||
String token;
|
||||
|
||||
token = x.nextToken();
|
||||
if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) {
|
||||
|
||||
// Response
|
||||
|
||||
jo.put("HTTP-Version", token);
|
||||
jo.put("Status-Code", x.nextToken());
|
||||
jo.put("Reason-Phrase", x.nextTo('\0'));
|
||||
x.next();
|
||||
|
||||
} else {
|
||||
|
||||
// Request
|
||||
|
||||
jo.put("Method", token);
|
||||
jo.put("Request-URI", x.nextToken());
|
||||
jo.put("HTTP-Version", x.nextToken());
|
||||
}
|
||||
|
||||
// Fields
|
||||
|
||||
while (x.more()) {
|
||||
String name = x.nextTo(':');
|
||||
x.next(':');
|
||||
jo.put(name, x.nextTo('\0'));
|
||||
x.next();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into an HTTP header. A request header must contain
|
||||
* <pre>{
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }</pre>
|
||||
* A response header must contain
|
||||
* <pre>{
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }</pre>
|
||||
* Any other members of the JSONObject will be output as HTTP fields.
|
||||
* The result will end with two CRLF pairs.
|
||||
* @param jo A JSONObject
|
||||
* @return An HTTP header string.
|
||||
* @throws JSONException if the object does not contain enough
|
||||
* information.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Status-Code"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Reason-Phrase"));
|
||||
} else if (jo.has("Method") && jo.has("Request-URI")) {
|
||||
sb.append(jo.getString("Method"));
|
||||
sb.append(' ');
|
||||
sb.append('"');
|
||||
sb.append(jo.getString("Request-URI"));
|
||||
sb.append('"');
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
} else {
|
||||
throw new JSONException("Not enough material for an HTTP header.");
|
||||
}
|
||||
sb.append(CRLF);
|
||||
// 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(value)) {
|
||||
sb.append(key);
|
||||
sb.append(": ");
|
||||
sb.append(jo.optString(key));
|
||||
sb.append(CRLF);
|
||||
}
|
||||
}
|
||||
sb.append(CRLF);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,7 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -43,8 +23,8 @@ public class HTTPTokener extends JSONTokener {
|
||||
|
||||
/**
|
||||
* Get the next token or string. This is used in parsing HTTP headers.
|
||||
* @throws JSONException
|
||||
* @return A String.
|
||||
* @throws JSONException if a syntax error occurs
|
||||
*/
|
||||
public String nextToken() throws JSONException {
|
||||
char c;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,9 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
||||
*
|
||||
@@ -1,27 +1,7 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2008 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -33,6 +13,13 @@ SOFTWARE.
|
||||
* @version 2016-01-30
|
||||
*/
|
||||
public class JSONML {
|
||||
|
||||
/**
|
||||
* Constructs a new JSONML object.
|
||||
*/
|
||||
public JSONML() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse XML values and store them in a JSONArray.
|
||||
* @param x The XMLTokener containing the source string.
|
||||
@@ -41,13 +28,38 @@ public class JSONML {
|
||||
* if we are at the outermost level.
|
||||
* @param keepStrings Don't type-convert text nodes and attribute values
|
||||
* @return A JSONArray if the value is the outermost tag, otherwise null.
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a parsing error occurs
|
||||
*/
|
||||
private static Object parse(
|
||||
XMLTokener x,
|
||||
boolean arrayForm,
|
||||
JSONArray ja,
|
||||
boolean keepStrings
|
||||
boolean keepStrings,
|
||||
int currentNestingDepth
|
||||
) throws JSONException {
|
||||
return parse(x,arrayForm, ja,
|
||||
keepStrings ? JSONMLParserConfiguration.KEEP_STRINGS : JSONMLParserConfiguration.ORIGINAL,
|
||||
currentNestingDepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse XML values and store them in a JSONArray.
|
||||
* @param x The XMLTokener containing the source string.
|
||||
* @param arrayForm true if array form, false if object form.
|
||||
* @param ja The JSONArray that is containing the current tag or null
|
||||
* if we are at the outermost level.
|
||||
* @param config The parser configuration:
|
||||
* JSONMLParserConfiguration.ORIGINAL is the default behaviour;
|
||||
* JSONMLParserConfiguration.KEEP_STRINGS means Don't type-convert text nodes and attribute values.
|
||||
* @return A JSONArray if the value is the outermost tag, otherwise null.
|
||||
* @throws JSONException if a parsing error occurs
|
||||
*/
|
||||
private static Object parse(
|
||||
XMLTokener x,
|
||||
boolean arrayForm,
|
||||
JSONArray ja,
|
||||
JSONMLParserConfiguration config,
|
||||
int currentNestingDepth
|
||||
) throws JSONException {
|
||||
String attribute;
|
||||
char c;
|
||||
@@ -172,7 +184,7 @@ public class JSONML {
|
||||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
newjo.accumulate(attribute, keepStrings ? ((String)token) :XML.stringToValue((String)token));
|
||||
newjo.accumulate(attribute, config.isKeepStrings() ? ((String)token) :XML.stringToValue((String)token));
|
||||
token = null;
|
||||
} else {
|
||||
newjo.accumulate(attribute, "");
|
||||
@@ -201,7 +213,12 @@ public class JSONML {
|
||||
if (token != XML.GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
closeTag = (String)parse(x, arrayForm, newja, keepStrings);
|
||||
|
||||
if (currentNestingDepth == config.getMaxNestingDepth()) {
|
||||
throw x.syntaxError("Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
|
||||
}
|
||||
|
||||
closeTag = (String)parse(x, arrayForm, newja, config, currentNestingDepth + 1);
|
||||
if (closeTag != null) {
|
||||
if (!closeTag.equals(tagName)) {
|
||||
throw x.syntaxError("Mismatched '" + tagName +
|
||||
@@ -223,7 +240,7 @@ public class JSONML {
|
||||
} else {
|
||||
if (ja != null) {
|
||||
ja.put(token instanceof String
|
||||
? keepStrings ? XML.unescape((String)token) :XML.stringToValue((String)token)
|
||||
? (config.isKeepStrings() ? XML.unescape((String)token) : XML.stringToValue((String)token))
|
||||
: token);
|
||||
}
|
||||
}
|
||||
@@ -238,13 +255,13 @@ public class JSONML {
|
||||
* attributes, then the second element will be JSONObject containing the
|
||||
* name/value pairs. If the tag contains children, then strings and
|
||||
* JSONArrays will represent the child tags.
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param string The source string.
|
||||
* @return A JSONArray containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, false);
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, JSONMLParserConfiguration.ORIGINAL, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -255,10 +272,10 @@ public class JSONML {
|
||||
* attributes, then the second element will be JSONObject containing the
|
||||
* name/value pairs. If the tag contains children, then strings and
|
||||
* JSONArrays will represent the child tags.
|
||||
* As opposed to toJSONArray this method does not attempt to convert
|
||||
* any text node or attribute value to any type
|
||||
* As opposed to toJSONArray this method does not attempt to convert
|
||||
* any text node or attribute value to any type
|
||||
* but just leaves it as a string.
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param string The source string.
|
||||
* @param keepStrings If true, then values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
@@ -266,7 +283,32 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string, boolean keepStrings) throws JSONException {
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, keepStrings);
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, keepStrings, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONArray using the JsonML transform. Each XML tag is represented as
|
||||
* a JSONArray in which the first element is the tag name. If the tag has
|
||||
* attributes, then the second element will be JSONObject containing the
|
||||
* name/value pairs. If the tag contains children, then strings and
|
||||
* JSONArrays will represent the child tags.
|
||||
* As opposed to toJSONArray this method does not attempt to convert
|
||||
* any text node or attribute value to any type
|
||||
* but just leaves it as a string.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param string The source string.
|
||||
* @param config The parser configuration:
|
||||
* JSONMLParserConfiguration.ORIGINAL is the default behaviour;
|
||||
* JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
* @return A JSONArray containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, config, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -277,10 +319,34 @@ public class JSONML {
|
||||
* attributes, then the second element will be JSONObject containing the
|
||||
* name/value pairs. If the tag contains children, then strings and
|
||||
* JSONArrays will represent the child content and tags.
|
||||
* As opposed to toJSONArray this method does not attempt to convert
|
||||
* any text node or attribute value to any type
|
||||
* As opposed to toJSONArray this method does not attempt to convert
|
||||
* any text node or attribute value to any type
|
||||
* but just leaves it as a string.
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param x An XMLTokener.
|
||||
* @param config The parser configuration:
|
||||
* JSONMLParserConfiguration.ORIGINAL is the default behaviour;
|
||||
* JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
* @return A JSONArray containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null, config, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONArray using the JsonML transform. Each XML tag is represented as
|
||||
* a JSONArray in which the first element is the tag name. If the tag has
|
||||
* attributes, then the second element will be JSONObject containing the
|
||||
* name/value pairs. If the tag contains children, then strings and
|
||||
* JSONArrays will represent the child content and tags.
|
||||
* As opposed to toJSONArray this method does not attempt to convert
|
||||
* any text node or attribute value to any type
|
||||
* but just leaves it as a string.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param x An XMLTokener.
|
||||
* @param keepStrings If true, then values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
@@ -288,7 +354,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null, keepStrings);
|
||||
return (JSONArray)parse(x, true, null, keepStrings, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -299,13 +365,13 @@ public class JSONML {
|
||||
* attributes, then the second element will be JSONObject containing the
|
||||
* name/value pairs. If the tag contains children, then strings and
|
||||
* JSONArrays will represent the child content and tags.
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param x An XMLTokener.
|
||||
* @return A JSONArray containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null, false);
|
||||
return (JSONArray)parse(x, true, null, false, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -317,16 +383,16 @@ public class JSONML {
|
||||
* contains children, the object will have a "childNodes" property which
|
||||
* will be an array of strings and JsonML JSONObjects.
|
||||
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param string The XML source text.
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, false);
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, false, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONObject using the JsonML transform. Each XML tag is represented as
|
||||
@@ -335,7 +401,7 @@ public class JSONML {
|
||||
* contains children, the object will have a "childNodes" property which
|
||||
* will be an array of strings and JsonML JSONObjects.
|
||||
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param string The XML source text.
|
||||
* @param keepStrings If true, then values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
@@ -343,10 +409,10 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, keepStrings);
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, keepStrings, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONObject using the JsonML transform. Each XML tag is represented as
|
||||
@@ -355,13 +421,35 @@ public class JSONML {
|
||||
* contains children, the object will have a "childNodes" property which
|
||||
* will be an array of strings and JsonML JSONObjects.
|
||||
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param string The XML source text.
|
||||
* @param config The parser configuration:
|
||||
* JSONMLParserConfiguration.ORIGINAL is the default behaviour;
|
||||
* JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, config, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONObject using the JsonML transform. Each XML tag is represented as
|
||||
* a JSONObject with a "tagName" property. If the tag has attributes, then
|
||||
* the attributes will be in the JSONObject as properties. If the tag
|
||||
* contains children, the object will have a "childNodes" property which
|
||||
* will be an array of strings and JsonML JSONObjects.
|
||||
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param x An XMLTokener of the XML source text.
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null, false);
|
||||
return (JSONObject)parse(x, false, null, false, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -373,7 +461,7 @@ public class JSONML {
|
||||
* contains children, the object will have a "childNodes" property which
|
||||
* will be an array of strings and JsonML JSONObjects.
|
||||
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param x An XMLTokener of the XML source text.
|
||||
* @param keepStrings If true, then values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
@@ -381,7 +469,29 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null, keepStrings);
|
||||
return (JSONObject)parse(x, false, null, keepStrings, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONObject using the JsonML transform. Each XML tag is represented as
|
||||
* a JSONObject with a "tagName" property. If the tag has attributes, then
|
||||
* the attributes will be in the JSONObject as properties. If the tag
|
||||
* contains children, the object will have a "childNodes" property which
|
||||
* will be an array of strings and JsonML JSONObjects.
|
||||
|
||||
* Comments, prologs, DTDs, and <pre>{@code <[ [ ]]>}</pre> are ignored.
|
||||
* @param x An XMLTokener of the XML source text.
|
||||
* @param config The parser configuration:
|
||||
* JSONMLParserConfiguration.ORIGINAL is the default behaviour;
|
||||
* JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean
|
||||
* or numeric values and will instead be left as strings
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null, config, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -462,6 +572,7 @@ public class JSONML {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverse the JSONML transformation, making an XML text from a JSONObject.
|
||||
* The JSONObject must contain a "tagName" property. If it has children,
|
||||
69
src/main/java/org/json/JSONMLParserConfiguration.java
Normal file
69
src/main/java/org/json/JSONMLParserConfiguration.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package org.json;
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Configuration object for the XML to JSONML parser. The configuration is immutable.
|
||||
*/
|
||||
@SuppressWarnings({""})
|
||||
public class JSONMLParserConfiguration extends ParserConfiguration {
|
||||
|
||||
/**
|
||||
* We can override the default maximum nesting depth if needed.
|
||||
*/
|
||||
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH;
|
||||
|
||||
/** Original Configuration of the XML to JSONML Parser. */
|
||||
public static final JSONMLParserConfiguration ORIGINAL
|
||||
= new JSONMLParserConfiguration();
|
||||
/** Original configuration of the XML to JSONML Parser except that values are kept as strings. */
|
||||
public static final JSONMLParserConfiguration KEEP_STRINGS
|
||||
= new JSONMLParserConfiguration().withKeepStrings(true);
|
||||
|
||||
/**
|
||||
* Default parser configuration. Does not keep strings (tries to implicitly convert values).
|
||||
*/
|
||||
public JSONMLParserConfiguration() {
|
||||
super();
|
||||
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser string processing and use the default CDATA Tag Name as "content".
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @param maxNestingDepth <code>int</code> to limit the nesting depth
|
||||
*/
|
||||
protected JSONMLParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
|
||||
super(keepStrings, maxNestingDepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a new instance of the same configuration.
|
||||
*/
|
||||
@Override
|
||||
protected JSONMLParserConfiguration clone() {
|
||||
// future modifications to this method should always ensure a "deep"
|
||||
// clone in the case of collections. i.e. if a Map is added as a configuration
|
||||
// item, a new map instance should be created and if possible each value in the
|
||||
// map should be cloned as well. If the values of the map are known to also
|
||||
// be immutable, then a shallow clone of the map is acceptable.
|
||||
return new JSONMLParserConfiguration(
|
||||
this.keepStrings,
|
||||
this.maxNestingDepth
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public JSONMLParserConfiguration withKeepStrings(final boolean newVal) {
|
||||
return super.withKeepStrings(newVal);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
|
||||
return super.withMaxNestingDepth(maxNestingDepth);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
152
src/main/java/org/json/JSONParserConfiguration.java
Normal file
152
src/main/java/org/json/JSONParserConfiguration.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package org.json;
|
||||
|
||||
/**
|
||||
* Configuration object for the JSON parser. The configuration is immutable.
|
||||
*/
|
||||
public class JSONParserConfiguration extends ParserConfiguration {
|
||||
/**
|
||||
* Used to indicate whether to overwrite duplicate key or not.
|
||||
*/
|
||||
private boolean overwriteDuplicateKey;
|
||||
|
||||
/**
|
||||
* Used to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.
|
||||
*/
|
||||
private boolean useNativeNulls;
|
||||
|
||||
/**
|
||||
* Configuration with the default values.
|
||||
*/
|
||||
public JSONParserConfiguration() {
|
||||
super();
|
||||
this.overwriteDuplicateKey = false;
|
||||
// DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing
|
||||
// this.strictMode = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.
|
||||
* Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed.
|
||||
*/
|
||||
private boolean strictMode;
|
||||
|
||||
@Override
|
||||
protected JSONParserConfiguration clone() {
|
||||
JSONParserConfiguration clone = new JSONParserConfiguration();
|
||||
clone.overwriteDuplicateKey = overwriteDuplicateKey;
|
||||
clone.strictMode = strictMode;
|
||||
clone.maxNestingDepth = maxNestingDepth;
|
||||
clone.keepStrings = keepStrings;
|
||||
clone.useNativeNulls = useNativeNulls;
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
||||
* when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into
|
||||
* JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException
|
||||
* if the maximum depth is reached.
|
||||
*
|
||||
* @param maxNestingDepth the maximum nesting depth allowed to the JSON parser
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
|
||||
JSONParserConfiguration clone = this.clone();
|
||||
clone.maxNestingDepth = maxNestingDepth;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls the parser's behavior when meeting duplicate keys.
|
||||
* If set to false, the parser will throw a JSONException when meeting a duplicate key.
|
||||
* Or the duplicate key's value will be overwritten.
|
||||
*
|
||||
* @param overwriteDuplicateKey defines should the parser overwrite duplicate keys.
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) {
|
||||
JSONParserConfiguration clone = this.clone();
|
||||
clone.overwriteDuplicateKey = overwriteDuplicateKey;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls the parser's behavior when meeting Java null values while converting maps.
|
||||
* If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject.
|
||||
* Or the map entry will be ignored.
|
||||
*
|
||||
* @param useNativeNulls defines if the parser should convert null values in Java maps
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public JSONParserConfiguration withUseNativeNulls(final boolean useNativeNulls) {
|
||||
JSONParserConfiguration clone = this.clone();
|
||||
clone.useNativeNulls = useNativeNulls;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the strict mode configuration for the JSON parser with default true value
|
||||
* <p>
|
||||
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
|
||||
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
|
||||
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
|
||||
* @return a new JSONParserConfiguration instance with the updated strict mode setting
|
||||
*/
|
||||
public JSONParserConfiguration withStrictMode() {
|
||||
return withStrictMode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the strict mode configuration for the JSON parser.
|
||||
* <p>
|
||||
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
|
||||
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
|
||||
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
|
||||
*
|
||||
* @param mode a boolean value indicating whether strict mode should be enabled or not
|
||||
* @return a new JSONParserConfiguration instance with the updated strict mode setting
|
||||
*/
|
||||
public JSONParserConfiguration withStrictMode(final boolean mode) {
|
||||
JSONParserConfiguration clone = this.clone();
|
||||
clone.strictMode = mode;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* The parser's behavior when meeting duplicate keys, controls whether the parser should
|
||||
* overwrite duplicate keys or not.
|
||||
*
|
||||
* @return The <code>overwriteDuplicateKey</code> configuration value.
|
||||
*/
|
||||
public boolean isOverwriteDuplicateKey() {
|
||||
return this.overwriteDuplicateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* The parser's behavior when meeting a null value in a java map, controls whether the parser should
|
||||
* write a JSON entry with a null value (<code>isUseNativeNulls() == true</code>)
|
||||
* or ignore that map entry (<code>isUseNativeNulls() == false</code>).
|
||||
*
|
||||
* @return The <code>useNativeNulls</code> configuration value.
|
||||
*/
|
||||
public boolean isUseNativeNulls() {
|
||||
return this.useNativeNulls;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.
|
||||
* Otherwise, the parser is more relaxed and might tolerate some invalid characters.
|
||||
*
|
||||
* @return the current strict mode setting.
|
||||
*/
|
||||
public boolean isStrictMode() {
|
||||
return this.strictMode;
|
||||
}
|
||||
}
|
||||
@@ -10,27 +10,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -62,12 +42,19 @@ public class JSONPointer {
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
/**
|
||||
* Constructs a new Builder object.
|
||||
*/
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
// Segments for the eventual JSONPointer string
|
||||
private final List<String> refTokens = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Creates a {@code JSONPointer} instance using the tokens previously set using the
|
||||
* {@link #append(String)} method calls.
|
||||
* @return a JSONPointer object
|
||||
*/
|
||||
public JSONPointer build() {
|
||||
return new JSONPointer(this.refTokens);
|
||||
@@ -182,14 +169,21 @@ public class JSONPointer {
|
||||
//}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new JSONPointer instance with the provided list of reference tokens.
|
||||
*
|
||||
* @param refTokens A list of strings representing the reference tokens for the JSON Pointer.
|
||||
* Each token identifies a step in the path to the targeted value.
|
||||
*/
|
||||
public JSONPointer(List<String> refTokens) {
|
||||
this.refTokens = new ArrayList<String>(refTokens);
|
||||
}
|
||||
|
||||
private String unescape(String token) {
|
||||
return token.replace("~1", "/").replace("~0", "~")
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\\\", "\\");
|
||||
/**
|
||||
* @see <a href="https://tools.ietf.org/html/rfc6901#section-3">rfc6901 section 3</a>
|
||||
*/
|
||||
private static String unescape(String token) {
|
||||
return token.replace("~1", "/").replace("~0", "~");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,7 +222,7 @@ public class JSONPointer {
|
||||
* @return the matched object. If no matching item is found a
|
||||
* @throws JSONPointerException is thrown if the index is out of bounds
|
||||
*/
|
||||
private Object readByIndexToken(Object current, String indexToken) throws JSONPointerException {
|
||||
private static Object readByIndexToken(Object current, String indexToken) throws JSONPointerException {
|
||||
try {
|
||||
int index = Integer.parseInt(indexToken);
|
||||
JSONArray currentArr = (JSONArray) current;
|
||||
@@ -262,21 +256,21 @@ public class JSONPointer {
|
||||
/**
|
||||
* Escapes path segment values to an unambiguous form.
|
||||
* The escape char to be inserted is '~'. The chars to be escaped
|
||||
* are ~, which maps to ~0, and /, which maps to ~1. Backslashes
|
||||
* and double quote chars are also escaped.
|
||||
* are ~, which maps to ~0, and /, which maps to ~1.
|
||||
* @param token the JSONPointer segment value to be escaped
|
||||
* @return the escaped value for the token
|
||||
*
|
||||
* @see <a href="https://tools.ietf.org/html/rfc6901#section-3">rfc6901 section 3</a>
|
||||
*/
|
||||
private String escape(String token) {
|
||||
private static String escape(String token) {
|
||||
return token.replace("~", "~0")
|
||||
.replace("/", "~1")
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"");
|
||||
.replace("/", "~1");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing the JSONPointer path value using URI
|
||||
* fragment identifier representation
|
||||
* @return a uri fragment string
|
||||
*/
|
||||
public String toURIFragment() {
|
||||
try {
|
||||
36
src/main/java/org/json/JSONPointerException.java
Normal file
36
src/main/java/org/json/JSONPointerException.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The JSONPointerException is thrown by {@link JSONPointer} if an error occurs
|
||||
* during evaluating a pointer.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-13
|
||||
*/
|
||||
public class JSONPointerException extends JSONException {
|
||||
private static final long serialVersionUID = 8872944667561856751L;
|
||||
|
||||
/**
|
||||
* Constructs a new JSONPointerException with the specified error message.
|
||||
*
|
||||
* @param message The detail message describing the reason for the exception.
|
||||
*/
|
||||
public JSONPointerException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new JSONPointerException with the specified error message and cause.
|
||||
*
|
||||
* @param message The detail message describing the reason for the exception.
|
||||
* @param cause The cause of the exception.
|
||||
*/
|
||||
public JSONPointerException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
23
src/main/java/org/json/JSONPropertyIgnore.java
Normal file
23
src/main/java/org/json/JSONPropertyIgnore.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Use this annotation on a getter method to override the Bean name
|
||||
* parser for Bean -> JSONObject mapping. If this annotation is
|
||||
* present at any level in the class hierarchy, then the method will
|
||||
* not be serialized from the bean into the JSONObject.
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RUNTIME)
|
||||
@Target({METHOD})
|
||||
public @interface JSONPropertyIgnore { }
|
||||
28
src/main/java/org/json/JSONPropertyName.java
Normal file
28
src/main/java/org/json/JSONPropertyName.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Use this annotation on a getter method to override the Bean name
|
||||
* parser for Bean -> JSONObject mapping. A value set to empty string <code>""</code>
|
||||
* will have the Bean parser fall back to the default field name processing.
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RUNTIME)
|
||||
@Target({METHOD})
|
||||
public @interface JSONPropertyName {
|
||||
/**
|
||||
* The value of the JSON property.
|
||||
* @return The name of the property as to be used in the JSON Object.
|
||||
*/
|
||||
String value();
|
||||
}
|
||||
@@ -1,4 +1,9 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
|
||||
* method so that a class can change the behavior of
|
||||
@@ -1,79 +1,59 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2006 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* JSONStringer provides a quick and convenient way of producing JSON text.
|
||||
* The texts produced strictly conform to JSON syntax rules. No whitespace is
|
||||
* added, so the results are ready for transmission or storage. Each instance of
|
||||
* JSONStringer can produce one JSON text.
|
||||
* <p>
|
||||
* A JSONStringer instance provides a <code>value</code> method for appending
|
||||
* values to the
|
||||
* text, and a <code>key</code>
|
||||
* method for adding keys before values in objects. There are <code>array</code>
|
||||
* and <code>endArray</code> methods that make and bound array values, and
|
||||
* <code>object</code> and <code>endObject</code> methods which make and bound
|
||||
* object values. All of these methods return the JSONWriter instance,
|
||||
* permitting cascade style. For example, <pre>
|
||||
* myString = new JSONStringer()
|
||||
* .object()
|
||||
* .key("JSON")
|
||||
* .value("Hello, World!")
|
||||
* .endObject()
|
||||
* .toString();</pre> which produces the string <pre>
|
||||
* {"JSON":"Hello, World!"}</pre>
|
||||
* <p>
|
||||
* The first method called must be <code>array</code> or <code>object</code>.
|
||||
* There are no methods for adding commas or colons. JSONStringer adds them for
|
||||
* you. Objects and arrays can be nested up to 20 levels deep.
|
||||
* <p>
|
||||
* This can sometimes be easier than using a JSONObject to build a string.
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class JSONStringer extends JSONWriter {
|
||||
/**
|
||||
* Make a fresh JSONStringer. It can be used to build one JSON text.
|
||||
*/
|
||||
public JSONStringer() {
|
||||
super(new StringWriter());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the JSON text. This method is used to obtain the product of the
|
||||
* JSONStringer instance. It will return <code>null</code> if there was a
|
||||
* problem in the construction of the JSON text (such as the calls to
|
||||
* <code>array</code> were not properly balanced with calls to
|
||||
* <code>endArray</code>).
|
||||
* @return The JSON text.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.mode == 'd' ? this.writer.toString() : null;
|
||||
}
|
||||
}
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* JSONStringer provides a quick and convenient way of producing JSON text.
|
||||
* The texts produced strictly conform to JSON syntax rules. No whitespace is
|
||||
* added, so the results are ready for transmission or storage. Each instance of
|
||||
* JSONStringer can produce one JSON text.
|
||||
* <p>
|
||||
* A JSONStringer instance provides a <code>value</code> method for appending
|
||||
* values to the
|
||||
* text, and a <code>key</code>
|
||||
* method for adding keys before values in objects. There are <code>array</code>
|
||||
* and <code>endArray</code> methods that make and bound array values, and
|
||||
* <code>object</code> and <code>endObject</code> methods which make and bound
|
||||
* object values. All of these methods return the JSONWriter instance,
|
||||
* permitting cascade style. For example, <pre>
|
||||
* myString = new JSONStringer()
|
||||
* .object()
|
||||
* .key("JSON")
|
||||
* .value("Hello, World!")
|
||||
* .endObject()
|
||||
* .toString();</pre> which produces the string <pre>
|
||||
* {"JSON":"Hello, World!"}</pre>
|
||||
* <p>
|
||||
* The first method called must be <code>array</code> or <code>object</code>.
|
||||
* There are no methods for adding commas or colons. JSONStringer adds them for
|
||||
* you. Objects and arrays can be nested up to 200 levels deep.
|
||||
* <p>
|
||||
* This can sometimes be easier than using a JSONObject to build a string.
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class JSONStringer extends JSONWriter {
|
||||
/**
|
||||
* Make a fresh JSONStringer. It can be used to build one JSON text.
|
||||
*/
|
||||
public JSONStringer() {
|
||||
super(new StringWriter());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the JSON text. This method is used to obtain the product of the
|
||||
* JSONStringer instance. It will return <code>null</code> if there was a
|
||||
* problem in the construction of the JSON text (such as the calls to
|
||||
* <code>array</code> were not properly balanced with calls to
|
||||
* <code>endArray</code>).
|
||||
* @return The JSON text.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.mode == 'd' ? this.writer.toString() : null;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,10 @@
|
||||
package org.json;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -56,13 +32,27 @@ public class JSONTokener {
|
||||
/** the number of characters read in the previous line. */
|
||||
private long characterPreviousLine;
|
||||
|
||||
// access to this object is required for strict mode checking
|
||||
private JSONParserConfiguration jsonParserConfiguration;
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
||||
*
|
||||
* @param reader A reader.
|
||||
* @param reader the source.
|
||||
*/
|
||||
public JSONTokener(Reader reader) {
|
||||
this(reader, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from a Reader with a given JSONParserConfiguration. The caller must close the Reader.
|
||||
*
|
||||
* @param reader the source.
|
||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||
*
|
||||
*/
|
||||
public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
|
||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||
this.reader = reader.markSupported()
|
||||
? reader
|
||||
: new BufferedReader(reader);
|
||||
@@ -75,25 +65,60 @@ public class JSONTokener {
|
||||
this.line = 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
||||
* @param inputStream The source.
|
||||
*/
|
||||
public JSONTokener(InputStream inputStream) {
|
||||
this(new InputStreamReader(inputStream));
|
||||
this(inputStream, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
||||
* @param inputStream The source.
|
||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||
*/
|
||||
public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
|
||||
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")), jsonParserConfiguration);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from a string.
|
||||
*
|
||||
* @param s A source string.
|
||||
* @param source A source string.
|
||||
*/
|
||||
public JSONTokener(String s) {
|
||||
this(new StringReader(s));
|
||||
public JSONTokener(String source) {
|
||||
this(new StringReader(source));
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
||||
* @param source The source.
|
||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||
*/
|
||||
public JSONTokener(String source, JSONParserConfiguration jsonParserConfiguration) {
|
||||
this(new StringReader(source), jsonParserConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter
|
||||
* @return jsonParserConfiguration
|
||||
*/
|
||||
public JSONParserConfiguration getJsonParserConfiguration() {
|
||||
return jsonParserConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter
|
||||
* @param jsonParserConfiguration new value for jsonParserConfiguration
|
||||
*
|
||||
* @deprecated method should not be used
|
||||
*/
|
||||
@Deprecated
|
||||
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back up one character. This provides a sort of lookahead capability,
|
||||
@@ -145,7 +170,7 @@ public class JSONTokener {
|
||||
|
||||
/**
|
||||
* Checks if the end of the input has been reached.
|
||||
*
|
||||
*
|
||||
* @return true if at the end of the file and we didn't step back
|
||||
*/
|
||||
public boolean end() {
|
||||
@@ -210,6 +235,12 @@ public class JSONTokener {
|
||||
return this.previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last character read from the input or '\0' if nothing has been read yet.
|
||||
* @return the last character read from the input.
|
||||
*/
|
||||
protected char getPrevious() { return this.previous;}
|
||||
|
||||
/**
|
||||
* Increments the internal indexes according to the previous character
|
||||
* read and the character passed as the current character.
|
||||
@@ -317,7 +348,8 @@ public class JSONTokener {
|
||||
case 0:
|
||||
case '\n':
|
||||
case '\r':
|
||||
throw this.syntaxError("Unterminated string");
|
||||
throw this.syntaxError("Unterminated string. " +
|
||||
"Character with int code " + (int) c + " is not allowed within a quoted string.");
|
||||
case '\\':
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
@@ -337,10 +369,12 @@ public class JSONTokener {
|
||||
sb.append('\r');
|
||||
break;
|
||||
case 'u':
|
||||
String next = this.next(4);
|
||||
try {
|
||||
sb.append((char)Integer.parseInt(this.next(4), 16));
|
||||
sb.append((char)Integer.parseInt(next, 16));
|
||||
} catch (NumberFormatException e) {
|
||||
throw this.syntaxError("Illegal escape.", e);
|
||||
throw this.syntaxError("Illegal escape. " +
|
||||
"\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
|
||||
}
|
||||
break;
|
||||
case '"':
|
||||
@@ -350,7 +384,7 @@ public class JSONTokener {
|
||||
sb.append(c);
|
||||
break;
|
||||
default:
|
||||
throw this.syntaxError("Illegal escape.");
|
||||
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -420,18 +454,38 @@ public class JSONTokener {
|
||||
*/
|
||||
public Object nextValue() throws JSONException {
|
||||
char c = this.nextClean();
|
||||
switch (c) {
|
||||
case '{':
|
||||
this.back();
|
||||
try {
|
||||
return new JSONObject(this, jsonParserConfiguration);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
case '[':
|
||||
this.back();
|
||||
try {
|
||||
return new JSONArray(this, jsonParserConfiguration);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
}
|
||||
return nextSimpleValue(c);
|
||||
}
|
||||
|
||||
Object nextSimpleValue(char c) {
|
||||
String string;
|
||||
|
||||
// Strict mode only allows strings with explicit double quotes
|
||||
if (jsonParserConfiguration != null &&
|
||||
jsonParserConfiguration.isStrictMode() &&
|
||||
c == '\'') {
|
||||
throw this.syntaxError("Strict mode error: Single quoted strings are not allowed");
|
||||
}
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
return this.nextString(c);
|
||||
case '{':
|
||||
this.back();
|
||||
return new JSONObject(this);
|
||||
case '[':
|
||||
this.back();
|
||||
return new JSONArray(this);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -456,7 +510,14 @@ public class JSONTokener {
|
||||
if ("".equals(string)) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
Object obj = JSONObject.stringToValue(string);
|
||||
// Strict mode only allows strings with explicit double quotes
|
||||
if (jsonParserConfiguration != null &&
|
||||
jsonParserConfiguration.isStrictMode() &&
|
||||
obj instanceof String) {
|
||||
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
@@ -528,4 +589,15 @@ public class JSONTokener {
|
||||
return " at " + this.index + " [character " + this.character + " line " +
|
||||
this.line + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying reader, releasing any resources associated with it.
|
||||
*
|
||||
* @throws IOException If an I/O error occurs while closing the reader.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if(reader!=null){
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,413 +1,394 @@
|
||||
package org.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
Copyright (c) 2006 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* JSONWriter provides a quick and convenient way of producing JSON text.
|
||||
* The texts produced strictly conform to JSON syntax rules. No whitespace is
|
||||
* added, so the results are ready for transmission or storage. Each instance of
|
||||
* JSONWriter can produce one JSON text.
|
||||
* <p>
|
||||
* A JSONWriter instance provides a <code>value</code> method for appending
|
||||
* values to the
|
||||
* text, and a <code>key</code>
|
||||
* method for adding keys before values in objects. There are <code>array</code>
|
||||
* and <code>endArray</code> methods that make and bound array values, and
|
||||
* <code>object</code> and <code>endObject</code> methods which make and bound
|
||||
* object values. All of these methods return the JSONWriter instance,
|
||||
* permitting a cascade style. For example, <pre>
|
||||
* new JSONWriter(myWriter)
|
||||
* .object()
|
||||
* .key("JSON")
|
||||
* .value("Hello, World!")
|
||||
* .endObject();</pre> which writes <pre>
|
||||
* {"JSON":"Hello, World!"}</pre>
|
||||
* <p>
|
||||
* The first method called must be <code>array</code> or <code>object</code>.
|
||||
* There are no methods for adding commas or colons. JSONWriter adds them for
|
||||
* you. Objects and arrays can be nested up to 200 levels deep.
|
||||
* <p>
|
||||
* This can sometimes be easier than using a JSONObject to build a string.
|
||||
* @author JSON.org
|
||||
* @version 2016-08-08
|
||||
*/
|
||||
public class JSONWriter {
|
||||
private static final int maxdepth = 200;
|
||||
|
||||
/**
|
||||
* The comma flag determines if a comma should be output before the next
|
||||
* value.
|
||||
*/
|
||||
private boolean comma;
|
||||
|
||||
/**
|
||||
* The current mode. Values:
|
||||
* 'a' (array),
|
||||
* 'd' (done),
|
||||
* 'i' (initial),
|
||||
* 'k' (key),
|
||||
* 'o' (object).
|
||||
*/
|
||||
protected char mode;
|
||||
|
||||
/**
|
||||
* The object/array stack.
|
||||
*/
|
||||
private final JSONObject stack[];
|
||||
|
||||
/**
|
||||
* The stack top index. A value of 0 indicates that the stack is empty.
|
||||
*/
|
||||
private int top;
|
||||
|
||||
/**
|
||||
* The writer that will receive the output.
|
||||
*/
|
||||
protected Appendable writer;
|
||||
|
||||
/**
|
||||
* Make a fresh JSONWriter. It can be used to build one JSON text.
|
||||
*/
|
||||
public JSONWriter(Appendable w) {
|
||||
this.comma = false;
|
||||
this.mode = 'i';
|
||||
this.stack = new JSONObject[maxdepth];
|
||||
this.top = 0;
|
||||
this.writer = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a value.
|
||||
* @param string A string value.
|
||||
* @return this
|
||||
* @throws JSONException If the value is out of sequence.
|
||||
*/
|
||||
private JSONWriter append(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
try {
|
||||
if (this.comma && this.mode == 'a') {
|
||||
this.writer.append(',');
|
||||
}
|
||||
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') {
|
||||
this.mode = 'k';
|
||||
}
|
||||
this.comma = true;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Value out of sequence.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin appending a new array. All values until the balancing
|
||||
* <code>endArray</code> will be appended to this array. The
|
||||
* <code>endArray</code> method must be called to mark the array's end.
|
||||
* @return this
|
||||
* @throws JSONException If the nesting is too deep, or if the object is
|
||||
* started in the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
*/
|
||||
public JSONWriter array() throws JSONException {
|
||||
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
|
||||
this.push(null);
|
||||
this.append("[");
|
||||
this.comma = false;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Misplaced array.");
|
||||
}
|
||||
|
||||
/**
|
||||
* End something.
|
||||
* @param m Mode
|
||||
* @param c Closing character
|
||||
* @return this
|
||||
* @throws JSONException If unbalanced.
|
||||
*/
|
||||
private JSONWriter end(char m, char c) throws JSONException {
|
||||
if (this.mode != m) {
|
||||
throw new JSONException(m == 'a'
|
||||
? "Misplaced endArray."
|
||||
: "Misplaced endObject.");
|
||||
}
|
||||
this.pop(m);
|
||||
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;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* End an array. This method most be called to balance calls to
|
||||
* <code>array</code>.
|
||||
* @return this
|
||||
* @throws JSONException If incorrectly nested.
|
||||
*/
|
||||
public JSONWriter endArray() throws JSONException {
|
||||
return this.end('a', ']');
|
||||
}
|
||||
|
||||
/**
|
||||
* End an object. This method most be called to balance calls to
|
||||
* <code>object</code>.
|
||||
* @return this
|
||||
* @throws JSONException If incorrectly nested.
|
||||
*/
|
||||
public JSONWriter endObject() throws JSONException {
|
||||
return this.end('k', '}');
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a key. The key will be associated with the next value. In an
|
||||
* object, every value must be preceded by a key.
|
||||
* @param string A key string.
|
||||
* @return this
|
||||
* @throws JSONException If the key is out of place. For example, keys
|
||||
* do not belong in arrays or if the key is null.
|
||||
*/
|
||||
public JSONWriter key(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if (this.mode == 'k') {
|
||||
try {
|
||||
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(',');
|
||||
}
|
||||
this.writer.append(JSONObject.quote(string));
|
||||
this.writer.append(':');
|
||||
this.comma = false;
|
||||
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);
|
||||
}
|
||||
}
|
||||
throw new JSONException("Misplaced key.");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begin appending a new object. All keys and values until the balancing
|
||||
* <code>endObject</code> will be appended to this object. The
|
||||
* <code>endObject</code> method must be called to mark the object's end.
|
||||
* @return this
|
||||
* @throws JSONException If the nesting is too deep, or if the object is
|
||||
* started in the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
*/
|
||||
public JSONWriter object() throws JSONException {
|
||||
if (this.mode == 'i') {
|
||||
this.mode = 'o';
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
this.append("{");
|
||||
this.push(new JSONObject());
|
||||
this.comma = false;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Misplaced object.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pop an array or object scope.
|
||||
* @param c The scope to close.
|
||||
* @throws JSONException If nesting is wrong.
|
||||
*/
|
||||
private void pop(char c) throws JSONException {
|
||||
if (this.top <= 0) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
char m = this.stack[this.top - 1] == null ? 'a' : 'k';
|
||||
if (m != c) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
this.top -= 1;
|
||||
this.mode = this.top == 0
|
||||
? 'd'
|
||||
: this.stack[this.top - 1] == null
|
||||
? 'a'
|
||||
: 'k';
|
||||
}
|
||||
|
||||
/**
|
||||
* Push an array or object scope.
|
||||
* @param jo The scope to open.
|
||||
* @throws JSONException If nesting is too deep.
|
||||
*/
|
||||
private void push(JSONObject jo) throws JSONException {
|
||||
if (this.top >= maxdepth) {
|
||||
throw new JSONException("Nesting too deep.");
|
||||
}
|
||||
this.stack[this.top] = jo;
|
||||
this.mode = jo == null ? 'a' : 'k';
|
||||
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> <small>(left
|
||||
* brace)</small> and ending with <code>}</code> <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) {
|
||||
String object;
|
||||
try {
|
||||
object = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (object != null) {
|
||||
return 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);
|
||||
if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) {
|
||||
// Close enough to a JSON number that we will return it unquoted
|
||||
return numberAsString;
|
||||
}
|
||||
// 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
|
||||
* <code>false</code>.
|
||||
* @param b A boolean.
|
||||
* @return this
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONWriter value(boolean b) throws JSONException {
|
||||
return this.append(b ? "true" : "false");
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a double value.
|
||||
* @param d A double.
|
||||
* @return this
|
||||
* @throws JSONException If the number is not finite.
|
||||
*/
|
||||
public JSONWriter value(double d) throws JSONException {
|
||||
return this.value(Double.valueOf(d));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a long value.
|
||||
* @param l A long.
|
||||
* @return this
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONWriter value(long l) throws JSONException {
|
||||
return this.append(Long.toString(l));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Append an object value.
|
||||
* @param object The object to append. It can be null, or a Boolean, Number,
|
||||
* String, JSONObject, or JSONArray, or an object that implements JSONString.
|
||||
* @return this
|
||||
* @throws JSONException If the value is out of sequence.
|
||||
*/
|
||||
public JSONWriter value(Object object) throws JSONException {
|
||||
return this.append(valueToString(object));
|
||||
}
|
||||
}
|
||||
package org.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* JSONWriter provides a quick and convenient way of producing JSON text.
|
||||
* The texts produced strictly conform to JSON syntax rules. No whitespace is
|
||||
* added, so the results are ready for transmission or storage. Each instance of
|
||||
* JSONWriter can produce one JSON text.
|
||||
* <p>
|
||||
* A JSONWriter instance provides a <code>value</code> method for appending
|
||||
* values to the
|
||||
* text, and a <code>key</code>
|
||||
* method for adding keys before values in objects. There are <code>array</code>
|
||||
* and <code>endArray</code> methods that make and bound array values, and
|
||||
* <code>object</code> and <code>endObject</code> methods which make and bound
|
||||
* object values. All of these methods return the JSONWriter instance,
|
||||
* permitting a cascade style. For example, <pre>
|
||||
* new JSONWriter(myWriter)
|
||||
* .object()
|
||||
* .key("JSON")
|
||||
* .value("Hello, World!")
|
||||
* .endObject();</pre> which writes <pre>
|
||||
* {"JSON":"Hello, World!"}</pre>
|
||||
* <p>
|
||||
* The first method called must be <code>array</code> or <code>object</code>.
|
||||
* There are no methods for adding commas or colons. JSONWriter adds them for
|
||||
* you. Objects and arrays can be nested up to 200 levels deep.
|
||||
* <p>
|
||||
* This can sometimes be easier than using a JSONObject to build a string.
|
||||
* @author JSON.org
|
||||
* @version 2016-08-08
|
||||
*/
|
||||
public class JSONWriter {
|
||||
private static final int maxdepth = 200;
|
||||
|
||||
/**
|
||||
* The comma flag determines if a comma should be output before the next
|
||||
* value.
|
||||
*/
|
||||
private boolean comma;
|
||||
|
||||
/**
|
||||
* The current mode. Values:
|
||||
* 'a' (array),
|
||||
* 'd' (done),
|
||||
* 'i' (initial),
|
||||
* 'k' (key),
|
||||
* 'o' (object).
|
||||
*/
|
||||
protected char mode;
|
||||
|
||||
/**
|
||||
* The object/array stack.
|
||||
*/
|
||||
private final JSONObject stack[];
|
||||
|
||||
/**
|
||||
* The stack top index. A value of 0 indicates that the stack is empty.
|
||||
*/
|
||||
private int top;
|
||||
|
||||
/**
|
||||
* The writer that will receive the output.
|
||||
*/
|
||||
protected Appendable writer;
|
||||
|
||||
/**
|
||||
* Make a fresh JSONWriter. It can be used to build one JSON text.
|
||||
* @param w an appendable object
|
||||
*/
|
||||
public JSONWriter(Appendable w) {
|
||||
this.comma = false;
|
||||
this.mode = 'i';
|
||||
this.stack = new JSONObject[maxdepth];
|
||||
this.top = 0;
|
||||
this.writer = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a value.
|
||||
* @param string A string value.
|
||||
* @return this
|
||||
* @throws JSONException If the value is out of sequence.
|
||||
*/
|
||||
private JSONWriter append(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
try {
|
||||
if (this.comma && this.mode == 'a') {
|
||||
this.writer.append(',');
|
||||
}
|
||||
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') {
|
||||
this.mode = 'k';
|
||||
}
|
||||
this.comma = true;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Value out of sequence.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin appending a new array. All values until the balancing
|
||||
* <code>endArray</code> will be appended to this array. The
|
||||
* <code>endArray</code> method must be called to mark the array's end.
|
||||
* @return this
|
||||
* @throws JSONException If the nesting is too deep, or if the object is
|
||||
* started in the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
*/
|
||||
public JSONWriter array() throws JSONException {
|
||||
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
|
||||
this.push(null);
|
||||
this.append("[");
|
||||
this.comma = false;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Misplaced array.");
|
||||
}
|
||||
|
||||
/**
|
||||
* End something.
|
||||
* @param m Mode
|
||||
* @param c Closing character
|
||||
* @return this
|
||||
* @throws JSONException If unbalanced.
|
||||
*/
|
||||
private JSONWriter end(char m, char c) throws JSONException {
|
||||
if (this.mode != m) {
|
||||
throw new JSONException(m == 'a'
|
||||
? "Misplaced endArray."
|
||||
: "Misplaced endObject.");
|
||||
}
|
||||
this.pop(m);
|
||||
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;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* End an array. This method most be called to balance calls to
|
||||
* <code>array</code>.
|
||||
* @return this
|
||||
* @throws JSONException If incorrectly nested.
|
||||
*/
|
||||
public JSONWriter endArray() throws JSONException {
|
||||
return this.end('a', ']');
|
||||
}
|
||||
|
||||
/**
|
||||
* End an object. This method most be called to balance calls to
|
||||
* <code>object</code>.
|
||||
* @return this
|
||||
* @throws JSONException If incorrectly nested.
|
||||
*/
|
||||
public JSONWriter endObject() throws JSONException {
|
||||
return this.end('k', '}');
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a key. The key will be associated with the next value. In an
|
||||
* object, every value must be preceded by a key.
|
||||
* @param string A key string.
|
||||
* @return this
|
||||
* @throws JSONException If the key is out of place. For example, keys
|
||||
* do not belong in arrays or if the key is null.
|
||||
*/
|
||||
public JSONWriter key(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if (this.mode == 'k') {
|
||||
try {
|
||||
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(',');
|
||||
}
|
||||
this.writer.append(JSONObject.quote(string));
|
||||
this.writer.append(':');
|
||||
this.comma = false;
|
||||
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);
|
||||
}
|
||||
}
|
||||
throw new JSONException("Misplaced key.");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begin appending a new object. All keys and values until the balancing
|
||||
* <code>endObject</code> will be appended to this object. The
|
||||
* <code>endObject</code> method must be called to mark the object's end.
|
||||
* @return this
|
||||
* @throws JSONException If the nesting is too deep, or if the object is
|
||||
* started in the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
*/
|
||||
public JSONWriter object() throws JSONException {
|
||||
if (this.mode == 'i') {
|
||||
this.mode = 'o';
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
this.append("{");
|
||||
this.push(new JSONObject());
|
||||
this.comma = false;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Misplaced object.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pop an array or object scope.
|
||||
* @param c The scope to close.
|
||||
* @throws JSONException If nesting is wrong.
|
||||
*/
|
||||
private void pop(char c) throws JSONException {
|
||||
if (this.top <= 0) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
char m = this.stack[this.top - 1] == null ? 'a' : 'k';
|
||||
if (m != c) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
this.top -= 1;
|
||||
this.mode = this.top == 0
|
||||
? 'd'
|
||||
: this.stack[this.top - 1] == null
|
||||
? 'a'
|
||||
: 'k';
|
||||
}
|
||||
|
||||
/**
|
||||
* Push an array or object scope.
|
||||
* @param jo The scope to open.
|
||||
* @throws JSONException If nesting is too deep.
|
||||
*/
|
||||
private void push(JSONObject jo) throws JSONException {
|
||||
if (this.top >= maxdepth) {
|
||||
throw new JSONException("Nesting too deep.");
|
||||
}
|
||||
this.stack[this.top] = jo;
|
||||
this.mode = jo == null ? 'a' : 'k';
|
||||
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> <small>(left
|
||||
* brace)</small> and ending with <code>}</code> <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) {
|
||||
String object;
|
||||
try {
|
||||
object = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (object != null) {
|
||||
return 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);
|
||||
if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) {
|
||||
// Close enough to a JSON number that we will return it unquoted
|
||||
return numberAsString;
|
||||
}
|
||||
// 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
|
||||
* <code>false</code>.
|
||||
* @param b A boolean.
|
||||
* @return this
|
||||
* @throws JSONException if a called function has an error
|
||||
*/
|
||||
public JSONWriter value(boolean b) throws JSONException {
|
||||
return this.append(b ? "true" : "false");
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a double value.
|
||||
* @param d A double.
|
||||
* @return this
|
||||
* @throws JSONException If the number is not finite.
|
||||
*/
|
||||
public JSONWriter value(double d) throws JSONException {
|
||||
return this.value(Double.valueOf(d));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a long value.
|
||||
* @param l A long.
|
||||
* @return this
|
||||
* @throws JSONException if a called function has an error
|
||||
*/
|
||||
public JSONWriter value(long l) throws JSONException {
|
||||
return this.append(Long.toString(l));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Append an object value.
|
||||
* @param object The object to append. It can be null, or a Boolean, Number,
|
||||
* String, JSONObject, or JSONArray, or an object that implements JSONString.
|
||||
* @return this
|
||||
* @throws JSONException If the value is out of sequence.
|
||||
*/
|
||||
public JSONWriter value(Object object) throws JSONException {
|
||||
return this.append(valueToString(object));
|
||||
}
|
||||
}
|
||||
126
src/main/java/org/json/ParserConfiguration.java
Normal file
126
src/main/java/org/json/ParserConfiguration.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package org.json;
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Configuration base object for parsers. The configuration is immutable.
|
||||
*/
|
||||
@SuppressWarnings({""})
|
||||
public class ParserConfiguration {
|
||||
/**
|
||||
* Used to indicate there's no defined limit to the maximum nesting depth when parsing a document.
|
||||
*/
|
||||
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;
|
||||
|
||||
/**
|
||||
* The default maximum nesting depth when parsing a document.
|
||||
*/
|
||||
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
|
||||
|
||||
/**
|
||||
* Specifies if values should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||
*/
|
||||
protected boolean keepStrings;
|
||||
|
||||
/**
|
||||
* The maximum nesting depth when parsing an object.
|
||||
*/
|
||||
protected int maxNestingDepth;
|
||||
|
||||
/**
|
||||
* Constructs a new ParserConfiguration with default settings.
|
||||
*/
|
||||
public ParserConfiguration() {
|
||||
this.keepStrings = false;
|
||||
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new ParserConfiguration with the specified settings.
|
||||
*
|
||||
* @param keepStrings A boolean indicating whether to preserve strings during parsing.
|
||||
* @param maxNestingDepth An integer representing the maximum allowed nesting depth.
|
||||
*/
|
||||
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.maxNestingDepth = maxNestingDepth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a new instance of the same configuration.
|
||||
*/
|
||||
@Override
|
||||
protected ParserConfiguration clone() {
|
||||
// future modifications to this method should always ensure a "deep"
|
||||
// clone in the case of collections. i.e. if a Map is added as a configuration
|
||||
// item, a new map instance should be created and if possible each value in the
|
||||
// map should be cloned as well. If the values of the map are known to also
|
||||
// be immutable, then a shallow clone of the map is acceptable.
|
||||
return new ParserConfiguration(
|
||||
this.keepStrings,
|
||||
this.maxNestingDepth
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||
*
|
||||
* @return The <code>keepStrings</code> configuration value.
|
||||
*/
|
||||
public boolean isKeepStrings() {
|
||||
return this.keepStrings;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||
*
|
||||
* @param newVal new value to use for the <code>keepStrings</code> configuration option.
|
||||
* @param <T> the type of the configuration object
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
|
||||
T newConfig = (T) this.clone();
|
||||
newConfig.keepStrings = newVal;
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum nesting depth that the parser will descend before throwing an exception
|
||||
* when parsing an object (e.g. Map, Collection) into JSON-related objects.
|
||||
*
|
||||
* @return the maximum nesting depth set for this configuration
|
||||
*/
|
||||
public int getMaxNestingDepth() {
|
||||
return maxNestingDepth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
||||
* when parsing an object (e.g. Map, Collection) into JSON-related objects.
|
||||
* The default max nesting depth is 512, which means the parser will throw a JsonException if
|
||||
* the maximum depth is reached.
|
||||
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
|
||||
* which means the parses will go as deep as the maximum call stack size allows.
|
||||
*
|
||||
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
|
||||
* @param <T> the type of the configuration object
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
|
||||
T newConfig = (T) this.clone();
|
||||
|
||||
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
|
||||
newConfig.maxNestingDepth = maxNestingDepth;
|
||||
} else {
|
||||
newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH;
|
||||
}
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,7 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.util.Enumeration;
|
||||
@@ -33,11 +13,18 @@ import java.util.Properties;
|
||||
* @version 2015-05-05
|
||||
*/
|
||||
public class Property {
|
||||
|
||||
/**
|
||||
* Constructs a new Property object.
|
||||
*/
|
||||
public Property() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
|
||||
* @param properties java.util.Properties
|
||||
* @return JSONObject
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function has an error
|
||||
*/
|
||||
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
|
||||
// can't use the new constructor for Android support
|
||||
@@ -57,7 +44,7 @@ public class Property {
|
||||
* Converts the JSONObject into a property file object.
|
||||
* @param jo JSONObject
|
||||
* @return java.util.Properties
|
||||
* @throws JSONException
|
||||
* @throws JSONException if a called function has an error
|
||||
*/
|
||||
public static Properties toProperties(JSONObject jo) throws JSONException {
|
||||
Properties properties = new Properties();
|
||||
92
src/main/java/org/json/StringBuilderWriter.java
Normal file
92
src/main/java/org/json/StringBuilderWriter.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package org.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* Performance optimised alternative for {@link java.io.StringWriter}
|
||||
* using internally a {@link StringBuilder} instead of a {@link StringBuffer}.
|
||||
*/
|
||||
public class StringBuilderWriter extends Writer {
|
||||
private final StringBuilder builder;
|
||||
|
||||
/**
|
||||
* Create a new string builder writer using the default initial string-builder buffer size.
|
||||
*/
|
||||
public StringBuilderWriter() {
|
||||
builder = new StringBuilder();
|
||||
lock = builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new string builder writer using the specified initial string-builder buffer size.
|
||||
*
|
||||
* @param initialSize The number of {@code char} values that will fit into this buffer
|
||||
* before it is automatically expanded
|
||||
*
|
||||
* @throws IllegalArgumentException If {@code initialSize} is negative
|
||||
*/
|
||||
public StringBuilderWriter(int initialSize) {
|
||||
builder = new StringBuilder(initialSize);
|
||||
lock = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int c) {
|
||||
builder.append((char) c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(char[] cbuf, int offset, int length) {
|
||||
if ((offset < 0) || (offset > cbuf.length) || (length < 0) ||
|
||||
((offset + length) > cbuf.length) || ((offset + length) < 0)) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (length == 0) {
|
||||
return;
|
||||
}
|
||||
builder.append(cbuf, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str) {
|
||||
builder.append(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str, int offset, int length) {
|
||||
builder.append(str, offset, offset + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilderWriter append(CharSequence csq) {
|
||||
write(String.valueOf(csq));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilderWriter append(CharSequence csq, int start, int end) {
|
||||
if (csq == null) {
|
||||
csq = "null";
|
||||
}
|
||||
return append(csq.subSequence(start, end));
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilderWriter append(char c) {
|
||||
write(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,13 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2015 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
@@ -38,6 +20,12 @@ import java.util.Iterator;
|
||||
@SuppressWarnings("boxing")
|
||||
public class XML {
|
||||
|
||||
/**
|
||||
* Constructs a new XML object.
|
||||
*/
|
||||
public XML() {
|
||||
}
|
||||
|
||||
/** The Character '&'. */
|
||||
public static final Character AMP = '&';
|
||||
|
||||
@@ -50,7 +38,7 @@ public class XML {
|
||||
/** The Character '='. */
|
||||
public static final Character EQ = '=';
|
||||
|
||||
/** The Character '>'. */
|
||||
/** The Character <pre>{@code '>'. }</pre>*/
|
||||
public static final Character GT = '>';
|
||||
|
||||
/** The Character '<'. */
|
||||
@@ -66,10 +54,15 @@ public class XML {
|
||||
public static final Character SLASH = '/';
|
||||
|
||||
/**
|
||||
* Null attrubute name
|
||||
* Null attribute name
|
||||
*/
|
||||
public static final String NULL_ATTR = "xsi:nil";
|
||||
|
||||
/**
|
||||
* Represents the XML attribute name for specifying type information.
|
||||
*/
|
||||
public static final String TYPE_ATTR = "xsi:type";
|
||||
|
||||
/**
|
||||
* Creates an iterator for navigating Code Points in a string instead of
|
||||
* characters. Once Java7 support is dropped, this can be replaced with
|
||||
@@ -113,13 +106,13 @@ public class XML {
|
||||
/**
|
||||
* Replace special characters with XML escapes:
|
||||
*
|
||||
* <pre>
|
||||
* & <small>(ampersand)</small> is replaced by &amp;
|
||||
* < <small>(less than)</small> is replaced by &lt;
|
||||
* > <small>(greater than)</small> is replaced by &gt;
|
||||
* " <small>(double quote)</small> is replaced by &quot;
|
||||
* ' <small>(single quote / apostrophe)</small> is replaced by &apos;
|
||||
* </pre>
|
||||
* <pre>{@code
|
||||
* & (ampersand) is replaced by &amp;
|
||||
* < (less than) is replaced by &lt;
|
||||
* > (greater than) is replaced by &gt;
|
||||
* " (double quote) is replaced by &quot;
|
||||
* ' (single quote / apostrophe) is replaced by &apos;
|
||||
* }</pre>
|
||||
*
|
||||
* @param string
|
||||
* The string to be escaped.
|
||||
@@ -244,17 +237,22 @@ public class XML {
|
||||
* The JSONObject that will include the new material.
|
||||
* @param name
|
||||
* The tag name.
|
||||
* @param config
|
||||
* The XML parser configuration.
|
||||
* @param currentNestingDepth
|
||||
* The current nesting depth.
|
||||
* @return true if the close tag is processed.
|
||||
* @throws JSONException
|
||||
* @throws JSONException Thrown if any parsing error occurs.
|
||||
*/
|
||||
private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config)
|
||||
private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, int currentNestingDepth)
|
||||
throws JSONException {
|
||||
char c;
|
||||
int i;
|
||||
JSONObject jsonobject = null;
|
||||
JSONObject jsonObject = null;
|
||||
String string;
|
||||
String tagName;
|
||||
Object token;
|
||||
XMLXsiTypeConverter<?> xmlXsiTypeConverter;
|
||||
|
||||
// Test for and skip past these forms:
|
||||
// <!-- ... -->
|
||||
@@ -284,7 +282,7 @@ public class XML {
|
||||
if (x.next() == '[') {
|
||||
string = x.nextCDATA();
|
||||
if (string.length() > 0) {
|
||||
context.accumulate(config.cDataTagName, string);
|
||||
context.accumulate(config.getcDataTagName(), string);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -332,8 +330,9 @@ public class XML {
|
||||
} else {
|
||||
tagName = (String) token;
|
||||
token = null;
|
||||
jsonobject = new JSONObject();
|
||||
jsonObject = new JSONObject();
|
||||
boolean nilAttributeFound = false;
|
||||
xmlXsiTypeConverter = null;
|
||||
for (;;) {
|
||||
if (token == null) {
|
||||
token = x.nextToken();
|
||||
@@ -348,19 +347,32 @@ public class XML {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
|
||||
if (config.convertNilAttributeToNull
|
||||
if (config.isConvertNilAttributeToNull()
|
||||
&& NULL_ATTR.equals(string)
|
||||
&& Boolean.parseBoolean((String) token)) {
|
||||
nilAttributeFound = true;
|
||||
} else if(config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty()
|
||||
&& TYPE_ATTR.equals(string)) {
|
||||
xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
|
||||
} else if (!nilAttributeFound) {
|
||||
jsonobject.accumulate(string,
|
||||
config.keepStrings
|
||||
? ((String) token)
|
||||
: stringToValue((String) token));
|
||||
Object obj = stringToValue((String) token);
|
||||
if (obj instanceof Boolean) {
|
||||
jsonObject.accumulate(string,
|
||||
config.isKeepBooleanAsString()
|
||||
? ((String) token)
|
||||
: obj);
|
||||
} else if (obj instanceof Number) {
|
||||
jsonObject.accumulate(string,
|
||||
config.isKeepNumberAsString()
|
||||
? ((String) token)
|
||||
: obj);
|
||||
} else {
|
||||
jsonObject.accumulate(string, stringToValue((String) token));
|
||||
}
|
||||
}
|
||||
token = null;
|
||||
} else {
|
||||
jsonobject.accumulate(string, "");
|
||||
jsonObject.accumulate(string, "");
|
||||
}
|
||||
|
||||
|
||||
@@ -369,12 +381,23 @@ public class XML {
|
||||
if (x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (nilAttributeFound) {
|
||||
context.accumulate(tagName, JSONObject.NULL);
|
||||
} else if (jsonobject.length() > 0) {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
if (config.getForceList().contains(tagName)) {
|
||||
// Force the value to be an array
|
||||
if (nilAttributeFound) {
|
||||
context.append(tagName, JSONObject.NULL);
|
||||
} else if (jsonObject.length() > 0) {
|
||||
context.append(tagName, jsonObject);
|
||||
} else {
|
||||
context.put(tagName, new JSONArray());
|
||||
}
|
||||
} else {
|
||||
context.accumulate(tagName, "");
|
||||
if (nilAttributeFound) {
|
||||
context.accumulate(tagName, JSONObject.NULL);
|
||||
} else if (jsonObject.length() > 0) {
|
||||
context.accumulate(tagName, jsonObject);
|
||||
} else {
|
||||
context.accumulate(tagName, "");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -390,22 +413,58 @@ public class XML {
|
||||
} else if (token instanceof String) {
|
||||
string = (String) token;
|
||||
if (string.length() > 0) {
|
||||
jsonobject.accumulate(config.cDataTagName,
|
||||
config.keepStrings ? string : stringToValue(string));
|
||||
if(xmlXsiTypeConverter != null) {
|
||||
jsonObject.accumulate(config.getcDataTagName(),
|
||||
stringToValue(string, xmlXsiTypeConverter));
|
||||
} else {
|
||||
Object obj = stringToValue((String) token);
|
||||
if (obj instanceof Boolean) {
|
||||
jsonObject.accumulate(config.getcDataTagName(),
|
||||
config.isKeepBooleanAsString()
|
||||
? ((String) token)
|
||||
: obj);
|
||||
} else if (obj instanceof Number) {
|
||||
jsonObject.accumulate(config.getcDataTagName(),
|
||||
config.isKeepNumberAsString()
|
||||
? ((String) token)
|
||||
: obj);
|
||||
} else {
|
||||
jsonObject.accumulate(config.getcDataTagName(), stringToValue((String) token));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if (token == LT) {
|
||||
// Nested element
|
||||
if (parse(x, jsonobject, tagName, config)) {
|
||||
if (jsonobject.length() == 0) {
|
||||
context.accumulate(tagName, "");
|
||||
} else if (jsonobject.length() == 1
|
||||
&& jsonobject.opt(config.cDataTagName) != null) {
|
||||
context.accumulate(tagName,
|
||||
jsonobject.opt(config.cDataTagName));
|
||||
if (currentNestingDepth == config.getMaxNestingDepth()) {
|
||||
throw x.syntaxError("Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
|
||||
}
|
||||
|
||||
if (parse(x, jsonObject, tagName, config, currentNestingDepth + 1)) {
|
||||
if (config.getForceList().contains(tagName)) {
|
||||
// Force the value to be an array
|
||||
if (jsonObject.length() == 0) {
|
||||
context.put(tagName, new JSONArray());
|
||||
} else if (jsonObject.length() == 1
|
||||
&& jsonObject.opt(config.getcDataTagName()) != null) {
|
||||
context.append(tagName, jsonObject.opt(config.getcDataTagName()));
|
||||
} else {
|
||||
context.append(tagName, jsonObject);
|
||||
}
|
||||
} else {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
if (jsonObject.length() == 0) {
|
||||
context.accumulate(tagName, "");
|
||||
} else if (jsonObject.length() == 1
|
||||
&& jsonObject.opt(config.getcDataTagName()) != null) {
|
||||
context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
|
||||
} else {
|
||||
if (!config.shouldTrimWhiteSpace()) {
|
||||
removeEmpty(jsonObject, config);
|
||||
}
|
||||
context.accumulate(tagName, jsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -416,6 +475,131 @@ public class XML {
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName
|
||||
* and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled.
|
||||
* @param jsonObject JSONObject which may require deletion
|
||||
* @param config The XMLParserConfiguration which includes the cDataTagName
|
||||
*/
|
||||
private static void removeEmpty(final JSONObject jsonObject, final XMLParserConfiguration config) {
|
||||
if (jsonObject.has(config.getcDataTagName())) {
|
||||
final Object s = jsonObject.get(config.getcDataTagName());
|
||||
if (s instanceof String) {
|
||||
if (isStringAllWhiteSpace(s.toString())) {
|
||||
jsonObject.remove(config.getcDataTagName());
|
||||
}
|
||||
}
|
||||
else if (s instanceof JSONArray) {
|
||||
final JSONArray sArray = (JSONArray) s;
|
||||
for (int k = sArray.length()-1; k >= 0; k--){
|
||||
final Object eachString = sArray.get(k);
|
||||
if (eachString instanceof String) {
|
||||
String s1 = (String) eachString;
|
||||
if (isStringAllWhiteSpace(s1)) {
|
||||
sArray.remove(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sArray.isEmpty()) {
|
||||
jsonObject.remove(config.getcDataTagName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isStringAllWhiteSpace(final String s) {
|
||||
for (int k = 0; k<s.length(); k++){
|
||||
final char eachChar = s.charAt(k);
|
||||
if (!Character.isWhitespace(eachChar)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
|
||||
*/
|
||||
private static Number stringToNumber(final String val) throws NumberFormatException {
|
||||
char initial = val.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
// decimal representation
|
||||
if (isDecimalNotation(val)) {
|
||||
// Use a BigDecimal all the time so we keep the original
|
||||
// representation. BigDecimal doesn't support -0.0, ensure we
|
||||
// keep that by forcing a decimal.
|
||||
try {
|
||||
BigDecimal bd = new BigDecimal(val);
|
||||
if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
|
||||
return Double.valueOf(-0.0);
|
||||
}
|
||||
return bd;
|
||||
} catch (NumberFormatException retryAsDouble) {
|
||||
// this is to support "Hex Floats" like this: 0x1.0P-1074
|
||||
try {
|
||||
Double d = Double.valueOf(val);
|
||||
if(d.isNaN() || d.isInfinite()) {
|
||||
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
||||
}
|
||||
return d;
|
||||
} catch (NumberFormatException ignore) {
|
||||
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
||||
}
|
||||
}
|
||||
}
|
||||
// block items like 00 01 etc. Java number parsers treat these as Octal.
|
||||
if(initial == '0' && val.length() > 1) {
|
||||
char at1 = val.charAt(1);
|
||||
if(at1 >= '0' && at1 <= '9') {
|
||||
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
||||
}
|
||||
} else if (initial == '-' && val.length() > 2) {
|
||||
char at1 = val.charAt(1);
|
||||
char at2 = val.charAt(2);
|
||||
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
|
||||
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
||||
}
|
||||
}
|
||||
// integer representation.
|
||||
// This will narrow any values to the smallest reasonable Object representation
|
||||
// (Integer, Long, or BigInteger)
|
||||
|
||||
// BigInteger down conversion: We use a similar bitLength compare as
|
||||
// BigInteger#intValueExact uses. Increases GC, but objects hold
|
||||
// only what they need. i.e. Less runtime overhead if the value is
|
||||
// long lived.
|
||||
BigInteger bi = new BigInteger(val);
|
||||
if(bi.bitLength() <= 31){
|
||||
return Integer.valueOf(bi.intValue());
|
||||
}
|
||||
if(bi.bitLength() <= 63){
|
||||
return Long.valueOf(bi.longValue());
|
||||
}
|
||||
return bi;
|
||||
}
|
||||
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
||||
}
|
||||
|
||||
/**
|
||||
* direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
|
||||
*/
|
||||
private static boolean isDecimalNotation(final String val) {
|
||||
return val.indexOf('.') > -1 || val.indexOf('e') > -1
|
||||
|| val.indexOf('E') > -1 || "-0".equals(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method tries to convert the given string value to the target object
|
||||
* @param string String to convert
|
||||
* @param typeConverter value converter to convert string to integer, boolean e.t.c
|
||||
* @return JSON value of this string or the string
|
||||
*/
|
||||
public static Object stringToValue(String string, XMLXsiTypeConverter<?> typeConverter) {
|
||||
if(typeConverter != null) {
|
||||
return typeConverter.convert(string);
|
||||
}
|
||||
return stringToValue(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is the same as {@link JSONObject#stringToValue(String)}.
|
||||
@@ -425,17 +609,20 @@ public class XML {
|
||||
*/
|
||||
// To maintain compatibility with the Android API, this method is a direct copy of
|
||||
// the one in JSONObject. Changes made here should be reflected there.
|
||||
// This method should not make calls out of the XML object.
|
||||
public static Object stringToValue(String string) {
|
||||
if (string.equals("")) {
|
||||
if ("".equals(string)) {
|
||||
return string;
|
||||
}
|
||||
if (string.equalsIgnoreCase("true")) {
|
||||
|
||||
// check JSON key words true/false/null
|
||||
if ("true".equalsIgnoreCase(string)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("false")) {
|
||||
if ("false".equalsIgnoreCase(string)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("null")) {
|
||||
if ("null".equalsIgnoreCase(string)) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
@@ -447,23 +634,7 @@ public class XML {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
// if we want full Big Number support this block can be replaced with:
|
||||
// return stringToNumber(string);
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|
||||
|| string.indexOf('E') > -1 || "-0".equals(string)) {
|
||||
Double d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
Long myLong = Long.valueOf(string);
|
||||
if (string.equals(myLong.toString())) {
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return Integer.valueOf(myLong.intValue());
|
||||
}
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
return stringToNumber(string);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
@@ -478,7 +649,8 @@ public class XML {
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* @param string
|
||||
@@ -498,7 +670,8 @@ public class XML {
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* @param reader The XML source reader.
|
||||
@@ -517,7 +690,8 @@ public class XML {
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
|
||||
@@ -544,7 +718,46 @@ public class XML {
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* All numbers are converted as strings, for 1, 01, 29.0 will not be coerced to
|
||||
* numbers but will instead be the exact value as seen in the XML document depending
|
||||
* on how flag is set.
|
||||
* All booleans are converted as strings, for true, false will not be coerced to
|
||||
* booleans but will instead be the exact value as seen in the XML document depending
|
||||
* on how flag is set.
|
||||
*
|
||||
* @param reader The XML source reader.
|
||||
* @param keepNumberAsString If true, then numeric values will not be coerced into
|
||||
* numeric values and will instead be left as strings
|
||||
* @param keepBooleanAsString If true, then boolean values will not be coerced into
|
||||
* * numeric values and will instead be left as strings
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown if there is an errors while parsing the string
|
||||
*/
|
||||
public static JSONObject toJSONObject(Reader reader, boolean keepNumberAsString, boolean keepBooleanAsString) throws JSONException {
|
||||
XMLParserConfiguration xmlParserConfiguration = new XMLParserConfiguration();
|
||||
if(keepNumberAsString) {
|
||||
xmlParserConfiguration = xmlParserConfiguration.withKeepNumberAsString(keepNumberAsString);
|
||||
}
|
||||
if(keepBooleanAsString) {
|
||||
xmlParserConfiguration = xmlParserConfiguration.withKeepBooleanAsString(keepBooleanAsString);
|
||||
}
|
||||
return toJSONObject(reader, xmlParserConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML into a
|
||||
* JSONObject. Some information may be lost in this transformation because
|
||||
* JSON is a data format and XML is a document format. XML uses elements,
|
||||
* attributes, and content text, while JSON uses unordered collections of
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
|
||||
@@ -557,11 +770,11 @@ public class XML {
|
||||
*/
|
||||
public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration config) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
XMLTokener x = new XMLTokener(reader);
|
||||
XMLTokener x = new XMLTokener(reader, config);
|
||||
while (x.more()) {
|
||||
x.skipPast("<");
|
||||
if(x.more()) {
|
||||
parse(x, jo, null, config);
|
||||
parse(x, jo, null, config, 0);
|
||||
}
|
||||
}
|
||||
return jo;
|
||||
@@ -575,7 +788,8 @@ public class XML {
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
|
||||
@@ -600,7 +814,40 @@ public class XML {
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* All numbers are converted as strings, for 1, 01, 29.0 will not be coerced to
|
||||
* numbers but will instead be the exact value as seen in the XML document depending
|
||||
* on how flag is set.
|
||||
* All booleans are converted as strings, for true, false will not be coerced to
|
||||
* booleans but will instead be the exact value as seen in the XML document depending
|
||||
* on how flag is set.
|
||||
*
|
||||
* @param string
|
||||
* The source string.
|
||||
* @param keepNumberAsString If true, then numeric values will not be coerced into
|
||||
* numeric values and will instead be left as strings
|
||||
* @param keepBooleanAsString If true, then boolean values will not be coerced into
|
||||
* numeric values and will instead be left as strings
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown if there is an errors while parsing the string
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string, boolean keepNumberAsString, boolean keepBooleanAsString) throws JSONException {
|
||||
return toJSONObject(new StringReader(string), keepNumberAsString, keepBooleanAsString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONObject. Some information may be lost in this transformation because
|
||||
* JSON is a data format and XML is a document format. XML uses elements,
|
||||
* attributes, and content text, while JSON uses unordered collections of
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||
* <[ [ ]]>}</pre>
|
||||
* are ignored.
|
||||
*
|
||||
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
|
||||
@@ -656,6 +903,28 @@ public class XML {
|
||||
*/
|
||||
public static String toString(final Object object, final String tagName, final XMLParserConfiguration config)
|
||||
throws JSONException {
|
||||
return toString(object, tagName, config, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, element-normal XML string,
|
||||
* either pretty print or single-lined depending on indent factor.
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @param tagName
|
||||
* The optional name of the enclosing tag.
|
||||
* @param config
|
||||
* Configuration that can control output to XML.
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @param indent
|
||||
* The current ident level in spaces.
|
||||
* @return
|
||||
* @throws JSONException
|
||||
*/
|
||||
private static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor, int indent)
|
||||
throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
JSONArray ja;
|
||||
JSONObject jo;
|
||||
@@ -665,9 +934,14 @@ public class XML {
|
||||
|
||||
// Emit <tagName>
|
||||
if (tagName != null) {
|
||||
sb.append(indent(indent));
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
if(indentFactor > 0){
|
||||
sb.append("\n");
|
||||
indent += indentFactor;
|
||||
}
|
||||
}
|
||||
|
||||
// Loop thru the keys.
|
||||
@@ -682,7 +956,7 @@ public class XML {
|
||||
}
|
||||
|
||||
// Emit content in body
|
||||
if (key.equals(config.cDataTagName)) {
|
||||
if (key.equals(config.getcDataTagName())) {
|
||||
if (value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
int jaLength = ja.length();
|
||||
@@ -710,31 +984,52 @@ public class XML {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
sb.append(toString(val, null, config));
|
||||
sb.append(toString(val, null, config, indentFactor, indent));
|
||||
sb.append("</");
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
} else {
|
||||
sb.append(toString(val, key, config));
|
||||
sb.append(toString(val, key, config, indentFactor, indent));
|
||||
}
|
||||
}
|
||||
} else if ("".equals(value)) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
if (config.isCloseEmptyTag()){
|
||||
sb.append(indent(indent));
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append(">");
|
||||
sb.append("</");
|
||||
sb.append(key);
|
||||
sb.append(">");
|
||||
if (indentFactor > 0) {
|
||||
sb.append("\n");
|
||||
}
|
||||
}else {
|
||||
sb.append(indent(indent));
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
if (indentFactor > 0) {
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Emit a new tag <k>
|
||||
|
||||
} else {
|
||||
sb.append(toString(value, key, config));
|
||||
sb.append(toString(value, key, config, indentFactor, indent));
|
||||
}
|
||||
}
|
||||
if (tagName != null) {
|
||||
|
||||
// Emit the </tagname> close tag
|
||||
// Emit the </tagName> close tag
|
||||
sb.append(indent(indent - indentFactor));
|
||||
sb.append("</");
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
if(indentFactor > 0){
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
@@ -753,15 +1048,85 @@ public class XML {
|
||||
// XML does not have good support for arrays. If an array
|
||||
// appears in a place where XML is lacking, synthesize an
|
||||
// <array> element.
|
||||
sb.append(toString(val, tagName == null ? "array" : tagName, config));
|
||||
sb.append(toString(val, tagName == null ? "array" : tagName, config, indentFactor, indent));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
string = (object == null) ? "null" : escape(object.toString());
|
||||
return (tagName == null) ? "\"" + string + "\""
|
||||
: (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
|
||||
+ ">" + string + "</" + tagName + ">";
|
||||
|
||||
string = (object == null) ? "null" : escape(object.toString());
|
||||
String indentationSuffix = (indentFactor > 0) ? "\n" : "";
|
||||
if(tagName == null){
|
||||
return indent(indent) + "\"" + string + "\"" + indentationSuffix;
|
||||
} else if(string.length() == 0){
|
||||
return indent(indent) + "<" + tagName + "/>" + indentationSuffix;
|
||||
} else {
|
||||
return indent(indent) + "<" + tagName
|
||||
+ ">" + string + "</" + tagName + ">" + indentationSuffix;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @return A string.
|
||||
* @throws JSONException Thrown if there is an error parsing the string
|
||||
*/
|
||||
public static String toString(Object object, int indentFactor){
|
||||
return toString(object, null, XMLParserConfiguration.ORIGINAL, indentFactor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @param tagName
|
||||
* The optional name of the enclosing tag.
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @return A string.
|
||||
* @throws JSONException Thrown if there is an error parsing the string
|
||||
*/
|
||||
public static String toString(final Object object, final String tagName, int indentFactor) {
|
||||
return toString(object, tagName, XMLParserConfiguration.ORIGINAL, indentFactor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @param tagName
|
||||
* The optional name of the enclosing tag.
|
||||
* @param config
|
||||
* Configuration that can control output to XML.
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @return A string.
|
||||
* @throws JSONException Thrown if there is an error parsing the string
|
||||
*/
|
||||
public static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor)
|
||||
throws JSONException {
|
||||
return toString(object, tagName, config, indentFactor, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a String consisting of a number of space characters specified by indent
|
||||
*
|
||||
* @param indent
|
||||
* The number of spaces to be appended to the String.
|
||||
* @return
|
||||
*/
|
||||
private static final String indent(int indent) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < indent; i++) {
|
||||
sb.append(' ');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
444
src/main/java/org/json/XMLParserConfiguration.java
Normal file
444
src/main/java/org/json/XMLParserConfiguration.java
Normal file
@@ -0,0 +1,444 @@
|
||||
package org.json;
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
* Configuration object for the XML parser. The configuration is immutable.
|
||||
* @author AylwardJ
|
||||
*/
|
||||
@SuppressWarnings({""})
|
||||
public class XMLParserConfiguration extends ParserConfiguration {
|
||||
|
||||
/**
|
||||
* The default maximum nesting depth when parsing a XML document to JSON.
|
||||
*/
|
||||
// public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; // We could override
|
||||
|
||||
/**
|
||||
* Allow user to control how numbers are parsed
|
||||
*/
|
||||
private boolean keepNumberAsString;
|
||||
|
||||
/**
|
||||
* Allow user to control how booleans are parsed
|
||||
*/
|
||||
private boolean keepBooleanAsString;
|
||||
|
||||
/** Original Configuration of the XML Parser. */
|
||||
public static final XMLParserConfiguration ORIGINAL
|
||||
= new XMLParserConfiguration();
|
||||
/** Original configuration of the XML Parser except that values are kept as strings. */
|
||||
public static final XMLParserConfiguration KEEP_STRINGS
|
||||
= new XMLParserConfiguration().withKeepStrings(true);
|
||||
|
||||
/**
|
||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||
* processing.
|
||||
*/
|
||||
private String cDataTagName;
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||
* should be kept as attribute(<code>false</code>), or they should be converted to
|
||||
* <code>null</code>(<code>true</code>)
|
||||
*/
|
||||
private boolean convertNilAttributeToNull;
|
||||
|
||||
/**
|
||||
* When creating an XML from JSON Object, an empty tag by default will self-close.
|
||||
* If it has to be closed explicitly, with empty content between start and end tag,
|
||||
* this flag is to be turned on.
|
||||
*/
|
||||
private boolean closeEmptyTag;
|
||||
|
||||
/**
|
||||
* This will allow type conversion for values in XML if xsi:type attribute is defined
|
||||
*/
|
||||
private Map<String, XMLXsiTypeConverter<?>> xsiTypeMap;
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies the tags whose values should be converted
|
||||
* to arrays
|
||||
*/
|
||||
private Set<String> forceList;
|
||||
|
||||
|
||||
/**
|
||||
* Flag to indicate whether white space should be trimmed when parsing XML.
|
||||
* The default behaviour is to trim white space. When this is set to false, inputting XML
|
||||
* with tags that are the same as the value of cDataTagName is unsupported. It is recommended to set cDataTagName
|
||||
* to a distinct value in this case.
|
||||
*/
|
||||
private boolean shouldTrimWhiteSpace;
|
||||
|
||||
/**
|
||||
* Default parser configuration. Does not keep strings (tries to implicitly convert
|
||||
* values), and the CDATA Tag Name is "content". Trims whitespace.
|
||||
*/
|
||||
public XMLParserConfiguration () {
|
||||
super();
|
||||
this.cDataTagName = "content";
|
||||
this.convertNilAttributeToNull = false;
|
||||
this.xsiTypeMap = Collections.emptyMap();
|
||||
this.forceList = Collections.emptySet();
|
||||
this.shouldTrimWhiteSpace = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser string processing and use the default CDATA Tag Name as "content".
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @deprecated This constructor has been deprecated in favor of using the new builder
|
||||
* pattern for the configuration.
|
||||
* This constructor may be removed in a future release.
|
||||
*/
|
||||
@Deprecated
|
||||
public XMLParserConfiguration (final boolean keepStrings) {
|
||||
this(keepStrings, "content", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser string processing to try and convert XML values to JSON values and
|
||||
* use the passed CDATA Tag Name the processing value. Pass <code>null</code> to
|
||||
* disable CDATA processing
|
||||
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
* @deprecated This constructor has been deprecated in favor of using the new builder
|
||||
* pattern for the configuration.
|
||||
* This constructor may be removed in a future release.
|
||||
*/
|
||||
@Deprecated
|
||||
public XMLParserConfiguration (final String cDataTagName) {
|
||||
this(false, cDataTagName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser to use custom settings.
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
* @deprecated This constructor has been deprecated in favor of using the new builder
|
||||
* pattern for the configuration.
|
||||
* This constructor may be removed in a future release.
|
||||
*/
|
||||
@Deprecated
|
||||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
|
||||
super(keepStrings, DEFAULT_MAXIMUM_NESTING_DEPTH);
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser to use custom settings.
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
* @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
|
||||
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
|
||||
* @deprecated This constructor has been deprecated in favor of using the new builder
|
||||
* pattern for the configuration.
|
||||
* This constructor may be removed or marked private in a future release.
|
||||
*/
|
||||
@Deprecated
|
||||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
|
||||
super(false, DEFAULT_MAXIMUM_NESTING_DEPTH);
|
||||
this.keepNumberAsString = keepStrings;
|
||||
this.keepBooleanAsString = keepStrings;
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser to use custom settings.
|
||||
* @param keepStrings <code>true</code> to parse all values as string.
|
||||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
|
||||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
* @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
|
||||
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
|
||||
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
|
||||
* xsi:type="integer" as integer, xsi:type="string" as string
|
||||
* @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays
|
||||
* @param maxNestingDepth <code>int</code> to limit the nesting depth
|
||||
* @param closeEmptyTag <code>boolean</code> to turn on explicit end tag for tag with empty value
|
||||
*/
|
||||
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
|
||||
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList,
|
||||
final int maxNestingDepth, final boolean closeEmptyTag, final boolean keepNumberAsString, final boolean keepBooleanAsString) {
|
||||
super(false, maxNestingDepth);
|
||||
this.keepNumberAsString = keepNumberAsString;
|
||||
this.keepBooleanAsString = keepBooleanAsString;
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
||||
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
|
||||
this.forceList = Collections.unmodifiableSet(forceList);
|
||||
this.closeEmptyTag = closeEmptyTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a new instance of the same configuration.
|
||||
*/
|
||||
@Override
|
||||
protected XMLParserConfiguration clone() {
|
||||
// future modifications to this method should always ensure a "deep"
|
||||
// clone in the case of collections. i.e. if a Map is added as a configuration
|
||||
// item, a new map instance should be created and if possible each value in the
|
||||
// map should be cloned as well. If the values of the map are known to also
|
||||
// be immutable, then a shallow clone of the map is acceptable.
|
||||
final XMLParserConfiguration config = new XMLParserConfiguration(
|
||||
this.keepStrings,
|
||||
this.cDataTagName,
|
||||
this.convertNilAttributeToNull,
|
||||
this.xsiTypeMap,
|
||||
this.forceList,
|
||||
this.maxNestingDepth,
|
||||
this.closeEmptyTag,
|
||||
this.keepNumberAsString,
|
||||
this.keepBooleanAsString
|
||||
);
|
||||
config.shouldTrimWhiteSpace = this.shouldTrimWhiteSpace;
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||
*
|
||||
* @param newVal
|
||||
* new value to use for the <code>keepStrings</code> configuration option.
|
||||
*
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public XMLParserConfiguration withKeepStrings(final boolean newVal) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
newConfig.keepStrings = newVal;
|
||||
newConfig.keepNumberAsString = newVal;
|
||||
newConfig.keepBooleanAsString = newVal;
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if numbers should be kept as strings (<code>1</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||
*
|
||||
* @param newVal
|
||||
* new value to use for the <code>keepNumberAsString</code> configuration option.
|
||||
*
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public XMLParserConfiguration withKeepNumberAsString(final boolean newVal) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
newConfig.keepNumberAsString = newVal;
|
||||
newConfig.keepStrings = newConfig.keepBooleanAsString && newConfig.keepNumberAsString;
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if booleans should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||
*
|
||||
* @param newVal
|
||||
* new value to use for the <code>withKeepBooleanAsString</code> configuration option.
|
||||
*
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public XMLParserConfiguration withKeepBooleanAsString(final boolean newVal) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
newConfig.keepBooleanAsString = newVal;
|
||||
newConfig.keepStrings = newConfig.keepBooleanAsString && newConfig.keepNumberAsString;
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||
* processing.
|
||||
*
|
||||
* @return The <code>cDataTagName</code> configuration value.
|
||||
*/
|
||||
public String getcDataTagName() {
|
||||
return this.cDataTagName;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSONML, specifies if numbers should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||
*
|
||||
* @return The <code>keepStrings</code> configuration value.
|
||||
*/
|
||||
public boolean isKeepNumberAsString() {
|
||||
return this.keepNumberAsString;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSONML, specifies if booleans should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||
*
|
||||
* @return The <code>keepStrings</code> configuration value.
|
||||
*/
|
||||
public boolean isKeepBooleanAsString() {
|
||||
return this.keepBooleanAsString;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||
* processing.
|
||||
*
|
||||
* @param newVal
|
||||
* new value to use for the <code>cDataTagName</code> configuration option.
|
||||
*
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public XMLParserConfiguration withcDataTagName(final String newVal) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
newConfig.cDataTagName = newVal;
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||
* should be kept as attribute(<code>false</code>), or they should be converted to
|
||||
* <code>null</code>(<code>true</code>)
|
||||
*
|
||||
* @return The <code>convertNilAttributeToNull</code> configuration value.
|
||||
*/
|
||||
public boolean isConvertNilAttributeToNull() {
|
||||
return this.convertNilAttributeToNull;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||
* should be kept as attribute(<code>false</code>), or they should be converted to
|
||||
* <code>null</code>(<code>true</code>)
|
||||
*
|
||||
* @param newVal
|
||||
* new value to use for the <code>convertNilAttributeToNull</code> configuration option.
|
||||
*
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
newConfig.convertNilAttributeToNull = newVal;
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies that the values with attribute xsi:type
|
||||
* will be converted to target type defined to client in this configuration
|
||||
* {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
|
||||
* xsi:type="integer" as integer, xsi:type="string" as string
|
||||
* @return <code>xsiTypeMap</code> unmodifiable configuration map.
|
||||
*/
|
||||
public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
|
||||
return this.xsiTypeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies that the values with attribute xsi:type
|
||||
* will be converted to target type defined to client in this configuration
|
||||
* {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
|
||||
* xsi:type="integer" as integer, xsi:type="string" as string
|
||||
* @param xsiTypeMap {@code new HashMap<String, XMLXsiTypeConverter<?>>()} to parse values with attribute
|
||||
* xsi:type="integer" as integer, xsi:type="string" as string
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
|
||||
newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
|
||||
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
|
||||
* @return <code>forceList</code> unmodifiable configuration set.
|
||||
*/
|
||||
public Set<String> getForceList() {
|
||||
return this.forceList;
|
||||
}
|
||||
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
|
||||
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
|
||||
* @param forceList {@code new HashSet<String>()} to parse the provided tags' values as arrays
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
public XMLParserConfiguration withForceList(final Set<String> forceList) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
Set<String> cloneForceList = new HashSet<String>(forceList);
|
||||
newConfig.forceList = Collections.unmodifiableSet(cloneForceList);
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
||||
* when parsing the XML into JSON. The default max nesting depth is 512, which means the parser
|
||||
* will throw a JsonException if the maximum depth is reached.
|
||||
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
|
||||
* which means the parses will go as deep as the maximum call stack size allows.
|
||||
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
|
||||
return super.withMaxNestingDepth(maxNestingDepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* To enable explicit end tag with empty value.
|
||||
* @param closeEmptyTag new value for the closeEmptyTag property
|
||||
* @return same instance of configuration with empty tag config updated
|
||||
*/
|
||||
public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){
|
||||
XMLParserConfiguration clonedConfiguration = this.clone();
|
||||
clonedConfiguration.closeEmptyTag = closeEmptyTag;
|
||||
return clonedConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether whitespace should be trimmed inside of tags. *NOTE* Do not use this if
|
||||
* you expect your XML tags to have names that are the same as cDataTagName as this is unsupported.
|
||||
* cDataTagName should be set to a distinct value in these cases.
|
||||
* @param shouldTrimWhiteSpace boolean to set trimming on or off. Off is default.
|
||||
* @return same instance of configuration with empty tag config updated
|
||||
*/
|
||||
public XMLParserConfiguration withShouldTrimWhitespace(boolean shouldTrimWhiteSpace){
|
||||
XMLParserConfiguration clonedConfiguration = this.clone();
|
||||
clonedConfiguration.shouldTrimWhiteSpace = shouldTrimWhiteSpace;
|
||||
return clonedConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser should automatically close empty XML tags.
|
||||
*
|
||||
* @return {@code true} if empty XML tags should be automatically closed, {@code false} otherwise.
|
||||
*/
|
||||
public boolean isCloseEmptyTag() {
|
||||
return this.closeEmptyTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser should trim white spaces from XML content.
|
||||
*
|
||||
* @return {@code true} if white spaces should be trimmed, {@code false} otherwise.
|
||||
*/
|
||||
public boolean shouldTrimWhiteSpace() {
|
||||
return this.shouldTrimWhiteSpace;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,7 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.io.Reader;
|
||||
@@ -40,6 +20,8 @@ public class XMLTokener extends JSONTokener {
|
||||
*/
|
||||
public static final java.util.HashMap<String, Character> entity;
|
||||
|
||||
private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;
|
||||
|
||||
static {
|
||||
entity = new java.util.HashMap<String, Character>(8);
|
||||
entity.put("amp", XML.AMP);
|
||||
@@ -65,6 +47,16 @@ public class XMLTokener extends JSONTokener {
|
||||
super(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an XMLTokener from a Reader and an XMLParserConfiguration.
|
||||
* @param r A source reader.
|
||||
* @param configuration the configuration that can be used to set certain flags
|
||||
*/
|
||||
public XMLTokener(Reader r, XMLParserConfiguration configuration) {
|
||||
super(r);
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the text in the CDATA block.
|
||||
* @return The string up to the <code>]]></code>.
|
||||
@@ -90,19 +82,20 @@ public class XMLTokener extends JSONTokener {
|
||||
|
||||
/**
|
||||
* Get the next XML outer token, trimming whitespace. There are two kinds
|
||||
* of tokens: the '<' character which begins a markup tag, and the content
|
||||
* of tokens: the <pre>{@code '<' }</pre> character which begins a markup
|
||||
* tag, and the content
|
||||
* text between markup tags.
|
||||
*
|
||||
* @return A string, or a '<' Character, or null if there is no more
|
||||
* source text.
|
||||
* @throws JSONException
|
||||
* @return A string, or a <pre>{@code '<' }</pre> Character, or null if
|
||||
* there is no more source text.
|
||||
* @throws JSONException if a called function has an error
|
||||
*/
|
||||
public Object nextContent() throws JSONException {
|
||||
char c;
|
||||
StringBuilder sb;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
} while (Character.isWhitespace(c) && configuration.shouldTrimWhiteSpace());
|
||||
if (c == 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -116,7 +109,9 @@ public class XMLTokener extends JSONTokener {
|
||||
}
|
||||
if (c == '<') {
|
||||
back();
|
||||
return sb.toString().trim();
|
||||
if (configuration.shouldTrimWhiteSpace()) {
|
||||
return sb.toString().trim();
|
||||
} else return sb.toString();
|
||||
}
|
||||
if (c == '&') {
|
||||
sb.append(nextEntity(c));
|
||||
@@ -129,8 +124,10 @@ public class XMLTokener extends JSONTokener {
|
||||
|
||||
|
||||
/**
|
||||
* <pre>{@code
|
||||
* Return the next entity. These entities are translated to Characters:
|
||||
* <code>& ' > < "</code>.
|
||||
* & ' > < ".
|
||||
* }</pre>
|
||||
* @param ampersand An ampersand character.
|
||||
* @return A Character or an entity String if the entity is not recognized.
|
||||
* @throws JSONException If missing ';' in XML entity.
|
||||
@@ -152,7 +149,7 @@ public class XMLTokener extends JSONTokener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unescapes an XML entity encoding;
|
||||
* Unescape an XML entity encoding;
|
||||
* @param e entity (only the actual entity value, not the preceding & or ending ;
|
||||
* @return
|
||||
*/
|
||||
@@ -164,7 +161,7 @@ public class XMLTokener extends JSONTokener {
|
||||
// if our entity is an encoded unicode point, parse it.
|
||||
if (e.charAt(0) == '#') {
|
||||
int cp;
|
||||
if (e.charAt(1) == 'x') {
|
||||
if (e.charAt(1) == 'x' || e.charAt(1) == 'X') {
|
||||
// hex encoded unicode
|
||||
cp = Integer.parseInt(e.substring(2), 16);
|
||||
} else {
|
||||
@@ -183,11 +180,14 @@ public class XMLTokener extends JSONTokener {
|
||||
|
||||
|
||||
/**
|
||||
* <pre>{@code
|
||||
* Returns the next XML meta token. This is used for skipping over <!...>
|
||||
* and <?...?> structures.
|
||||
* @return Syntax characters (<code>< > / = ! ?</code>) are returned as
|
||||
* }</pre>
|
||||
* @return <pre>{@code Syntax characters (< > / = ! ?) are returned as
|
||||
* Character, and strings and names are returned as Boolean. We don't care
|
||||
* what the values actually are.
|
||||
* }</pre>
|
||||
* @throws JSONException If a string is not properly closed or if the XML
|
||||
* is badly structured.
|
||||
*/
|
||||
@@ -232,6 +232,7 @@ public class XMLTokener extends JSONTokener {
|
||||
}
|
||||
switch (c) {
|
||||
case 0:
|
||||
throw syntaxError("Unterminated string");
|
||||
case '<':
|
||||
case '>':
|
||||
case '/':
|
||||
@@ -249,10 +250,12 @@ public class XMLTokener extends JSONTokener {
|
||||
|
||||
|
||||
/**
|
||||
* <pre>{@code
|
||||
* Get the next XML Token. These tokens are found inside of angle
|
||||
* brackets. It may be one of these characters: <code>/ > = ! ?</code> or it
|
||||
* brackets. It may be one of these characters: / > = ! ? or it
|
||||
* may be a string wrapped in single quotes or double quotes, or it may be a
|
||||
* name.
|
||||
* }</pre>
|
||||
* @return a String or a Character.
|
||||
* @throws JSONException If the XML is not well formed.
|
||||
*/
|
||||
53
src/main/java/org/json/XMLXsiTypeConverter.java
Normal file
53
src/main/java/org/json/XMLXsiTypeConverter.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package org.json;
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Type conversion configuration interface to be used with xsi:type attributes.
|
||||
* <pre>
|
||||
* <b>XML Sample</b>
|
||||
* {@code
|
||||
* <root>
|
||||
* <asString xsi:type="string">12345</asString>
|
||||
* <asInt xsi:type="integer">54321</asInt>
|
||||
* </root>
|
||||
* }
|
||||
* <b>JSON Output</b>
|
||||
* {@code
|
||||
* {
|
||||
* "root" : {
|
||||
* "asString" : "12345",
|
||||
* "asInt": 54321
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* <b>Usage</b>
|
||||
* {@code
|
||||
* Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
|
||||
* xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
|
||||
* @Override public String convert(final String value) {
|
||||
* return value;
|
||||
* }
|
||||
* });
|
||||
* xsiTypeMap.put("integer", new XMLXsiTypeConverter<Integer>() {
|
||||
* @Override public Integer convert(final String value) {
|
||||
* return Integer.valueOf(value);
|
||||
* }
|
||||
* });
|
||||
* }
|
||||
* </pre>
|
||||
* @author kumar529
|
||||
* @param <T> return type of convert method
|
||||
*/
|
||||
public interface XMLXsiTypeConverter<T> {
|
||||
|
||||
/**
|
||||
* Converts an XML xsi:type attribute value to the specified type {@code T}.
|
||||
*
|
||||
* @param value The string representation of the XML xsi:type attribute value to be converted.
|
||||
* @return An object of type {@code T} representing the converted value.
|
||||
*/
|
||||
T convert(String value);
|
||||
}
|
||||
392
src/test/java/org/json/junit/CDLTest.java
Normal file
392
src/test/java/org/json/junit/CDLTest.java
Normal file
@@ -0,0 +1,392 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONArray;
|
||||
import org.json.CDL;
|
||||
|
||||
/**
|
||||
* Tests for CDL.java.
|
||||
* CDL provides an application level API, but it is not used by the
|
||||
* reference app. To test it, strings will be converted to JSON-Java classes
|
||||
* and then converted back.
|
||||
*/
|
||||
public class CDLTest {
|
||||
|
||||
/**
|
||||
* String of lines where the column names are in the first row,
|
||||
* and all subsequent rows are values. All keys and values should be legal.
|
||||
*/
|
||||
private static final String LINES = "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" +
|
||||
"val1, val2, val3, val4, val5, val6, val7\n" +
|
||||
"1, 2, 3, 4\t, 5, 6, 7\n" +
|
||||
"true, false, true, true, false, false, false\n" +
|
||||
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
|
||||
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
|
||||
|
||||
|
||||
/**
|
||||
* CDL.toJSONArray() adds all values as strings, with no filtering or
|
||||
* conversions. For testing, this means that the expected JSONObject
|
||||
* values all must be quoted in the cases where the JSONObject parsing
|
||||
* might normally convert the value into a non-string.
|
||||
*/
|
||||
private static final String EXPECTED_LINES =
|
||||
"[ " +
|
||||
"{" +
|
||||
"\"Col 1\":\"val1\", " +
|
||||
"\"Col 2\":\"val2\", " +
|
||||
"\"Col 3\":\"val3\", " +
|
||||
"\"Col 4\":\"val4\", " +
|
||||
"\"Col 5\":\"val5\", " +
|
||||
"\"Col 6\":\"val6\", " +
|
||||
"\"Col 7\":\"val7\"" +
|
||||
"}, " +
|
||||
" {" +
|
||||
"\"Col 1\":\"1\", " +
|
||||
"\"Col 2\":\"2\", " +
|
||||
"\"Col 3\":\"3\", " +
|
||||
"\"Col 4\":\"4\", " +
|
||||
"\"Col 5\":\"5\", " +
|
||||
"\"Col 6\":\"6\", " +
|
||||
"\"Col 7\":\"7\"" +
|
||||
"}, " +
|
||||
" {" +
|
||||
"\"Col 1\":\"true\", " +
|
||||
"\"Col 2\":\"false\", " +
|
||||
"\"Col 3\":\"true\", " +
|
||||
"\"Col 4\":\"true\", " +
|
||||
"\"Col 5\":\"false\", " +
|
||||
"\"Col 6\":\"false\", " +
|
||||
"\"Col 7\":\"false\"" +
|
||||
"}, " +
|
||||
"{" +
|
||||
"\"Col 1\":\"0.23\", " +
|
||||
"\"Col 2\":\"57.42\", " +
|
||||
"\"Col 3\":\"5e27\", " +
|
||||
"\"Col 4\":\"-234.879\", " +
|
||||
"\"Col 5\":\"2.34e5\", " +
|
||||
"\"Col 6\":\"0.0\", " +
|
||||
"\"Col 7\":\"9e-3\"" +
|
||||
"}, " +
|
||||
"{" +
|
||||
"\"Col 1\":\"va\tl1\", " +
|
||||
"\"Col 2\":\"v\bal2\", " +
|
||||
"\"Col 3\":\"val3\", " +
|
||||
"\"Col 4\":\"val\f4\", " +
|
||||
"\"Col 5\":\"val5\", " +
|
||||
"\"Col 6\":\"va'l6\", " +
|
||||
"\"Col 7\":\"val7\"" +
|
||||
"}" +
|
||||
"]";
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONArray from a null string.
|
||||
* Expect a NullPointerException.
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void exceptionOnNullString() {
|
||||
String nullStr = null;
|
||||
CDL.toJSONArray(nullStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONArray from a string with unbalanced quotes
|
||||
* in column title line. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void unbalancedQuoteInName() {
|
||||
String badLine = "Col1, \"Col2\nVal1, Val2";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 12 [character 0 line 2]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONArray from a string with unbalanced quotes
|
||||
* in value line. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void unbalancedQuoteInValue() {
|
||||
String badLine = "Col1, Col2\n\"Val1, Val2";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 22 [character 11 line 2]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONArray from a string with null char
|
||||
* in column title line. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void nullInName() {
|
||||
String badLine = "C\0ol1, Col2\nVal1, Val2";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad character 'o' (111). at 2 [character 3 line 1]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void unbalancedEscapedQuote(){
|
||||
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 26 [character 15 line 2]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Csv parsing skip last row if last field of this row is empty #943
|
||||
*/
|
||||
@Test
|
||||
public void csvParsingCatchesLastRow(){
|
||||
String data = "Field 1,Field 2,Field 3\n" +
|
||||
"value11,value12,\n" +
|
||||
"value21,value22,";
|
||||
|
||||
JSONArray jsonArray = CDL.toJSONArray(data);
|
||||
|
||||
JSONArray expectedJsonArray = new JSONArray();
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("Field 1", "value11");
|
||||
jsonObject.put("Field 2", "value12");
|
||||
jsonObject.put("Field 3", "");
|
||||
expectedJsonArray.put(jsonObject);
|
||||
|
||||
jsonObject = new JSONObject();
|
||||
jsonObject.put("Field 1", "value21");
|
||||
jsonObject.put("Field 2", "value22");
|
||||
jsonObject.put("Field 3", "");
|
||||
expectedJsonArray.put(jsonObject);
|
||||
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that there is no error for a single escaped quote within a properly embedded quote.
|
||||
*/
|
||||
@Test
|
||||
public void singleEscapedQuote(){
|
||||
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
|
||||
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
|
||||
|
||||
String cdlStr = CDL.toString(jsonArray);
|
||||
assertTrue(cdlStr.contains("Col1"));
|
||||
assertTrue(cdlStr.contains("Col2"));
|
||||
assertTrue(cdlStr.contains("Val1"));
|
||||
assertTrue(cdlStr.contains("\"Val2"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that there is no error for a single escaped quote within a properly
|
||||
* embedded quote when not the last value.
|
||||
*/
|
||||
@Test
|
||||
public void singleEscapedQuoteMiddleString(){
|
||||
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4";
|
||||
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
|
||||
|
||||
String cdlStr = CDL.toString(jsonArray);
|
||||
assertTrue(cdlStr.contains("Col1"));
|
||||
assertTrue(cdlStr.contains("Col2"));
|
||||
assertTrue(cdlStr.contains("Val1"));
|
||||
assertTrue(cdlStr.contains("\"Val2"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to create a JSONArray with an escape quote and no enclosing quotes.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void badEscapedQuote(){
|
||||
String badLine = "Col1, Col2\nVal1, \"\"Val2";
|
||||
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
//System.out.println("Message" + e.getMessage());
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad character 'V' (86). at 20 [character 9 line 2]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* call toString with a null array
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullJSONArrayToString() {
|
||||
CDL.toString((JSONArray)null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray from an empty string
|
||||
*/
|
||||
@Test
|
||||
public void emptyString() {
|
||||
String emptyStr = "";
|
||||
JSONArray jsonArray = CDL.toJSONArray(emptyStr);
|
||||
assertNull("CDL should return null when the input string is empty", jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray with only 1 row
|
||||
*/
|
||||
@Test
|
||||
public void onlyColumnNames() {
|
||||
String columnNameStr = "col1, col2, col3";
|
||||
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
|
||||
assertNull("CDL should return null when only 1 row is given",
|
||||
jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray from string containing only whitespace and commas
|
||||
*/
|
||||
@Test
|
||||
public void emptyLinesToJSONArray() {
|
||||
String str = " , , , \n , , , ";
|
||||
JSONArray jsonArray = CDL.toJSONArray(str);
|
||||
assertNull("JSONArray should be null for no content",
|
||||
jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* call toString with a null array
|
||||
*/
|
||||
@Test
|
||||
public void emptyJSONArrayToString() {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
String str = CDL.toString(jsonArray);
|
||||
assertNull("CDL should return null for toString(null)",
|
||||
str);
|
||||
}
|
||||
|
||||
/**
|
||||
* call toString with a null arrays for names and values
|
||||
*/
|
||||
@Test
|
||||
public void nullJSONArraysToString() {
|
||||
String str = CDL.toString(null, null);
|
||||
assertNull("CDL should return null for toString(null)",
|
||||
str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a JSONArray that was not built by CDL, some chars may be
|
||||
* found that would otherwise be filtered out by CDL.
|
||||
*/
|
||||
@Test
|
||||
public void checkSpecialChars() {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonArray.put(jsonObject);
|
||||
// \r will be filtered from name
|
||||
jsonObject.put("Col \r1", "V1");
|
||||
// \r will be filtered from value
|
||||
jsonObject.put("Col 2", "V2\r");
|
||||
assertEquals("expected length should be 1", 1, jsonArray.length());
|
||||
String cdlStr = CDL.toString(jsonArray);
|
||||
jsonObject = jsonArray.getJSONObject(0);
|
||||
assertTrue(cdlStr.contains("\"Col 1\""));
|
||||
assertTrue(cdlStr.contains("Col 2"));
|
||||
assertTrue(cdlStr.contains("V1"));
|
||||
assertTrue(cdlStr.contains("\"V2\""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray from a string of lines
|
||||
*/
|
||||
@Test
|
||||
public void textToJSONArray() {
|
||||
JSONArray jsonArray = CDL.toJSONArray(LINES);
|
||||
JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
@Test
|
||||
public void textToJSONArrayPipeDelimited() {
|
||||
char delimiter = '|';
|
||||
JSONArray jsonArray = CDL.toJSONArray(LINES.replaceAll(",", String.valueOf(delimiter)), delimiter);
|
||||
JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray from a JSONArray of titles and a
|
||||
* string of value lines
|
||||
*/
|
||||
@Test
|
||||
public void jsonArrayToJSONArray() {
|
||||
String nameArrayStr = "[\"Col1\", \"Col2\"]";
|
||||
String values = "V1, V2";
|
||||
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
||||
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
|
||||
JSONArray expectedJsonArray = new JSONArray("[{\"Col1\":\"V1\",\"Col2\":\"V2\"}]");
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray from a string of lines,
|
||||
* then convert to string and then back to JSONArray
|
||||
*/
|
||||
@Test
|
||||
public void textToJSONArrayAndBackToString() {
|
||||
JSONArray jsonArray = CDL.toJSONArray(LINES);
|
||||
String jsonStr = CDL.toString(jsonArray);
|
||||
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSONArray from a string of lines,
|
||||
* then convert to string and then back to JSONArray
|
||||
* with a custom delimiter
|
||||
*/
|
||||
@Test
|
||||
public void textToJSONArrayAndBackToStringCustomDelimiter() {
|
||||
JSONArray jsonArray = CDL.toJSONArray(LINES, ',');
|
||||
String jsonStr = CDL.toString(jsonArray, ';');
|
||||
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr, ';');
|
||||
JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
190
src/test/java/org/json/junit/CookieListTest.java
Normal file
190
src/test/java/org/json/junit/CookieListTest.java
Normal file
@@ -0,0 +1,190 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.jayway.jsonpath.*;
|
||||
|
||||
/**
|
||||
* HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
|
||||
* <p>
|
||||
* A cookie list is a JSONObject whose members are presumed to be cookie
|
||||
* name/value pairs. Entries are unescaped while being added, and escaped in
|
||||
* the toString() output.
|
||||
* Unescaping means to convert %hh hex strings to the ascii equivalent
|
||||
* and converting '+' to ' '.
|
||||
* Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings.
|
||||
* <p>
|
||||
* CookieList should not be considered as just a list of Cookie objects:<br>
|
||||
* - CookieList stores a cookie name/value pair as a single entry; Cookie stores
|
||||
* it as 2 entries (key="name" and key="value").<br>
|
||||
* - CookieList requires multiple name/value pairs as input; Cookie allows the
|
||||
* 'secure' name with no associated value<br>
|
||||
* - CookieList has no special handling for attribute name/value pairs.<br>
|
||||
*/
|
||||
public class CookieListTest {
|
||||
|
||||
/**
|
||||
* Attempts to create a CookieList from a null string.
|
||||
* Expects a NullPointerException.
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullCookieListException() {
|
||||
String cookieStr = null;
|
||||
CookieList.toJSONObject(cookieStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a CookieList from a malformed string.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void malFormedCookieListException() {
|
||||
String cookieStr = "thisCookieHasNoEqualsChar";
|
||||
try {
|
||||
CookieList.toJSONObject(cookieStr);
|
||||
fail("should throw an exception");
|
||||
} catch (JSONException e) {
|
||||
/**
|
||||
* Not sure of the missing char, but full string compare fails
|
||||
*/
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a CookieList from an empty string.
|
||||
*/
|
||||
@Test
|
||||
public void emptyStringCookieList() {
|
||||
String cookieStr = "";
|
||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||
assertTrue(jsonObject.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* CookieList with the simplest cookie - a name/value pair with no delimiter.
|
||||
*/
|
||||
@Test
|
||||
public void simpleCookieList() {
|
||||
String cookieStr = "SID=31d4d96e407aad42";
|
||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
|
||||
}
|
||||
|
||||
/**
|
||||
* CookieList with a single a cookie which has a name/value pair and delimiter.
|
||||
*/
|
||||
@Test
|
||||
public void simpleCookieListWithDelimiter() {
|
||||
String cookieStr = "SID=31d4d96e407aad42;";
|
||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
|
||||
}
|
||||
|
||||
/**
|
||||
* CookieList with multiple cookies consisting of name/value pairs
|
||||
* with delimiters.
|
||||
*/
|
||||
@Test
|
||||
public void multiPartCookieList() {
|
||||
String cookieStr =
|
||||
"name1=myCookieValue1; "+
|
||||
" name2=myCookieValue2;"+
|
||||
"name3=myCookieValue3;"+
|
||||
" name4=myCookieValue4; "+
|
||||
"name5=myCookieValue5;"+
|
||||
" name6=myCookieValue6;";
|
||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
|
||||
assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
|
||||
assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
|
||||
assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
|
||||
assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
|
||||
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
|
||||
}
|
||||
|
||||
/**
|
||||
* CookieList from a JSONObject with valid key and null value
|
||||
*/
|
||||
@Test
|
||||
public void convertCookieListWithNullValueToString() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("key", JSONObject.NULL);
|
||||
String cookieToStr = CookieList.toString(jsonObject);
|
||||
assertTrue("toString() should be empty", "".equals(cookieToStr));
|
||||
}
|
||||
|
||||
/**
|
||||
* CookieList with multiple entries converted to a JSON document.
|
||||
*/
|
||||
@Test
|
||||
public void convertCookieListToString() {
|
||||
String cookieStr =
|
||||
"name1=myCookieValue1; "+
|
||||
" name2=myCookieValue2;"+
|
||||
"name3=myCookieValue3;"+
|
||||
" name4=myCookieValue4; "+
|
||||
"name5=myCookieValue5;"+
|
||||
" name6=myCookieValue6;";
|
||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||
// exercise CookieList.toString()
|
||||
String cookieListString = CookieList.toString(jsonObject);
|
||||
// have to convert it back for validation
|
||||
jsonObject = CookieList.toJSONObject(cookieListString);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
|
||||
assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
|
||||
assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
|
||||
assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
|
||||
assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
|
||||
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
|
||||
}
|
||||
|
||||
/**
|
||||
* CookieList with multiple entries and some '+' chars and URL-encoded
|
||||
* values converted to a JSON document.
|
||||
*/
|
||||
@Test
|
||||
public void convertEncodedCookieListToString() {
|
||||
String cookieStr =
|
||||
"name1=myCookieValue1; "+
|
||||
" name2=my+Cookie+Value+2;"+
|
||||
"name3=my%2BCookie%26Value%3B3%3D;"+
|
||||
" name4=my%25CookieValue4; "+
|
||||
"name5=myCookieValue5;"+
|
||||
" name6=myCookieValue6;";
|
||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
|
||||
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(jsonObject.query("/name2")));
|
||||
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(jsonObject.query("/name3")));
|
||||
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(jsonObject.query("/name4")));
|
||||
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
|
||||
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
|
||||
}
|
||||
}
|
||||
242
src/test/java/org/json/junit/CookieTest.java
Normal file
242
src/test/java/org/json/junit/CookieTest.java
Normal file
@@ -0,0 +1,242 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* HTTP cookie specification: RFC6265
|
||||
* <p>
|
||||
* At its most basic, a cookie is a name=value pair. The value may be subdivided
|
||||
* into other cookies, but that is not tested here. The cookie may also include
|
||||
* certain named attributes, delimited by semicolons.
|
||||
* <p>
|
||||
* The Cookie.toString() method emits certain attributes if present: expires,
|
||||
* domain, path, secure. All but secure are name-value pairs. Other attributes
|
||||
* are not included in the toString() output.
|
||||
* <p>
|
||||
* A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values.
|
||||
*/
|
||||
public class CookieTest {
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONObject from a null string.
|
||||
* Expects a NullPointerException.
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullCookieException() {
|
||||
String cookieStr = null;
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONObject from a cookie string with
|
||||
* no '=' char.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void malFormedNameValueException() {
|
||||
String cookieStr = "thisCookieHasNoEqualsChar";
|
||||
try {
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONObject from a cookie string
|
||||
* with embedded ';' char.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void booleanAttribute() {
|
||||
String cookieStr = "this=Cookie;myAttribute";
|
||||
JSONObject jo = Cookie.toJSONObject(cookieStr);
|
||||
assertTrue("has key 'name'", jo.has("name"));
|
||||
assertTrue("has key 'value'", jo.has("value"));
|
||||
assertTrue("has key 'myAttribute'", jo.has("myattribute"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONObject from an empty cookie string.<br>
|
||||
* Note: Cookie throws an exception, but CookieList does not.<br>
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void emptyStringCookieException() {
|
||||
String cookieStr = "";
|
||||
try {
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Cookies must have a 'name'",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* Attempts to create a JSONObject from an cookie string where the name is blank.<br>
|
||||
* Note: Cookie throws an exception, but CookieList does not.<br>
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void emptyNameCookieException() {
|
||||
String cookieStr = " = value ";
|
||||
try {
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Cookies must have a 'name'",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cookie from a simple name/value pair with no delimiter
|
||||
*/
|
||||
@Test
|
||||
public void simpleCookie() {
|
||||
String cookieStr = "SID=31d4d96e407aad42";
|
||||
String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}";
|
||||
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a cookie with all of the supported attributes in a
|
||||
* JSONObject. The secure attribute, which has no value, is treated
|
||||
* as a boolean.
|
||||
*/
|
||||
@Test
|
||||
public void multiPartCookie() {
|
||||
String cookieStr =
|
||||
"PH=deleted; "+
|
||||
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
|
||||
"path=/; "+
|
||||
" domain=.yahoo.com;"+
|
||||
"secure";
|
||||
String expectedCookieStr =
|
||||
"{"+
|
||||
"\"name\":\"PH\","+
|
||||
"\"value\":\"deleted\","+
|
||||
"\"path\":\"/\","+
|
||||
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
|
||||
"\"domain\":\".yahoo.com\","+
|
||||
"\"secure\":true"+
|
||||
"}";
|
||||
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cookie.toString() will emit the non-standard "thiswont=beIncluded"
|
||||
* attribute, and the attribute is still stored in the JSONObject.
|
||||
* This test confirms both behaviors.
|
||||
*/
|
||||
@Test
|
||||
public void convertCookieToString() {
|
||||
String cookieStr =
|
||||
"PH=deleted; "+
|
||||
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
|
||||
"path=/; "+
|
||||
" domain=.yahoo.com;"+
|
||||
"thisWont=beIncluded;"+
|
||||
"secure";
|
||||
String expectedCookieStr =
|
||||
"{\"thiswont\":\"beIncluded\","+
|
||||
"\"path\":\"/\","+
|
||||
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
|
||||
"\"domain\":\".yahoo.com\","+
|
||||
"\"name\":\"PH\","+
|
||||
"\"secure\":true,"+
|
||||
"\"value\":\"deleted\"}";
|
||||
// Add the nonstandard attribute to the expected cookie string
|
||||
String expectedDirectCompareCookieStr = expectedCookieStr;
|
||||
// convert all strings into JSONObjects
|
||||
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||
JSONObject expectedDirectCompareJsonObject =
|
||||
new JSONObject(expectedDirectCompareCookieStr);
|
||||
// emit the string
|
||||
String cookieToStr = Cookie.toString(jsonObject);
|
||||
// create a final JSONObject from the string
|
||||
JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr);
|
||||
// JSONObject should contain the nonstandard string
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedDirectCompareJsonObject);
|
||||
// JSONObject -> string -> JSONObject should not contain the nonstandard string
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* A string may be URL-encoded when converting to JSONObject.
|
||||
* If found, '+' is converted to ' ', and %hh hex strings are converted
|
||||
* to their ascii char equivalents. This test confirms the decoding
|
||||
* behavior.
|
||||
*/
|
||||
@Test
|
||||
public void convertEncodedCookieToString() {
|
||||
String cookieStr =
|
||||
"PH=deleted; "+
|
||||
" expires=Wed,+19-Mar-2014+17:53:53+GMT;"+
|
||||
"path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+
|
||||
" domain=.yahoo.com;"+
|
||||
"secure";
|
||||
String expectedCookieStr =
|
||||
"{\"path\":\"/+this/is&/a/spec;segment=\","+
|
||||
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
|
||||
"\"domain\":\".yahoo.com\","+
|
||||
"\"name\":\"PH\","+
|
||||
"\"secure\":true,"+
|
||||
"\"value\":\"deleted\"}";
|
||||
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||
String cookieToStr = Cookie.toString(jsonObject);
|
||||
JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* A public API method performs a URL encoding for selected chars
|
||||
* in a string. Control chars, '+', '%', '=', ';' are all encoded
|
||||
* as %hh hex strings. The string is also trimmed.
|
||||
* This test confirms that behavior.
|
||||
*/
|
||||
@Test
|
||||
public void escapeString() {
|
||||
String str = " +%\r\n\t\b%=;;; ";
|
||||
String expectedStr = "%2b%25%0d%0a%09%08%25%3d%3b%3b%3b";
|
||||
String actualStr = Cookie.escape(str);
|
||||
assertTrue("expect escape() to encode correctly. Actual: " +actualStr+
|
||||
" expected: " +expectedStr, expectedStr.equals(actualStr));
|
||||
}
|
||||
|
||||
/**
|
||||
* A public API method performs URL decoding for strings.
|
||||
* '+' is converted to space and %hh hex strings are converted to
|
||||
* their ascii equivalent values. The string is not trimmed.
|
||||
* This test confirms that behavior.
|
||||
*/
|
||||
@Test
|
||||
public void unescapeString() {
|
||||
String str = " +%2b%25%0d%0a%09%08%25%3d%3b%3b%3b+ ";
|
||||
String expectedStr = " +%\r\n\t\b%=;;; ";
|
||||
String actualStr = Cookie.unescape(str);
|
||||
assertTrue("expect unescape() to decode correctly. Actual: " +actualStr+
|
||||
" expected: " +expectedStr, expectedStr.equals(actualStr));
|
||||
}
|
||||
}
|
||||
433
src/test/java/org/json/junit/EnumTest.java
Normal file
433
src/test/java/org/json/junit/EnumTest.java
Normal file
@@ -0,0 +1,433 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.json.junit.data.MyEnum;
|
||||
import org.json.junit.data.MyEnumClass;
|
||||
import org.json.junit.data.MyEnumField;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.jayway.jsonpath.Configuration;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
/**
|
||||
* Enums are not explicitly supported in JSON-Java. But because enums act like
|
||||
* classes, all required behavior is already be present in some form.
|
||||
* These tests explore how enum serialization works with JSON-Java.
|
||||
*/
|
||||
public class EnumTest {
|
||||
|
||||
/**
|
||||
* To serialize an enum by its getters, use the JSONObject Object constructor.
|
||||
* The JSONObject ctor handles enum like any other bean. A JSONobject
|
||||
* is created whose entries are the getter name/value pairs.
|
||||
*/
|
||||
@Test
|
||||
public void jsonObjectFromEnum() {
|
||||
// If there are no getters then the object is empty.
|
||||
MyEnum myEnum = MyEnum.VAL2;
|
||||
JSONObject jsonObject = new JSONObject(myEnum);
|
||||
assertTrue("simple enum has no getters", jsonObject.isEmpty());
|
||||
|
||||
// enum with a getters should create a non-empty object
|
||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||
jsonObject = new JSONObject(myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||
.parse(jsonObject.toString());
|
||||
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expecting val 2", "val 2".equals(jsonObject.query("/value")));
|
||||
assertTrue("expecting 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
|
||||
|
||||
/**
|
||||
* class which contains enum instances. Each enum should be stored
|
||||
* in its own JSONObject
|
||||
*/
|
||||
MyEnumClass myEnumClass = new MyEnumClass();
|
||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
||||
jsonObject = new JSONObject(myEnumClass);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected 2 myEnumField items", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
|
||||
assertTrue("expected 0 myEnum items", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
|
||||
|
||||
assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField")));
|
||||
assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum")));
|
||||
}
|
||||
|
||||
/**
|
||||
* To serialize an enum by its set of allowed values, use getNames()
|
||||
* and the JSONObject Object with names constructor.
|
||||
*/
|
||||
@Test
|
||||
public void jsonObjectFromEnumWithNames() {
|
||||
String [] names;
|
||||
JSONObject jsonObject;
|
||||
|
||||
MyEnum myEnum = MyEnum.VAL1;
|
||||
names = JSONObject.getNames(myEnum);
|
||||
// The values will be MyEnum fields
|
||||
jsonObject = new JSONObject(myEnum, names);
|
||||
|
||||
// validate JSON object
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||
assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
|
||||
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
|
||||
assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
|
||||
|
||||
MyEnumField myEnumField = MyEnumField.VAL3;
|
||||
names = JSONObject.getNames(myEnumField);
|
||||
// The values will be MyEnmField fields
|
||||
jsonObject = new JSONObject(myEnumField, names);
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
|
||||
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
|
||||
assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that enums are handled consistently between JSONArray and JSONObject
|
||||
*/
|
||||
@Test
|
||||
public void verifyEnumConsistency(){
|
||||
JSONObject jo = new JSONObject();
|
||||
|
||||
jo.put("value", MyEnumField.VAL2);
|
||||
String expected="{\"value\":\"VAL2\"}";
|
||||
String actual = jo.toString();
|
||||
assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
|
||||
|
||||
jo.accumulate("value", MyEnumField.VAL1);
|
||||
expected="{\"value\":[\"VAL2\",\"VAL1\"]}";
|
||||
actual = jo.toString();
|
||||
assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
|
||||
|
||||
jo.remove("value");
|
||||
jo.append("value", MyEnumField.VAL1);
|
||||
expected="{\"value\":[\"VAL1\"]}";
|
||||
actual = jo.toString();
|
||||
assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
|
||||
|
||||
jo.put("value", EnumSet.of(MyEnumField.VAL2));
|
||||
expected="{\"value\":[\"VAL2\"]}";
|
||||
actual = jo.toString();
|
||||
assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
|
||||
|
||||
JSONArray ja = new JSONArray();
|
||||
ja.put(MyEnumField.VAL2);
|
||||
jo.put("value", ja);
|
||||
actual = jo.toString();
|
||||
assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
|
||||
|
||||
jo.put("value", new MyEnumField[]{MyEnumField.VAL2});
|
||||
actual = jo.toString();
|
||||
assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* To serialize by assigned value, use the put() methods. The value
|
||||
* will be stored as a enum type.
|
||||
*/
|
||||
@Test
|
||||
public void enumPut() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
MyEnum myEnum = MyEnum.VAL2;
|
||||
jsonObject.put("myEnum", myEnum);
|
||||
MyEnumField myEnumField = MyEnumField.VAL1;
|
||||
jsonObject.putOnce("myEnumField", myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 2 top level objects", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
|
||||
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/myEnumField")));
|
||||
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(myEnum);
|
||||
jsonArray.put(1, myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||
assertTrue("expected 2 top level objects", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
|
||||
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonArray.query("/1")));
|
||||
|
||||
/**
|
||||
* Leaving these tests because they exercise get, opt, and remove
|
||||
*/
|
||||
assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0)));
|
||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1)));
|
||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The default action of valueToString() is to call object.toString().
|
||||
* For enums, this means the assigned value will be returned as a string.
|
||||
*/
|
||||
@Test
|
||||
public void enumValueToString() {
|
||||
String expectedStr1 = "\"VAL1\"";
|
||||
String expectedStr2 = "\"VAL1\"";
|
||||
MyEnum myEnum = MyEnum.VAL1;
|
||||
MyEnumField myEnumField = MyEnumField.VAL1;
|
||||
MyEnumClass myEnumClass = new MyEnumClass();
|
||||
|
||||
String str1 = JSONObject.valueToString(myEnum);
|
||||
assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1,
|
||||
str1.equals(expectedStr1));
|
||||
String str2 = JSONObject.valueToString(myEnumField);
|
||||
assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2,
|
||||
str2.equals(expectedStr2));
|
||||
|
||||
/**
|
||||
* However, an enum within another class will not be rendered
|
||||
* unless that class overrides default toString()
|
||||
*/
|
||||
String expectedStr3 = "\"org.json.junit.data.MyEnumClass@";
|
||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||
myEnumClass.setMyEnumField(MyEnumField.VAL1);
|
||||
String str3 = JSONObject.valueToString(myEnumClass);
|
||||
assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3,
|
||||
str3.startsWith(expectedStr3));
|
||||
}
|
||||
|
||||
/**
|
||||
* In whatever form the enum was added to the JSONObject or JSONArray,
|
||||
* json[Object|Array].toString should serialize it in a reasonable way.
|
||||
*/
|
||||
@Test
|
||||
public void enumToString() {
|
||||
MyEnum myEnum = MyEnum.VAL2;
|
||||
JSONObject jsonObject = new JSONObject(myEnum);
|
||||
String expectedStr = "{}";
|
||||
assertTrue("myEnum toString() should be empty", expectedStr.equals(jsonObject.toString()));
|
||||
|
||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||
jsonObject = new JSONObject(myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value")));
|
||||
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
|
||||
|
||||
MyEnumClass myEnumClass = new MyEnumClass();
|
||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
||||
jsonObject = new JSONObject(myEnumClass);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
|
||||
assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
|
||||
|
||||
String [] names = JSONObject.getNames(myEnum);
|
||||
jsonObject = new JSONObject(myEnum, names);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||
assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
|
||||
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
|
||||
assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
|
||||
|
||||
names = JSONObject.getNames(myEnumField);
|
||||
jsonObject = new JSONObject(myEnumField, names);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
|
||||
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
|
||||
assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
|
||||
|
||||
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
|
||||
jsonObject = new JSONObject();
|
||||
jsonObject.putOpt("myEnum", myEnum);
|
||||
jsonObject.putOnce("myEnumField", myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
|
||||
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/myEnumField")));
|
||||
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(myEnum);
|
||||
jsonArray.put(1, myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||
assertTrue("expected 2 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
|
||||
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonArray.query("/1")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap should handle enums exactly as a value type like Integer, Boolean, or String.
|
||||
*/
|
||||
@Test
|
||||
public void wrap() {
|
||||
assertTrue("simple enum has no getters", JSONObject.wrap(MyEnum.VAL2) instanceof MyEnum);
|
||||
|
||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("enum",myEnumField);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 1 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/enum")));
|
||||
|
||||
MyEnumClass myEnumClass = new MyEnumClass();
|
||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
||||
jsonObject = (JSONObject)JSONObject.wrap(myEnumClass);
|
||||
|
||||
// validate JSON content
|
||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||
assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
|
||||
assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
|
||||
|
||||
assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField")));
|
||||
assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum")));
|
||||
}
|
||||
|
||||
/**
|
||||
* It was determined that some API methods should be added to
|
||||
* support enums:<br>
|
||||
* JSONObject.getEnum(class, key)<br>
|
||||
* JSONObject.optEnum(class, key)<br>
|
||||
* JSONObject.optEnum(class, key, default)<br>
|
||||
* JSONArray.getEnum(class, index)<br>
|
||||
* JSONArray.optEnum(class, index)<br>
|
||||
* JSONArray.optEnum(class, index, default)<br>
|
||||
* <p>
|
||||
* Exercise these enum API methods on JSONObject and JSONArray
|
||||
*/
|
||||
@Test
|
||||
public void enumAPI() {
|
||||
MyEnumClass myEnumClass = new MyEnumClass();
|
||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("strKey", "value");
|
||||
jsonObject.put("strKey2", "VAL1");
|
||||
jsonObject.put("enumKey", myEnumField);
|
||||
jsonObject.put("enumClassKey", myEnumClass);
|
||||
|
||||
// get a plain old enum
|
||||
MyEnumField actualEnum = jsonObject.getEnum(MyEnumField.class, "enumKey");
|
||||
assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2);
|
||||
|
||||
// try to get the wrong value
|
||||
try {
|
||||
actualEnum = jsonObject.getEnum(MyEnumField.class, "strKey");
|
||||
assertTrue("should throw an exception for wrong key", false);
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
// get a class that contains an enum
|
||||
MyEnumClass actualEnumClass = (MyEnumClass)jsonObject.get("enumClassKey");
|
||||
assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
|
||||
|
||||
// opt a plain old enum
|
||||
actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey");
|
||||
assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
|
||||
|
||||
// opt the wrong value
|
||||
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey");
|
||||
assertTrue("opt null", actualEnum == null);
|
||||
|
||||
// opt a class that contains an enum
|
||||
actualEnumClass = (MyEnumClass)jsonObject.opt("enumClassKey");
|
||||
assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
|
||||
|
||||
// opt with default a plain old enum
|
||||
actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey", null);
|
||||
assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
|
||||
|
||||
// opt with default the wrong value
|
||||
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null);
|
||||
assertNull("opt null", actualEnum);
|
||||
|
||||
// opt with default the string value
|
||||
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey2", null);
|
||||
assertEquals(MyEnumField.VAL1, actualEnum);
|
||||
|
||||
// opt with default an index that does not exist
|
||||
actualEnum = jsonObject.optEnum(MyEnumField.class, "noKey", null);
|
||||
assertNull("opt null", actualEnum);
|
||||
|
||||
assertNull("Expected Null when the enum class is null",
|
||||
jsonObject.optEnum(null, "enumKey"));
|
||||
|
||||
/**
|
||||
* Exercise the proposed enum API methods on JSONArray
|
||||
*/
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put("value");
|
||||
jsonArray.put(myEnumField);
|
||||
jsonArray.put(myEnumClass);
|
||||
|
||||
// get a plain old enum
|
||||
actualEnum = jsonArray.getEnum(MyEnumField.class, 1);
|
||||
assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2);
|
||||
|
||||
// try to get the wrong value
|
||||
try {
|
||||
actualEnum = jsonArray.getEnum(MyEnumField.class, 0);
|
||||
assertTrue("should throw an exception for wrong index", false);
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
// get a class that contains an enum
|
||||
actualEnumClass = (MyEnumClass)jsonArray.get(2);
|
||||
assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
|
||||
|
||||
// opt a plain old enum
|
||||
actualEnum = jsonArray.optEnum(MyEnumField.class, 1);
|
||||
assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
|
||||
|
||||
// opt the wrong value
|
||||
actualEnum = jsonArray.optEnum(MyEnumField.class, 0);
|
||||
assertTrue("opt null", actualEnum == null);
|
||||
|
||||
// opt a class that contains an enum
|
||||
actualEnumClass = (MyEnumClass)jsonArray.opt(2);
|
||||
assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
|
||||
|
||||
// opt with default a plain old enum
|
||||
actualEnum = jsonArray.optEnum(MyEnumField.class, 1, null);
|
||||
assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
|
||||
|
||||
// opt with default the wrong value
|
||||
actualEnum = jsonArray.optEnum(MyEnumField.class, 0, null);
|
||||
assertTrue("opt null", actualEnum == null);
|
||||
|
||||
// opt with default an index that does not exist
|
||||
actualEnum = jsonArray.optEnum(MyEnumField.class, 3, null);
|
||||
assertTrue("opt null", actualEnum == null);
|
||||
|
||||
}
|
||||
}
|
||||
200
src/test/java/org/json/junit/HTTPTest.java
Normal file
200
src/test/java/org/json/junit/HTTPTest.java
Normal file
@@ -0,0 +1,200 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for JSON-Java HTTP.java. See RFC7230.
|
||||
*/
|
||||
public class HTTPTest {
|
||||
|
||||
/**
|
||||
* Attempt to call HTTP.toJSONObject() with a null string
|
||||
* Expects a NUllPointerException.
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullHTTPException() {
|
||||
String httpStr = null;
|
||||
HTTP.toJSONObject(httpStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to call HTTP.toJSONObject() with a string containing
|
||||
* an empty object. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void notEnoughHTTPException() {
|
||||
String httpStr = "{}";
|
||||
JSONObject jsonObject = new JSONObject(httpStr);
|
||||
try {
|
||||
HTTP.toString(jsonObject);
|
||||
assertTrue("Expected to throw exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Not enough material for an HTTP header.".equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling HTTP.toJSONObject() with an empty string will result in a
|
||||
* populated JSONObject with keys but no values for Request-URI, Method,
|
||||
* and HTTP-Version.
|
||||
*/
|
||||
@Test
|
||||
public void emptyStringHTTPRequest() {
|
||||
String httpStr = "";
|
||||
String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a Request-URI, Method,
|
||||
* and HTTP-Version.
|
||||
*/
|
||||
@Test
|
||||
public void simpleHTTPRequest() {
|
||||
String httpStr = "GET /hello.txt HTTP/1.1";
|
||||
String expectedHTTPStr =
|
||||
"{\"Request-URI\":\"/hello.txt\",\"Method\":\"GET\",\"HTTP-Version\":\"HTTP/1.1\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a response string containing a
|
||||
* HTTP-Version, Status-Code, and Reason.
|
||||
*/
|
||||
@Test
|
||||
public void simpleHTTPResponse() {
|
||||
String httpStr = "HTTP/1.1 200 OK";
|
||||
String expectedHTTPStr =
|
||||
"{\"HTTP-Version\":\"HTTP/1.1\",\"Status-Code\":\"200\",\"Reason-Phrase\":\"OK\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full request string including
|
||||
* request headers.
|
||||
*/
|
||||
@Test
|
||||
public void extendedHTTPRequest() {
|
||||
String httpStr =
|
||||
"POST /enlighten/calais.asmx HTTP/1.1\n"+
|
||||
"Host: api.opencalais.com\n"+
|
||||
"Content-Type: text/xml; charset=utf-8\n"+
|
||||
"Content-Length: 100\n"+
|
||||
"SOAPAction: \"http://clearforest.com/Enlighten\"";
|
||||
String expectedHTTPStr =
|
||||
"{"+
|
||||
"\"Request-URI\":\"/enlighten/calais.asmx\","+
|
||||
"\"Host\":\"api.opencalais.com\","+
|
||||
"\"Method\":\"POST\","+
|
||||
"\"HTTP-Version\":\"HTTP/1.1\","+
|
||||
"\"Content-Length\":\"100\","+
|
||||
"\"Content-Type\":\"text/xml; charset=utf-8\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
/**
|
||||
* Not too easy for JSONObject to parse a string with embedded quotes.
|
||||
* For the sake of the test, add it here.
|
||||
*/
|
||||
expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\"");
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full response string including
|
||||
* response headers.
|
||||
*/
|
||||
@Test
|
||||
public void extendedHTTPResponse() {
|
||||
String httpStr =
|
||||
"HTTP/1.1 200 OK\n"+
|
||||
"Content-Type: text/xml; charset=utf-8\n"+
|
||||
"Content-Length: 100\n";
|
||||
String expectedHTTPStr =
|
||||
"{\"HTTP-Version\":\"HTTP/1.1\","+
|
||||
"\"Status-Code\":\"200\","+
|
||||
"\"Content-Length\":\"100\","+
|
||||
"\"Reason-Phrase\":\"OK\","+
|
||||
"\"Content-Type\":\"text/xml; charset=utf-8\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full POST request string including
|
||||
* response headers, then convert it back into an HTTP string.
|
||||
*/
|
||||
@Test
|
||||
public void convertHTTPRequestToString() {
|
||||
String httpStr =
|
||||
"POST /enlighten/calais.asmx HTTP/1.1\n"+
|
||||
"Host: api.opencalais.com\n"+
|
||||
"Content-Type: text/xml; charset=utf-8\n"+
|
||||
"Content-Length: 100";
|
||||
String expectedHTTPStr =
|
||||
"{"+
|
||||
"\"Request-URI\":\"/enlighten/calais.asmx\","+
|
||||
"\"Host\":\"api.opencalais.com\","+
|
||||
"\"Method\":\"POST\","+
|
||||
"\"HTTP-Version\":\"HTTP/1.1\","+
|
||||
"\"Content-Length\":\"100\","+
|
||||
"\"Content-Type\":\"text/xml; charset=utf-8\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
String httpToStr = HTTP.toString(jsonObject);
|
||||
/**
|
||||
* JSONObject objects to crlfs and any trailing chars.
|
||||
* For the sake of the test, simplify the resulting string
|
||||
*/
|
||||
httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
|
||||
httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
|
||||
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full response string including
|
||||
* response headers, then convert it back into an HTTP string.
|
||||
*/
|
||||
@Test
|
||||
public void convertHTTPResponseToString() {
|
||||
String httpStr =
|
||||
"HTTP/1.1 200 OK\n"+
|
||||
"Content-Type: text/xml; charset=utf-8\n"+
|
||||
"Content-Length: 100\n";
|
||||
String expectedHTTPStr =
|
||||
"{\"HTTP-Version\":\"HTTP/1.1\","+
|
||||
"\"Status-Code\":\"200\","+
|
||||
"\"Content-Length\":\"100\","+
|
||||
"\"Reason-Phrase\":\"OK\","+
|
||||
"\"Content-Type\":\"text/xml; charset=utf-8\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
String httpToStr = HTTP.toString(jsonObject);
|
||||
/**
|
||||
* JSONObject objects to crlfs and any trailing chars.
|
||||
* For the sake of the test, simplify the resulting string
|
||||
*/
|
||||
httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
|
||||
httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
|
||||
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
}
|
||||
1547
src/test/java/org/json/junit/JSONArrayTest.java
Normal file
1547
src/test/java/org/json/junit/JSONArrayTest.java
Normal file
File diff suppressed because it is too large
Load Diff
989
src/test/java/org/json/junit/JSONMLTest.java
Normal file
989
src/test/java/org/json/junit/JSONMLTest.java
Normal file
@@ -0,0 +1,989 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for org.json.JSONML.java
|
||||
*
|
||||
* Certain inputs are expected to result in exceptions. These tests are
|
||||
* executed first. JSONML provides an API to:
|
||||
* Convert an XML string into a JSONArray or a JSONObject.
|
||||
* Convert a JSONArray or JSONObject into an XML string.
|
||||
* Both fromstring and tostring operations operations should be symmetrical
|
||||
* within the limits of JSONML.
|
||||
* It should be possible to perform the following operations, which should
|
||||
* result in the original string being recovered, within the limits of the
|
||||
* underlying classes:
|
||||
* Convert a string -> JSONArray -> string -> JSONObject -> string
|
||||
* Convert a string -> JSONObject -> string -> JSONArray -> string
|
||||
*
|
||||
*/
|
||||
public class JSONMLTest {
|
||||
|
||||
/**
|
||||
* Attempts to transform a null XML string to JSON.
|
||||
* Expects a NullPointerException
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullXMLException() {
|
||||
String xmlStr = null;
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to transform an empty string to JSON.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void emptyXMLException() {
|
||||
String xmlStr = "";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad XML at 0 [character 1 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to call JSONML.toString() with a null JSONArray.
|
||||
* Expects a NullPointerException.
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullJSONXMLException() {
|
||||
/**
|
||||
* Tries to convert a null JSONArray to XML.
|
||||
*/
|
||||
JSONArray jsonArray= null;
|
||||
JSONML.toString(jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to call JSONML.toString() with a null JSONArray.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void emptyJSONXMLException() {
|
||||
/**
|
||||
* Tries to convert an empty JSONArray to XML.
|
||||
*/
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
try {
|
||||
JSONML.toString(jsonArray);
|
||||
assertTrue("Expecting an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"JSONArray[0] not found.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to transform an non-XML string to JSON.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void nonXMLException() {
|
||||
/**
|
||||
* Attempts to transform a nonXML string to JSON
|
||||
*/
|
||||
String xmlStr = "{ \"this is\": \"not xml\"}";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad XML at 23 [character 24 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to transform a JSON document with XML content that
|
||||
* does not follow JSONML conventions (element name is not first value
|
||||
* in a nested JSONArray) to a JSONArray then back to string.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void emptyTagException() {
|
||||
/**
|
||||
* jsonArrayStr is used to build a JSONArray which is then
|
||||
* turned into XML. For this transformation, all arrays represent
|
||||
* elements and the first array entry is the name of the element.
|
||||
* In this case, one of the arrays does not have a name
|
||||
*/
|
||||
String jsonArrayStr =
|
||||
"[\"addresses\","+
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
|
||||
// this array has no name
|
||||
"["+
|
||||
"[\"name\"],"+
|
||||
"[\"nocontent\"],"+
|
||||
"\">\""+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(jsonArrayStr);
|
||||
try {
|
||||
JSONML.toString(jsonArray);
|
||||
assertTrue("Expecting an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"JSONArray[0] is not a String (class org.json.JSONArray).",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to transform a JSON document with XML content that
|
||||
* does not follow JSONML conventions (element tag has an embedded space)
|
||||
* to a JSONArray then back to string. Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void spaceInTagException() {
|
||||
/**
|
||||
* jsonArrayStr is used to build a JSONArray which is then
|
||||
* turned into XML. For this transformation, all arrays represent
|
||||
* elements and the first array entry is the name of the element.
|
||||
* In this case, one of the element names has an embedded space,
|
||||
* which is not allowed.
|
||||
*/
|
||||
String jsonArrayStr =
|
||||
"[\"addresses\","+
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
|
||||
// this array has an invalid name
|
||||
"[\"addr esses\","+
|
||||
"[\"name\"],"+
|
||||
"[\"nocontent\"],"+
|
||||
"\">\""+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(jsonArrayStr);
|
||||
try {
|
||||
JSONML.toString(jsonArray);
|
||||
assertTrue("Expecting an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"'addr esses' contains a space character.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to transform a malformed XML document
|
||||
* (element tag has a frontslash) to a JSONArray.\
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void invalidSlashInTagException() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* In this case, the XML is invalid because the 'name' element
|
||||
* contains an invalid frontslash.
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name/x>\n"+
|
||||
" <street>abc street</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Misshaped tag at 176 [character 14 line 4]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Malformed XML text (invalid tagname) is transformed into a JSONArray.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void invalidBangInTagException() {
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name/>\n"+
|
||||
" <!>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Misshaped meta tag at 215 [character 12 line 7]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Malformed XML text (invalid tagname, no close bracket) is transformed\
|
||||
* into a JSONArray. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void invalidBangNoCloseInTagException() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* In this case, the XML is invalid because an element
|
||||
* starts with '!' and has no closing tag
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name/>\n"+
|
||||
" <!\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Misshaped meta tag at 214 [character 12 line 7]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Malformed XML text (tagname with no close bracket) is transformed\
|
||||
* into a JSONArray. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void noCloseStartTagException() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* In this case, the XML is invalid because an element
|
||||
* has no closing '>'.
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name/>\n"+
|
||||
" <abc\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Misplaced '<' at 194 [character 5 line 6]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Malformed XML text (endtag with no name) is transformed\
|
||||
* into a JSONArray. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void noCloseEndTagException() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* In this case, the XML is invalid because an element
|
||||
* has no name after the closing tag '</'.
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name/>\n"+
|
||||
" <abc/>\n"+
|
||||
" </>\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
assertTrue("Expecting an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Expected a closing name instead of '>'.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Malformed XML text (endtag with no close bracket) is transformed\
|
||||
* into a JSONArray. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void noCloseEndBraceException() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* In this case, the XML is invalid because an element
|
||||
* has '>' after the closing tag '</' and name.
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation=\"test.xsd\">\n"+
|
||||
" <address>\n"+
|
||||
" <name/>\n"+
|
||||
" <abc/>\n"+
|
||||
" </address\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Misplaced '<' at 206 [character 1 line 7]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Malformed XML text (incomplete CDATA string) is transformed\
|
||||
* into a JSONArray. Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void invalidCDATABangInTagException() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* In this case, the XML is invalid because an element
|
||||
* does not have a complete CDATA string.
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name>Joe Tester</name>\n"+
|
||||
" <![[]>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
try {
|
||||
JSONML.toJSONArray(xmlStr);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected 'CDATA[' at 204 [character 11 line 5]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an XML document into a JSONArray, then use JSONML.toString()
|
||||
* to convert it into a string. This string is then converted back into
|
||||
* a JSONArray. Both JSONArrays are compared against a control to
|
||||
* confirm the contents.
|
||||
*/
|
||||
@Test
|
||||
public void toJSONArray() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONArray.
|
||||
* Each element becomes a JSONArray:
|
||||
* 1st entry = elementname
|
||||
* 2nd entry = attributes object (if present)
|
||||
* 3rd entry = content (if present)
|
||||
* 4th entry = child element JSONArrays (if present)
|
||||
* The result is compared against an expected JSONArray.
|
||||
* The transformed JSONArray is then transformed back into a string
|
||||
* which is used to create a final JSONArray, which is also compared
|
||||
* against the expected JSONArray.
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
"<address attr1=\"attrValue1\" attr2=\"attrValue2\" attr3=\"attrValue3\">\n"+
|
||||
"<name nameType=\"mine\">myName</name>\n"+
|
||||
"<nocontent/>>\n"+
|
||||
"</address>\n"+
|
||||
"</addresses>";
|
||||
String expectedStr =
|
||||
"[\"addresses\","+
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
|
||||
"[\"address\","+
|
||||
"{\"attr1\":\"attrValue1\",\"attr2\":\"attrValue2\",\"attr3\":\"attrValue3\"},"+
|
||||
"[\"name\", {\"nameType\":\"mine\"},\"myName\"],"+
|
||||
"[\"nocontent\"],"+
|
||||
"\">\""+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
||||
String xmlToStr = JSONML.toString(jsonArray);
|
||||
JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an XML document into a JSONObject. Use JSONML.toString() to
|
||||
* convert it back into a string, and then re-convert it into a JSONObject.
|
||||
* Both JSONObjects are compared against a control JSONObject to confirm
|
||||
* the contents.
|
||||
* <p>
|
||||
* Next convert the XML document into a JSONArray. Use JSONML.toString() to
|
||||
* convert it back into a string, and then re-convert it into a JSONArray.
|
||||
* Both JSONArrays are compared against a control JSONArray to confirm
|
||||
* the contents.
|
||||
* <p>
|
||||
* This test gives a comprehensive example of how the JSONML
|
||||
* transformations work.
|
||||
*/
|
||||
@Test
|
||||
public void toJSONObjectToJSONArray() {
|
||||
/**
|
||||
* xmlStr contains XML text which is transformed into a JSONObject,
|
||||
* restored to XML, transformed into a JSONArray, and then restored
|
||||
* to XML again. Both JSONObject and JSONArray should contain the same
|
||||
* information and should produce the same XML, allowing for non-ordered
|
||||
* attributes.
|
||||
*
|
||||
* Transformation to JSONObject:
|
||||
* The elementName is stored as a string where key="tagName"
|
||||
* Attributes are simply stored as key/value pairs
|
||||
* If the element has either content or child elements, they are stored
|
||||
* in a jsonArray with key="childNodes".
|
||||
*
|
||||
* Transformation to JSONArray:
|
||||
* 1st entry = elementname
|
||||
* 2nd entry = attributes object (if present)
|
||||
* 3rd entry = content (if present)
|
||||
* 4th entry = child element JSONArrays (if present)
|
||||
*/
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
"<address addrType=\"my address\">\n"+
|
||||
"<name nameType=\"my name\">Joe Tester</name>\n"+
|
||||
"<street><![CDATA[Baker street 5]]></street>\n"+
|
||||
"<NothingHere except=\"an attribute\"/>\n"+
|
||||
"<TrueValue>true</TrueValue>\n"+
|
||||
"<FalseValue>false</FalseValue>\n"+
|
||||
"<NullValue>null</NullValue>\n"+
|
||||
"<PositiveValue>42</PositiveValue>\n"+
|
||||
"<NegativeValue>-23</NegativeValue>\n"+
|
||||
"<DoubleValue>-23.45</DoubleValue>\n"+
|
||||
"<Nan>-23x.45</Nan>\n"+
|
||||
"<ArrayOfNum>\n"+
|
||||
"<value>1</value>\n"+
|
||||
"<value>2</value>\n"+
|
||||
"<value><subValue svAttr=\"svValue\">abc</subValue></value>\n"+
|
||||
"<value>3</value>\n"+
|
||||
"<value>4.1</value>\n"+
|
||||
"<value>5.2</value>\n"+
|
||||
"</ArrayOfNum>\n"+
|
||||
"</address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
String expectedJSONObjectStr =
|
||||
"{"+
|
||||
"\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"childNodes\":["+
|
||||
"{"+
|
||||
"\"childNodes\":["+
|
||||
"{"+
|
||||
"\"childNodes\":[\"Joe Tester\"],"+
|
||||
"\"nameType\":\"my name\","+
|
||||
"\"tagName\":\"name\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[\"Baker street 5\"],"+
|
||||
"\"tagName\":\"street\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"tagName\":\"NothingHere\","+
|
||||
"\"except\":\"an attribute\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[true],"+
|
||||
"\"tagName\":\"TrueValue\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[false],"+
|
||||
"\"tagName\":\"FalseValue\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[null],"+
|
||||
"\"tagName\":\"NullValue\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[42],"+
|
||||
"\"tagName\":\"PositiveValue\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[-23],"+
|
||||
"\"tagName\":\"NegativeValue\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[-23.45],"+
|
||||
"\"tagName\":\"DoubleValue\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[\"-23x.45\"],"+
|
||||
"\"tagName\":\"Nan\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":["+
|
||||
"{"+
|
||||
"\"childNodes\":[1],"+
|
||||
"\"tagName\":\"value\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[2],"+
|
||||
"\"tagName\":\"value\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":["+
|
||||
"{"+
|
||||
"\"childNodes\":[\"abc\"],"+
|
||||
"\"svAttr\":\"svValue\","+
|
||||
"\"tagName\":\"subValue\""+
|
||||
"}"+
|
||||
"],"+
|
||||
"\"tagName\":\"value\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[3],"+
|
||||
"\"tagName\":\"value\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[4.1],"+
|
||||
"\"tagName\":\"value\""+
|
||||
"},"+
|
||||
"{"+
|
||||
"\"childNodes\":[5.2],"+
|
||||
"\"tagName\":\"value\""+
|
||||
"}"+
|
||||
"],"+
|
||||
"\"tagName\":\"ArrayOfNum\""+
|
||||
"}"+
|
||||
"],"+
|
||||
"\"addrType\":\"my address\","+
|
||||
"\"tagName\":\"address\""+
|
||||
"}"+
|
||||
"],"+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
|
||||
"\"tagName\":\"addresses\""+
|
||||
"}";
|
||||
|
||||
String expectedJSONArrayStr =
|
||||
"["+
|
||||
"\"addresses\","+
|
||||
"{"+
|
||||
"\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
"},"+
|
||||
"["+
|
||||
"\"address\","+
|
||||
"{"+
|
||||
"\"addrType\":\"my address\""+
|
||||
"},"+
|
||||
"["+
|
||||
"\"name\","+
|
||||
"{"+
|
||||
"\"nameType\":\"my name\""+
|
||||
"},"+
|
||||
"\"Joe Tester\""+
|
||||
"],"+
|
||||
"[\"street\",\"Baker street 5\"],"+
|
||||
"["+
|
||||
"\"NothingHere\","+
|
||||
"{\"except\":\"an attribute\"}"+
|
||||
"],"+
|
||||
"[\"TrueValue\",true],"+
|
||||
"[\"FalseValue\",false],"+
|
||||
"[\"NullValue\",null],"+
|
||||
"[\"PositiveValue\",42],"+
|
||||
"[\"NegativeValue\",-23],"+
|
||||
"[\"DoubleValue\",-23.45],"+
|
||||
"[\"Nan\",\"-23x.45\"],"+
|
||||
"["+
|
||||
"\"ArrayOfNum\","+
|
||||
"[\"value\",1],"+
|
||||
"[\"value\",2],"+
|
||||
"[\"value\","+
|
||||
"["+
|
||||
"\"subValue\","+
|
||||
"{\"svAttr\":\"svValue\"},"+
|
||||
"\"abc\""+
|
||||
"]"+
|
||||
"],"+
|
||||
"[\"value\",3],"+
|
||||
"[\"value\",4.1],"+
|
||||
"[\"value\",5.2]"+
|
||||
"]"+
|
||||
"]"+
|
||||
"]";
|
||||
|
||||
// make a JSONObject and make sure it looks as expected
|
||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedJSONObjectStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
|
||||
// restore the XML, then make another JSONObject and make sure it
|
||||
// looks as expected
|
||||
String jsonObjectXmlToStr = JSONML.toString(jsonObject);
|
||||
JSONObject finalJsonObject = JSONML.toJSONObject(jsonObjectXmlToStr);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
|
||||
|
||||
// create a JSON array from the original string and make sure it
|
||||
// looks as expected
|
||||
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
|
||||
|
||||
// restore the XML, then make another JSONArray and make sure it
|
||||
// looks as expected
|
||||
String jsonArrayXmlToStr = JSONML.toString(jsonArray);
|
||||
JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
|
||||
// lastly, confirm the restored JSONObject XML and JSONArray XML look
|
||||
// reasonably similar
|
||||
JSONObject jsonObjectFromObject = JSONML.toJSONObject(jsonObjectXmlToStr);
|
||||
JSONObject jsonObjectFromArray = JSONML.toJSONObject(jsonArrayXmlToStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObjectFromObject, jsonObjectFromArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an XML document which contains embedded comments into
|
||||
* a JSONArray. Use JSONML.toString() to turn it into a string, then
|
||||
* reconvert it into a JSONArray. Compare both JSONArrays to a control
|
||||
* JSONArray to confirm the contents.
|
||||
* <p>
|
||||
* This test shows how XML comments are handled.
|
||||
*/
|
||||
@Test
|
||||
public void commentsInXML() {
|
||||
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<!-- this is a comment -->\n"+
|
||||
"<addresses>\n"+
|
||||
"<address>\n"+
|
||||
"<!-- <!--[CDATA[ this is -- <another> comment ]] -->\n"+
|
||||
"<name>Joe Tester</name>\n"+
|
||||
"<!-- this is a - multi line \n"+
|
||||
"comment -->\n"+
|
||||
"<street>Baker street 5</street>\n"+
|
||||
"</address>\n"+
|
||||
"</addresses>";
|
||||
String expectedStr =
|
||||
"[\"addresses\","+
|
||||
"[\"address\","+
|
||||
"[\"name\",\"Joe Tester\"],"+
|
||||
"[\"street\",\"Baker street 5\"]"+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
||||
String xmlToStr = JSONML.toString(jsonArray);
|
||||
JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string with lost leading zero and converted "True" to true. See test
|
||||
* result in comment below.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_jsonOutput() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]";
|
||||
final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false);
|
||||
assertEquals(expectedJsonString, actualJsonOutput.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string cannot be reverted to original xml when type guessing is used.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_reversibility() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false));
|
||||
assertNotEquals(revertedXml, originalXml);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string cannot be reverted to original xml when type guessing is used.
|
||||
* When we force all the values as string, the original text comes back.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_reversibility2() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
|
||||
final JSONArray json = JSONML.toJSONArray(originalXml,true);
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
|
||||
final String reverseXml = JSONML.toString(json);
|
||||
assertEquals(originalXml, reverseXml);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON can be reverted to original xml.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_reversibility3() {
|
||||
final String originalXml = "<readResult><errors someAttr=\"arrtValue\"><code>400</code></errors><errors><code>402</code></errors></readResult>";
|
||||
final JSONArray jsonArray = JSONML.toJSONArray(originalXml, false);
|
||||
final String revertedXml = JSONML.toString(jsonArray);
|
||||
assertEquals(revertedXml, originalXml);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string cannot be reverted to original xml. See test result in
|
||||
* comment below.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONObject_reversibility() {
|
||||
final String originalXml = "<readResult><errors someAttr=\"arrtValue\"><code>400</code></errors><errors><code>402</code></errors></readResult>";
|
||||
final JSONObject originalObject=JSONML.toJSONObject(originalXml,false);
|
||||
final String originalJson = originalObject.toString();
|
||||
final String xml = JSONML.toString(originalObject);
|
||||
final JSONObject revertedObject = JSONML.toJSONObject(xml, false);
|
||||
final String newJson = revertedObject.toString();
|
||||
assertTrue("JSON Objects are not similar", originalObject.similar(revertedObject));
|
||||
assertTrue("JSON Strings are not similar", new JSONObject(originalJson).similar(new JSONObject(newJson)));
|
||||
}
|
||||
|
||||
// these tests do not pass for the following reasons:
|
||||
// 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence
|
||||
// or other HTML specific entities would fail on reversability
|
||||
// 2. Our JSON implementation for storing the XML attributes uses the standard unordered map.
|
||||
// This means that <tag attr1="v1" attr2="v2" /> can not be reversed reliably.
|
||||
//
|
||||
// /**
|
||||
// * Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't.
|
||||
// * Technically JsonML should be able to transform any valid xhtml document, but ours only supports
|
||||
// * standard XML entities, not HTML entities.
|
||||
// */
|
||||
// @Test
|
||||
// public void testAttributeConversionReversabilityHTML() {
|
||||
// final String originalXml = "<table class=\"MyTable\" style=\"background-color:yellow\"><tr><td class=\"MyTD\" style=\"border:1px solid black\">#5D28D1</td><td class=\"MyTD\" style=\"background-color:red\">Example text here</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AF44EF</td><td class=\"MyTD\" style=\"background-color:green\">127310656</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AAD034</td><td class=\"MyTD\" style=\"background-color:blue\"> <span style=\"background-color:maroon\">©</span> </td></tr></table>";
|
||||
// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]";
|
||||
// final JSONArray json = JSONML.toJSONArray(originalXml,true);
|
||||
// final String actualJsonString = json.toString();
|
||||
//
|
||||
// final String reverseXml = JSONML.toString(json);
|
||||
// assertNotEquals(originalXml, reverseXml);
|
||||
//
|
||||
// assertNotEquals(expectedJsonString, actualJsonString);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Test texts taken from jsonml.org but modified to have XML entities only.
|
||||
// */
|
||||
// @Test
|
||||
// public void testAttributeConversionReversabilityXML() {
|
||||
// final String originalXml = "<table class=\"MyTable\" style=\"background-color:yellow\"><tr><td class=\"MyTD\" style=\"border:1px solid black\">#5D28D1</td><td class=\"MyTD\" style=\"background-color:red\">Example text here</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AF44EF</td><td class=\"MyTD\" style=\"background-color:green\">127310656</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AAD034</td><td class=\"MyTD\" style=\"background-color:blue\">&<span style=\"background-color:maroon\">></span><</td></tr></table>";
|
||||
// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"&\",[\"span\",{ \"style\" : \"background-color:maroon\" },\">\"],\"<\"]]]";
|
||||
// final JSONArray jsonML = JSONML.toJSONArray(originalXml,true);
|
||||
// final String actualJsonString = jsonML.toString();
|
||||
//
|
||||
// final String reverseXml = JSONML.toString(jsonML);
|
||||
// // currently not equal because the hashing of the attribute objects makes the attribute
|
||||
// // order not happen the same way twice
|
||||
// assertEquals(originalXml, reverseXml);
|
||||
//
|
||||
// assertEquals(expectedJsonString, actualJsonString);
|
||||
// }
|
||||
|
||||
@Test (timeout = 6000)
|
||||
public void testIssue484InfinteLoop1() {
|
||||
try {
|
||||
JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA?<!C*^P^Y?^X9?~?t?)??,z^XA???S}?Q??.q?j????]");
|
||||
fail("Exception expected for invalid JSON.");
|
||||
} catch (JSONException ex) {
|
||||
assertEquals("Exception string did not match: ",
|
||||
"Unterminated string at 271 [character 272 line 1]",
|
||||
ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test (timeout = 6000)
|
||||
public void testIssue484InfinteLoop2() {
|
||||
try {
|
||||
String input = "??*\n" +
|
||||
"??|?CglR??`??>?w??PIlr??D?$?-?o??O?*??{OD?Y??`2a????NM?bq?:O?>S$?J?B.gUK?m\b??zE???!v]???????c??????h???s???g???`?qbi??:Zl?)?}1^??k?0??:$V?$?Ovs(}J??????2;gQ????Tg?K?`?h%c?hmGA?<!C*?9?~?t?)??,zA???S}?Q??.q?j????]";
|
||||
JSONML.toJSONObject(input);
|
||||
fail("Exception expected for invalid JSON.");
|
||||
} catch (JSONException ex) {
|
||||
assertEquals("Exception string did not match: ",
|
||||
"Unterminated string at 242 [character 238 line 2]",
|
||||
ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJSONArrayMaxNestingDepthOf42IsRespected() {
|
||||
final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", "<a>");
|
||||
|
||||
final int maxNestingDepth = 42;
|
||||
|
||||
try {
|
||||
JSONML.toJSONArray(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
|
||||
|
||||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
|
||||
e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJSONArrayMaxNestingDepthIsRespectedWithValidXML() {
|
||||
final String perfectlyFineXML = "<Test>\n" +
|
||||
" <employee>\n" +
|
||||
" <name>sonoo</name>\n" +
|
||||
" <salary>56000</salary>\n" +
|
||||
" <married>true</married>\n" +
|
||||
" </employee>\n" +
|
||||
"</Test>\n";
|
||||
|
||||
final int maxNestingDepth = 1;
|
||||
|
||||
try {
|
||||
JSONML.toJSONArray(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
|
||||
|
||||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
|
||||
e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJSONArrayMaxNestingDepthWithValidFittingXML() {
|
||||
final String perfectlyFineXML = "<Test>\n" +
|
||||
" <employee>\n" +
|
||||
" <name>sonoo</name>\n" +
|
||||
" <salary>56000</salary>\n" +
|
||||
" <married>true</married>\n" +
|
||||
" </employee>\n" +
|
||||
"</Test>\n";
|
||||
|
||||
final int maxNestingDepth = 3;
|
||||
|
||||
try {
|
||||
JSONML.toJSONArray(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
fail("XML document should be parsed as its maximum depth fits the maxNestingDepth " +
|
||||
"parameter of the JSONMLParserConfiguration used");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJSONObjectMaxDefaultNestingDepthIsRespected() {
|
||||
final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", "<a>");
|
||||
|
||||
try {
|
||||
JSONML.toJSONObject(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL);
|
||||
|
||||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
|
||||
e.getMessage().startsWith("Maximum nesting depth of " + JSONMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJSONObjectUnlimitedNestingDepthIsPossible() {
|
||||
int actualDepth = JSONMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH +10;
|
||||
final String deeperThanDefaultMax = new String(new char[actualDepth]).replace("\0", "<a>") +
|
||||
"value" +
|
||||
new String(new char[actualDepth]).replace("\0", "</a>");
|
||||
|
||||
try {
|
||||
JSONML.toJSONObject(deeperThanDefaultMax, JSONMLParserConfiguration.ORIGINAL
|
||||
.withMaxNestingDepth(JSONMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
fail("XML document should be parsed beyond the default maximum depth if maxNestingDepth " +
|
||||
"parameter is set to -1 in JSONMLParserConfiguration");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJSONObjectMaxNestingDepthOf42IsRespected() {
|
||||
final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", "<a>");
|
||||
|
||||
final int maxNestingDepth = 42;
|
||||
|
||||
try {
|
||||
JSONML.toJSONObject(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
|
||||
|
||||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
|
||||
e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJSONObjectMaxNestingDepthIsRespectedWithValidXML() {
|
||||
final String perfectlyFineXML = "<Test>\n" +
|
||||
" <employee>\n" +
|
||||
" <name>sonoo</name>\n" +
|
||||
" <salary>56000</salary>\n" +
|
||||
" <married>true</married>\n" +
|
||||
" </employee>\n" +
|
||||
"</Test>\n";
|
||||
|
||||
final int maxNestingDepth = 1;
|
||||
|
||||
try {
|
||||
JSONML.toJSONObject(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
|
||||
|
||||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
|
||||
e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJSONObjectMaxNestingDepthWithValidFittingXML() {
|
||||
final String perfectlyFineXML = "<Test>\n" +
|
||||
" <employee>\n" +
|
||||
" <name>sonoo</name>\n" +
|
||||
" <salary>56000</salary>\n" +
|
||||
" <married>true</married>\n" +
|
||||
" </employee>\n" +
|
||||
"</Test>\n";
|
||||
|
||||
final int maxNestingDepth = 3;
|
||||
|
||||
try {
|
||||
JSONML.toJSONObject(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
fail("XML document should be parsed as its maximum depth fits the maxNestingDepth " +
|
||||
"parameter of the JSONMLParserConfiguration used");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
60
src/test/java/org/json/junit/JSONObjectLocaleTest.java
Executable file
60
src/test/java/org/json/junit/JSONObjectLocaleTest.java
Executable file
@@ -0,0 +1,60 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.json.junit.data.MyLocaleBean;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
* Note: This file is saved as UTF-8. Do not save as ASCII or the tests will
|
||||
* fail.
|
||||
*
|
||||
*/
|
||||
public class JSONObjectLocaleTest {
|
||||
/**
|
||||
* JSONObject built from a bean with locale-specific keys.
|
||||
* In the Turkish alphabet, there are 2 versions of the letter "i".
|
||||
* 'eh' I ı (dotless i)
|
||||
* 'ee' İ i (dotted i)
|
||||
* A problem can occur when parsing the public get methods for a bean.
|
||||
* If the method starts with getI... then the key name will be lowercased
|
||||
* to 'i' in English, and 'ı' in Turkish.
|
||||
* We want the keys to be consistent regardless of locale, so JSON-Java
|
||||
* lowercase operations are made to be locale-neutral by specifying
|
||||
* Locale.ROOT. This causes 'I' to be universally lowercased to 'i'
|
||||
* regardless of the locale currently in effect.
|
||||
*/
|
||||
@Test
|
||||
public void jsonObjectByLocaleBean() {
|
||||
|
||||
MyLocaleBean myLocaleBean = new MyLocaleBean();
|
||||
|
||||
/**
|
||||
* This is just the control case which happens when the locale.ROOT
|
||||
* lowercasing behavior is the same as the current locale.
|
||||
*/
|
||||
Locale.setDefault(new Locale("en"));
|
||||
JSONObject jsonen = new JSONObject(myLocaleBean);
|
||||
assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length());
|
||||
assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i"));
|
||||
assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id"));
|
||||
|
||||
/**
|
||||
* Without the JSON-Java change, these keys would be stored internally as
|
||||
* starting with the letter, 'ı' (dotless i), since the lowercasing of
|
||||
* the getI and getId keys would be specific to the Turkish locale.
|
||||
*/
|
||||
Locale.setDefault(new Locale("tr"));
|
||||
JSONObject jsontr = new JSONObject(myLocaleBean);
|
||||
assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length());
|
||||
assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i"));
|
||||
assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id"));
|
||||
}
|
||||
}
|
||||
146
src/test/java/org/json/junit/JSONObjectNumberTest.java
Normal file
146
src/test/java/org/json/junit/JSONObjectNumberTest.java
Normal file
@@ -0,0 +1,146 @@
|
||||
package org.json.junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class JSONObjectNumberTest {
|
||||
private final String objectString;
|
||||
private Integer value = 50;
|
||||
|
||||
@Parameters(name = "{index}: {0}")
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{"{\"value\":50}", 1},
|
||||
{"{\"value\":50.0}", 1},
|
||||
{"{\"value\":5e1}", 1},
|
||||
{"{\"value\":5E1}", 1},
|
||||
{"{\"value\":5e1}", 1},
|
||||
{"{\"value\":\"50\"}", 1},
|
||||
{"{\"value\":-50}", -1},
|
||||
{"{\"value\":-50.0}", -1},
|
||||
{"{\"value\":-5e1}", -1},
|
||||
{"{\"value\":-5E1}", -1},
|
||||
{"{\"value\":-5e1}", -1},
|
||||
{"{\"value\":\"-50\"}", -1}
|
||||
// JSON does not support octal or hex numbers;
|
||||
// see https://stackoverflow.com/a/52671839/6323312
|
||||
// "{value:062}", // octal 50
|
||||
// "{value:0x32}" // hex 50
|
||||
});
|
||||
}
|
||||
|
||||
public JSONObjectNumberTest(String objectString, int resultIsNegative) {
|
||||
this.objectString = objectString;
|
||||
this.value *= resultIsNegative;
|
||||
}
|
||||
|
||||
private JSONObject object;
|
||||
|
||||
@Before
|
||||
public void setJsonObject() {
|
||||
object = new JSONObject(objectString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNumber() {
|
||||
assertEquals(value.intValue(), object.getNumber("value").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBigDecimal() {
|
||||
assertTrue(BigDecimal.valueOf(value).compareTo(object.getBigDecimal("value")) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBigInteger() {
|
||||
assertEquals(BigInteger.valueOf(value), object.getBigInteger("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFloat() {
|
||||
assertEquals(value.floatValue(), object.getFloat("value"), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDouble() {
|
||||
assertEquals(value.doubleValue(), object.getDouble("value"), 0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInt() {
|
||||
assertEquals(value.intValue(), object.getInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLong() {
|
||||
assertEquals(value.longValue(), object.getLong("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptNumber() {
|
||||
assertEquals(value.intValue(), object.optNumber("value").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptBigDecimal() {
|
||||
assertTrue(BigDecimal.valueOf(value).compareTo(object.optBigDecimal("value", null)) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptBigInteger() {
|
||||
assertEquals(BigInteger.valueOf(value), object.optBigInteger("value", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptFloat() {
|
||||
assertEquals(value.floatValue(), object.optFloat("value"), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptFloatObject() {
|
||||
assertEquals((Float) value.floatValue(), object.optFloatObject("value"), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptDouble() {
|
||||
assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptDoubleObject() {
|
||||
assertEquals((Double) value.doubleValue(), object.optDoubleObject("value"), 0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptInt() {
|
||||
assertEquals(value.intValue(), object.optInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptIntegerObject() {
|
||||
assertEquals((Integer) value.intValue(), object.optIntegerObject("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptLong() {
|
||||
assertEquals(value.longValue(), object.optLong("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptLongObject() {
|
||||
assertEquals((Long) value.longValue(), object.optLongObject("value"));
|
||||
}
|
||||
}
|
||||
4015
src/test/java/org/json/junit/JSONObjectTest.java
Normal file
4015
src/test/java/org/json/junit/JSONObjectTest.java
Normal file
File diff suppressed because one or more lines are too long
624
src/test/java/org/json/junit/JSONParserConfigurationTest.java
Normal file
624
src/test/java/org/json/junit/JSONParserConfigurationTest.java
Normal file
@@ -0,0 +1,624 @@
|
||||
package org.json.junit;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONParserConfiguration;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class JSONParserConfigurationTest {
|
||||
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
||||
|
||||
@Test(expected = JSONException.class)
|
||||
public void testThrowException() {
|
||||
new JSONObject(TEST_SOURCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverwrite() {
|
||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE,
|
||||
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
|
||||
|
||||
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void strictModeIsCloned(){
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true)
|
||||
.withMaxNestingDepth(12);
|
||||
|
||||
assertTrue(jsonParserConfiguration.isStrictMode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void maxNestingDepthIsCloned(){
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.<JSONParserConfiguration>withKeepStrings(true)
|
||||
.withStrictMode(true);
|
||||
|
||||
assertTrue(jsonParserConfiguration.isKeepStrings());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useNativeNullsIsCloned() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withUseNativeNulls(true)
|
||||
.withStrictMode(true);
|
||||
assertTrue(jsonParserConfiguration.isUseNativeNulls());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyDuplicateKeyThenMaxDepth() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withOverwriteDuplicateKey(true)
|
||||
.withMaxNestingDepth(42);
|
||||
|
||||
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyMaxDepthThenDuplicateKey() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withMaxNestingDepth(42)
|
||||
.withOverwriteDuplicateKey(true);
|
||||
|
||||
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInput_testStrictModeTrue_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONArrayList();
|
||||
// this is a lot easier to debug when things stop working
|
||||
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||
String testCase = strictModeInputTestCases.get(i);
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||
String s = jsonArray.toString();
|
||||
String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
|
||||
fail(msg);
|
||||
} catch (Exception e) {
|
||||
// its all good
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObjects_testStrictModeTrue_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONObjectList();
|
||||
// this is a lot easier to debug when things stop working
|
||||
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||
String testCase = strictModeInputTestCases.get(i);
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||
String s = jsonObject.toString();
|
||||
String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
|
||||
fail(msg);
|
||||
} catch (Exception e) {
|
||||
// its all good
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "[]";
|
||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||
assertEquals(testCase, jsonArray.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyObject_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "{}";
|
||||
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||
assertEquals(testCase, jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidNestedArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "[[\"c\"], [10.2], [true, false, true]]";
|
||||
|
||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||
JSONArray arrayShouldContainStringAt0 = jsonArray.getJSONArray(0);
|
||||
JSONArray arrayShouldContainNumberAt0 = jsonArray.getJSONArray(1);
|
||||
JSONArray arrayShouldContainBooleanAt0 = jsonArray.getJSONArray(2);
|
||||
|
||||
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
|
||||
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
|
||||
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidNestedObject_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "{\"a0\":[\"c\"], \"a1\":[10.2], \"a2\":[true, false, true]}";
|
||||
|
||||
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||
JSONArray arrayShouldContainStringAt0 = jsonObject.getJSONArray("a0");
|
||||
JSONArray arrayShouldContainNumberAt0 = jsonObject.getJSONArray("a1");
|
||||
JSONArray arrayShouldContainBooleanAt0 = jsonObject.getJSONArray("a2");
|
||||
|
||||
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
|
||||
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
|
||||
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException(){
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "[[]]";
|
||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||
assertEquals(testCase, jsonArray.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidEmptyArrayInsideObject_testStrictModeTrue_shouldNotThrowJsonException(){
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "{\"a0\":[]}";
|
||||
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||
assertEquals(testCase, jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException(){
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
String testCase = "[[]]";
|
||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||
assertEquals(testCase, jsonArray.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidEmptyArrayInsideObject_testStrictModeFalse_shouldNotThrowJsonException(){
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
String testCase = "{\"a0\":[]}";
|
||||
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||
assertEquals(testCase, jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidStringArray_testStrictModeTrue_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "[badString]";
|
||||
JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Value 'badString' is not surrounded by quotes at 10 [character 11 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidStringObject_testStrictModeTrue_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "{\"a0\":badString}";
|
||||
JSONException je = assertThrows(JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Value 'badString' is not surrounded by quotes at 15 [character 16 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowNullArrayInStrictMode() {
|
||||
String expected = "[null]";
|
||||
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||
assertEquals(expected, jsonArray.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowNullObjectInStrictMode() {
|
||||
String expected = "{\"a0\":null}";
|
||||
JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||
assertEquals(expected, jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNumericArray() {
|
||||
String expected = "[10]";
|
||||
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||
assertEquals(expected, jsonArray.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNumericObject() {
|
||||
String expected = "{\"a0\":10}";
|
||||
JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||
assertEquals(expected, jsonObject.toString());
|
||||
}
|
||||
@Test
|
||||
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
|
||||
String compliantJsonArrayAsString = lines.collect(Collectors.joining());
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompliantJSONObjectFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonObject.json"))) {
|
||||
String compliantJsonObjectAsString = lines.collect(Collectors.joining());
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
new JSONObject(compliantJsonObjectAsString, jsonParserConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONArrayList();
|
||||
|
||||
// this is a lot easier to debug when things stop working
|
||||
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||
String testCase = strictModeInputTestCases.get(i);
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
||||
fail(String.format("Noncompliant array index: %d", i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObjects_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONObjectList();
|
||||
|
||||
// this is a lot easier to debug when things stop working
|
||||
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||
String testCase = strictModeInputTestCases.get(i);
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
||||
fail(String.format("Noncompliant array index: %d", i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "[1,2];[3,4]";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Unparsed characters found at end of input text at 6 [character 7 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "{\"a0\":[1,2];\"a1\":[3,4]}";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Invalid character ';' found at 12 [character 13 line 1]", je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "[\"1\",\"2\"];[3,4]";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Unparsed characters found at end of input text at 10 [character 11 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObjectWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "{\"a0\":[\"1\",\"2\"];\"a1\":[3,4]}";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Invalid character ';' found at 16 [character 17 line 1]", je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "[{\"test\": implied}]";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Value 'implied' is not surrounded by quotes at 17 [character 18 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
String testCase = "{\"a0\":{\"test\": implied}]}";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||
assertEquals("Strict mode error: Value 'implied' is not surrounded by quotes at 22 [character 23 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
String testCase = "[{\"test\": implied}]";
|
||||
new JSONArray(testCase, jsonParserConfiguration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
String testCase = "{\"a0\":{\"test\": implied}}";
|
||||
new JSONObject(testCase, jsonParserConfiguration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonCompliantQuotesArray_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCaseOne = "[\"abc', \"test\"]";
|
||||
String testCaseTwo = "['abc\", \"test\"]";
|
||||
String testCaseThree = "['abc']";
|
||||
String testCaseFour = "[{'testField': \"testValue\"}]";
|
||||
|
||||
JSONException jeOne = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
|
||||
JSONException jeTwo = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
|
||||
JSONException jeThree = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseThree, jsonParserConfiguration));
|
||||
JSONException jeFour = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseFour, jsonParserConfiguration));
|
||||
|
||||
assertEquals(
|
||||
"Expected a ',' or ']' at 10 [character 11 line 1]",
|
||||
jeOne.getMessage());
|
||||
assertEquals(
|
||||
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||
jeTwo.getMessage());
|
||||
assertEquals(
|
||||
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||
jeThree.getMessage());
|
||||
assertEquals(
|
||||
"Strict mode error: Single quoted strings are not allowed at 3 [character 4 line 1]",
|
||||
jeFour.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonCompliantQuotesObject_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCaseOne = "{\"abc': \"test\"}";
|
||||
String testCaseTwo = "{'abc\": \"test\"}";
|
||||
String testCaseThree = "{\"a\":'abc'}";
|
||||
String testCaseFour = "{'testField': \"testValue\"}";
|
||||
|
||||
JSONException jeOne = assertThrows(JSONException.class,
|
||||
() -> new JSONObject(testCaseOne, jsonParserConfiguration));
|
||||
JSONException jeTwo = assertThrows(JSONException.class,
|
||||
() -> new JSONObject(testCaseTwo, jsonParserConfiguration));
|
||||
JSONException jeThree = assertThrows(JSONException.class,
|
||||
() -> new JSONObject(testCaseThree, jsonParserConfiguration));
|
||||
JSONException jeFour = assertThrows(JSONException.class,
|
||||
() -> new JSONObject(testCaseFour, jsonParserConfiguration));
|
||||
|
||||
assertEquals(
|
||||
"Expected a ':' after a key at 10 [character 11 line 1]",
|
||||
jeOne.getMessage());
|
||||
assertEquals(
|
||||
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||
jeTwo.getMessage());
|
||||
assertEquals(
|
||||
"Strict mode error: Single quoted strings are not allowed at 6 [character 7 line 1]",
|
||||
jeThree.getMessage());
|
||||
assertEquals(
|
||||
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||
jeFour.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnbalancedQuotesArray_testStrictModeFalse_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
String testCaseOne = "[\"abc', \"test\"]";
|
||||
String testCaseTwo = "['abc\", \"test\"]";
|
||||
|
||||
JSONException jeOne = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
|
||||
JSONException jeTwo = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
|
||||
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnbalancedQuotesObject_testStrictModeFalse_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
String testCaseOne = "{\"abc': \"test\"}";
|
||||
String testCaseTwo = "{'abc\": \"test\"}";
|
||||
|
||||
JSONException jeOne = assertThrows(JSONException.class,
|
||||
() -> new JSONObject(testCaseOne, jsonParserConfiguration));
|
||||
JSONException jeTwo = assertThrows(JSONException.class,
|
||||
() -> new JSONObject(testCaseTwo, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Expected a ':' after a key at 10 [character 11 line 1]", jeOne.getMessage());
|
||||
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "[{test: implied}]";
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Strict mode error: Value 'test' is not surrounded by quotes at 6 [character 7 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "{test: implied}";
|
||||
JSONException je = assertThrows("expected non-compliant json but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Strict mode error: Value 'test' is not surrounded by quotes at 5 [character 6 line 1]",
|
||||
je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingJSONTokener_shouldThrowJSONException() {
|
||||
JSONException exception = assertThrows(JSONException.class, () -> {
|
||||
new JSONObject(new JSONTokener("{\"key\":\"value\"} invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
|
||||
});
|
||||
|
||||
assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingString_shouldThrowJSONException() {
|
||||
JSONException exception = assertThrows(JSONException.class, () -> {
|
||||
new JSONObject("{\"key\":\"value\"} invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
|
||||
});
|
||||
assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingJSONTokener_shouldThrowJSONException() {
|
||||
JSONException exception = assertThrows(JSONException.class, () -> {
|
||||
new JSONArray(new JSONTokener("[\"value\"] invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
|
||||
});
|
||||
|
||||
assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingString_shouldThrowJSONException() {
|
||||
JSONException exception = assertThrows(JSONException.class, () -> {
|
||||
new JSONArray("[\"value\"] invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
|
||||
});
|
||||
assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||
* this class.
|
||||
*
|
||||
* @return List with JSON strings.
|
||||
*/
|
||||
private List<String> getNonCompliantJSONArrayList() {
|
||||
return Arrays.asList(
|
||||
"[1],",
|
||||
"[1,]",
|
||||
"[,]",
|
||||
"[,,]",
|
||||
"[[1],\"sa\",[2]]a",
|
||||
"[1],\"dsa\": \"test\"",
|
||||
"[[a]]",
|
||||
"[]asdf",
|
||||
"[]]",
|
||||
"[]}",
|
||||
"[][",
|
||||
"[]{",
|
||||
"[],",
|
||||
"[]:",
|
||||
"[],[",
|
||||
"[],{",
|
||||
"[1,2];[3,4]",
|
||||
"[test]",
|
||||
"[{'testSingleQuote': 'testSingleQuote'}]",
|
||||
"[1, 2,3]:[4,5]",
|
||||
"[{test: implied}]",
|
||||
"[{\"test\": implied}]",
|
||||
"[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]",
|
||||
"[{test: \"implied\"}]");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||
* this class.
|
||||
*
|
||||
* @return List with JSON strings.
|
||||
*/
|
||||
private List<String> getNonCompliantJSONObjectList() {
|
||||
return Arrays.asList(
|
||||
"{\"a\":1},",
|
||||
"{\"a\":1,}",
|
||||
"{\"a0\":[1],\"a1\":\"sa\",\"a2\":[2]}a",
|
||||
"{\"a\":1},\"dsa\": \"test\"",
|
||||
"{\"a\":[a]}",
|
||||
"{}asdf",
|
||||
"{}}",
|
||||
"{}]",
|
||||
"{}{",
|
||||
"{}[",
|
||||
"{},",
|
||||
"{}:",
|
||||
"{},{",
|
||||
"{},[",
|
||||
"{\"a0\":[1,2];\"a1\":[3,4]}",
|
||||
"{\"a\":test}",
|
||||
"{a:{'testSingleQuote': 'testSingleQuote'}}",
|
||||
"{\"a0\":1, \"a1\":2,\"a2\":3}:{\"a3\":4,\"a4\":5}",
|
||||
"{\"a\":{test: implied}}",
|
||||
"{a:{\"test\": implied}}",
|
||||
"{a:[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]}",
|
||||
"{a:{test: \"implied\"}}"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
398
src/test/java/org/json/junit/JSONPointerTest.java
Normal file
398
src/test/java/org/json/junit/JSONPointerTest.java
Normal file
@@ -0,0 +1,398 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONPointer;
|
||||
import org.json.JSONPointerException;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JSONPointerTest {
|
||||
|
||||
private static final JSONObject document;
|
||||
private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
|
||||
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
|
||||
"\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
|
||||
"\"m~n\":8}";
|
||||
|
||||
|
||||
static {
|
||||
@SuppressWarnings("resource")
|
||||
InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
|
||||
if(resourceAsStream == null) {
|
||||
throw new ExceptionInInitializerError("Unable to locate test file. Please check your development environment configuration");
|
||||
}
|
||||
document = new JSONObject(new JSONTokener(resourceAsStream));
|
||||
}
|
||||
|
||||
private Object query(String pointer) {
|
||||
return new JSONPointer(pointer).queryFrom(document);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyPointer() {
|
||||
assertTrue(new JSONObject(EXPECTED_COMPLETE_DOCUMENT).similar(query("")));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void nullPointer() {
|
||||
new JSONPointer((String) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectPropertyQuery() {
|
||||
assertEquals("[\"bar\",\"baz\"]", query("/foo").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void arrayIndexQuery() {
|
||||
assertEquals("bar", query("/foo/0"));
|
||||
}
|
||||
|
||||
@Test(expected = JSONPointerException.class)
|
||||
public void stringPropOfArrayFailure() {
|
||||
query("/foo/bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryByEmptyKey() {
|
||||
assertEquals(0, query("/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryByEmptyKeySubObject() {
|
||||
JSONObject json = new JSONObject("{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some" +
|
||||
" other value\"}");
|
||||
JSONObject obj = (JSONObject) query("/obj/");
|
||||
assertTrue(json.similar(obj));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryByEmptyKeySubObjectSubOject() {
|
||||
assertEquals("empty key of an object with an empty key", query("/obj//"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryByEmptyKeySubObjectValue() {
|
||||
assertEquals("Some other value", query("/obj//subKey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void slashEscaping() {
|
||||
assertEquals(1, query("/a~1b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tildeEscaping() {
|
||||
assertEquals(8, query("/m~0n"));
|
||||
}
|
||||
|
||||
/**
|
||||
* We pass backslashes as-is
|
||||
*
|
||||
* @see <a href="https://tools.ietf.org/html/rfc6901#section-3">rfc6901 section 3</a>
|
||||
*/
|
||||
@Test
|
||||
public void backslashHandling() {
|
||||
assertEquals(5, query("/i\\j"));
|
||||
}
|
||||
|
||||
/**
|
||||
* We pass quotations as-is
|
||||
*
|
||||
* @see <a href="https://tools.ietf.org/html/rfc6901#section-3">rfc6901 section 3</a>
|
||||
*/
|
||||
@Test
|
||||
public void quotationHandling() {
|
||||
assertEquals(6, query("/k\"l"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whitespaceKey() {
|
||||
assertEquals(7, query("/ "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriFragmentNotation() {
|
||||
assertEquals("[\"bar\",\"baz\"]", query("#/foo").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriFragmentNotationRoot() {
|
||||
assertTrue(new JSONObject(EXPECTED_COMPLETE_DOCUMENT).similar(query("#")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriFragmentPercentHandling() {
|
||||
assertEquals(2, query("#/c%25d"));
|
||||
assertEquals(3, query("#/e%5Ef"));
|
||||
assertEquals(4, query("#/g%7Ch"));
|
||||
assertEquals(8, query("#/m~0n"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void syntaxError() {
|
||||
new JSONPointer("key");
|
||||
}
|
||||
|
||||
@Test(expected = JSONPointerException.class)
|
||||
public void arrayIndexFailure() {
|
||||
query("/foo/2");
|
||||
}
|
||||
|
||||
@Test(expected = JSONPointerException.class)
|
||||
public void primitiveFailure() {
|
||||
query("/obj/key/failure");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builderTest() {
|
||||
JSONPointer pointer = JSONPointer.builder()
|
||||
.append("obj")
|
||||
.append("other~key").append("another/key")
|
||||
.append(0)
|
||||
.build();
|
||||
assertEquals("val", pointer.queryFrom(document));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void nullToken() {
|
||||
JSONPointer.builder().append(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringEscaping() {
|
||||
JSONPointer pointer = JSONPointer.builder()
|
||||
.append("obj")
|
||||
.append("other~key").append("another/key")
|
||||
.append("\"")
|
||||
.append(0)
|
||||
.build();
|
||||
assertEquals("/obj/other~0key/another~1key/\"/0", pointer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyPointerToString() {
|
||||
assertEquals("", new JSONPointer("").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toURIFragment() {
|
||||
assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
|
||||
assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
|
||||
assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
|
||||
assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenListIsCopiedInConstructor() {
|
||||
JSONPointer.Builder b = JSONPointer.builder().append("key1");
|
||||
JSONPointer jp1 = b.build();
|
||||
b.append("key2");
|
||||
JSONPointer jp2 = b.build();
|
||||
if(jp1.toString().equals(jp2.toString())) {
|
||||
fail("Oops, my pointers are sharing a backing array");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage for JSONObject query(String)
|
||||
*/
|
||||
@Test
|
||||
public void queryFromJSONObject() {
|
||||
String str = "{"+
|
||||
"\"stringKey\":\"hello world!\","+
|
||||
"\"arrayKey\":[0,1,2],"+
|
||||
"\"objectKey\": {"+
|
||||
"\"a\":\"aVal\","+
|
||||
"\"b\":\"bVal\""+
|
||||
"}"+
|
||||
"}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
Object obj = jsonObject.query("/stringKey");
|
||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||
obj = jsonObject.query("/arrayKey/1");
|
||||
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||
obj = jsonObject.query("/objectKey/b");
|
||||
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||
try {
|
||||
obj = jsonObject.query("/a/b/c");
|
||||
assertTrue("Expected JSONPointerException", false);
|
||||
} catch (JSONPointerException e) {
|
||||
assertTrue("Expected bad key/value exception",
|
||||
"value [null] is not an array or object therefore its key b cannot be resolved".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage for JSONObject query(JSONPointer)
|
||||
*/
|
||||
@Test
|
||||
public void queryFromJSONObjectUsingPointer() {
|
||||
String str = "{"+
|
||||
"\"stringKey\":\"hello world!\","+
|
||||
"\"arrayKey\":[0,1,2],"+
|
||||
"\"objectKey\": {"+
|
||||
"\"a\":\"aVal\","+
|
||||
"\"b\":\"bVal\""+
|
||||
"}"+
|
||||
"}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
Object obj = jsonObject.query(new JSONPointer("/stringKey"));
|
||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||
obj = jsonObject.query(new JSONPointer("/arrayKey/1"));
|
||||
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||
obj = jsonObject.query(new JSONPointer("/objectKey/b"));
|
||||
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||
try {
|
||||
obj = jsonObject.query(new JSONPointer("/a/b/c"));
|
||||
assertTrue("Expected JSONPointerException", false);
|
||||
} catch (JSONPointerException e) {
|
||||
assertTrue("Expected bad key/value exception",
|
||||
"value [null] is not an array or object therefore its key b cannot be resolved".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage for JSONObject optQuery(JSONPointer)
|
||||
*/
|
||||
@Test
|
||||
public void optQueryFromJSONObjectUsingPointer() {
|
||||
String str = "{"+
|
||||
"\"stringKey\":\"hello world!\","+
|
||||
"\"arrayKey\":[0,1,2],"+
|
||||
"\"objectKey\": {"+
|
||||
"\"a\":\"aVal\","+
|
||||
"\"b\":\"bVal\""+
|
||||
"}"+
|
||||
"}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
Object obj = jsonObject.optQuery(new JSONPointer("/stringKey"));
|
||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||
obj = jsonObject.optQuery(new JSONPointer("/arrayKey/1"));
|
||||
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||
obj = jsonObject.optQuery(new JSONPointer("/objectKey/b"));
|
||||
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||
obj = jsonObject.optQuery(new JSONPointer("/a/b/c"));
|
||||
assertTrue("Expected null", obj == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage for JSONArray query(String)
|
||||
*/
|
||||
@Test
|
||||
public void queryFromJSONArray() {
|
||||
String str = "["+
|
||||
"\"hello world!\","+
|
||||
"[0,1,2],"+
|
||||
"{"+
|
||||
"\"a\":\"aVal\","+
|
||||
"\"b\":\"bVal\""+
|
||||
"}"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
Object obj = jsonArray.query("/0");
|
||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||
obj = jsonArray.query("/1/1");
|
||||
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||
obj = jsonArray.query("/2/b");
|
||||
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||
try {
|
||||
obj = jsonArray.query("/a/b/c");
|
||||
assertTrue("Expected JSONPointerException", false);
|
||||
} catch (JSONPointerException e) {
|
||||
assertTrue("Expected bad index exception",
|
||||
"a is not an array index".equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage for JSONArray query(JSONPointer)
|
||||
*/
|
||||
@Test
|
||||
public void queryFromJSONArrayUsingPointer() {
|
||||
String str = "["+
|
||||
"\"hello world!\","+
|
||||
"[0,1,2],"+
|
||||
"{"+
|
||||
"\"a\":\"aVal\","+
|
||||
"\"b\":\"bVal\""+
|
||||
"}"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
Object obj = jsonArray.query(new JSONPointer("/0"));
|
||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||
obj = jsonArray.query(new JSONPointer("/1/1"));
|
||||
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||
obj = jsonArray.query(new JSONPointer("/2/b"));
|
||||
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||
try {
|
||||
obj = jsonArray.query(new JSONPointer("/a/b/c"));
|
||||
assertTrue("Expected JSONPointerException", false);
|
||||
} catch (JSONPointerException e) {
|
||||
assertTrue("Expected bad index exception",
|
||||
"a is not an array index".equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage for JSONArray optQuery(JSONPointer)
|
||||
*/
|
||||
@Test
|
||||
public void optQueryFromJSONArrayUsingPointer() {
|
||||
String str = "["+
|
||||
"\"hello world!\","+
|
||||
"[0,1,2],"+
|
||||
"{"+
|
||||
"\"a\":\"aVal\","+
|
||||
"\"b\":\"bVal\""+
|
||||
"}"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
Object obj = jsonArray.optQuery(new JSONPointer("/0"));
|
||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||
obj = jsonArray.optQuery(new JSONPointer("/1/1"));
|
||||
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||
obj = jsonArray.optQuery(new JSONPointer("/2/b"));
|
||||
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||
obj = jsonArray.optQuery(new JSONPointer("/a/b/c"));
|
||||
assertTrue("Expected null", obj == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* When creating a jsonObject we need to parse escaped characters "\\\\"
|
||||
* --> it's the string representation of "\\", so when query'ing via the JSONPointer
|
||||
* we DON'T escape them
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void queryFromJSONObjectUsingPointer0() {
|
||||
String str = "{"+
|
||||
"\"string\\\\\\\\Key\":\"hello world!\","+
|
||||
|
||||
"\"\\\\\":\"slash test\"" +
|
||||
"}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
|
||||
// as "\\" which means when querying, we need to use "\\"
|
||||
Object twoBackslahObj = jsonObject.optQuery(new JSONPointer("/\\"));
|
||||
assertEquals("slash test", twoBackslahObj);
|
||||
|
||||
Object fourBackslashObj = jsonObject.optQuery(new JSONPointer("/string\\\\Key"));
|
||||
assertEquals("hello world!", fourBackslashObj);
|
||||
}
|
||||
}
|
||||
401
src/test/java/org/json/junit/JSONStringTest.java
Normal file
401
src/test/java/org/json/junit/JSONStringTest.java
Normal file
@@ -0,0 +1,401 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for JSONString implementations, and the difference between
|
||||
* {@link JSONObject#valueToString} and {@link JSONObject#writeValue}.
|
||||
*/
|
||||
public class JSONStringTest {
|
||||
|
||||
/**
|
||||
* This tests the JSONObject.writeValue() method. We can't test directly
|
||||
* due to it being a package-protected method. Instead, we can call
|
||||
* JSONArray.write(), which delegates the writing of each entry to
|
||||
* writeValue().
|
||||
*/
|
||||
@Test
|
||||
public void writeValues() throws Exception {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put((Object)null);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[null]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
jsonArray.put(JSONObject.NULL);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[null]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
jsonArray.put(new JSONObject());
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[{}]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
jsonArray.put(new JSONArray());
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[[]]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
|
||||
jsonArray.put((Object)singleMap);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
List<?> singleList = Collections.singletonList("entry1");
|
||||
jsonArray.put((Object)singleList);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
int[] intArray = new int[] { 1, 2, 3 };
|
||||
jsonArray.put(intArray);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
jsonArray.put(24);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[24]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
jsonArray.put("string value");
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[\"string value\"]".equals(output));
|
||||
|
||||
jsonArray = new JSONArray();
|
||||
jsonArray.put(true);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[true]".equals(output));
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This tests the JSONObject.valueToString() method. These should be
|
||||
* identical to the values above, except for the enclosing [ and ].
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
@Test
|
||||
public void valuesToString() throws Exception {
|
||||
|
||||
String output = JSONObject.valueToString(null);
|
||||
assertTrue("String values should be equal", "null".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(JSONObject.NULL);
|
||||
assertTrue("String values should be equal", "null".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(new JSONObject());
|
||||
assertTrue("String values should be equal", "{}".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(new JSONArray());
|
||||
assertTrue("String values should be equal", "[]".equals(output));
|
||||
|
||||
Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
|
||||
output = JSONObject.valueToString(singleMap);
|
||||
assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output));
|
||||
|
||||
List<?> singleList = Collections.singletonList("entry1");
|
||||
output = JSONObject.valueToString(singleList);
|
||||
assertTrue("String values should be equal", "[\"entry1\"]".equals(output));
|
||||
|
||||
int[] intArray = new int[] { 1, 2, 3 };
|
||||
output = JSONObject.valueToString(intArray);
|
||||
assertTrue("String values should be equal", "[1,2,3]".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(24);
|
||||
assertTrue("String values should be equal", "24".equals(output));
|
||||
|
||||
output = JSONObject.valueToString("string value");
|
||||
assertTrue("String values should be equal", "\"string value\"".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(true);
|
||||
assertTrue("String values should be equal", "true".equals(output));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test what happens when toJSONString() returns a well-formed JSON value.
|
||||
* This is the usual case.
|
||||
*/
|
||||
@Test
|
||||
public void testJSONStringValue() throws Exception {
|
||||
JSONStringValue jsonString = new JSONStringValue();
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
jsonArray.put(jsonString);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(jsonString);
|
||||
assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test what happens when toJSONString() returns null. In one case,
|
||||
* use the object's toString() method. In the other, throw a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void testJSONNullStringValue() throws Exception {
|
||||
JSONNullStringValue jsonString = new JSONNullStringValue();
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
jsonArray.put(jsonString);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
|
||||
|
||||
// The only different between writeValue() and valueToString():
|
||||
// in this case, valueToString throws a JSONException
|
||||
try {
|
||||
output = JSONObject.valueToString(jsonString);
|
||||
fail("Expected an exception, got a String value");
|
||||
} catch (Exception e) {
|
||||
assertTrue("Expected JSONException", e instanceof JSONException);
|
||||
assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
|
||||
}
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test what happens when toJSONString() returns an exception. In both
|
||||
* cases, a JSONException is thrown, with the cause and message set from
|
||||
* the original exception.
|
||||
*/
|
||||
@Test
|
||||
public void testJSONStringExceptionValue() {
|
||||
JSONStringExceptionValue jsonString = new JSONStringExceptionValue();
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
jsonArray.put(jsonString);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
jsonArray.write(writer).toString();
|
||||
fail("Expected an exception, got a String value");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Unable to write JSONArray value at index: 0", e.getMessage());
|
||||
} catch(Exception e) {
|
||||
fail("Expected JSONException");
|
||||
} finally {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (Exception e){}
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject.valueToString(jsonString);
|
||||
fail("Expected an exception, got a String value");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
|
||||
} catch(Exception e) {
|
||||
fail("Expected JSONException");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test what happens when a Java object's toString() returns a String value.
|
||||
* This is the usual case.
|
||||
*/
|
||||
@Test
|
||||
public void testStringValue() throws Exception {
|
||||
StringValue nonJsonString = new StringValue();
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
jsonArray.put(nonJsonString);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(nonJsonString);
|
||||
assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test what happens when a Java object's toString() returns null.
|
||||
* Defaults to empty string.
|
||||
*/
|
||||
@Test
|
||||
public void testNullStringValue() throws Exception {
|
||||
NullStringValue nonJsonString = new NullStringValue();
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
jsonArray.put(nonJsonString);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
String output = jsonArray.write(writer).toString();
|
||||
assertTrue("String values should be equal", "[\"\"]".equals(output));
|
||||
|
||||
output = JSONObject.valueToString(nonJsonString);
|
||||
assertTrue("String values should be equal", "\"\"".equals(output));
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumJSONString() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("key", MyEnum.MY_ENUM);
|
||||
assertEquals("{\"key\":\"myJsonString\"}", jsonObject.toString());
|
||||
}
|
||||
|
||||
private enum MyEnum implements JSONString {
|
||||
MY_ENUM;
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
return "\"myJsonString\"";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSONString that returns a valid JSON string value.
|
||||
*/
|
||||
private static final class JSONStringValue implements JSONString {
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
return "\"the JSON string value\"";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "the toString value for JSONStringValue";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSONString that returns null when calling toJSONString().
|
||||
*/
|
||||
private static final class JSONNullStringValue implements JSONString {
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "the toString value";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSONString that throw an exception when calling toJSONString().
|
||||
*/
|
||||
private static final class JSONStringExceptionValue implements JSONString {
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
throw new IllegalStateException("the exception value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "the toString value for JSONStringExceptionValue";
|
||||
}
|
||||
}
|
||||
|
||||
public static final class StringValue {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "the toString value for StringValue";
|
||||
}
|
||||
}
|
||||
|
||||
public static final class NullStringValue {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
357
src/test/java/org/json/junit/JSONStringerTest.java
Normal file
357
src/test/java/org/json/junit/JSONStringerTest.java
Normal file
@@ -0,0 +1,357 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.jayway.jsonpath.*;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for JSON-Java JSONStringer and JSONWriter.
|
||||
*/
|
||||
public class JSONStringerTest {
|
||||
|
||||
/**
|
||||
* Object with a null key.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void nullKeyException() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.object();
|
||||
try {
|
||||
jsonStringer.key(null);
|
||||
assertTrue("Expected an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Null key.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a key with no object.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void outOfSequenceException() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
try {
|
||||
jsonStringer.key("hi");
|
||||
assertTrue("Expected an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Misplaced key.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Missplace an array.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void missplacedArrayException() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.object().endObject();
|
||||
try {
|
||||
jsonStringer.array();
|
||||
assertTrue("Expected an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Misplaced array.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Missplace an endErray.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void missplacedEndArrayException() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.object();
|
||||
try {
|
||||
jsonStringer.endArray();
|
||||
assertTrue("Expected an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Misplaced endArray.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Missplace an endObject.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void missplacedEndObjectException() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.array();
|
||||
try {
|
||||
jsonStringer.endObject();
|
||||
assertTrue("Expected an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Misplaced endObject.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Missplace an object.
|
||||
* Expects a JSONException.
|
||||
*/
|
||||
@Test
|
||||
public void missplacedObjectException() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.object().endObject();
|
||||
try {
|
||||
jsonStringer.object();
|
||||
assertTrue("Expected an exception", false);
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Misplaced object.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceeds implementation max nesting depth.
|
||||
* Expects a JSONException
|
||||
*/
|
||||
@Test
|
||||
public void exceedNestDepthException() {
|
||||
try {
|
||||
JSONStringer s = new JSONStringer();
|
||||
s.object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object();
|
||||
s.key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
|
||||
key("k").object().key("k").object().key("k").object().key("k").object().key("k").object();
|
||||
fail("Expected an exception message");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"Nesting too deep.".
|
||||
equals(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a JSON doc using JSONString API calls,
|
||||
* then convert to JSONObject
|
||||
*/
|
||||
@Test
|
||||
public void simpleObjectString() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.object();
|
||||
jsonStringer.key("trueValue").value(true);
|
||||
jsonStringer.key("falseValue").value(false);
|
||||
jsonStringer.key("nullValue").value(null);
|
||||
jsonStringer.key("stringValue").value("hello world!");
|
||||
jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!");
|
||||
jsonStringer.key("intValue").value(42);
|
||||
jsonStringer.key("doubleValue").value(-23.45e67);
|
||||
jsonStringer.endObject();
|
||||
String str = jsonStringer.toString();
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 7 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 7);
|
||||
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue")));
|
||||
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue")));
|
||||
assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
|
||||
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
|
||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
|
||||
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
|
||||
assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a JSON doc using JSONString API calls,
|
||||
* then convert to JSONArray
|
||||
*/
|
||||
@Test
|
||||
public void simpleArrayString() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.array();
|
||||
jsonStringer.value(true);
|
||||
jsonStringer.value(false);
|
||||
jsonStringer.value(null);
|
||||
jsonStringer.value("hello world!");
|
||||
jsonStringer.value(42);
|
||||
jsonStringer.value(-23.45e67);
|
||||
jsonStringer.endArray();
|
||||
String str = jsonStringer.toString();
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||
assertTrue("expected 6 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
|
||||
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
|
||||
assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/2")));
|
||||
assertTrue("expected hello world!", "hello world!".equals(jsonArray.query("/3")));
|
||||
assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/4")));
|
||||
assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonArray.query("/5")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a nested JSON doc using JSONString API calls, then convert to
|
||||
* JSONObject. Will create a long cascade of output by reusing the
|
||||
* returned values..
|
||||
*/
|
||||
@Test
|
||||
public void complexObjectString() {
|
||||
JSONStringer jsonStringer = new JSONStringer();
|
||||
jsonStringer.object().
|
||||
key("trueValue").value(true).
|
||||
key("falseValue").value(false).
|
||||
key("nullValue").value(null).
|
||||
key("stringValue").value("hello world!").
|
||||
key("object2").object().
|
||||
key("k1").value("v1").
|
||||
key("k2").value("v2").
|
||||
key("k3").value("v3").
|
||||
key("array1").array().
|
||||
value(1).
|
||||
value(2).
|
||||
object().
|
||||
key("k4").value("v4").
|
||||
key("k5").value("v5").
|
||||
key("k6").value("v6").
|
||||
key("array2").array().
|
||||
value(5).
|
||||
value(6).
|
||||
value(7).
|
||||
value(8).
|
||||
endArray().
|
||||
endObject().
|
||||
value(3).
|
||||
value(4).
|
||||
endArray().
|
||||
endObject().
|
||||
key("complexStringValue").value("h\be\tllo w\u1234orld!").
|
||||
key("intValue").value(42).
|
||||
key("doubleValue").value(-23.45e67).
|
||||
endObject();
|
||||
String str = jsonStringer.toString();
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
|
||||
// validate JSON content
|
||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||
assertTrue("expected 8 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 8);
|
||||
assertTrue("expected 4 object2 items", ((Map<?,?>)(JsonPath.read(doc, "$.object2"))).size() == 4);
|
||||
assertTrue("expected 5 array1 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1"))).size() == 5);
|
||||
assertTrue("expected 4 array[2] items", ((Map<?,?>)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4);
|
||||
assertTrue("expected 4 array1[2].array2 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4);
|
||||
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue")));
|
||||
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue")));
|
||||
assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
|
||||
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
|
||||
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
|
||||
assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
|
||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
|
||||
assertTrue("expected v1", "v1".equals(jsonObject.query("/object2/k1")));
|
||||
assertTrue("expected v2", "v2".equals(jsonObject.query("/object2/k2")));
|
||||
assertTrue("expected v3", "v3".equals(jsonObject.query("/object2/k3")));
|
||||
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/object2/array1/0")));
|
||||
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/object2/array1/1")));
|
||||
assertTrue("expected v4", "v4".equals(jsonObject.query("/object2/array1/2/k4")));
|
||||
assertTrue("expected v5", "v5".equals(jsonObject.query("/object2/array1/2/k5")));
|
||||
assertTrue("expected v6", "v6".equals(jsonObject.query("/object2/array1/2/k6")));
|
||||
assertTrue("expected 5", Integer.valueOf(5).equals(jsonObject.query("/object2/array1/2/array2/0")));
|
||||
assertTrue("expected 6", Integer.valueOf(6).equals(jsonObject.query("/object2/array1/2/array2/1")));
|
||||
assertTrue("expected 7", Integer.valueOf(7).equals(jsonObject.query("/object2/array1/2/array2/2")));
|
||||
assertTrue("expected 8", Integer.valueOf(8).equals(jsonObject.query("/object2/array1/2/array2/3")));
|
||||
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/object2/array1/3")));
|
||||
assertTrue("expected 4", Integer.valueOf(4).equals(jsonObject.query("/object2/array1/4")));
|
||||
}
|
||||
|
||||
}
|
||||
373
src/test/java/org/json/junit/JSONTokenerTest.java
Normal file
373
src/test/java/org/json/junit/JSONTokenerTest.java
Normal file
@@ -0,0 +1,373 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test specific to the {@link org.json.JSONTokener} class.
|
||||
* @author John Aylward
|
||||
*
|
||||
*/
|
||||
public class JSONTokenerTest {
|
||||
|
||||
/**
|
||||
* verify that back() fails as expected.
|
||||
* @throws IOException thrown if something unexpected happens.
|
||||
*/
|
||||
@Test
|
||||
public void verifyBackFailureZeroIndex() throws IOException {
|
||||
Reader reader = new StringReader("some test string");
|
||||
try {
|
||||
final JSONTokener tokener = new JSONTokener(reader);
|
||||
try {
|
||||
// this should fail since the index is 0;
|
||||
tokener.back();
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Stepping back two steps is not supported", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||
}
|
||||
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* verify that back() fails as expected.
|
||||
* @throws IOException thrown if something unexpected happens.
|
||||
*/
|
||||
@Test
|
||||
public void verifyBackFailureDoubleBack() throws IOException {
|
||||
Reader reader = new StringReader("some test string");
|
||||
try {
|
||||
final JSONTokener tokener = new JSONTokener(reader);
|
||||
tokener.next();
|
||||
tokener.back();
|
||||
try {
|
||||
// this should fail since the index is 0;
|
||||
tokener.back();
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Stepping back two steps is not supported", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValid() {
|
||||
checkValid("0",Number.class);
|
||||
checkValid(" 0 ",Number.class);
|
||||
checkValid("23",Number.class);
|
||||
checkValid("23.5",Number.class);
|
||||
checkValid(" 23.5 ",Number.class);
|
||||
checkValid("null",null);
|
||||
checkValid(" null ",null);
|
||||
checkValid("true",Boolean.class);
|
||||
checkValid(" true\n",Boolean.class);
|
||||
checkValid("false",Boolean.class);
|
||||
checkValid("\nfalse ",Boolean.class);
|
||||
checkValid("{}",JSONObject.class);
|
||||
checkValid(" {} ",JSONObject.class);
|
||||
checkValid("{\"a\":1}",JSONObject.class);
|
||||
checkValid(" {\"a\":1} ",JSONObject.class);
|
||||
checkValid("[]",JSONArray.class);
|
||||
checkValid(" [] ",JSONArray.class);
|
||||
checkValid("[1,2]",JSONArray.class);
|
||||
checkValid("\n\n[1,2]\n\n",JSONArray.class);
|
||||
|
||||
// Test should fail if default strictMode is true, pass if false
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
try {
|
||||
checkValid("1 2", String.class);
|
||||
assertEquals("Expected to throw exception due to invalid string", true, false);
|
||||
} catch (JSONException e) { }
|
||||
} else {
|
||||
checkValid("1 2", String.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrors() {
|
||||
// Check that stream can detect that a value is found after
|
||||
// the first one
|
||||
checkError(" { \"a\":1 } 4 ");
|
||||
checkError("null \"a\"");
|
||||
checkError("{} true");
|
||||
}
|
||||
|
||||
private Object checkValid(String testStr, Class<?> aClass) {
|
||||
Object result = nextValue(testStr);
|
||||
|
||||
// Check class of object returned
|
||||
if( null == aClass ) {
|
||||
if(JSONObject.NULL.equals(result)) {
|
||||
// OK
|
||||
} else {
|
||||
throw new JSONException("Unexpected class: "+result.getClass().getSimpleName());
|
||||
}
|
||||
} else {
|
||||
if( null == result ) {
|
||||
throw new JSONException("Unexpected null result");
|
||||
} else if(!aClass.isAssignableFrom(result.getClass()) ) {
|
||||
throw new JSONException("Unexpected class: "+result.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void checkError(String testStr) {
|
||||
try {
|
||||
nextValue(testStr);
|
||||
|
||||
fail("Error should be triggered: (\""+testStr+"\")");
|
||||
} catch (JSONException e) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that JSONTokener can read a stream that contains a value. After
|
||||
* the reading is done, check that the stream is left in the correct state
|
||||
* by reading the characters after. All valid cases should reach end of stream.
|
||||
* @param testStr
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private Object nextValue(String testStr) throws JSONException {
|
||||
StringReader sr = new StringReader(testStr);
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(sr);
|
||||
|
||||
Object result = tokener.nextValue();
|
||||
|
||||
if( result == null ) {
|
||||
throw new JSONException("Unable to find value token in JSON stream: ("+tokener+"): "+testStr);
|
||||
}
|
||||
|
||||
char c = tokener.nextClean();
|
||||
if( 0 != c ) {
|
||||
throw new JSONException("Unexpected character found at end of JSON stream: "+c+ " ("+tokener+"): "+testStr);
|
||||
}
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
sr.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the failure of the skipTo method with a buffered reader. Preferably
|
||||
* we'd like this not to fail but at this time we don't have a good recovery.
|
||||
*
|
||||
* @throws IOException thrown if something unexpected happens.
|
||||
*/
|
||||
@Test
|
||||
public void testSkipToFailureWithBufferedReader() throws IOException {
|
||||
final byte[] superLongBuffer = new byte[1000001];
|
||||
// fill our buffer
|
||||
for(int i=0;i<superLongBuffer.length;i++) {
|
||||
superLongBuffer[i] = 'A';
|
||||
}
|
||||
|
||||
Reader reader = new BufferedReader(new InputStreamReader(
|
||||
new ByteArrayInputStream(superLongBuffer)));
|
||||
try {
|
||||
final JSONTokener tokener = new JSONTokener(reader);
|
||||
try {
|
||||
// this should fail since the internal markAhead buffer is only 1,000,000
|
||||
// but 'B' doesn't exist in our buffer that is 1,000,001 in size
|
||||
tokener.skipTo('B');
|
||||
fail("Expected exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Mark invalid", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the success of the skipTo method with a String reader.
|
||||
*
|
||||
* @throws IOException thrown if something unexpected happens.
|
||||
*/
|
||||
@Test
|
||||
public void testSkipToSuccessWithStringReader() throws IOException {
|
||||
final StringBuilder superLongBuffer = new StringBuilder(1000001);
|
||||
// fill our buffer
|
||||
for(int i=0;i<superLongBuffer.length();i++) {
|
||||
superLongBuffer.append('A');
|
||||
}
|
||||
Reader reader = new StringReader(superLongBuffer.toString());
|
||||
try {
|
||||
final JSONTokener tokener = new JSONTokener(reader);
|
||||
try {
|
||||
// this should not fail since the internal markAhead is ignored for StringReaders
|
||||
tokener.skipTo('B');
|
||||
} catch (Exception e) {
|
||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that next and back are working properly and tracking the correct positions
|
||||
* with different new line combinations.
|
||||
*/
|
||||
@Test
|
||||
public void testNextBackComboWithNewLines() {
|
||||
final String testString = "this is\nA test\r\nWith some different\rNew Lines";
|
||||
// ^ ^ ^ ^
|
||||
// index positions 0 8 16 36
|
||||
final JSONTokener tokener = new JSONTokener(testString);
|
||||
assertEquals(" at 0 [character 1 line 1]", tokener.toString());
|
||||
assertEquals('t',tokener.next());
|
||||
assertEquals(" at 1 [character 2 line 1]", tokener.toString());
|
||||
tokener.skipTo('\n');
|
||||
assertEquals("skipTo() improperly modifying indexes"," at 7 [character 8 line 1]", tokener.toString());
|
||||
assertEquals('\n',tokener.next());
|
||||
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
|
||||
assertEquals('A',tokener.next());
|
||||
assertEquals(" at 9 [character 1 line 2]", tokener.toString());
|
||||
tokener.back();
|
||||
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
|
||||
tokener.skipTo('\r');
|
||||
assertEquals("skipTo() improperly modifying indexes"," at 14 [character 6 line 2]", tokener.toString());
|
||||
// verify \r\n combo doesn't increment the line twice
|
||||
assertEquals('\r', tokener.next());
|
||||
assertEquals(" at 15 [character 0 line 3]", tokener.toString());
|
||||
assertEquals('\n', tokener.next());
|
||||
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
|
||||
// verify stepping back after reading the \n of an \r\n combo doesn't increment the line incorrectly
|
||||
tokener.back();
|
||||
assertEquals(" at 15 [character 6 line 2]", tokener.toString());
|
||||
assertEquals('\n', tokener.next());
|
||||
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
|
||||
assertEquals('W', tokener.next());
|
||||
assertEquals(" at 17 [character 1 line 3]", tokener.toString());
|
||||
assertEquals('i', tokener.next());
|
||||
assertEquals(" at 18 [character 2 line 3]", tokener.toString());
|
||||
tokener.skipTo('\r');
|
||||
assertEquals("skipTo() improperly modifying indexes"," at 35 [character 19 line 3]", tokener.toString());
|
||||
assertEquals('\r', tokener.next());
|
||||
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
|
||||
tokener.back();
|
||||
assertEquals(" at 35 [character 19 line 3]", tokener.toString());
|
||||
assertEquals('\r', tokener.next());
|
||||
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
|
||||
assertEquals('N', tokener.next());
|
||||
assertEquals(" at 37 [character 1 line 4]", tokener.toString());
|
||||
|
||||
// verify we get the same data just walking though, no calls to back
|
||||
final JSONTokener t2 = new JSONTokener(testString);
|
||||
for(int i=0; i<7; i++) {
|
||||
assertTrue(t2.toString().startsWith(" at " + i + " "));
|
||||
assertEquals(testString.charAt(i), t2.next());
|
||||
}
|
||||
assertEquals(" at 7 [character 8 line 1]", t2.toString());
|
||||
assertEquals(testString.charAt(7), t2.next());
|
||||
assertEquals(" at 8 [character 0 line 2]", t2.toString());
|
||||
for(int i=8; i<14; i++) {
|
||||
assertTrue(t2.toString().startsWith(" at " + i + " "));
|
||||
assertEquals(testString.charAt(i), t2.next());
|
||||
}
|
||||
assertEquals(" at 14 [character 6 line 2]", t2.toString());
|
||||
assertEquals('\r', t2.next());
|
||||
assertEquals(" at 15 [character 0 line 3]", t2.toString());
|
||||
assertEquals('\n', t2.next());
|
||||
assertEquals(" at 16 [character 0 line 3]", t2.toString());
|
||||
assertEquals('W', t2.next());
|
||||
assertEquals(" at 17 [character 1 line 3]", t2.toString());
|
||||
for(int i=17; i<37; i++) {
|
||||
assertTrue(t2.toString().startsWith(" at " + i + " "));
|
||||
assertEquals(testString.charAt(i), t2.next());
|
||||
}
|
||||
assertEquals(" at 37 [character 1 line 4]", t2.toString());
|
||||
for(int i=37; i<testString.length(); i++) {
|
||||
assertTrue(t2.toString().startsWith(" at " + i + " "));
|
||||
assertEquals(testString.charAt(i), t2.next());
|
||||
}
|
||||
assertEquals(" at "+ testString.length() +" [character 9 line 4]", t2.toString());
|
||||
// end of the input
|
||||
assertEquals(0, t2.next());
|
||||
assertFalse(t2.more());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoClose(){
|
||||
Reader reader = new StringReader("some test string");
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(reader);
|
||||
tokener.close();
|
||||
tokener.next();
|
||||
} catch (Exception exception){
|
||||
assertEquals("Stream closed", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
|
||||
String input = "{\"invalidInput\": [],}";
|
||||
JSONTokener tokener = new JSONTokener(input);
|
||||
|
||||
// Test should fail if default strictMode is true, pass if false
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
try {
|
||||
Object value = tokener.nextValue();
|
||||
assertEquals(new JSONObject(input).toString(), value.toString());
|
||||
assertEquals("Expected to throw exception due to invalid string", true, false);
|
||||
} catch (JSONException e) { }
|
||||
} else {
|
||||
Object value = tokener.nextValue();
|
||||
assertEquals(new JSONObject(input).toString(), value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
|
||||
String input = "[\"invalidInput\",]";
|
||||
JSONTokener tokener = new JSONTokener(input);
|
||||
|
||||
// Test should fail if default strictMode is true, pass if false
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
try {
|
||||
Object value = tokener.nextValue();
|
||||
assertEquals(new JSONArray(input).toString(), value.toString());
|
||||
assertEquals("Expected to throw exception due to invalid string", true, false);
|
||||
} catch (JSONException e) { }
|
||||
} else {
|
||||
Object value = tokener.nextValue();
|
||||
assertEquals(new JSONArray(input).toString(), value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
102
src/test/java/org/json/junit/PropertyTest.java
Normal file
102
src/test/java/org/json/junit/PropertyTest.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for JSON-Java Property
|
||||
*/
|
||||
public class PropertyTest {
|
||||
|
||||
/**
|
||||
* JSONObject from null properties object should
|
||||
* result in an empty JSONObject.
|
||||
*/
|
||||
@Test
|
||||
public void shouldHandleNullProperties() {
|
||||
Properties properties = null;
|
||||
JSONObject jsonObject = Property.toJSONObject(properties);
|
||||
assertTrue("jsonObject should be empty", jsonObject.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONObject from empty properties object should
|
||||
* result in an empty JSONObject.
|
||||
*/
|
||||
@Test
|
||||
public void shouldHandleEmptyProperties() {
|
||||
Properties properties = new Properties();
|
||||
JSONObject jsonObject = Property.toJSONObject(properties);
|
||||
assertTrue("jsonObject should be empty", jsonObject.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONObject from simple properties object.
|
||||
*/
|
||||
@Test
|
||||
public void shouldHandleProperties() {
|
||||
Properties properties = new Properties();
|
||||
|
||||
properties.put("Illinois", "Springfield");
|
||||
properties.put("Missouri", "Jefferson City");
|
||||
properties.put("Washington", "Olympia");
|
||||
properties.put("California", "Sacramento");
|
||||
properties.put("Indiana", "Indianapolis");
|
||||
|
||||
JSONObject jsonObject = Property.toJSONObject(properties);
|
||||
|
||||
assertTrue("jsonObject should contain 5 items", jsonObject.length() == 5);
|
||||
assertTrue("jsonObject should contain Illinois property",
|
||||
"Springfield".equals(jsonObject.get("Illinois")));
|
||||
assertTrue("jsonObject should contain Missouri property",
|
||||
"Jefferson City".equals(jsonObject.get("Missouri")));
|
||||
assertTrue("jsonObject should contain Washington property",
|
||||
"Olympia".equals(jsonObject.get("Washington")));
|
||||
assertTrue("jsonObject should contain California property",
|
||||
"Sacramento".equals(jsonObject.get("California")));
|
||||
assertTrue("jsonObject should contain Indiana property",
|
||||
"Indianapolis".equals(jsonObject.get("Indiana")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Null JSONObject toProperties() should result in an empty
|
||||
* Properties object.
|
||||
*/
|
||||
@Test
|
||||
public void shouldHandleNullJSONProperty() {
|
||||
JSONObject jsonObject= null;
|
||||
Properties properties = Property.toProperties(jsonObject);
|
||||
assertTrue("properties should be empty",
|
||||
properties.size() == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Properties should convert to JSONObject, and back to
|
||||
* Properties without changing.
|
||||
*/
|
||||
@Test
|
||||
public void shouldHandleJSONProperty() {
|
||||
Properties properties = new Properties();
|
||||
|
||||
properties.put("Illinois", "Springfield");
|
||||
properties.put("Missouri", "Jefferson City");
|
||||
properties.put("Washington", "Olympia");
|
||||
properties.put("California", "Sacramento");
|
||||
properties.put("Indiana", "Indianapolis");
|
||||
|
||||
JSONObject jsonObject = Property.toJSONObject(properties);
|
||||
Properties jsonProperties = Property.toProperties(jsonObject);
|
||||
|
||||
assertTrue("property objects should match",
|
||||
properties.equals(jsonProperties));
|
||||
}
|
||||
}
|
||||
60
src/test/java/org/json/junit/StringBuilderWriterTest.java
Normal file
60
src/test/java/org/json/junit/StringBuilderWriterTest.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package org.json.junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.json.StringBuilderWriter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringBuilderWriterTest {
|
||||
private StringBuilderWriter writer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
writer = new StringBuilderWriter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteChar() {
|
||||
writer.write('a');
|
||||
assertEquals("a", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteCharArray() {
|
||||
char[] chars = {'a', 'b', 'c'};
|
||||
writer.write(chars, 0, 3);
|
||||
assertEquals("abc", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteString() {
|
||||
writer.write("hello");
|
||||
assertEquals("hello", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteStringWithOffsetAndLength() {
|
||||
writer.write("hello world", 6, 5);
|
||||
assertEquals("world", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendCharSequence() {
|
||||
writer.append("hello");
|
||||
assertEquals("hello", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendCharSequenceWithStartAndEnd() {
|
||||
CharSequence csq = "hello world";
|
||||
writer.append(csq, 6, 11);
|
||||
assertEquals("world", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendChar() {
|
||||
writer.append('a');
|
||||
assertEquals("a", writer.toString());
|
||||
}
|
||||
}
|
||||
201
src/test/java/org/json/junit/Util.java
Normal file
201
src/test/java/org/json/junit/Util.java
Normal file
@@ -0,0 +1,201 @@
|
||||
package org.json.junit;
|
||||
|
||||
/*
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.json.*;
|
||||
|
||||
/**
|
||||
* These are helpful utility methods that perform basic comparisons
|
||||
* between various objects. In most cases, the comparisons are not
|
||||
* order-dependent, or else the order is known.
|
||||
*/
|
||||
public class Util {
|
||||
|
||||
/**
|
||||
* Compares two JSONArrays for equality.
|
||||
* The arrays need not be in the same order.
|
||||
* @param jsonArray created by the code to be tested
|
||||
* @param expectedJsonArray created specifically for comparing
|
||||
*/
|
||||
public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
|
||||
JSONArray expectedJsonArray) {
|
||||
assertTrue("jsonArray lengths should be equal",
|
||||
jsonArray.length() == expectedJsonArray.length());
|
||||
for (int i = 0; i < jsonArray.length(); ++i) {
|
||||
Object value = jsonArray.get(i);
|
||||
Object expectedValue = expectedJsonArray.get(i);
|
||||
compareActualVsExpectedObjects(value, expectedValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two JSONObjects for equality. The objects need not be
|
||||
* in the same order
|
||||
* @param jsonObject created by the code to be tested
|
||||
* @param expectedJsonObject created specifically for comparing
|
||||
*/
|
||||
public static void compareActualVsExpectedJsonObjects(
|
||||
JSONObject jsonObject, JSONObject expectedJsonObject) {
|
||||
assertTrue("jsonObjects should have the same length",
|
||||
jsonObject.length() == expectedJsonObject.length());
|
||||
Iterator<String> keys = jsonObject.keys();
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
Object value = jsonObject.get(key);
|
||||
Object expectedValue = expectedJsonObject.get(key);
|
||||
compareActualVsExpectedObjects(value, expectedValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two objects for equality. Might be JSONArray, JSONObject,
|
||||
* or something else.
|
||||
* @param value created by the code to be tested
|
||||
* @param expectedValue created specifically for comparing
|
||||
*/
|
||||
private static void compareActualVsExpectedObjects(Object value,
|
||||
Object expectedValue) {
|
||||
if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
|
||||
// Compare JSONObjects
|
||||
JSONObject jsonObject = (JSONObject)value;
|
||||
JSONObject expectedJsonObject = (JSONObject)expectedValue;
|
||||
compareActualVsExpectedJsonObjects(
|
||||
jsonObject, expectedJsonObject);
|
||||
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
|
||||
// Compare JSONArrays
|
||||
JSONArray jsonArray = (JSONArray)value;
|
||||
JSONArray expectedJsonArray = (JSONArray)expectedValue;
|
||||
compareActualVsExpectedJsonArrays(
|
||||
jsonArray, expectedJsonArray);
|
||||
} else {
|
||||
/**
|
||||
* Compare all other types using toString(). First, the types must
|
||||
* also be equal, unless both are Number type. Certain helper
|
||||
* classes (e.g. XML) may create Long instead of Integer for small
|
||||
* int values.
|
||||
*/
|
||||
if (!(value instanceof Number && expectedValue instanceof Number)) {
|
||||
// Non-Number and non-matching types
|
||||
assertEquals("object types should be equal ",
|
||||
expectedValue.getClass().toString(),
|
||||
value.getClass().toString()
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Same types or both Numbers, compare by toString()
|
||||
*/
|
||||
assertEquals("values should be equal",
|
||||
expectedValue.toString(),
|
||||
value.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that all JSONObject maps are the same as the default ctor
|
||||
* @param jsonObjects list of objects to be tested
|
||||
*/
|
||||
public static void checkJSONObjectsMaps(List<JSONObject> jsonObjects) {
|
||||
if (jsonObjects == null || jsonObjects.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Class<? extends Map> mapType = new JSONObject().getMapType();
|
||||
for (JSONObject jsonObject : jsonObjects) {
|
||||
if (jsonObject != null) {
|
||||
assertTrue(mapType == jsonObject.getMapType());
|
||||
checkJSONObjectMaps(jsonObject, mapType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that all JSONObject maps are the same as the default ctor
|
||||
* @param jsonObject the object to be tested
|
||||
*/
|
||||
public static void checkJSONObjectMaps(JSONObject jsonObject) {
|
||||
if (jsonObject != null) {
|
||||
checkJSONObjectMaps(jsonObject, jsonObject.getMapType());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that all JSONObject maps are the same as mapType
|
||||
* @param jsonObject object to be tested
|
||||
* @param mapType mapType to test against
|
||||
*/
|
||||
public static void checkJSONObjectMaps(JSONObject jsonObject, Class<? extends Map> mapType) {
|
||||
if (mapType == null) {
|
||||
mapType = new JSONObject().getMapType();
|
||||
}
|
||||
Set<String> keys = jsonObject.keySet();
|
||||
for (String key : keys) {
|
||||
Object val = jsonObject.get(key);
|
||||
if (val instanceof JSONObject) {
|
||||
JSONObject jsonObjectVal = (JSONObject) val;
|
||||
assertTrue(mapType == ((JSONObject) val).getMapType());
|
||||
checkJSONObjectMaps(jsonObjectVal, mapType);
|
||||
} else if (val instanceof JSONArray) {
|
||||
JSONArray jsonArrayVal = (JSONArray)val;
|
||||
checkJSONArrayMaps(jsonArrayVal, mapType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that all JSONObject maps in the JSONArray object match the default map
|
||||
* @param jsonArrays list of JSONArray objects to be tested
|
||||
*/
|
||||
public static void checkJSONArraysMaps(List<JSONArray> jsonArrays) {
|
||||
if (jsonArrays == null || jsonArrays.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Class<? extends Map> mapType = new JSONObject().getMapType();
|
||||
for (JSONArray jsonArray : jsonArrays) {
|
||||
if (jsonArray != null) {
|
||||
checkJSONArrayMaps(jsonArray, mapType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that all JSONObject maps in the JSONArray object match mapType
|
||||
* @param jsonArray object to be tested
|
||||
* @param mapType map type to be tested against
|
||||
*/
|
||||
public static void checkJSONArrayMaps(JSONArray jsonArray, Class<? extends Map> mapType) {
|
||||
if (jsonArray == null) {
|
||||
return;
|
||||
}
|
||||
if (mapType == null) {
|
||||
mapType = new JSONObject().getMapType();
|
||||
}
|
||||
Iterator<Object> it = jsonArray.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object val = it.next();
|
||||
if (val instanceof JSONObject) {
|
||||
JSONObject jsonObjectVal = (JSONObject)val;
|
||||
checkJSONObjectMaps(jsonObjectVal, mapType);
|
||||
} else if (val instanceof JSONArray) {
|
||||
JSONArray jsonArrayVal = (JSONArray)val;
|
||||
checkJSONArrayMaps(jsonArrayVal, mapType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that all JSONObject maps nested in the JSONArray match
|
||||
* the default mapType
|
||||
* @param jsonArray the object to be tested
|
||||
*/
|
||||
public static void checkJSONArrayMaps(JSONArray jsonArray) {
|
||||
if (jsonArray != null) {
|
||||
checkJSONArrayMaps(jsonArray, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
1236
src/test/java/org/json/junit/XMLConfigurationTest.java
Executable file
1236
src/test/java/org/json/junit/XMLConfigurationTest.java
Executable file
File diff suppressed because it is too large
Load Diff
1432
src/test/java/org/json/junit/XMLTest.java
Normal file
1432
src/test/java/org/json/junit/XMLTest.java
Normal file
File diff suppressed because it is too large
Load Diff
13
src/test/java/org/json/junit/data/BrokenToString.java
Normal file
13
src/test/java/org/json/junit/data/BrokenToString.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* test class for verifying write errors.
|
||||
* @author John Aylward
|
||||
*
|
||||
*/
|
||||
public class BrokenToString {
|
||||
@Override
|
||||
public String toString() {
|
||||
throw new IllegalStateException("Something went horribly wrong!");
|
||||
}
|
||||
}
|
||||
66
src/test/java/org/json/junit/data/ExceptionalBean.java
Normal file
66
src/test/java/org/json/junit/data/ExceptionalBean.java
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Object for testing the exception handling in {@link org.json.JSONObject#populateMap}.
|
||||
*
|
||||
* @author John Aylward
|
||||
*/
|
||||
public class ExceptionalBean {
|
||||
/**
|
||||
* @return a closeable.
|
||||
*/
|
||||
public Closeable getCloseable() {
|
||||
// anonymous inner class did not work...
|
||||
return new MyCloseable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nothing really. Just can't be void.
|
||||
* @throws IllegalAccessException
|
||||
* always thrown
|
||||
*/
|
||||
public int getIllegalAccessException() throws IllegalAccessException {
|
||||
throw new IllegalAccessException("Yup, it's illegal");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nothing really. Just can't be void.
|
||||
* @throws IllegalArgumentException
|
||||
* always thrown
|
||||
*/
|
||||
public int getIllegalArgumentException() throws IllegalArgumentException {
|
||||
throw new IllegalArgumentException("Yup, it's illegal");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nothing really. Just can't be void.
|
||||
* @throws InvocationTargetException
|
||||
* always thrown
|
||||
*/
|
||||
public int getInvocationTargetException() throws InvocationTargetException {
|
||||
throw new InvocationTargetException(new Exception("Yup, it's illegal"));
|
||||
}
|
||||
|
||||
/** My closeable class. */
|
||||
public static final class MyCloseable implements Closeable {
|
||||
|
||||
/**
|
||||
* @return a string
|
||||
*/
|
||||
public String getString() {
|
||||
return "Yup, it's closeable";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
throw new IOException("Closing is too hard!");
|
||||
}
|
||||
}
|
||||
}
|
||||
180
src/test/java/org/json/junit/data/Fraction.java
Normal file
180
src/test/java/org/json/junit/data/Fraction.java
Normal file
@@ -0,0 +1,180 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* basic fraction class, no frills.
|
||||
* @author John Aylward
|
||||
*
|
||||
*/
|
||||
public class Fraction extends Number implements Comparable<Fraction> {
|
||||
/**
|
||||
* serial id.
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* value as a big decimal.
|
||||
*/
|
||||
private final BigDecimal bigDecimal;
|
||||
|
||||
/**
|
||||
* value of the denominator.
|
||||
*/
|
||||
private final BigInteger denominator;
|
||||
/**
|
||||
* value of the numerator.
|
||||
*/
|
||||
private final BigInteger numerator;
|
||||
|
||||
/**
|
||||
* @param numerator
|
||||
* numerator
|
||||
* @param denominator
|
||||
* denominator
|
||||
*/
|
||||
public Fraction(final BigInteger numerator, final BigInteger denominator) {
|
||||
super();
|
||||
if (numerator == null || denominator == null) {
|
||||
throw new IllegalArgumentException("All values must be non-null");
|
||||
}
|
||||
if (denominator.compareTo(BigInteger.ZERO)==0) {
|
||||
throw new IllegalArgumentException("Divide by zero");
|
||||
}
|
||||
|
||||
final BigInteger n;
|
||||
final BigInteger d;
|
||||
// normalize fraction
|
||||
if (denominator.signum()<0) {
|
||||
n = numerator.negate();
|
||||
d = denominator.negate();
|
||||
} else {
|
||||
n = numerator;
|
||||
d = denominator;
|
||||
}
|
||||
this.numerator = n;
|
||||
this.denominator = d;
|
||||
if (n.compareTo(BigInteger.ZERO)==0) {
|
||||
this.bigDecimal = BigDecimal.ZERO;
|
||||
} else if (n.compareTo(d)==0) {// i.e. 4/4, 10/10
|
||||
this.bigDecimal = BigDecimal.ONE;
|
||||
} else {
|
||||
this.bigDecimal = new BigDecimal(this.numerator).divide(new BigDecimal(this.denominator),
|
||||
RoundingMode.HALF_EVEN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param numerator
|
||||
* numerator
|
||||
* @param denominator
|
||||
* denominator
|
||||
*/
|
||||
public Fraction(final long numerator, final long denominator) {
|
||||
this(BigInteger.valueOf(numerator),BigInteger.valueOf(denominator));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the decimal
|
||||
*/
|
||||
public BigDecimal bigDecimalValue() {
|
||||
return this.bigDecimal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final Fraction o) {
|
||||
// .equals call this, so no .equals compare allowed
|
||||
|
||||
// if they are the same reference, just return equals
|
||||
if (this == o) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// if my denominators are already equal, just compare the numerators
|
||||
if (this.denominator.compareTo(o.denominator)==0) {
|
||||
return this.numerator.compareTo(o.numerator);
|
||||
}
|
||||
|
||||
// get numerators of common denominators
|
||||
// a x ay xb
|
||||
// --- --- = ---- ----
|
||||
// b y by yb
|
||||
final BigInteger thisN = this.numerator.multiply(o.denominator);
|
||||
final BigInteger otherN = o.numerator.multiply(this.denominator);
|
||||
|
||||
return thisN.compareTo(otherN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
return this.bigDecimal.doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (this.getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Fraction other = (Fraction) obj;
|
||||
return this.compareTo(other) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float floatValue() {
|
||||
return this.bigDecimal.floatValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the denominator
|
||||
*/
|
||||
public BigInteger getDenominator() {
|
||||
return this.denominator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the numerator
|
||||
*/
|
||||
public BigInteger getNumerator() {
|
||||
return this.numerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (this.bigDecimal == null ? 0 : this.bigDecimal.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return this.bigDecimal.intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longValue() {
|
||||
return this.bigDecimal.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.numerator + "/" + this.denominator;
|
||||
}
|
||||
}
|
||||
79
src/test/java/org/json/junit/data/GenericBean.java
Normal file
79
src/test/java/org/json/junit/data/GenericBean.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author John Aylward
|
||||
*
|
||||
* @param <T>
|
||||
* generic number value
|
||||
*/
|
||||
public class GenericBean<T extends Number> implements MyBean {
|
||||
/**
|
||||
* @param genericValue
|
||||
* value to initiate with
|
||||
*/
|
||||
public GenericBean(T genericValue) {
|
||||
super();
|
||||
this.genericValue = genericValue;
|
||||
}
|
||||
|
||||
/** */
|
||||
protected T genericValue;
|
||||
/** to be used by the calling test to see how often the getter is called */
|
||||
public int genericGetCounter;
|
||||
/** to be used by the calling test to see how often the setter is called */
|
||||
public int genericSetCounter;
|
||||
|
||||
/** @return the genericValue */
|
||||
public T getGenericValue() {
|
||||
this.genericGetCounter++;
|
||||
return this.genericValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param genericValue
|
||||
* generic value to set
|
||||
*/
|
||||
public void setGenericValue(T genericValue) {
|
||||
this.genericSetCounter++;
|
||||
this.genericValue = genericValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getIntKey() {
|
||||
return Integer.valueOf(42);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDoubleKey() {
|
||||
return Double.valueOf(4.2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStringKey() {
|
||||
return "MyString Key";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEscapeStringKey() {
|
||||
return "\"My String with \"s";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isTrueKey() {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isFalseKey() {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringReader getStringReaderKey() {
|
||||
return new StringReader("Some String Value in a reader");
|
||||
}
|
||||
|
||||
}
|
||||
69
src/test/java/org/json/junit/data/GenericBeanInt.java
Normal file
69
src/test/java/org/json/junit/data/GenericBeanInt.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* @author john
|
||||
*
|
||||
*/
|
||||
public class GenericBeanInt extends GenericBean<Integer> {
|
||||
/** */
|
||||
final char a = 'A';
|
||||
|
||||
/** @return the a */
|
||||
public char getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should not be beanable
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
public boolean getable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should not be beanable
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
public boolean get() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should not be beanable
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
public boolean is() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be beanable
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
public boolean isB() {
|
||||
return this.genericValue.equals((Integer.valueOf(this.a+1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param genericValue
|
||||
* the value to initiate with.
|
||||
*/
|
||||
public GenericBeanInt(Integer genericValue) {
|
||||
super(genericValue);
|
||||
}
|
||||
|
||||
/** override to generate a bridge method */
|
||||
@Override
|
||||
public Integer getGenericValue() {
|
||||
return super.getGenericValue();
|
||||
}
|
||||
|
||||
}
|
||||
16
src/test/java/org/json/junit/data/MyBean.java
Normal file
16
src/test/java/org/json/junit/data/MyBean.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Used in testing when Bean behavior is needed
|
||||
*/
|
||||
public interface MyBean {
|
||||
public Integer getIntKey();
|
||||
public Double getDoubleKey();
|
||||
public String getStringKey();
|
||||
public String getEscapeStringKey();
|
||||
public Boolean isTrueKey();
|
||||
public Boolean isFalseKey();
|
||||
public StringReader getStringReaderKey();
|
||||
}
|
||||
20
src/test/java/org/json/junit/data/MyBeanCustomName.java
Normal file
20
src/test/java/org/json/junit/data/MyBeanCustomName.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import org.json.JSONPropertyName;
|
||||
|
||||
/**
|
||||
* Test bean for the {@link JSONPropertyName} annotation.
|
||||
*/
|
||||
public class MyBeanCustomName implements MyBeanCustomNameInterface {
|
||||
public int getSomeInt() { return 42; }
|
||||
@JSONPropertyName("")
|
||||
public long getSomeLong() { return 42L; }
|
||||
@JSONPropertyName("myStringField")
|
||||
public String getSomeString() { return "someStringValue"; }
|
||||
@JSONPropertyName("Some Weird NAme that Normally Wouldn't be possible!")
|
||||
public double getMyDouble() { return 0.0d; }
|
||||
@Override
|
||||
public float getSomeFloat() { return 2.0f; }
|
||||
@Override
|
||||
public int getIgnoredInt() { return 40; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import org.json.JSONPropertyIgnore;
|
||||
import org.json.JSONPropertyName;
|
||||
|
||||
public interface MyBeanCustomNameInterface {
|
||||
@JSONPropertyName("InterfaceField")
|
||||
float getSomeFloat();
|
||||
@JSONPropertyIgnore
|
||||
int getIgnoredInt();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.json.junit.data;
|
||||
|
||||
import org.json.JSONPropertyIgnore;
|
||||
import org.json.JSONPropertyName;
|
||||
|
||||
/**
|
||||
* Test bean to verify that the {@link org.json.JSONPropertyName} annotation
|
||||
* is inherited.
|
||||
*/
|
||||
public class MyBeanCustomNameSubClass extends MyBeanCustomName {
|
||||
@Override
|
||||
@JSONPropertyName("forcedInt")
|
||||
public int getIgnoredInt() { return 42*42; }
|
||||
@Override
|
||||
@JSONPropertyName("newIntFieldName")
|
||||
public int getSomeInt() { return 43; }
|
||||
@Override
|
||||
public String getSomeString() { return "subClassString"; }
|
||||
@Override
|
||||
@JSONPropertyName("AMoreNormalName")
|
||||
public double getMyDouble() { return 1.0d; }
|
||||
@Override
|
||||
public float getSomeFloat() { return 3.0f; }
|
||||
@JSONPropertyIgnore
|
||||
@JSONPropertyName("ShouldBeIgnored")
|
||||
public boolean getShouldNotBeJSON() { return true; }
|
||||
@JSONPropertyName("Getable")
|
||||
public boolean getable() { return true; }
|
||||
}
|
||||
11
src/test/java/org/json/junit/data/MyBigNumberBean.java
Normal file
11
src/test/java/org/json/junit/data/MyBigNumberBean.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.math.*;
|
||||
|
||||
/**
|
||||
* Used in testing when a Bean containing big numbers is needed
|
||||
*/
|
||||
public interface MyBigNumberBean {
|
||||
public BigInteger getBigInteger();
|
||||
public BigDecimal getBigDecimal();
|
||||
}
|
||||
10
src/test/java/org/json/junit/data/MyEnum.java
Normal file
10
src/test/java/org/json/junit/data/MyEnum.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* An enum with no methods or data
|
||||
*/
|
||||
public enum MyEnum {
|
||||
VAL1,
|
||||
VAL2,
|
||||
VAL3;
|
||||
}
|
||||
22
src/test/java/org/json/junit/data/MyEnumClass.java
Normal file
22
src/test/java/org/json/junit/data/MyEnumClass.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* this is simply a class that contains some enum instances
|
||||
*/
|
||||
public class MyEnumClass {
|
||||
private MyEnum myEnum;
|
||||
private MyEnumField myEnumField;
|
||||
|
||||
public MyEnum getMyEnum() {
|
||||
return this.myEnum;
|
||||
}
|
||||
public void setMyEnum(MyEnum myEnum) {
|
||||
this.myEnum = myEnum;
|
||||
}
|
||||
public MyEnumField getMyEnumField() {
|
||||
return this.myEnumField;
|
||||
}
|
||||
public void setMyEnumField(MyEnumField myEnumField) {
|
||||
this.myEnumField = myEnumField;
|
||||
}
|
||||
}
|
||||
28
src/test/java/org/json/junit/data/MyEnumField.java
Normal file
28
src/test/java/org/json/junit/data/MyEnumField.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* An enum that contains getters and some internal fields
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public enum MyEnumField {
|
||||
VAL1(1, "val 1"),
|
||||
VAL2(2, "val 2"),
|
||||
VAL3(3, "val 3");
|
||||
|
||||
private String value;
|
||||
private Integer intVal;
|
||||
private MyEnumField(Integer intVal, String value) {
|
||||
this.value = value;
|
||||
this.intVal = intVal;
|
||||
}
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
public Integer getIntVal() {
|
||||
return this.intVal;
|
||||
}
|
||||
@Override
|
||||
public String toString(){
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
14
src/test/java/org/json/junit/data/MyJsonString.java
Normal file
14
src/test/java/org/json/junit/data/MyJsonString.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import org.json.*;
|
||||
|
||||
/**
|
||||
* Used in testing when a JSONString is needed
|
||||
*/
|
||||
public class MyJsonString implements JSONString {
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
return "my string";
|
||||
}
|
||||
}
|
||||
12
src/test/java/org/json/junit/data/MyLocaleBean.java
Executable file
12
src/test/java/org/json/junit/data/MyLocaleBean.java
Executable file
@@ -0,0 +1,12 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
public class MyLocaleBean {
|
||||
private final String id = "beanId";
|
||||
private final String i = "beanI";
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
public String getI() {
|
||||
return this.i;
|
||||
}
|
||||
}
|
||||
97
src/test/java/org/json/junit/data/MyNumber.java
Normal file
97
src/test/java/org/json/junit/data/MyNumber.java
Normal file
@@ -0,0 +1,97 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Number override for testing. Number overrides should always override
|
||||
* toString, hashCode, and Equals.
|
||||
*
|
||||
* @see <a
|
||||
* href="https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html">The
|
||||
* Numbers Classes</a>
|
||||
* @see <a
|
||||
* href="https://docs.oracle.com/javase/tutorial/java/data/numberformat.html">Formatting
|
||||
* Numeric Print Output</a>
|
||||
*
|
||||
* @author John Aylward
|
||||
*/
|
||||
public class MyNumber extends Number {
|
||||
private Number number = BigDecimal.valueOf(42);
|
||||
/**
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* @return number!
|
||||
*/
|
||||
public Number getNumber() {
|
||||
return this.number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return getNumber().intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longValue() {
|
||||
return getNumber().longValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float floatValue() {
|
||||
return getNumber().floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
return getNumber().doubleValue();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*
|
||||
* Number overrides should in general always override the toString method.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return getNumber().toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.number == null) ? 0 : this.number.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof MyNumber)) {
|
||||
return false;
|
||||
}
|
||||
MyNumber other = (MyNumber) obj;
|
||||
if (this.number == null) {
|
||||
if (other.number != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!this.number.equals(other.number)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
13
src/test/java/org/json/junit/data/MyNumberContainer.java
Normal file
13
src/test/java/org/json/junit/data/MyNumberContainer.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* Class that holds our MyNumber override as a property.
|
||||
* @author John Aylward
|
||||
*/
|
||||
public class MyNumberContainer {
|
||||
private MyNumber myNumber = new MyNumber();
|
||||
/**
|
||||
* @return a MyNumber.
|
||||
*/
|
||||
public Number getMyNumber() {return this.myNumber;}
|
||||
}
|
||||
10
src/test/java/org/json/junit/data/MyPublicClass.java
Normal file
10
src/test/java/org/json/junit/data/MyPublicClass.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* Need a class with some public data members for testing
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public class MyPublicClass {
|
||||
public Integer publicInt = 42;
|
||||
public String publicString = "abc";
|
||||
}
|
||||
23
src/test/java/org/json/junit/data/RecursiveBean.java
Normal file
23
src/test/java/org/json/junit/data/RecursiveBean.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* test class for verifying if recursively defined bean can be correctly identified
|
||||
* @author Zetmas
|
||||
*
|
||||
*/
|
||||
public class RecursiveBean {
|
||||
private String name;
|
||||
private Object reference;
|
||||
private Object reference2;
|
||||
public String getName() { return name; }
|
||||
public Object getRef() {return reference;}
|
||||
public Object getRef2() {return reference2;}
|
||||
public void setRef(Object refObj) {reference = refObj;}
|
||||
public void setRef2(Object refObj) {reference2 = refObj;}
|
||||
|
||||
public RecursiveBean(String name) {
|
||||
this.name = name;
|
||||
reference = null;
|
||||
reference2 = null;
|
||||
}
|
||||
}
|
||||
33
src/test/java/org/json/junit/data/RecursiveBeanEquals.java
Normal file
33
src/test/java/org/json/junit/data/RecursiveBeanEquals.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/** test class for verifying if recursively defined bean can be correctly identified */
|
||||
public class RecursiveBeanEquals {
|
||||
private final String name;
|
||||
private Object reference;
|
||||
|
||||
public RecursiveBeanEquals(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Object getRef() {
|
||||
return reference;
|
||||
}
|
||||
|
||||
public void setRef(Object refObj) {
|
||||
reference = refObj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof RecursiveBeanEquals && name.equals(((RecursiveBeanEquals) other).name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
91
src/test/java/org/json/junit/data/Singleton.java
Normal file
91
src/test/java/org/json/junit/data/Singleton.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* Sample singleton for use with bean testing.
|
||||
*
|
||||
* @author John Aylward
|
||||
*
|
||||
*/
|
||||
public final class Singleton {
|
||||
/** */
|
||||
private int someInt;
|
||||
/** */
|
||||
private String someString;
|
||||
/** single instance. */
|
||||
private static final Singleton INSTANCE = new Singleton();
|
||||
|
||||
/** @return the singleton instance. */
|
||||
public static final Singleton getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/** */
|
||||
private Singleton() {
|
||||
if (INSTANCE != null) {
|
||||
throw new IllegalStateException("Already instantiated");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/** @return someInt */
|
||||
public int getSomeInt() {
|
||||
return this.someInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets someInt.
|
||||
*
|
||||
* @param someInt
|
||||
* the someInt to set
|
||||
*/
|
||||
public void setSomeInt(int someInt) {
|
||||
this.someInt = someInt;
|
||||
}
|
||||
|
||||
/** @return someString */
|
||||
public String getSomeString() {
|
||||
return this.someString;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets someString.
|
||||
*
|
||||
* @param someString
|
||||
* the someString to set
|
||||
*/
|
||||
public void setSomeString(String someString) {
|
||||
this.someString = someString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + this.someInt;
|
||||
result = prime * result + ((this.someString == null) ? 0 : this.someString.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Singleton other = (Singleton) obj;
|
||||
if (this.someInt != other.someInt)
|
||||
return false;
|
||||
if (this.someString == null) {
|
||||
if (other.someString != null)
|
||||
return false;
|
||||
} else if (!this.someString.equals(other.someString))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
62
src/test/java/org/json/junit/data/SingletonEnum.java
Normal file
62
src/test/java/org/json/junit/data/SingletonEnum.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
/**
|
||||
* Sample singleton done as an Enum for use with bean testing.
|
||||
*
|
||||
* @author John Aylward
|
||||
*
|
||||
*/
|
||||
public enum SingletonEnum {
|
||||
/**
|
||||
* the singleton instance.
|
||||
*/
|
||||
INSTANCE;
|
||||
/** */
|
||||
private int someInt;
|
||||
/** */
|
||||
private String someString;
|
||||
|
||||
/** single instance. */
|
||||
|
||||
/**
|
||||
* @return the singleton instance. In a real application, I'd hope no one did
|
||||
* this to an enum singleton.
|
||||
*/
|
||||
public static final SingletonEnum getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/** */
|
||||
private SingletonEnum() {
|
||||
}
|
||||
|
||||
/** @return someInt */
|
||||
public int getSomeInt() {
|
||||
return this.someInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets someInt.
|
||||
*
|
||||
* @param someInt
|
||||
* the someInt to set
|
||||
*/
|
||||
public void setSomeInt(int someInt) {
|
||||
this.someInt = someInt;
|
||||
}
|
||||
|
||||
/** @return someString */
|
||||
public String getSomeString() {
|
||||
return this.someString;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets someString.
|
||||
*
|
||||
* @param someString
|
||||
* the someString to set
|
||||
*/
|
||||
public void setSomeString(String someString) {
|
||||
this.someString = someString;
|
||||
}
|
||||
}
|
||||
19
src/test/java/org/json/junit/data/StringsResourceBundle.java
Normal file
19
src/test/java/org/json/junit/data/StringsResourceBundle.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A resource bundle class
|
||||
*/
|
||||
public class StringsResourceBundle extends ListResourceBundle {
|
||||
@Override
|
||||
public Object[][] getContents() {
|
||||
return contents;
|
||||
}
|
||||
static final Object[][] contents = {
|
||||
{"greetings.hello", "Hello, "},
|
||||
{"greetings.world", "World!"},
|
||||
{"farewells.later", "Later, "},
|
||||
{"farewells.gator", "Alligator!"}
|
||||
};
|
||||
}
|
||||
68
src/test/java/org/json/junit/data/WeirdList.java
Normal file
68
src/test/java/org/json/junit/data/WeirdList.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author John Aylward
|
||||
*/
|
||||
public class WeirdList {
|
||||
/** */
|
||||
private final List<Integer> list = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* @param vals
|
||||
*/
|
||||
public WeirdList(Integer... vals) {
|
||||
this.list.addAll(Arrays.asList(vals));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a copy of the list
|
||||
*/
|
||||
public List<Integer> get() {
|
||||
return new ArrayList<>(this.list);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a copy of the list
|
||||
*/
|
||||
public List<Integer> getALL() {
|
||||
return new ArrayList<>(this.list);
|
||||
}
|
||||
|
||||
/**
|
||||
* get a value at an index.
|
||||
*
|
||||
* @param i
|
||||
* index to get
|
||||
* @return the value at the index
|
||||
*/
|
||||
public Integer get(int i) {
|
||||
return this.list.get(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* get a value at an index.
|
||||
*
|
||||
* @param i
|
||||
* index to get
|
||||
* @return the value at the index
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public int getInt(int i) {
|
||||
return this.list.get(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value
|
||||
* new value to add to the end of the list
|
||||
*/
|
||||
public void add(Integer value) {
|
||||
this.list.add(value);
|
||||
}
|
||||
}
|
||||
189
src/test/resources/Issue537.json
Normal file
189
src/test/resources/Issue537.json
Normal file
@@ -0,0 +1,189 @@
|
||||
{
|
||||
"clinical_study": {
|
||||
"brief_summary": {
|
||||
"textblock": "CLEAR SYNERGY is an international multi center 2x2 randomized placebo controlled trial of"
|
||||
},
|
||||
"brief_title": "CLEAR SYNERGY Neutrophil Substudy",
|
||||
"overall_status": "Recruiting",
|
||||
"eligibility": {
|
||||
"study_pop": {
|
||||
"textblock": "Patients who are randomized to the drug RCT portion of the CLEAR SYNERGY (OASIS 9) trial"
|
||||
},
|
||||
"minimum_age": "19 Years",
|
||||
"sampling_method": "Non-Probability Sample",
|
||||
"gender": "All",
|
||||
"criteria": {
|
||||
"textblock": "Inclusion Criteria:"
|
||||
},
|
||||
"healthy_volunteers": "No",
|
||||
"maximum_age": "110 Years"
|
||||
},
|
||||
"number_of_groups": "2",
|
||||
"source": "NYU Langone Health",
|
||||
"location_countries": {
|
||||
"country": "United States"
|
||||
},
|
||||
"study_design_info": {
|
||||
"time_perspective": "Prospective",
|
||||
"observational_model": "Other"
|
||||
},
|
||||
"last_update_submitted_qc": "September 10, 2019",
|
||||
"intervention_browse": {
|
||||
"mesh_term": "Colchicine"
|
||||
},
|
||||
"official_title": "Studies on the Effects of Colchicine on Neutrophil Biology in Acute Myocardial Infarction: A Substudy of the CLEAR SYNERGY (OASIS 9) Trial",
|
||||
"primary_completion_date": {
|
||||
"type": "Anticipated",
|
||||
"content": "February 1, 2021"
|
||||
},
|
||||
"sponsors": {
|
||||
"lead_sponsor": {
|
||||
"agency_class": "Other",
|
||||
"agency": "NYU Langone Health"
|
||||
},
|
||||
"collaborator": [
|
||||
{
|
||||
"agency_class": "Other",
|
||||
"agency": "Population Health Research Institute"
|
||||
},
|
||||
{
|
||||
"agency_class": "NIH",
|
||||
"agency": "National Heart, Lung, and Blood Institute (NHLBI)"
|
||||
}
|
||||
]
|
||||
},
|
||||
"overall_official": {
|
||||
"role": "Principal Investigator",
|
||||
"affiliation": "NYU School of Medicine",
|
||||
"last_name": "Binita Shah, MD"
|
||||
},
|
||||
"overall_contact_backup": {
|
||||
"last_name": "Binita Shah, MD"
|
||||
},
|
||||
"condition_browse": {
|
||||
"mesh_term": [
|
||||
"Myocardial Infarction",
|
||||
"ST Elevation Myocardial Infarction",
|
||||
"Infarction"
|
||||
]
|
||||
},
|
||||
"overall_contact": {
|
||||
"phone": "646-501-9648",
|
||||
"last_name": "Fatmira Curovic",
|
||||
"email": "fatmira.curovic@nyumc.org"
|
||||
},
|
||||
"responsible_party": {
|
||||
"responsible_party_type": "Principal Investigator",
|
||||
"investigator_title": "Assistant Professor of Medicine",
|
||||
"investigator_full_name": "Binita Shah",
|
||||
"investigator_affiliation": "NYU Langone Health"
|
||||
},
|
||||
"study_first_submitted_qc": "March 12, 2019",
|
||||
"start_date": {
|
||||
"type": "Actual",
|
||||
"content": "March 4, 2019"
|
||||
},
|
||||
"has_expanded_access": "No",
|
||||
"study_first_posted": {
|
||||
"type": "Actual",
|
||||
"content": "March 14, 2019"
|
||||
},
|
||||
"arm_group": [
|
||||
{
|
||||
"arm_group_label": "Colchicine"
|
||||
},
|
||||
{
|
||||
"arm_group_label": "Placebo"
|
||||
}
|
||||
],
|
||||
"primary_outcome": {
|
||||
"measure": "soluble L-selectin",
|
||||
"time_frame": "between baseline and 3 months",
|
||||
"description": "Change in soluble L-selectin between baseline and 3 mo after STEMI in the placebo vs. colchicine groups."
|
||||
},
|
||||
"secondary_outcome": [
|
||||
{
|
||||
"measure": "Other soluble markers of neutrophil activity",
|
||||
"time_frame": "between baseline and 3 months",
|
||||
"description": "Other markers of neutrophil activity will be evaluated at baseline and 3 months after STEMI (myeloperoxidase, matrix metalloproteinase-9, neutrophil gelatinase-associated lipocalin, neutrophil elastase, intercellular/vascular cellular adhesion molecules)"
|
||||
},
|
||||
{
|
||||
"measure": "Markers of systemic inflammation",
|
||||
"time_frame": "between baseline and 3 months",
|
||||
"description": "Markers of systemic inflammation will be evaluated at baseline and 3 months after STEMI (high sensitive CRP, IL-1β)"
|
||||
},
|
||||
{
|
||||
"measure": "Neutrophil-driven responses that may further propagate injury",
|
||||
"time_frame": "between baseline and 3 months",
|
||||
"description": "Neutrophil-driven responses that may further propagate injury will be evaluated at baseline and 3 months after STEMI (neutrophil extracellular traps, neutrophil-derived microparticles)"
|
||||
}
|
||||
],
|
||||
"oversight_info": {
|
||||
"is_fda_regulated_drug": "No",
|
||||
"is_fda_regulated_device": "No",
|
||||
"has_dmc": "No"
|
||||
},
|
||||
"last_update_posted": {
|
||||
"type": "Actual",
|
||||
"content": "September 12, 2019"
|
||||
},
|
||||
"id_info": {
|
||||
"nct_id": "NCT03874338",
|
||||
"org_study_id": "18-01323",
|
||||
"secondary_id": "1R01HL146206"
|
||||
},
|
||||
"enrollment": {
|
||||
"type": "Anticipated",
|
||||
"content": "670"
|
||||
},
|
||||
"study_first_submitted": "March 12, 2019",
|
||||
"condition": [
|
||||
"Neutrophils.Hypersegmented | Bld-Ser-Plas",
|
||||
"STEMI - ST Elevation Myocardial Infarction"
|
||||
],
|
||||
"study_type": "Observational",
|
||||
"required_header": {
|
||||
"download_date": "ClinicalTrials.gov processed this data on July 19, 2020",
|
||||
"link_text": "Link to the current ClinicalTrials.gov record.",
|
||||
"url": "https://clinicaltrials.gov/show/NCT03874338"
|
||||
},
|
||||
"last_update_submitted": "September 10, 2019",
|
||||
"completion_date": {
|
||||
"type": "Anticipated",
|
||||
"content": "February 1, 2022"
|
||||
},
|
||||
"location": {
|
||||
"contact": {
|
||||
"phone": "646-501-9648",
|
||||
"last_name": "Fatmira Curovic",
|
||||
"email": "fatmira.curovic@nyumc.org"
|
||||
},
|
||||
"facility": {
|
||||
"address": {
|
||||
"zip": "10016",
|
||||
"country": "United States",
|
||||
"city": "New York",
|
||||
"state": "New York"
|
||||
},
|
||||
"name": "NYU School of Medicine"
|
||||
},
|
||||
"status": "Recruiting",
|
||||
"contact_backup": {
|
||||
"last_name": "Binita Shah, MD"
|
||||
}
|
||||
},
|
||||
"intervention": {
|
||||
"intervention_type": "Drug",
|
||||
"arm_group_label": [
|
||||
"Colchicine",
|
||||
"Placebo"
|
||||
],
|
||||
"description": "Participants in the main CLEAR SYNERGY trial are randomized to colchicine/spironolactone versus placebo in a 2x2 factorial design. The substudy is interested in the evaluation of biospecimens obtained from patients in the colchicine vs placebo group.",
|
||||
"intervention_name": "Colchicine Pill"
|
||||
},
|
||||
"patient_data": {
|
||||
"sharing_ipd": "No"
|
||||
},
|
||||
"verification_date": "September 2019"
|
||||
}
|
||||
}
|
||||
169
src/test/resources/Issue537.xml
Normal file
169
src/test/resources/Issue537.xml
Normal file
@@ -0,0 +1,169 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<clinical_study>
|
||||
<!-- This xml conforms to an XML Schema at:
|
||||
https://clinicaltrials.gov/ct2/html/images/info/public.xsd -->
|
||||
<required_header>
|
||||
<download_date>ClinicalTrials.gov processed this data on July 19, 2020</download_date>
|
||||
<link_text>Link to the current ClinicalTrials.gov record.</link_text>
|
||||
<url>https://clinicaltrials.gov/show/NCT03874338</url>
|
||||
</required_header>
|
||||
<id_info>
|
||||
<org_study_id>18-01323</org_study_id>
|
||||
<secondary_id>1R01HL146206</secondary_id>
|
||||
<nct_id>NCT03874338</nct_id>
|
||||
</id_info>
|
||||
<brief_title>CLEAR SYNERGY Neutrophil Substudy</brief_title>
|
||||
<official_title>Studies on the Effects of Colchicine on Neutrophil Biology in Acute Myocardial Infarction: A Substudy of the CLEAR SYNERGY (OASIS 9) Trial</official_title>
|
||||
<sponsors>
|
||||
<lead_sponsor>
|
||||
<agency>NYU Langone Health</agency>
|
||||
<agency_class>Other</agency_class>
|
||||
</lead_sponsor>
|
||||
<collaborator>
|
||||
<agency>Population Health Research Institute</agency>
|
||||
<agency_class>Other</agency_class>
|
||||
</collaborator>
|
||||
<collaborator>
|
||||
<agency>National Heart, Lung, and Blood Institute (NHLBI)</agency>
|
||||
<agency_class>NIH</agency_class>
|
||||
</collaborator>
|
||||
</sponsors>
|
||||
<source>NYU Langone Health</source>
|
||||
<oversight_info>
|
||||
<has_dmc>No</has_dmc>
|
||||
<is_fda_regulated_drug>No</is_fda_regulated_drug>
|
||||
<is_fda_regulated_device>No</is_fda_regulated_device>
|
||||
</oversight_info>
|
||||
<brief_summary>
|
||||
<textblock>
|
||||
CLEAR SYNERGY is an international multi center 2x2 randomized placebo controlled trial of
|
||||
</textblock>
|
||||
</brief_summary>
|
||||
<overall_status>Recruiting</overall_status>
|
||||
<start_date type="Actual">March 4, 2019</start_date>
|
||||
<completion_date type="Anticipated">February 1, 2022</completion_date>
|
||||
<primary_completion_date type="Anticipated">February 1, 2021</primary_completion_date>
|
||||
<study_type>Observational</study_type>
|
||||
<has_expanded_access>No</has_expanded_access>
|
||||
<study_design_info>
|
||||
<observational_model>Other</observational_model>
|
||||
<time_perspective>Prospective</time_perspective>
|
||||
</study_design_info>
|
||||
<primary_outcome>
|
||||
<measure>soluble L-selectin</measure>
|
||||
<time_frame>between baseline and 3 months</time_frame>
|
||||
<description>Change in soluble L-selectin between baseline and 3 mo after STEMI in the placebo vs. colchicine groups.</description>
|
||||
</primary_outcome>
|
||||
<secondary_outcome>
|
||||
<measure>Other soluble markers of neutrophil activity</measure>
|
||||
<time_frame>between baseline and 3 months</time_frame>
|
||||
<description>Other markers of neutrophil activity will be evaluated at baseline and 3 months after STEMI (myeloperoxidase, matrix metalloproteinase-9, neutrophil gelatinase-associated lipocalin, neutrophil elastase, intercellular/vascular cellular adhesion molecules)</description>
|
||||
</secondary_outcome>
|
||||
<secondary_outcome>
|
||||
<measure>Markers of systemic inflammation</measure>
|
||||
<time_frame>between baseline and 3 months</time_frame>
|
||||
<description>Markers of systemic inflammation will be evaluated at baseline and 3 months after STEMI (high sensitive CRP, IL-1β)</description>
|
||||
</secondary_outcome>
|
||||
<secondary_outcome>
|
||||
<measure>Neutrophil-driven responses that may further propagate injury</measure>
|
||||
<time_frame>between baseline and 3 months</time_frame>
|
||||
<description>Neutrophil-driven responses that may further propagate injury will be evaluated at baseline and 3 months after STEMI (neutrophil extracellular traps, neutrophil-derived microparticles)</description>
|
||||
</secondary_outcome>
|
||||
<number_of_groups>2</number_of_groups>
|
||||
<enrollment type="Anticipated">670</enrollment>
|
||||
<condition>Neutrophils.Hypersegmented | Bld-Ser-Plas</condition>
|
||||
<condition>STEMI - ST Elevation Myocardial Infarction</condition>
|
||||
<arm_group>
|
||||
<arm_group_label>Colchicine</arm_group_label>
|
||||
</arm_group>
|
||||
<arm_group>
|
||||
<arm_group_label>Placebo</arm_group_label>
|
||||
</arm_group>
|
||||
<intervention>
|
||||
<intervention_type>Drug</intervention_type>
|
||||
<intervention_name>Colchicine Pill</intervention_name>
|
||||
<description>Participants in the main CLEAR SYNERGY trial are randomized to colchicine/spironolactone versus placebo in a 2x2 factorial design. The substudy is interested in the evaluation of biospecimens obtained from patients in the colchicine vs placebo group.</description>
|
||||
<arm_group_label>Colchicine</arm_group_label>
|
||||
<arm_group_label>Placebo</arm_group_label>
|
||||
</intervention>
|
||||
<eligibility>
|
||||
<study_pop>
|
||||
<textblock>
|
||||
Patients who are randomized to the drug RCT portion of the CLEAR SYNERGY (OASIS 9) trial
|
||||
</textblock>
|
||||
</study_pop>
|
||||
<sampling_method>Non-Probability Sample</sampling_method>
|
||||
<criteria>
|
||||
<textblock>
|
||||
Inclusion Criteria:
|
||||
</textblock>
|
||||
</criteria>
|
||||
<gender>All</gender>
|
||||
<minimum_age>19 Years</minimum_age>
|
||||
<maximum_age>110 Years</maximum_age>
|
||||
<healthy_volunteers>No</healthy_volunteers>
|
||||
</eligibility>
|
||||
<overall_official>
|
||||
<last_name>Binita Shah, MD</last_name>
|
||||
<role>Principal Investigator</role>
|
||||
<affiliation>NYU School of Medicine</affiliation>
|
||||
</overall_official>
|
||||
<overall_contact>
|
||||
<last_name>Fatmira Curovic</last_name>
|
||||
<phone>646-501-9648</phone>
|
||||
<email>fatmira.curovic@nyumc.org</email>
|
||||
</overall_contact>
|
||||
<overall_contact_backup>
|
||||
<last_name>Binita Shah, MD</last_name>
|
||||
</overall_contact_backup>
|
||||
<location>
|
||||
<facility>
|
||||
<name>NYU School of Medicine</name>
|
||||
<address>
|
||||
<city>New York</city>
|
||||
<state>New York</state>
|
||||
<zip>10016</zip>
|
||||
<country>United States</country>
|
||||
</address>
|
||||
</facility>
|
||||
<status>Recruiting</status>
|
||||
<contact>
|
||||
<last_name>Fatmira Curovic</last_name>
|
||||
<phone>646-501-9648</phone>
|
||||
<email>fatmira.curovic@nyumc.org</email>
|
||||
</contact>
|
||||
<contact_backup>
|
||||
<last_name>Binita Shah, MD</last_name>
|
||||
</contact_backup>
|
||||
</location>
|
||||
<location_countries>
|
||||
<country>United States</country>
|
||||
</location_countries>
|
||||
<verification_date>September 2019</verification_date>
|
||||
<study_first_submitted>March 12, 2019</study_first_submitted>
|
||||
<study_first_submitted_qc>March 12, 2019</study_first_submitted_qc>
|
||||
<study_first_posted type="Actual">March 14, 2019</study_first_posted>
|
||||
<last_update_submitted>September 10, 2019</last_update_submitted>
|
||||
<last_update_submitted_qc>September 10, 2019</last_update_submitted_qc>
|
||||
<last_update_posted type="Actual">September 12, 2019</last_update_posted>
|
||||
<responsible_party>
|
||||
<responsible_party_type>Principal Investigator</responsible_party_type>
|
||||
<investigator_affiliation>NYU Langone Health</investigator_affiliation>
|
||||
<investigator_full_name>Binita Shah</investigator_full_name>
|
||||
<investigator_title>Assistant Professor of Medicine</investigator_title>
|
||||
</responsible_party>
|
||||
<condition_browse>
|
||||
<!-- CAUTION: The following MeSH terms are assigned with an imperfect algorithm -->
|
||||
<mesh_term>Myocardial Infarction</mesh_term>
|
||||
<mesh_term>ST Elevation Myocardial Infarction</mesh_term>
|
||||
<mesh_term>Infarction</mesh_term>
|
||||
</condition_browse>
|
||||
<intervention_browse>
|
||||
<!-- CAUTION: The following MeSH terms are assigned with an imperfect algorithm -->
|
||||
<mesh_term>Colchicine</mesh_term>
|
||||
</intervention_browse>
|
||||
<patient_data>
|
||||
<sharing_ipd>No</sharing_ipd>
|
||||
</patient_data>
|
||||
<!-- Results have not yet been posted for this study -->
|
||||
</clinical_study>
|
||||
704
src/test/resources/Issue593.json
Normal file
704
src/test/resources/Issue593.json
Normal file
@@ -0,0 +1,704 @@
|
||||
{
|
||||
"success": true,
|
||||
"error": null,
|
||||
"response": [
|
||||
{
|
||||
"loc": {
|
||||
"long": 31.25,
|
||||
"lat": 30.063
|
||||
},
|
||||
"interval": "day",
|
||||
"place": {
|
||||
"name": "cairo",
|
||||
"state": "qh",
|
||||
"country": "eg"
|
||||
},
|
||||
"periods": [
|
||||
{
|
||||
"timestamp": 1665032400,
|
||||
"validTime": "2022-10-06T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-06T07:00:00+02:00",
|
||||
"maxTempC": 32,
|
||||
"maxTempF": 90,
|
||||
"minTempC": 19,
|
||||
"minTempF": 66,
|
||||
"avgTempC": 25,
|
||||
"avgTempF": 78,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 32,
|
||||
"maxFeelslikeF": 89,
|
||||
"minFeelslikeC": 21,
|
||||
"minFeelslikeF": 70,
|
||||
"avgFeelslikeC": 26,
|
||||
"avgFeelslikeF": 80,
|
||||
"feelslikeC": 21,
|
||||
"feelslikeF": 70,
|
||||
"maxDewpointC": 17,
|
||||
"maxDewpointF": 63,
|
||||
"minDewpointC": 11,
|
||||
"minDewpointF": 52,
|
||||
"avgDewpointC": 14,
|
||||
"avgDewpointF": 58,
|
||||
"dewpointC": 17,
|
||||
"dewpointF": 63,
|
||||
"maxHumidity": 77,
|
||||
"minHumidity": 29,
|
||||
"humidity": 77,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1015,
|
||||
"pressureIN": 29.97,
|
||||
"windDir": "N",
|
||||
"windDirDEG": 353,
|
||||
"windSpeedKTS": 5,
|
||||
"windSpeedKPH": 9,
|
||||
"windSpeedMPH": 6,
|
||||
"windGustKTS": 21,
|
||||
"windGustKPH": 40,
|
||||
"windGustMPH": 25,
|
||||
"windDirMax": "NNW",
|
||||
"windDirMaxDEG": 342,
|
||||
"windSpeedMaxKTS": 9,
|
||||
"windSpeedMaxKPH": 16,
|
||||
"windSpeedMaxMPH": 10,
|
||||
"windDirMin": "N",
|
||||
"windDirMinDEG": 353,
|
||||
"windSpeedMinKTS": 1,
|
||||
"windSpeedMinKPH": 2,
|
||||
"windSpeedMinMPH": 1,
|
||||
"windDir80m": "N",
|
||||
"windDir80mDEG": 11,
|
||||
"windSpeed80mKTS": 12,
|
||||
"windSpeed80mKPH": 22,
|
||||
"windSpeed80mMPH": 13,
|
||||
"windGust80mKTS": 22,
|
||||
"windGust80mKPH": 41,
|
||||
"windGust80mMPH": 25,
|
||||
"windDirMax80m": "NNW",
|
||||
"windDirMax80mDEG": 343,
|
||||
"windSpeedMax80mKTS": 22,
|
||||
"windSpeedMax80mKPH": 41,
|
||||
"windSpeedMax80mMPH": 25,
|
||||
"windDirMin80m": "E",
|
||||
"windDirMin80mDEG": 95,
|
||||
"windSpeedMin80mKTS": 8,
|
||||
"windSpeedMin80mKPH": 15,
|
||||
"windSpeedMin80mMPH": 10,
|
||||
"sky": 22,
|
||||
"cloudsCoded": "FW",
|
||||
"weather": "Mostly Sunny",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Mostly Sunny",
|
||||
"weatherPrimaryCoded": "::FW",
|
||||
"icon": "fair.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": 6,
|
||||
"solradWM2": 5608,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 778,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665028274,
|
||||
"sunset": 1665070502,
|
||||
"sunriseISO": "2022-10-06T05:51:14+02:00",
|
||||
"sunsetISO": "2022-10-06T17:35:02+02:00"
|
||||
},
|
||||
{
|
||||
"timestamp": 1665118800,
|
||||
"validTime": "2022-10-07T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-07T07:00:00+02:00",
|
||||
"maxTempC": 30,
|
||||
"maxTempF": 86,
|
||||
"minTempC": 19,
|
||||
"minTempF": 66,
|
||||
"avgTempC": 24,
|
||||
"avgTempF": 76,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 29,
|
||||
"maxFeelslikeF": 85,
|
||||
"minFeelslikeC": 19,
|
||||
"minFeelslikeF": 67,
|
||||
"avgFeelslikeC": 24,
|
||||
"avgFeelslikeF": 76,
|
||||
"feelslikeC": 19,
|
||||
"feelslikeF": 67,
|
||||
"maxDewpointC": 15,
|
||||
"maxDewpointF": 60,
|
||||
"minDewpointC": 10,
|
||||
"minDewpointF": 50,
|
||||
"avgDewpointC": 12,
|
||||
"avgDewpointF": 54,
|
||||
"dewpointC": 15,
|
||||
"dewpointF": 60,
|
||||
"maxHumidity": 77,
|
||||
"minHumidity": 30,
|
||||
"humidity": 77,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1014,
|
||||
"pressureIN": 29.95,
|
||||
"windDir": "NW",
|
||||
"windDirDEG": 325,
|
||||
"windSpeedKTS": 1,
|
||||
"windSpeedKPH": 2,
|
||||
"windSpeedMPH": 1,
|
||||
"windGustKTS": 16,
|
||||
"windGustKPH": 29,
|
||||
"windGustMPH": 18,
|
||||
"windDirMax": "WNW",
|
||||
"windDirMaxDEG": 298,
|
||||
"windSpeedMaxKTS": 7,
|
||||
"windSpeedMaxKPH": 13,
|
||||
"windSpeedMaxMPH": 8,
|
||||
"windDirMin": "NW",
|
||||
"windDirMinDEG": 325,
|
||||
"windSpeedMinKTS": 1,
|
||||
"windSpeedMinKPH": 2,
|
||||
"windSpeedMinMPH": 1,
|
||||
"windDir80m": "NNW",
|
||||
"windDir80mDEG": 347,
|
||||
"windSpeed80mKTS": 6,
|
||||
"windSpeed80mKPH": 10,
|
||||
"windSpeed80mMPH": 6,
|
||||
"windGust80mKTS": 20,
|
||||
"windGust80mKPH": 37,
|
||||
"windGust80mMPH": 23,
|
||||
"windDirMax80m": "NW",
|
||||
"windDirMax80mDEG": 316,
|
||||
"windSpeedMax80mKTS": 20,
|
||||
"windSpeedMax80mKPH": 37,
|
||||
"windSpeedMax80mMPH": 23,
|
||||
"windDirMin80m": "NNW",
|
||||
"windDirMin80mDEG": 347,
|
||||
"windSpeedMin80mKTS": 6,
|
||||
"windSpeedMin80mKPH": 10,
|
||||
"windSpeedMin80mMPH": 6,
|
||||
"sky": 30,
|
||||
"cloudsCoded": "FW",
|
||||
"weather": "Mostly Sunny",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Mostly Sunny",
|
||||
"weatherPrimaryCoded": "::FW",
|
||||
"icon": "fair.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": 6,
|
||||
"solradWM2": 5486,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 742,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665114710,
|
||||
"sunset": 1665156831,
|
||||
"sunriseISO": "2022-10-07T05:51:50+02:00",
|
||||
"sunsetISO": "2022-10-07T17:33:51+02:00"
|
||||
},
|
||||
{
|
||||
"timestamp": 1665205200,
|
||||
"validTime": "2022-10-08T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-08T07:00:00+02:00",
|
||||
"maxTempC": 30,
|
||||
"maxTempF": 87,
|
||||
"minTempC": 19,
|
||||
"minTempF": 66,
|
||||
"avgTempC": 25,
|
||||
"avgTempF": 76,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 30,
|
||||
"maxFeelslikeF": 86,
|
||||
"minFeelslikeC": 19,
|
||||
"minFeelslikeF": 67,
|
||||
"avgFeelslikeC": 25,
|
||||
"avgFeelslikeF": 76,
|
||||
"feelslikeC": 19,
|
||||
"feelslikeF": 67,
|
||||
"maxDewpointC": 15,
|
||||
"maxDewpointF": 59,
|
||||
"minDewpointC": 11,
|
||||
"minDewpointF": 52,
|
||||
"avgDewpointC": 13,
|
||||
"avgDewpointF": 56,
|
||||
"dewpointC": 15,
|
||||
"dewpointF": 59,
|
||||
"maxHumidity": 76,
|
||||
"minHumidity": 32,
|
||||
"humidity": 76,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1014,
|
||||
"pressureIN": 29.94,
|
||||
"windDir": "NNE",
|
||||
"windDirDEG": 21,
|
||||
"windSpeedKTS": 1,
|
||||
"windSpeedKPH": 2,
|
||||
"windSpeedMPH": 1,
|
||||
"windGustKTS": 17,
|
||||
"windGustKPH": 32,
|
||||
"windGustMPH": 20,
|
||||
"windDirMax": "WNW",
|
||||
"windDirMaxDEG": 301,
|
||||
"windSpeedMaxKTS": 7,
|
||||
"windSpeedMaxKPH": 13,
|
||||
"windSpeedMaxMPH": 8,
|
||||
"windDirMin": "NNE",
|
||||
"windDirMinDEG": 21,
|
||||
"windSpeedMinKTS": 1,
|
||||
"windSpeedMinKPH": 2,
|
||||
"windSpeedMinMPH": 1,
|
||||
"windDir80m": "NW",
|
||||
"windDir80mDEG": 309,
|
||||
"windSpeed80mKTS": 5,
|
||||
"windSpeed80mKPH": 9,
|
||||
"windSpeed80mMPH": 5,
|
||||
"windGust80mKTS": 17,
|
||||
"windGust80mKPH": 31,
|
||||
"windGust80mMPH": 19,
|
||||
"windDirMax80m": "NW",
|
||||
"windDirMax80mDEG": 322,
|
||||
"windSpeedMax80mKTS": 17,
|
||||
"windSpeedMax80mKPH": 31,
|
||||
"windSpeedMax80mMPH": 19,
|
||||
"windDirMin80m": "NW",
|
||||
"windDirMin80mDEG": 309,
|
||||
"windSpeedMin80mKTS": 5,
|
||||
"windSpeedMin80mKPH": 9,
|
||||
"windSpeedMin80mMPH": 5,
|
||||
"sky": 47,
|
||||
"cloudsCoded": "SC",
|
||||
"weather": "Partly Cloudy",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Partly Cloudy",
|
||||
"weatherPrimaryCoded": "::SC",
|
||||
"icon": "pcloudy.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": 7,
|
||||
"solradWM2": 4785,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 682,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665201146,
|
||||
"sunset": 1665243161,
|
||||
"sunriseISO": "2022-10-08T05:52:26+02:00",
|
||||
"sunsetISO": "2022-10-08T17:32:41+02:00"
|
||||
},
|
||||
{
|
||||
"timestamp": 1665291600,
|
||||
"validTime": "2022-10-09T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-09T07:00:00+02:00",
|
||||
"maxTempC": 31,
|
||||
"maxTempF": 87,
|
||||
"minTempC": 19,
|
||||
"minTempF": 67,
|
||||
"avgTempC": 25,
|
||||
"avgTempF": 77,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 30,
|
||||
"maxFeelslikeF": 86,
|
||||
"minFeelslikeC": 20,
|
||||
"minFeelslikeF": 67,
|
||||
"avgFeelslikeC": 25,
|
||||
"avgFeelslikeF": 77,
|
||||
"feelslikeC": 20,
|
||||
"feelslikeF": 67,
|
||||
"maxDewpointC": 17,
|
||||
"maxDewpointF": 63,
|
||||
"minDewpointC": 11,
|
||||
"minDewpointF": 52,
|
||||
"avgDewpointC": 14,
|
||||
"avgDewpointF": 57,
|
||||
"dewpointC": 17,
|
||||
"dewpointF": 63,
|
||||
"maxHumidity": 86,
|
||||
"minHumidity": 31,
|
||||
"humidity": 86,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1016,
|
||||
"pressureIN": 29.99,
|
||||
"windDir": "N",
|
||||
"windDirDEG": 356,
|
||||
"windSpeedKTS": 2,
|
||||
"windSpeedKPH": 4,
|
||||
"windSpeedMPH": 2,
|
||||
"windGustKTS": 19,
|
||||
"windGustKPH": 36,
|
||||
"windGustMPH": 22,
|
||||
"windDirMax": "NNW",
|
||||
"windDirMaxDEG": 343,
|
||||
"windSpeedMaxKTS": 8,
|
||||
"windSpeedMaxKPH": 14,
|
||||
"windSpeedMaxMPH": 9,
|
||||
"windDirMin": "N",
|
||||
"windDirMinDEG": 356,
|
||||
"windSpeedMinKTS": 2,
|
||||
"windSpeedMinKPH": 4,
|
||||
"windSpeedMinMPH": 2,
|
||||
"windDir80m": "NW",
|
||||
"windDir80mDEG": 316,
|
||||
"windSpeed80mKTS": 5,
|
||||
"windSpeed80mKPH": 9,
|
||||
"windSpeed80mMPH": 6,
|
||||
"windGust80mKTS": 20,
|
||||
"windGust80mKPH": 36,
|
||||
"windGust80mMPH": 23,
|
||||
"windDirMax80m": "N",
|
||||
"windDirMax80mDEG": 354,
|
||||
"windSpeedMax80mKTS": 20,
|
||||
"windSpeedMax80mKPH": 36,
|
||||
"windSpeedMax80mMPH": 23,
|
||||
"windDirMin80m": "NW",
|
||||
"windDirMin80mDEG": 316,
|
||||
"windSpeedMin80mKTS": 5,
|
||||
"windSpeedMin80mKPH": 9,
|
||||
"windSpeedMin80mMPH": 6,
|
||||
"sky": 47,
|
||||
"cloudsCoded": "SC",
|
||||
"weather": "Partly Cloudy",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Partly Cloudy",
|
||||
"weatherPrimaryCoded": "::SC",
|
||||
"icon": "pcloudy.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": 7,
|
||||
"solradWM2": 4768,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 726,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665287583,
|
||||
"sunset": 1665329491,
|
||||
"sunriseISO": "2022-10-09T05:53:03+02:00",
|
||||
"sunsetISO": "2022-10-09T17:31:31+02:00"
|
||||
},
|
||||
{
|
||||
"timestamp": 1665378000,
|
||||
"validTime": "2022-10-10T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-10T07:00:00+02:00",
|
||||
"maxTempC": 31,
|
||||
"maxTempF": 87,
|
||||
"minTempC": 21,
|
||||
"minTempF": 70,
|
||||
"avgTempC": 26,
|
||||
"avgTempF": 78,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 30,
|
||||
"maxFeelslikeF": 86,
|
||||
"minFeelslikeC": 21,
|
||||
"minFeelslikeF": 69,
|
||||
"avgFeelslikeC": 25,
|
||||
"avgFeelslikeF": 78,
|
||||
"feelslikeC": 21,
|
||||
"feelslikeF": 69,
|
||||
"maxDewpointC": 16,
|
||||
"maxDewpointF": 61,
|
||||
"minDewpointC": 13,
|
||||
"minDewpointF": 55,
|
||||
"avgDewpointC": 14,
|
||||
"avgDewpointF": 58,
|
||||
"dewpointC": 16,
|
||||
"dewpointF": 61,
|
||||
"maxHumidity": 75,
|
||||
"minHumidity": 35,
|
||||
"humidity": 75,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1017,
|
||||
"pressureIN": 30.03,
|
||||
"windDir": "N",
|
||||
"windDirDEG": 358,
|
||||
"windSpeedKTS": 2,
|
||||
"windSpeedKPH": 4,
|
||||
"windSpeedMPH": 2,
|
||||
"windGustKTS": 16,
|
||||
"windGustKPH": 30,
|
||||
"windGustMPH": 19,
|
||||
"windDirMax": "N",
|
||||
"windDirMaxDEG": 10,
|
||||
"windSpeedMaxKTS": 8,
|
||||
"windSpeedMaxKPH": 15,
|
||||
"windSpeedMaxMPH": 9,
|
||||
"windDirMin": "N",
|
||||
"windDirMinDEG": 358,
|
||||
"windSpeedMinKTS": 2,
|
||||
"windSpeedMinKPH": 4,
|
||||
"windSpeedMinMPH": 2,
|
||||
"windDir80m": "N",
|
||||
"windDir80mDEG": 8,
|
||||
"windSpeed80mKTS": 7,
|
||||
"windSpeed80mKPH": 13,
|
||||
"windSpeed80mMPH": 8,
|
||||
"windGust80mKTS": 19,
|
||||
"windGust80mKPH": 36,
|
||||
"windGust80mMPH": 22,
|
||||
"windDirMax80m": "N",
|
||||
"windDirMax80mDEG": 10,
|
||||
"windSpeedMax80mKTS": 19,
|
||||
"windSpeedMax80mKPH": 36,
|
||||
"windSpeedMax80mMPH": 22,
|
||||
"windDirMin80m": "E",
|
||||
"windDirMin80mDEG": 91,
|
||||
"windSpeedMin80mKTS": 7,
|
||||
"windSpeedMin80mKPH": 13,
|
||||
"windSpeedMin80mMPH": 8,
|
||||
"sky": 64,
|
||||
"cloudsCoded": "SC",
|
||||
"weather": "Partly Cloudy",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Partly Cloudy",
|
||||
"weatherPrimaryCoded": "::SC",
|
||||
"icon": "pcloudy.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": 6,
|
||||
"solradWM2": 4494,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 597,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665374020,
|
||||
"sunset": 1665415821,
|
||||
"sunriseISO": "2022-10-10T05:53:40+02:00",
|
||||
"sunsetISO": "2022-10-10T17:30:21+02:00"
|
||||
},
|
||||
{
|
||||
"timestamp": 1665464400,
|
||||
"validTime": "2022-10-11T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-11T07:00:00+02:00",
|
||||
"maxTempC": 31,
|
||||
"maxTempF": 87,
|
||||
"minTempC": 21,
|
||||
"minTempF": 70,
|
||||
"avgTempC": 26,
|
||||
"avgTempF": 78,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 31,
|
||||
"maxFeelslikeF": 87,
|
||||
"minFeelslikeC": 22,
|
||||
"minFeelslikeF": 72,
|
||||
"avgFeelslikeC": 26,
|
||||
"avgFeelslikeF": 79,
|
||||
"feelslikeC": 22,
|
||||
"feelslikeF": 72,
|
||||
"maxDewpointC": 17,
|
||||
"maxDewpointF": 62,
|
||||
"minDewpointC": 11,
|
||||
"minDewpointF": 51,
|
||||
"avgDewpointC": 13,
|
||||
"avgDewpointF": 55,
|
||||
"dewpointC": 17,
|
||||
"dewpointF": 62,
|
||||
"maxHumidity": 71,
|
||||
"minHumidity": 30,
|
||||
"humidity": 71,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1015,
|
||||
"pressureIN": 29.98,
|
||||
"windDir": "NNE",
|
||||
"windDirDEG": 13,
|
||||
"windSpeedKTS": 8,
|
||||
"windSpeedKPH": 15,
|
||||
"windSpeedMPH": 9,
|
||||
"windGustKTS": 15,
|
||||
"windGustKPH": 28,
|
||||
"windGustMPH": 17,
|
||||
"windDirMax": "NNE",
|
||||
"windDirMaxDEG": 28,
|
||||
"windSpeedMaxKTS": 15,
|
||||
"windSpeedMaxKPH": 28,
|
||||
"windSpeedMaxMPH": 18,
|
||||
"windDirMin": "NNE",
|
||||
"windDirMinDEG": 14,
|
||||
"windSpeedMinKTS": 7,
|
||||
"windSpeedMinKPH": 14,
|
||||
"windSpeedMinMPH": 8,
|
||||
"windDir80m": "NNE",
|
||||
"windDir80mDEG": 16,
|
||||
"windSpeed80mKTS": 10,
|
||||
"windSpeed80mKPH": 19,
|
||||
"windSpeed80mMPH": 12,
|
||||
"windGust80mKTS": 17,
|
||||
"windGust80mKPH": 31,
|
||||
"windGust80mMPH": 19,
|
||||
"windDirMax80m": "NNE",
|
||||
"windDirMax80mDEG": 28,
|
||||
"windSpeedMax80mKTS": 17,
|
||||
"windSpeedMax80mKPH": 31,
|
||||
"windSpeedMax80mMPH": 19,
|
||||
"windDirMin80m": "NNE",
|
||||
"windDirMin80mDEG": 13,
|
||||
"windSpeedMin80mKTS": 9,
|
||||
"windSpeedMin80mKPH": 18,
|
||||
"windSpeedMin80mMPH": 11,
|
||||
"sky": 0,
|
||||
"cloudsCoded": "CL",
|
||||
"weather": "Sunny",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Sunny",
|
||||
"weatherPrimaryCoded": "::CL",
|
||||
"icon": "sunny.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": null,
|
||||
"solradWM2": 5450,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 758,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665460458,
|
||||
"sunset": 1665502153,
|
||||
"sunriseISO": "2022-10-11T05:54:18+02:00",
|
||||
"sunsetISO": "2022-10-11T17:29:13+02:00"
|
||||
},
|
||||
{
|
||||
"timestamp": 1665550800,
|
||||
"validTime": "2022-10-12T07:00:00+02:00",
|
||||
"dateTimeISO": "2022-10-12T07:00:00+02:00",
|
||||
"maxTempC": 31,
|
||||
"maxTempF": 88,
|
||||
"minTempC": 21,
|
||||
"minTempF": 69,
|
||||
"avgTempC": 26,
|
||||
"avgTempF": 79,
|
||||
"tempC": null,
|
||||
"tempF": null,
|
||||
"maxFeelslikeC": 31,
|
||||
"maxFeelslikeF": 88,
|
||||
"minFeelslikeC": 22,
|
||||
"minFeelslikeF": 72,
|
||||
"avgFeelslikeC": 26,
|
||||
"avgFeelslikeF": 80,
|
||||
"feelslikeC": 22,
|
||||
"feelslikeF": 72,
|
||||
"maxDewpointC": 16,
|
||||
"maxDewpointF": 60,
|
||||
"minDewpointC": 11,
|
||||
"minDewpointF": 51,
|
||||
"avgDewpointC": 13,
|
||||
"avgDewpointF": 55,
|
||||
"dewpointC": 16,
|
||||
"dewpointF": 60,
|
||||
"maxHumidity": 68,
|
||||
"minHumidity": 29,
|
||||
"humidity": 68,
|
||||
"pop": 0,
|
||||
"precipMM": 0,
|
||||
"precipIN": 0,
|
||||
"iceaccum": null,
|
||||
"iceaccumMM": null,
|
||||
"iceaccumIN": null,
|
||||
"snowCM": 0,
|
||||
"snowIN": 0,
|
||||
"pressureMB": 1014,
|
||||
"pressureIN": 29.95,
|
||||
"windDir": "NNE",
|
||||
"windDirDEG": 12,
|
||||
"windSpeedKTS": 8,
|
||||
"windSpeedKPH": 15,
|
||||
"windSpeedMPH": 9,
|
||||
"windGustKTS": 15,
|
||||
"windGustKPH": 28,
|
||||
"windGustMPH": 17,
|
||||
"windDirMax": "E",
|
||||
"windDirMaxDEG": 96,
|
||||
"windSpeedMaxKTS": 14,
|
||||
"windSpeedMaxKPH": 26,
|
||||
"windSpeedMaxMPH": 16,
|
||||
"windDirMin": "NNE",
|
||||
"windDirMinDEG": 12,
|
||||
"windSpeedMinKTS": 7,
|
||||
"windSpeedMinKPH": 13,
|
||||
"windSpeedMinMPH": 8,
|
||||
"windDir80m": "NNE",
|
||||
"windDir80mDEG": 15,
|
||||
"windSpeed80mKTS": 10,
|
||||
"windSpeed80mKPH": 19,
|
||||
"windSpeed80mMPH": 12,
|
||||
"windGust80mKTS": 18,
|
||||
"windGust80mKPH": 33,
|
||||
"windGust80mMPH": 21,
|
||||
"windDirMax80m": "E",
|
||||
"windDirMax80mDEG": 96,
|
||||
"windSpeedMax80mKTS": 18,
|
||||
"windSpeedMax80mKPH": 33,
|
||||
"windSpeedMax80mMPH": 21,
|
||||
"windDirMin80m": "NNE",
|
||||
"windDirMin80mDEG": 15,
|
||||
"windSpeedMin80mKTS": 10,
|
||||
"windSpeedMin80mKPH": 18,
|
||||
"windSpeedMin80mMPH": 11,
|
||||
"sky": 27,
|
||||
"cloudsCoded": "FW",
|
||||
"weather": "Mostly Sunny",
|
||||
"weatherCoded": [],
|
||||
"weatherPrimary": "Mostly Sunny",
|
||||
"weatherPrimaryCoded": "::FW",
|
||||
"icon": "fair.png",
|
||||
"visibilityKM": 24.135,
|
||||
"visibilityMI": 15,
|
||||
"uvi": null,
|
||||
"solradWM2": 4740,
|
||||
"solradMinWM2": 0,
|
||||
"solradMaxWM2": 743,
|
||||
"isDay": true,
|
||||
"maxCoverage": "",
|
||||
"sunrise": 1665546895,
|
||||
"sunset": 1665588484,
|
||||
"sunriseISO": "2022-10-12T05:54:55+02:00",
|
||||
"sunsetISO": "2022-10-12T17:28:04+02:00"
|
||||
}
|
||||
],
|
||||
"profile": {
|
||||
"tz": "Africa/Cairo",
|
||||
"elevM": 23,
|
||||
"elevFT": 75
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
691
src/test/resources/Issue593.xml
Normal file
691
src/test/resources/Issue593.xml
Normal file
@@ -0,0 +1,691 @@
|
||||
<success>true</success>
|
||||
<response>
|
||||
<loc>
|
||||
<long>31.25</long>
|
||||
<lat>30.063</lat>
|
||||
</loc>
|
||||
<profile>
|
||||
<elevM>23</elevM>
|
||||
<tz>Africa/Cairo</tz>
|
||||
<elevFT>75</elevFT>
|
||||
</profile>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-06T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>E</windDirMin80m>
|
||||
<windDirMin80mDEG>95</windDirMin80mDEG>
|
||||
<feelslikeC>21</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>10</windSpeedMaxMPH>
|
||||
<windDirDEG>353</windDirDEG>
|
||||
<windDir>N</windDir>
|
||||
<sunriseISO>2022-10-06T05:51:14+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>9</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>66</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Mostly Sunny</weather>
|
||||
<sunsetISO>2022-10-06T17:35:02+02:00</sunsetISO>
|
||||
<maxFeelslikeC>32</maxFeelslikeC>
|
||||
<humidity>77</humidity>
|
||||
<windDir80m>N</windDir80m>
|
||||
<maxFeelslikeF>89</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>22</sky>
|
||||
<windGust80mMPH>25</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>25</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Mostly Sunny</weatherPrimary>
|
||||
<windGust80mKPH>41</windGust80mKPH>
|
||||
<avgDewpointF>58</avgDewpointF>
|
||||
<windSpeedMax80mKPH>41</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>22</windGust80mKTS>
|
||||
<avgDewpointC>14</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>22</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>353</windDirMinDEG>
|
||||
<windSpeedMaxKPH>16</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>8</windSpeedMin80mKTS>
|
||||
<feelslikeF>70</feelslikeF>
|
||||
<validTime>2022-10-06T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>10</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>778</solradMaxWM2>
|
||||
<avgTempC>25</avgTempC>
|
||||
<windSpeedMin80mKPH>15</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::FW</weatherPrimaryCoded>
|
||||
<sunrise>1665028274</sunrise>
|
||||
<avgTempF>78</avgTempF>
|
||||
<windDirMin>N</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>fair.png</icon>
|
||||
<minFeelslikeC>21</minFeelslikeC>
|
||||
<dewpointC>17</dewpointC>
|
||||
<cloudsCoded>FW</cloudsCoded>
|
||||
<minFeelslikeF>70</minFeelslikeF>
|
||||
<minHumidity>29</minHumidity>
|
||||
<dewpointF>63</dewpointF>
|
||||
<windSpeed80mKTS>12</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>NNW</windDirMax>
|
||||
<windSpeed80mMPH>13</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>22</windSpeed80mKPH>
|
||||
<windDir80mDEG>11</windDir80mDEG>
|
||||
<maxTempC>32</maxTempC>
|
||||
<pressureMB>1015</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665032400</timestamp>
|
||||
<maxTempF>90</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>11</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>1</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>343</windDirMax80mDEG>
|
||||
<windGustKTS>21</windGustKTS>
|
||||
<windSpeedMinKPH>2</windSpeedMinKPH>
|
||||
<maxDewpointF>63</maxDewpointF>
|
||||
<windSpeedMinMPH>1</windSpeedMinMPH>
|
||||
<avgFeelslikeC>26</avgFeelslikeC>
|
||||
<uvi>6</uvi>
|
||||
<windDirMax80m>NNW</windDirMax80m>
|
||||
<maxDewpointC>17</maxDewpointC>
|
||||
<pressureIN>29.97</pressureIN>
|
||||
<avgFeelslikeF>80</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>19</minTempC>
|
||||
<minDewpointF>52</minDewpointF>
|
||||
<windSpeedKTS>5</windSpeedKTS>
|
||||
<sunset>1665070502</sunset>
|
||||
<solradWM2>5608</solradWM2>
|
||||
<windSpeedKPH>9</windSpeedKPH>
|
||||
<windGustMPH>25</windGustMPH>
|
||||
<maxHumidity>77</maxHumidity>
|
||||
<windSpeedMPH>6</windSpeedMPH>
|
||||
<windGustKPH>40</windGustKPH>
|
||||
<windDirMaxDEG>342</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-07T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>NNW</windDirMin80m>
|
||||
<windDirMin80mDEG>347</windDirMin80mDEG>
|
||||
<feelslikeC>19</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>8</windSpeedMaxMPH>
|
||||
<windDirDEG>325</windDirDEG>
|
||||
<windDir>NW</windDir>
|
||||
<sunriseISO>2022-10-07T05:51:50+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>7</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>66</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Mostly Sunny</weather>
|
||||
<sunsetISO>2022-10-07T17:33:51+02:00</sunsetISO>
|
||||
<maxFeelslikeC>29</maxFeelslikeC>
|
||||
<humidity>77</humidity>
|
||||
<windDir80m>NNW</windDir80m>
|
||||
<maxFeelslikeF>85</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>30</sky>
|
||||
<windGust80mMPH>23</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>23</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Mostly Sunny</weatherPrimary>
|
||||
<windGust80mKPH>37</windGust80mKPH>
|
||||
<avgDewpointF>54</avgDewpointF>
|
||||
<windSpeedMax80mKPH>37</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>20</windGust80mKTS>
|
||||
<avgDewpointC>12</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>20</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>325</windDirMinDEG>
|
||||
<windSpeedMaxKPH>13</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>6</windSpeedMin80mKTS>
|
||||
<feelslikeF>67</feelslikeF>
|
||||
<validTime>2022-10-07T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>6</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>742</solradMaxWM2>
|
||||
<avgTempC>24</avgTempC>
|
||||
<windSpeedMin80mKPH>10</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::FW</weatherPrimaryCoded>
|
||||
<sunrise>1665114710</sunrise>
|
||||
<avgTempF>76</avgTempF>
|
||||
<windDirMin>NW</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>fair.png</icon>
|
||||
<minFeelslikeC>19</minFeelslikeC>
|
||||
<dewpointC>15</dewpointC>
|
||||
<cloudsCoded>FW</cloudsCoded>
|
||||
<minFeelslikeF>67</minFeelslikeF>
|
||||
<minHumidity>30</minHumidity>
|
||||
<dewpointF>60</dewpointF>
|
||||
<windSpeed80mKTS>6</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>WNW</windDirMax>
|
||||
<windSpeed80mMPH>6</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>10</windSpeed80mKPH>
|
||||
<windDir80mDEG>347</windDir80mDEG>
|
||||
<maxTempC>30</maxTempC>
|
||||
<pressureMB>1014</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665118800</timestamp>
|
||||
<maxTempF>86</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>10</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>1</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>316</windDirMax80mDEG>
|
||||
<windGustKTS>16</windGustKTS>
|
||||
<windSpeedMinKPH>2</windSpeedMinKPH>
|
||||
<maxDewpointF>60</maxDewpointF>
|
||||
<windSpeedMinMPH>1</windSpeedMinMPH>
|
||||
<avgFeelslikeC>24</avgFeelslikeC>
|
||||
<uvi>6</uvi>
|
||||
<windDirMax80m>NW</windDirMax80m>
|
||||
<maxDewpointC>15</maxDewpointC>
|
||||
<pressureIN>29.95</pressureIN>
|
||||
<avgFeelslikeF>76</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>19</minTempC>
|
||||
<minDewpointF>50</minDewpointF>
|
||||
<windSpeedKTS>1</windSpeedKTS>
|
||||
<sunset>1665156831</sunset>
|
||||
<solradWM2>5486</solradWM2>
|
||||
<windSpeedKPH>2</windSpeedKPH>
|
||||
<windGustMPH>18</windGustMPH>
|
||||
<maxHumidity>77</maxHumidity>
|
||||
<windSpeedMPH>1</windSpeedMPH>
|
||||
<windGustKPH>29</windGustKPH>
|
||||
<windDirMaxDEG>298</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-08T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>NW</windDirMin80m>
|
||||
<windDirMin80mDEG>309</windDirMin80mDEG>
|
||||
<feelslikeC>19</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>8</windSpeedMaxMPH>
|
||||
<windDirDEG>21</windDirDEG>
|
||||
<windDir>NNE</windDir>
|
||||
<sunriseISO>2022-10-08T05:52:26+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>7</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>66</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Partly Cloudy</weather>
|
||||
<sunsetISO>2022-10-08T17:32:41+02:00</sunsetISO>
|
||||
<maxFeelslikeC>30</maxFeelslikeC>
|
||||
<humidity>76</humidity>
|
||||
<windDir80m>NW</windDir80m>
|
||||
<maxFeelslikeF>86</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>47</sky>
|
||||
<windGust80mMPH>19</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>19</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Partly Cloudy</weatherPrimary>
|
||||
<windGust80mKPH>31</windGust80mKPH>
|
||||
<avgDewpointF>56</avgDewpointF>
|
||||
<windSpeedMax80mKPH>31</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>17</windGust80mKTS>
|
||||
<avgDewpointC>13</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>17</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>21</windDirMinDEG>
|
||||
<windSpeedMaxKPH>13</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>5</windSpeedMin80mKTS>
|
||||
<feelslikeF>67</feelslikeF>
|
||||
<validTime>2022-10-08T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>5</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>682</solradMaxWM2>
|
||||
<avgTempC>25</avgTempC>
|
||||
<windSpeedMin80mKPH>9</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::SC</weatherPrimaryCoded>
|
||||
<sunrise>1665201146</sunrise>
|
||||
<avgTempF>76</avgTempF>
|
||||
<windDirMin>NNE</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>pcloudy.png</icon>
|
||||
<minFeelslikeC>19</minFeelslikeC>
|
||||
<dewpointC>15</dewpointC>
|
||||
<cloudsCoded>SC</cloudsCoded>
|
||||
<minFeelslikeF>67</minFeelslikeF>
|
||||
<minHumidity>32</minHumidity>
|
||||
<dewpointF>59</dewpointF>
|
||||
<windSpeed80mKTS>5</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>WNW</windDirMax>
|
||||
<windSpeed80mMPH>5</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>9</windSpeed80mKPH>
|
||||
<windDir80mDEG>309</windDir80mDEG>
|
||||
<maxTempC>30</maxTempC>
|
||||
<pressureMB>1014</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665205200</timestamp>
|
||||
<maxTempF>87</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>11</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>1</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>322</windDirMax80mDEG>
|
||||
<windGustKTS>17</windGustKTS>
|
||||
<windSpeedMinKPH>2</windSpeedMinKPH>
|
||||
<maxDewpointF>59</maxDewpointF>
|
||||
<windSpeedMinMPH>1</windSpeedMinMPH>
|
||||
<avgFeelslikeC>25</avgFeelslikeC>
|
||||
<uvi>7</uvi>
|
||||
<windDirMax80m>NW</windDirMax80m>
|
||||
<maxDewpointC>15</maxDewpointC>
|
||||
<pressureIN>29.94</pressureIN>
|
||||
<avgFeelslikeF>76</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>19</minTempC>
|
||||
<minDewpointF>52</minDewpointF>
|
||||
<windSpeedKTS>1</windSpeedKTS>
|
||||
<sunset>1665243161</sunset>
|
||||
<solradWM2>4785</solradWM2>
|
||||
<windSpeedKPH>2</windSpeedKPH>
|
||||
<windGustMPH>20</windGustMPH>
|
||||
<maxHumidity>76</maxHumidity>
|
||||
<windSpeedMPH>1</windSpeedMPH>
|
||||
<windGustKPH>32</windGustKPH>
|
||||
<windDirMaxDEG>301</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-09T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>NW</windDirMin80m>
|
||||
<windDirMin80mDEG>316</windDirMin80mDEG>
|
||||
<feelslikeC>20</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>9</windSpeedMaxMPH>
|
||||
<windDirDEG>356</windDirDEG>
|
||||
<windDir>N</windDir>
|
||||
<sunriseISO>2022-10-09T05:53:03+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>8</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>67</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Partly Cloudy</weather>
|
||||
<sunsetISO>2022-10-09T17:31:31+02:00</sunsetISO>
|
||||
<maxFeelslikeC>30</maxFeelslikeC>
|
||||
<humidity>86</humidity>
|
||||
<windDir80m>NW</windDir80m>
|
||||
<maxFeelslikeF>86</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>47</sky>
|
||||
<windGust80mMPH>23</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>23</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Partly Cloudy</weatherPrimary>
|
||||
<windGust80mKPH>36</windGust80mKPH>
|
||||
<avgDewpointF>57</avgDewpointF>
|
||||
<windSpeedMax80mKPH>36</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>20</windGust80mKTS>
|
||||
<avgDewpointC>14</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>20</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>356</windDirMinDEG>
|
||||
<windSpeedMaxKPH>14</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>5</windSpeedMin80mKTS>
|
||||
<feelslikeF>67</feelslikeF>
|
||||
<validTime>2022-10-09T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>6</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>726</solradMaxWM2>
|
||||
<avgTempC>25</avgTempC>
|
||||
<windSpeedMin80mKPH>9</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::SC</weatherPrimaryCoded>
|
||||
<sunrise>1665287583</sunrise>
|
||||
<avgTempF>77</avgTempF>
|
||||
<windDirMin>N</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>pcloudy.png</icon>
|
||||
<minFeelslikeC>20</minFeelslikeC>
|
||||
<dewpointC>17</dewpointC>
|
||||
<cloudsCoded>SC</cloudsCoded>
|
||||
<minFeelslikeF>67</minFeelslikeF>
|
||||
<minHumidity>31</minHumidity>
|
||||
<dewpointF>63</dewpointF>
|
||||
<windSpeed80mKTS>5</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>NNW</windDirMax>
|
||||
<windSpeed80mMPH>6</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>9</windSpeed80mKPH>
|
||||
<windDir80mDEG>316</windDir80mDEG>
|
||||
<maxTempC>31</maxTempC>
|
||||
<pressureMB>1016</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665291600</timestamp>
|
||||
<maxTempF>87</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>11</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>2</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>354</windDirMax80mDEG>
|
||||
<windGustKTS>19</windGustKTS>
|
||||
<windSpeedMinKPH>4</windSpeedMinKPH>
|
||||
<maxDewpointF>63</maxDewpointF>
|
||||
<windSpeedMinMPH>2</windSpeedMinMPH>
|
||||
<avgFeelslikeC>25</avgFeelslikeC>
|
||||
<uvi>7</uvi>
|
||||
<windDirMax80m>N</windDirMax80m>
|
||||
<maxDewpointC>17</maxDewpointC>
|
||||
<pressureIN>29.99</pressureIN>
|
||||
<avgFeelslikeF>77</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>19</minTempC>
|
||||
<minDewpointF>52</minDewpointF>
|
||||
<windSpeedKTS>2</windSpeedKTS>
|
||||
<sunset>1665329491</sunset>
|
||||
<solradWM2>4768</solradWM2>
|
||||
<windSpeedKPH>4</windSpeedKPH>
|
||||
<windGustMPH>22</windGustMPH>
|
||||
<maxHumidity>86</maxHumidity>
|
||||
<windSpeedMPH>2</windSpeedMPH>
|
||||
<windGustKPH>36</windGustKPH>
|
||||
<windDirMaxDEG>343</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-10T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>E</windDirMin80m>
|
||||
<windDirMin80mDEG>91</windDirMin80mDEG>
|
||||
<feelslikeC>21</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>9</windSpeedMaxMPH>
|
||||
<windDirDEG>358</windDirDEG>
|
||||
<windDir>N</windDir>
|
||||
<sunriseISO>2022-10-10T05:53:40+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>8</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>70</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Partly Cloudy</weather>
|
||||
<sunsetISO>2022-10-10T17:30:21+02:00</sunsetISO>
|
||||
<maxFeelslikeC>30</maxFeelslikeC>
|
||||
<humidity>75</humidity>
|
||||
<windDir80m>N</windDir80m>
|
||||
<maxFeelslikeF>86</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>64</sky>
|
||||
<windGust80mMPH>22</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>22</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Partly Cloudy</weatherPrimary>
|
||||
<windGust80mKPH>36</windGust80mKPH>
|
||||
<avgDewpointF>58</avgDewpointF>
|
||||
<windSpeedMax80mKPH>36</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>19</windGust80mKTS>
|
||||
<avgDewpointC>14</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>19</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>358</windDirMinDEG>
|
||||
<windSpeedMaxKPH>15</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>7</windSpeedMin80mKTS>
|
||||
<feelslikeF>69</feelslikeF>
|
||||
<validTime>2022-10-10T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>8</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>597</solradMaxWM2>
|
||||
<avgTempC>26</avgTempC>
|
||||
<windSpeedMin80mKPH>13</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::SC</weatherPrimaryCoded>
|
||||
<sunrise>1665374020</sunrise>
|
||||
<avgTempF>78</avgTempF>
|
||||
<windDirMin>N</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>pcloudy.png</icon>
|
||||
<minFeelslikeC>21</minFeelslikeC>
|
||||
<dewpointC>16</dewpointC>
|
||||
<cloudsCoded>SC</cloudsCoded>
|
||||
<minFeelslikeF>69</minFeelslikeF>
|
||||
<minHumidity>35</minHumidity>
|
||||
<dewpointF>61</dewpointF>
|
||||
<windSpeed80mKTS>7</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>N</windDirMax>
|
||||
<windSpeed80mMPH>8</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>13</windSpeed80mKPH>
|
||||
<windDir80mDEG>8</windDir80mDEG>
|
||||
<maxTempC>31</maxTempC>
|
||||
<pressureMB>1017</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665378000</timestamp>
|
||||
<maxTempF>87</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>13</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>2</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>10</windDirMax80mDEG>
|
||||
<windGustKTS>16</windGustKTS>
|
||||
<windSpeedMinKPH>4</windSpeedMinKPH>
|
||||
<maxDewpointF>61</maxDewpointF>
|
||||
<windSpeedMinMPH>2</windSpeedMinMPH>
|
||||
<avgFeelslikeC>25</avgFeelslikeC>
|
||||
<uvi>6</uvi>
|
||||
<windDirMax80m>N</windDirMax80m>
|
||||
<maxDewpointC>16</maxDewpointC>
|
||||
<pressureIN>30.03</pressureIN>
|
||||
<avgFeelslikeF>78</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>21</minTempC>
|
||||
<minDewpointF>55</minDewpointF>
|
||||
<windSpeedKTS>2</windSpeedKTS>
|
||||
<sunset>1665415821</sunset>
|
||||
<solradWM2>4494</solradWM2>
|
||||
<windSpeedKPH>4</windSpeedKPH>
|
||||
<windGustMPH>19</windGustMPH>
|
||||
<maxHumidity>75</maxHumidity>
|
||||
<windSpeedMPH>2</windSpeedMPH>
|
||||
<windGustKPH>30</windGustKPH>
|
||||
<windDirMaxDEG>10</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-11T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>NNE</windDirMin80m>
|
||||
<windDirMin80mDEG>13</windDirMin80mDEG>
|
||||
<feelslikeC>22</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>18</windSpeedMaxMPH>
|
||||
<windDirDEG>13</windDirDEG>
|
||||
<windDir>NNE</windDir>
|
||||
<sunriseISO>2022-10-11T05:54:18+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>15</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>70</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Sunny</weather>
|
||||
<sunsetISO>2022-10-11T17:29:13+02:00</sunsetISO>
|
||||
<maxFeelslikeC>31</maxFeelslikeC>
|
||||
<humidity>71</humidity>
|
||||
<windDir80m>NNE</windDir80m>
|
||||
<maxFeelslikeF>87</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>0</sky>
|
||||
<windGust80mMPH>19</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>19</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Sunny</weatherPrimary>
|
||||
<windGust80mKPH>31</windGust80mKPH>
|
||||
<avgDewpointF>55</avgDewpointF>
|
||||
<windSpeedMax80mKPH>31</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>17</windGust80mKTS>
|
||||
<avgDewpointC>13</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>17</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>14</windDirMinDEG>
|
||||
<windSpeedMaxKPH>28</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>9</windSpeedMin80mKTS>
|
||||
<feelslikeF>72</feelslikeF>
|
||||
<validTime>2022-10-11T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>11</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>758</solradMaxWM2>
|
||||
<avgTempC>26</avgTempC>
|
||||
<windSpeedMin80mKPH>18</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::CL</weatherPrimaryCoded>
|
||||
<sunrise>1665460458</sunrise>
|
||||
<avgTempF>78</avgTempF>
|
||||
<windDirMin>NNE</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>sunny.png</icon>
|
||||
<minFeelslikeC>22</minFeelslikeC>
|
||||
<dewpointC>17</dewpointC>
|
||||
<cloudsCoded>CL</cloudsCoded>
|
||||
<minFeelslikeF>72</minFeelslikeF>
|
||||
<minHumidity>30</minHumidity>
|
||||
<dewpointF>62</dewpointF>
|
||||
<windSpeed80mKTS>10</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>NNE</windDirMax>
|
||||
<windSpeed80mMPH>12</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>19</windSpeed80mKPH>
|
||||
<windDir80mDEG>16</windDir80mDEG>
|
||||
<maxTempC>31</maxTempC>
|
||||
<pressureMB>1015</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665464400</timestamp>
|
||||
<maxTempF>87</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>11</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>7</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>28</windDirMax80mDEG>
|
||||
<windGustKTS>15</windGustKTS>
|
||||
<windSpeedMinKPH>14</windSpeedMinKPH>
|
||||
<maxDewpointF>62</maxDewpointF>
|
||||
<windSpeedMinMPH>8</windSpeedMinMPH>
|
||||
<avgFeelslikeC>26</avgFeelslikeC>
|
||||
<uvi>null</uvi>
|
||||
<windDirMax80m>NNE</windDirMax80m>
|
||||
<maxDewpointC>17</maxDewpointC>
|
||||
<pressureIN>29.98</pressureIN>
|
||||
<avgFeelslikeF>79</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>21</minTempC>
|
||||
<minDewpointF>51</minDewpointF>
|
||||
<windSpeedKTS>8</windSpeedKTS>
|
||||
<sunset>1665502153</sunset>
|
||||
<solradWM2>5450</solradWM2>
|
||||
<windSpeedKPH>15</windSpeedKPH>
|
||||
<windGustMPH>17</windGustMPH>
|
||||
<maxHumidity>71</maxHumidity>
|
||||
<windSpeedMPH>9</windSpeedMPH>
|
||||
<windGustKPH>28</windGustKPH>
|
||||
<windDirMaxDEG>28</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<periods>
|
||||
<dateTimeISO>2022-10-12T07:00:00+02:00</dateTimeISO>
|
||||
<windDirMin80m>NNE</windDirMin80m>
|
||||
<windDirMin80mDEG>15</windDirMin80mDEG>
|
||||
<feelslikeC>22</feelslikeC>
|
||||
<visibilityMI>15</visibilityMI>
|
||||
<windSpeedMaxMPH>16</windSpeedMaxMPH>
|
||||
<windDirDEG>12</windDirDEG>
|
||||
<windDir>NNE</windDir>
|
||||
<sunriseISO>2022-10-12T05:54:55+02:00</sunriseISO>
|
||||
<iceaccumMM>null</iceaccumMM>
|
||||
<windSpeedMaxKTS>14</windSpeedMaxKTS>
|
||||
<iceaccumIN>null</iceaccumIN>
|
||||
<minTempF>69</minTempF>
|
||||
<snowIN>0</snowIN>
|
||||
<weather>Mostly Sunny</weather>
|
||||
<sunsetISO>2022-10-12T17:28:04+02:00</sunsetISO>
|
||||
<maxFeelslikeC>31</maxFeelslikeC>
|
||||
<humidity>68</humidity>
|
||||
<windDir80m>NNE</windDir80m>
|
||||
<maxFeelslikeF>88</maxFeelslikeF>
|
||||
<precipMM>0</precipMM>
|
||||
<sky>27</sky>
|
||||
<windGust80mMPH>21</windGust80mMPH>
|
||||
<windSpeedMax80mMPH>21</windSpeedMax80mMPH>
|
||||
<weatherPrimary>Mostly Sunny</weatherPrimary>
|
||||
<windGust80mKPH>33</windGust80mKPH>
|
||||
<avgDewpointF>55</avgDewpointF>
|
||||
<windSpeedMax80mKPH>33</windSpeedMax80mKPH>
|
||||
<windGust80mKTS>18</windGust80mKTS>
|
||||
<avgDewpointC>13</avgDewpointC>
|
||||
<precipIN>0</precipIN>
|
||||
<windSpeedMax80mKTS>18</windSpeedMax80mKTS>
|
||||
<windDirMinDEG>12</windDirMinDEG>
|
||||
<windSpeedMaxKPH>26</windSpeedMaxKPH>
|
||||
<windSpeedMin80mKTS>10</windSpeedMin80mKTS>
|
||||
<feelslikeF>72</feelslikeF>
|
||||
<validTime>2022-10-12T07:00:00+02:00</validTime>
|
||||
<windSpeedMin80mMPH>11</windSpeedMin80mMPH>
|
||||
<solradMaxWM2>743</solradMaxWM2>
|
||||
<avgTempC>26</avgTempC>
|
||||
<windSpeedMin80mKPH>18</windSpeedMin80mKPH>
|
||||
<weatherPrimaryCoded>::FW</weatherPrimaryCoded>
|
||||
<sunrise>1665546895</sunrise>
|
||||
<avgTempF>79</avgTempF>
|
||||
<windDirMin>NNE</windDirMin>
|
||||
<maxCoverage/>
|
||||
<icon>fair.png</icon>
|
||||
<minFeelslikeC>22</minFeelslikeC>
|
||||
<dewpointC>16</dewpointC>
|
||||
<cloudsCoded>FW</cloudsCoded>
|
||||
<minFeelslikeF>72</minFeelslikeF>
|
||||
<minHumidity>29</minHumidity>
|
||||
<dewpointF>60</dewpointF>
|
||||
<windSpeed80mKTS>10</windSpeed80mKTS>
|
||||
<pop>0</pop>
|
||||
<snowCM>0</snowCM>
|
||||
<windDirMax>E</windDirMax>
|
||||
<windSpeed80mMPH>12</windSpeed80mMPH>
|
||||
<windSpeed80mKPH>19</windSpeed80mKPH>
|
||||
<windDir80mDEG>15</windDir80mDEG>
|
||||
<maxTempC>31</maxTempC>
|
||||
<pressureMB>1014</pressureMB>
|
||||
<visibilityKM>24.135</visibilityKM>
|
||||
<timestamp>1665550800</timestamp>
|
||||
<maxTempF>88</maxTempF>
|
||||
<tempF>null</tempF>
|
||||
<minDewpointC>11</minDewpointC>
|
||||
<solradMinWM2>0</solradMinWM2>
|
||||
<windSpeedMinKTS>7</windSpeedMinKTS>
|
||||
<windDirMax80mDEG>96</windDirMax80mDEG>
|
||||
<windGustKTS>15</windGustKTS>
|
||||
<windSpeedMinKPH>13</windSpeedMinKPH>
|
||||
<maxDewpointF>60</maxDewpointF>
|
||||
<windSpeedMinMPH>8</windSpeedMinMPH>
|
||||
<avgFeelslikeC>26</avgFeelslikeC>
|
||||
<uvi>null</uvi>
|
||||
<windDirMax80m>E</windDirMax80m>
|
||||
<maxDewpointC>16</maxDewpointC>
|
||||
<pressureIN>29.95</pressureIN>
|
||||
<avgFeelslikeF>80</avgFeelslikeF>
|
||||
<iceaccum>null</iceaccum>
|
||||
<isDay>true</isDay>
|
||||
<minTempC>21</minTempC>
|
||||
<minDewpointF>51</minDewpointF>
|
||||
<windSpeedKTS>8</windSpeedKTS>
|
||||
<sunset>1665588484</sunset>
|
||||
<solradWM2>4740</solradWM2>
|
||||
<windSpeedKPH>15</windSpeedKPH>
|
||||
<windGustMPH>17</windGustMPH>
|
||||
<maxHumidity>68</maxHumidity>
|
||||
<windSpeedMPH>9</windSpeedMPH>
|
||||
<windGustKPH>28</windGustKPH>
|
||||
<windDirMaxDEG>96</windDirMaxDEG>
|
||||
<tempC>null</tempC>
|
||||
</periods>
|
||||
<interval>day</interval>
|
||||
<place>
|
||||
<country>eg</country>
|
||||
<name>cairo</name>
|
||||
<state>qh</state>
|
||||
</place>
|
||||
</response>
|
||||
<error>null</error>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user