1 | // |
2 | // JSONTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "JSONTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Path.h" |
15 | #include "Poco/Environment.h" |
16 | #include "Poco/File.h" |
17 | #include "Poco/FileStream.h" |
18 | #include "Poco/Glob.h" |
19 | #include "Poco/UTF8Encoding.h" |
20 | #include "Poco/Latin1Encoding.h" |
21 | #include "Poco/TextConverter.h" |
22 | #include "Poco/Nullable.h" |
23 | #include "Poco/Dynamic/Struct.h" |
24 | #include "Poco/DateTime.h" |
25 | #include "Poco/DateTimeFormatter.h" |
26 | #include <set> |
27 | #include <iostream> |
28 | |
29 | |
30 | using namespace Poco::JSON; |
31 | using namespace Poco::Dynamic; |
32 | using Poco::DynamicStruct; |
33 | using Poco::DateTime; |
34 | using Poco::DateTimeFormatter; |
35 | |
36 | JSONTest::JSONTest(const std::string& name): CppUnit::TestCase("JSON" ) |
37 | { |
38 | |
39 | } |
40 | |
41 | |
42 | JSONTest::~JSONTest() |
43 | { |
44 | |
45 | } |
46 | |
47 | |
48 | void JSONTest::setUp() |
49 | { |
50 | } |
51 | |
52 | |
53 | void JSONTest::tearDown() |
54 | { |
55 | } |
56 | |
57 | |
58 | void JSONTest::testNullProperty() |
59 | { |
60 | std::string json = "{ \"test\" : null }" ; |
61 | Parser parser; |
62 | |
63 | Var result; |
64 | try |
65 | { |
66 | result = parser.parse(json); |
67 | } |
68 | catch(JSONException& jsone) |
69 | { |
70 | std::cout << jsone.message() << std::endl; |
71 | assertTrue (false); |
72 | } |
73 | assertTrue (result.type() == typeid(Object::Ptr)); |
74 | |
75 | Object::Ptr object = result.extract<Object::Ptr>(); |
76 | assertTrue (object->isNull("test" )); |
77 | Var test = object->get("test" ); |
78 | assertTrue (test.isEmpty()); |
79 | |
80 | Poco::Nullable<int> test2 = object->getNullableValue<int>("test" ); |
81 | assertTrue (test2.isNull()); |
82 | |
83 | DynamicStruct ds = *object; |
84 | assertTrue (ds["test" ].isEmpty()); |
85 | |
86 | const DynamicStruct& rds = *object; |
87 | assertTrue (rds["test" ].isEmpty()); |
88 | } |
89 | |
90 | |
91 | void JSONTest::testTrueProperty() |
92 | { |
93 | std::string json = "{ \"test\" : true }" ; |
94 | Parser parser; |
95 | Var result; |
96 | |
97 | try |
98 | { |
99 | result = parser.parse(json); |
100 | } |
101 | catch(JSONException& jsone) |
102 | { |
103 | std::cout << jsone.message() << std::endl; |
104 | assertTrue (false); |
105 | } |
106 | |
107 | assertTrue (result.type() == typeid(Object::Ptr)); |
108 | |
109 | Object::Ptr object = result.extract<Object::Ptr>(); |
110 | Var test = object->get("test" ); |
111 | assertTrue (test.type() == typeid(bool)); |
112 | bool value = test; |
113 | assertTrue (value); |
114 | |
115 | DynamicStruct ds = *object; |
116 | assertTrue (!ds["test" ].isEmpty()); |
117 | assertTrue (ds["test" ]); |
118 | |
119 | const DynamicStruct& rds = *object; |
120 | assertTrue (!rds["test" ].isEmpty()); |
121 | assertTrue (rds["test" ]); |
122 | } |
123 | |
124 | |
125 | void JSONTest::testFalseProperty() |
126 | { |
127 | std::string json = "{ \"test\" : false }" ; |
128 | Parser parser; |
129 | Var result; |
130 | |
131 | try |
132 | { |
133 | result = parser.parse(json); |
134 | } |
135 | catch(JSONException& jsone) |
136 | { |
137 | std::cout << jsone.message() << std::endl; |
138 | assertTrue (false); |
139 | } |
140 | |
141 | assertTrue (result.type() == typeid(Object::Ptr)); |
142 | |
143 | Object::Ptr object = result.extract<Object::Ptr>(); |
144 | Var test = object->get("test" ); |
145 | assertTrue (test.type() == typeid(bool)); |
146 | bool value = test; |
147 | assertTrue (!value); |
148 | |
149 | DynamicStruct ds = *object; |
150 | assertTrue (!ds["test" ].isEmpty()); |
151 | assertTrue (!ds["test" ]); |
152 | |
153 | const DynamicStruct& rds = *object; |
154 | assertTrue (!rds["test" ].isEmpty()); |
155 | assertTrue (!rds["test" ]); |
156 | } |
157 | |
158 | |
159 | void JSONTest::testNumberProperty() |
160 | { |
161 | testNumber(1969); |
162 | testNumber(-1969); |
163 | testNumber(1969.5); |
164 | testNumber(-1969.5); |
165 | } |
166 | |
167 | |
168 | void JSONTest::testUnsignedNumberProperty() |
169 | { |
170 | // 4294967295 == unsigned(-1) |
171 | std::string json = "{ \"test\" : 4294967295 }" ; |
172 | Parser parser; |
173 | Var result; |
174 | |
175 | try |
176 | { |
177 | result = parser.parse(json); |
178 | } |
179 | catch(JSONException& jsone) |
180 | { |
181 | std::cout << jsone.message() << std::endl; |
182 | assertTrue (false); |
183 | } |
184 | |
185 | assertTrue (result.type() == typeid(Object::Ptr)); |
186 | |
187 | Object::Ptr object = result.extract<Object::Ptr>(); |
188 | Var test = object->get("test" ); |
189 | assertTrue (test.isNumeric()); |
190 | assertTrue (test.isInteger()); |
191 | Poco::UInt32 value = test; |
192 | assertTrue (value == -1); |
193 | |
194 | DynamicStruct ds = *object; |
195 | assertTrue (!ds["test" ].isEmpty()); |
196 | assertTrue (ds["test" ].isNumeric()); |
197 | assertTrue (ds["test" ].isInteger()); |
198 | assertTrue (ds["test" ] == 4294967295u); |
199 | value = ds["test" ]; |
200 | assertTrue (value == -1); |
201 | |
202 | const DynamicStruct& rds = *object; |
203 | assertTrue (!rds["test" ].isEmpty()); |
204 | assertTrue (rds["test" ].isNumeric()); |
205 | assertTrue (rds["test" ].isInteger()); |
206 | assertTrue (rds["test" ] == 4294967295u); |
207 | value = rds["test" ]; |
208 | assertTrue (value == -1); |
209 | } |
210 | |
211 | #if defined(POCO_HAVE_INT64) |
212 | |
213 | |
214 | void JSONTest::testNumber64Property() |
215 | { |
216 | std::string json = "{ \"test\" : -5000000000000000 }" ; |
217 | Parser parser; |
218 | Var result; |
219 | |
220 | try |
221 | { |
222 | result = parser.parse(json); |
223 | } |
224 | catch(JSONException& jsone) |
225 | { |
226 | std::cout << jsone.message() << std::endl; |
227 | assertTrue (false); |
228 | } |
229 | |
230 | assertTrue (result.type() == typeid(Object::Ptr)); |
231 | |
232 | Object object = *result.extract<Object::Ptr>(); |
233 | Var test = object.get("test" ); |
234 | assertTrue (test.isInteger()); |
235 | Poco::Int64 value = test; |
236 | assertTrue (value == -5000000000000000); |
237 | |
238 | DynamicStruct ds = object; |
239 | assertTrue (!ds["test" ].isEmpty()); |
240 | assertTrue (ds["test" ].isNumeric()); |
241 | assertTrue (ds["test" ].isInteger()); |
242 | assertTrue (ds["test" ] == static_cast<Poco::Int64>(-5000000000000000)); |
243 | value = ds["test" ]; |
244 | assertTrue (value == -5000000000000000); |
245 | |
246 | const DynamicStruct& rds = object; |
247 | assertTrue (!rds["test" ].isEmpty()); |
248 | assertTrue (rds["test" ].isNumeric()); |
249 | assertTrue (rds["test" ].isInteger()); |
250 | assertTrue (rds["test" ] == static_cast<Poco::Int64>(-5000000000000000)); |
251 | value = rds["test" ]; |
252 | assertTrue (value == -5000000000000000); |
253 | } |
254 | |
255 | |
256 | void JSONTest::testUnsignedNumber64Property() |
257 | { |
258 | // 18446744073709551615 == UInt64(-1) |
259 | std::string json = "{ \"test\" : 18446744073709551615 }" ; |
260 | Parser parser; |
261 | Var result; |
262 | |
263 | try |
264 | { |
265 | result = parser.parse(json); |
266 | } |
267 | catch(JSONException& jsone) |
268 | { |
269 | std::cout << jsone.message() << std::endl; |
270 | assertTrue (false); |
271 | } |
272 | |
273 | assertTrue (result.type() == typeid(Object::Ptr)); |
274 | |
275 | Object::Ptr object = result.extract<Object::Ptr>(); |
276 | Var test = object->get("test" ); |
277 | assertTrue (test.isInteger()); |
278 | Poco::UInt64 value = test; |
279 | assertTrue (value == -1); |
280 | /* TODO: clang has trouble here |
281 | DynamicStruct ds = *object; |
282 | assertTrue (!ds["test"].isEmpty()); |
283 | assertTrue (ds["test"].isNumeric()); |
284 | assertTrue (ds["test"].isInteger()); |
285 | assertTrue (ds["test"] == 18446744073709551615); |
286 | value = ds["test"]; |
287 | assertTrue (value == -1); |
288 | |
289 | const DynamicStruct& rds = *object; |
290 | assertTrue (!rds["test"].isEmpty()); |
291 | assertTrue (rds["test"].isNumeric()); |
292 | assertTrue (rds["test"].isInteger()); |
293 | assertTrue (rds["test"] == 18446744073709551615); |
294 | value = rds["test"]; |
295 | assertTrue (value == -1); |
296 | */ |
297 | } |
298 | |
299 | #endif |
300 | |
301 | |
302 | void JSONTest::testStringProperty() |
303 | { |
304 | std::string json = "{ \"test\" : \"value\" }" ; |
305 | Parser parser; |
306 | Var result; |
307 | |
308 | try |
309 | { |
310 | result = parser.parse(json); |
311 | } |
312 | catch(JSONException& jsone) |
313 | { |
314 | std::cout << jsone.message() << std::endl; |
315 | assertTrue (false); |
316 | } |
317 | |
318 | assertTrue (result.type() == typeid(Object::Ptr)); |
319 | |
320 | Object object = *result.extract<Object::Ptr>(); |
321 | Var test = object.get("test" ); |
322 | assertTrue (test.isString()); |
323 | std::string value = test.convert<std::string>(); |
324 | assertTrue (value.compare("value" ) == 0); |
325 | |
326 | object.set("test2" , 'a'); |
327 | std::ostringstream ostr; |
328 | object.stringify(ostr); |
329 | assertTrue (ostr.str() == "{\"test\":\"value\",\"test2\":\"a\"}" ); |
330 | |
331 | DynamicStruct ds = object; |
332 | assertTrue (!ds["test" ].isEmpty()); |
333 | assertTrue (ds["test" ].isString()); |
334 | assertTrue (!ds["test" ].isInteger()); |
335 | assertTrue (ds["test" ] == "value" ); |
336 | value = ds["test" ].toString(); |
337 | assertTrue (value == "value" ); |
338 | |
339 | const DynamicStruct& rds = object; |
340 | assertTrue (!rds["test" ].isEmpty()); |
341 | assertTrue (rds["test" ].isString()); |
342 | assertTrue (!rds["test" ].isInteger()); |
343 | assertTrue (rds["test" ] == "value" ); |
344 | value = rds["test" ].toString(); |
345 | assertTrue (value == "value" ); |
346 | } |
347 | |
348 | |
349 | void JSONTest::testEmptyObject() |
350 | { |
351 | std::string json = "{}" ; |
352 | Parser parser; |
353 | Var result; |
354 | |
355 | try |
356 | { |
357 | result = parser.parse(json); |
358 | } |
359 | catch(JSONException& jsone) |
360 | { |
361 | std::cout << jsone.message() << std::endl; |
362 | assertTrue (false); |
363 | } |
364 | |
365 | assertTrue (result.type() == typeid(Object::Ptr)); |
366 | |
367 | Object::Ptr object = result.extract<Object::Ptr>(); |
368 | assertTrue (object->size() == 0); |
369 | |
370 | DynamicStruct ds = *object; |
371 | assertTrue (ds.size() == 0); |
372 | |
373 | const DynamicStruct& rds = *object; |
374 | assertTrue (rds.size() == 0); |
375 | } |
376 | |
377 | |
378 | void JSONTest::testEmptyPropertyName() |
379 | { |
380 | std::string json = "{\"\": 42}" ; |
381 | Parser parser; |
382 | Var result; |
383 | |
384 | try |
385 | { |
386 | result = parser.parse(json); |
387 | } |
388 | catch(JSONException& jsone) |
389 | { |
390 | std::cout << jsone.message() << std::endl; |
391 | assertTrue (false); |
392 | } |
393 | |
394 | assertTrue (result.type() == typeid(Object::Ptr)); |
395 | |
396 | Object::Ptr object = result.extract<Object::Ptr>(); |
397 | assertTrue (object->size() == 1); |
398 | |
399 | DynamicStruct ds = *object; |
400 | assertTrue (ds.size() == 1); |
401 | |
402 | const DynamicStruct& rds = *object; |
403 | assertTrue (rds.size() == 1); |
404 | |
405 | assertTrue (ds["" ] == 42); |
406 | } |
407 | |
408 | |
409 | void JSONTest::testComplexObject() |
410 | { |
411 | std::string json = |
412 | "{" |
413 | "\"id\": 1," |
414 | "\"jsonrpc\": \"2.0\"," |
415 | "\"total\": 1," |
416 | "\"result\": [" |
417 | "{" |
418 | "\"id\": 1," |
419 | "\"guid\": \"67acfb26-17eb-4a75-bdbd-f0669b7d8f73\"," |
420 | "\"picture\": \"http://placehold.it/32x32\"," |
421 | "\"age\": 40," |
422 | "\"name\": \"Angelina Crossman\"," |
423 | "\"gender\": \"female\"," |
424 | "\"company\": \"Raylog\"," |
425 | "\"phone\": \"892-470-2803\"," |
426 | "\"email\": \"angelina@raylog.com\"," |
427 | "\"address\": \"20726, CostaMesa, Horatio Streets\"," |
428 | "\"about\": \"Consectetuer suscipit volutpat eros dolor euismod, " |
429 | "et dignissim in feugiat sed, ea tation exerci quis. Consectetuer, " |
430 | "dolor dolore ad vero ullamcorper, tincidunt facilisis at in facilisi, " |
431 | "iusto illum illum. Autem nibh, sed elit consequat volutpat tation, " |
432 | "nisl lorem lorem sed tation, facilisis dolore. Augue odio molestie, " |
433 | "dolor zzril nostrud aliquam sed, wisi dolor et ut iusto, ea. Magna " |
434 | "ex qui facilisi, hendrerit quis in eros ut, zzril nibh dolore nisl " |
435 | "suscipit, vulputate elit ut lobortis exerci, nulla dolore eros at " |
436 | "aliquam, ullamcorper vero ad iusto. Adipiscing, nisl eros exerci " |
437 | "nisl vel, erat in luptatum in duis, iusto.\"," |
438 | "\"registered\": \"2008-04-09T11:13:17 +05:00\"," |
439 | "\"tags\": [" |
440 | "\"ut\"," |
441 | "\"accumsan\"," |
442 | "\"feugait\"," |
443 | "\"ex\"," |
444 | "\"odio\"," |
445 | "\"consequat\"," |
446 | "\"delenit\"" |
447 | "]," |
448 | "\"friends\": [" |
449 | "{" |
450 | "\"id\": 1," |
451 | "\"name\": \"Hailey Hardman\"" |
452 | "}," |
453 | "{" |
454 | "\"id\": 2," |
455 | "\"name\": \"Bailey Oldridge\"" |
456 | "}," |
457 | "{" |
458 | "\"id\": 3," |
459 | "\"name\": \"Makayla Campbell\"" |
460 | "}" |
461 | "]" |
462 | "}" |
463 | "]" |
464 | "}" ; |
465 | |
466 | Parser parser; |
467 | Var result; |
468 | |
469 | try |
470 | { |
471 | result = parser.parse(json); |
472 | } |
473 | catch(JSONException& jsone) |
474 | { |
475 | std::cout << jsone.message() << std::endl; |
476 | assertTrue (false); |
477 | } |
478 | |
479 | assertTrue (result.type() == typeid(Object::Ptr)); |
480 | |
481 | Object::Ptr object = result.extract<Object::Ptr>(); |
482 | assertTrue (object->size() > 0); |
483 | |
484 | Object::NameList names = object->getNames(); |
485 | assertTrue (names.size() == 4); |
486 | assertTrue (names[0] == "id" ); |
487 | assertTrue (names[1] == "jsonrpc" ); |
488 | assertTrue (names[2] == "result" ); |
489 | assertTrue (names[3] == "total" ); |
490 | |
491 | DynamicStruct ds = *object; |
492 | assertTrue (ds.size() > 0); |
493 | assertTrue (ds["id" ] == 1); |
494 | assertTrue (ds["jsonrpc" ] == 2.0); |
495 | |
496 | assertTrue (ds["result" ].isArray()); |
497 | assertTrue (ds["result" ].size() == 1); |
498 | assertTrue (ds["result" ][0].isStruct()); |
499 | assertTrue (ds["result" ][0]["id" ] == 1); |
500 | assertTrue (ds["result" ][0]["guid" ] == "67acfb26-17eb-4a75-bdbd-f0669b7d8f73" ); |
501 | assertTrue (ds["result" ][0]["age" ] == 40); |
502 | assertTrue (ds["result" ][0]["name" ] == "Angelina Crossman" ); |
503 | assertTrue (ds["result" ][0]["gender" ] == "female" ); |
504 | assertTrue (ds["result" ][0]["company" ] == "Raylog" ); |
505 | assertTrue (ds["result" ][0]["phone" ] == "892-470-2803" ); |
506 | assertTrue (ds["result" ][0]["email" ] == "angelina@raylog.com" ); |
507 | assertTrue (ds["result" ][0]["address" ] == "20726, CostaMesa, Horatio Streets" ); |
508 | assertTrue (ds["result" ][0]["about" ] == "Consectetuer suscipit volutpat eros dolor euismod, " |
509 | "et dignissim in feugiat sed, ea tation exerci quis. Consectetuer, " |
510 | "dolor dolore ad vero ullamcorper, tincidunt facilisis at in facilisi, " |
511 | "iusto illum illum. Autem nibh, sed elit consequat volutpat tation, " |
512 | "nisl lorem lorem sed tation, facilisis dolore. Augue odio molestie, " |
513 | "dolor zzril nostrud aliquam sed, wisi dolor et ut iusto, ea. Magna " |
514 | "ex qui facilisi, hendrerit quis in eros ut, zzril nibh dolore nisl " |
515 | "suscipit, vulputate elit ut lobortis exerci, nulla dolore eros at " |
516 | "aliquam, ullamcorper vero ad iusto. Adipiscing, nisl eros exerci " |
517 | "nisl vel, erat in luptatum in duis, iusto." ); |
518 | assertTrue (ds["result" ][0]["registered" ] == "2008-04-09T11:13:17 +05:00" ); |
519 | |
520 | assertTrue (ds["result" ][0]["tags" ].isArray()); |
521 | assertTrue (ds["result" ][0]["tags" ].size() == 7); |
522 | assertTrue (ds["result" ][0]["tags" ][0] == "ut" ); |
523 | assertTrue (ds["result" ][0]["tags" ][1] == "accumsan" ); |
524 | assertTrue (ds["result" ][0]["tags" ][2] == "feugait" ); |
525 | assertTrue (ds["result" ][0]["tags" ][3] == "ex" ); |
526 | assertTrue (ds["result" ][0]["tags" ][4] == "odio" ); |
527 | assertTrue (ds["result" ][0]["tags" ][5] == "consequat" ); |
528 | assertTrue (ds["result" ][0]["tags" ][6] == "delenit" ); |
529 | |
530 | assertTrue (ds["result" ][0]["friends" ][0].isStruct()); |
531 | assertTrue (ds["result" ][0]["friends" ][0]["id" ] == 1); |
532 | assertTrue (ds["result" ][0]["friends" ][0]["name" ] == "Hailey Hardman" ); |
533 | assertTrue (ds["result" ][0]["friends" ][1]["id" ] == 2); |
534 | assertTrue (ds["result" ][0]["friends" ][1]["name" ] == "Bailey Oldridge" ); |
535 | assertTrue (ds["result" ][0]["friends" ][2]["id" ] == 3); |
536 | assertTrue (ds["result" ][0]["friends" ][2]["name" ] == "Makayla Campbell" ); |
537 | |
538 | const DynamicStruct& rds = *object; |
539 | assertTrue (rds.size() > 0); |
540 | } |
541 | |
542 | |
543 | void JSONTest::testDoubleProperty() |
544 | { |
545 | std::string json = "{ \"test\" : 1234.5 }" ; |
546 | Parser parser; |
547 | Var result; |
548 | |
549 | try |
550 | { |
551 | result = parser.parse(json); |
552 | } |
553 | catch(JSONException& jsone) |
554 | { |
555 | std::cout << jsone.message() << std::endl; |
556 | assertTrue (false); |
557 | } |
558 | |
559 | assertTrue (result.type() == typeid(Object::Ptr)); |
560 | |
561 | Object::Ptr object = result.extract<Object::Ptr>(); |
562 | Var test = object->get("test" ); |
563 | assertTrue (test.isNumeric()); |
564 | double value = test; |
565 | assertTrue (value == 1234.5); |
566 | |
567 | DynamicStruct ds = *object; |
568 | assertTrue (ds["test" ] == 1234.5); |
569 | } |
570 | |
571 | |
572 | void JSONTest::testDouble2Property() |
573 | { |
574 | std::string json = "{ \"test\" : 12e34 }" ; |
575 | Parser parser; |
576 | Var result; |
577 | |
578 | try |
579 | { |
580 | result = parser.parse(json); |
581 | } |
582 | catch(JSONException& jsone) |
583 | { |
584 | std::cout << jsone.message() << std::endl; |
585 | assertTrue (false); |
586 | } |
587 | |
588 | assertTrue (result.type() == typeid(Object::Ptr)); |
589 | |
590 | Object::Ptr object = result.extract<Object::Ptr>(); |
591 | Var test = object->get("test" ); |
592 | assertTrue (test.isNumeric()); |
593 | double value = test; |
594 | assertTrue (value >= 1.99e34 && value <= 12.01e34); |
595 | } |
596 | |
597 | |
598 | void JSONTest::testDouble3Property() |
599 | { |
600 | std::string json = "{ \"test\" : 12e-34 }" ; |
601 | Parser parser; |
602 | Var result; |
603 | |
604 | try |
605 | { |
606 | result = parser.parse(json); |
607 | } |
608 | catch(JSONException& jsone) |
609 | { |
610 | std::cout << jsone.message() << std::endl; |
611 | assertTrue (false); |
612 | } |
613 | |
614 | assertTrue (result.type() == typeid(Object::Ptr)); |
615 | |
616 | Object::Ptr object = result.extract<Object::Ptr>(); |
617 | Var test = object->get("test" ); |
618 | assertTrue (test.isNumeric()); |
619 | double value = test; |
620 | assertTrue (value == 12e-34); |
621 | } |
622 | |
623 | |
624 | void JSONTest::testObjectProperty() |
625 | { |
626 | std::string json = "{ \"test\" : { \"property\" : \"value\" } }" ; |
627 | Parser parser; |
628 | Var result; |
629 | |
630 | try |
631 | { |
632 | result = parser.parse(json); |
633 | } |
634 | catch(JSONException& jsone) |
635 | { |
636 | std::cout << jsone.message() << std::endl; |
637 | assertTrue (false); |
638 | } |
639 | |
640 | assertTrue (result.type() == typeid(Object::Ptr)); |
641 | |
642 | Object::Ptr object = result.extract<Object::Ptr>(); |
643 | assertTrue (object->isObject("test" )); |
644 | assertTrue (!object->isArray("test" )); |
645 | |
646 | assertTrue (!object->isArray("nonExistentKey" )); |
647 | assertTrue (!object->isObject("nonExistentKey" )); |
648 | |
649 | Var test = object->get("test" ); |
650 | assertTrue (test.type() == typeid(Object::Ptr)); |
651 | Object::Ptr subObject = test.extract<Object::Ptr>(); |
652 | |
653 | test = subObject->get("property" ); |
654 | assertTrue (test.isString()); |
655 | std::string value = test.convert<std::string>(); |
656 | assertTrue (value.compare("value" ) == 0); |
657 | |
658 | DynamicStruct ds = *object; |
659 | assertTrue (ds["test" ].isStruct()); |
660 | assertTrue (ds["test" ]["property" ] == "value" ); |
661 | |
662 | // make sure that Object is recognized as such |
663 | { |
664 | Object obj; |
665 | Object inner; |
666 | inner.set("some_number" , 5); |
667 | inner.set("some_string" , "xyz" ); |
668 | std::string key = "new_object" ; |
669 | obj.set(key, inner); |
670 | assertTrue (obj.isObject(key)); |
671 | } |
672 | |
673 | // make sure that Object pointer is recognized as Object |
674 | { |
675 | Object obj; |
676 | Poco::JSON::Object::Ptr inner = new Poco::JSON::Object; |
677 | inner->set("some_number" , 5); |
678 | inner->set("some_string" , "xyz" ); |
679 | std::string key = "new_object" ; |
680 | obj.set(key, inner); |
681 | assertTrue (obj.isObject(key)); |
682 | } |
683 | } |
684 | |
685 | |
686 | void JSONTest::testObjectArray() |
687 | { |
688 | std::string json = "{ \"test\" : { \"test1\" : [1, 2, 3], \"test2\" : 4 } }" ; |
689 | Parser parser; |
690 | Var result; |
691 | |
692 | try |
693 | { |
694 | result = parser.parse(json); |
695 | } |
696 | catch(JSONException& jsone) |
697 | { |
698 | std::cout << jsone.message() << std::endl; |
699 | assertTrue (false); |
700 | } |
701 | |
702 | assertTrue (result.type() == typeid(Object::Ptr)); |
703 | Object::Ptr object = result.extract<Object::Ptr>(); |
704 | assertTrue (object->isObject("test" )); |
705 | Object::Ptr subObject = object->getObject("test" ); |
706 | assertTrue (!subObject->isObject("test1" )); |
707 | assertTrue (subObject->isArray("test1" )); |
708 | assertTrue (!subObject->isObject("test2" )); |
709 | assertTrue (!subObject->isArray("test2" )); |
710 | |
711 | DynamicStruct ds = *object; |
712 | assertTrue (ds.size() > 0); |
713 | assertTrue (ds.size() == 1); |
714 | |
715 | assertTrue (ds["test" ].isStruct()); |
716 | assertTrue (ds["test" ]["test1" ].isArray()); |
717 | assertTrue (ds["test" ]["test1" ].size() == 3); |
718 | assertTrue (ds["test" ]["test1" ][0] == 1); |
719 | assertTrue (ds["test" ]["test1" ][1] == 2); |
720 | assertTrue (ds["test" ]["test1" ][2] == 3); |
721 | assertTrue (ds["test" ]["test2" ] == 4); |
722 | |
723 | object->set("test3" , "another top level value" ); |
724 | ds = std::move(*object); |
725 | assertTrue (ds.size() == 2); |
726 | assertTrue (ds["test" ].isStruct()); |
727 | assertTrue (ds["test" ]["test1" ].isArray()); |
728 | assertTrue (ds["test" ]["test1" ].size() == 3); |
729 | assertTrue (ds["test" ]["test1" ][0] == 1); |
730 | assertTrue (ds["test" ]["test1" ][1] == 2); |
731 | assertTrue (ds["test" ]["test1" ][2] == 3); |
732 | assertTrue (ds["test" ]["test2" ] == 4); |
733 | assertTrue (ds["test3" ] == "another top level value" ); |
734 | } |
735 | |
736 | |
737 | void JSONTest::testArrayOfObjects() |
738 | { |
739 | std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]" ; |
740 | Parser parser; |
741 | Var result; |
742 | |
743 | try |
744 | { |
745 | result = parser.parse(json); |
746 | } |
747 | catch(JSONException& jsone) |
748 | { |
749 | std::cout << jsone.message() << std::endl; |
750 | assertTrue (false); |
751 | } |
752 | |
753 | assertTrue (result.isArray()); |
754 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
755 | Poco::JSON::Array::Ptr arr = result.extract<Poco::JSON::Array::Ptr>(); |
756 | Object::Ptr object = arr->getObject(0); |
757 | assertTrue (object->getValue<int>("test" ) == 0); |
758 | Object::Ptr subObject = arr->getObject(1); |
759 | Poco::JSON::Array::Ptr subArr = subObject->getArray("test1" ); |
760 | result = subArr->get(0); |
761 | assertTrue (result == 1); |
762 | |
763 | Poco::Dynamic::Array da = *arr; |
764 | assertTrue (da.size() == 2); |
765 | assertTrue (da[0].isStruct()); |
766 | assertTrue (da[0]["test" ] == 0); |
767 | assertTrue (da[1].isStruct()); |
768 | assertTrue (da[1]["test1" ].isArray()); |
769 | assertTrue (da[1]["test1" ][0] == 1); |
770 | assertTrue (da[1]["test1" ][1] == 2); |
771 | assertTrue (da[1]["test1" ][2] == 3); |
772 | assertTrue (da[1]["test2" ] == 4); |
773 | } |
774 | |
775 | |
776 | void JSONTest::testEmptyArray() |
777 | { |
778 | std::string json = "[]" ; |
779 | Parser parser; |
780 | Var result; |
781 | |
782 | try |
783 | { |
784 | result = parser.parse(json); |
785 | } |
786 | catch(JSONException& jsone) |
787 | { |
788 | std::cout << jsone.message() << std::endl; |
789 | assertTrue (false); |
790 | } |
791 | |
792 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
793 | |
794 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
795 | assertTrue (array->size() == 0); |
796 | |
797 | Poco::Dynamic::Array da = *array; |
798 | assertTrue (da.size() == 0); |
799 | } |
800 | |
801 | |
802 | void JSONTest::testNestedArray() |
803 | { |
804 | std::string json = "[[[[]]]]" ; |
805 | Parser parser; |
806 | Var result; |
807 | |
808 | try |
809 | { |
810 | result = parser.parse(json); |
811 | } |
812 | catch(JSONException& jsone) |
813 | { |
814 | std::cout << jsone.message() << std::endl; |
815 | assertTrue (false); |
816 | } |
817 | |
818 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
819 | |
820 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
821 | assertTrue (array->size() == 1); |
822 | |
823 | Poco::Dynamic::Array da = *array; |
824 | assertTrue (da.size() == 1); |
825 | assertTrue (da[0].size() == 1); |
826 | assertTrue (da[0][0].size() == 1); |
827 | assertTrue (da[0][0][0].size() == 0); |
828 | } |
829 | |
830 | |
831 | void JSONTest::testNullElement() |
832 | { |
833 | std::string json = "[ null ]" ; |
834 | Parser parser; |
835 | Var result; |
836 | |
837 | try |
838 | { |
839 | result = parser.parse(json); |
840 | } |
841 | catch(JSONException& jsone) |
842 | { |
843 | std::cout << jsone.message() << std::endl; |
844 | assertTrue (false); |
845 | } |
846 | |
847 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
848 | |
849 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
850 | assertTrue (array->isNull(0)); |
851 | Var test = array->get(0); |
852 | assertTrue (test.isEmpty()); |
853 | |
854 | Poco::Dynamic::Array da = *array; |
855 | assertTrue (da.size() == 1); |
856 | assertTrue (da[0].isEmpty()); |
857 | } |
858 | |
859 | |
860 | void JSONTest::testTrueElement() |
861 | { |
862 | std::string json = "[ true ]" ; |
863 | Parser parser; |
864 | Var result; |
865 | |
866 | try |
867 | { |
868 | result = parser.parse(json); |
869 | } |
870 | catch(JSONException& jsone) |
871 | { |
872 | std::cout << jsone.message() << std::endl; |
873 | assertTrue (false); |
874 | } |
875 | |
876 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
877 | |
878 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
879 | Var test = array->get(0); |
880 | assertTrue (test.type() == typeid(bool)); |
881 | bool value = test; |
882 | assertTrue (value); |
883 | |
884 | Poco::Dynamic::Array da = *array; |
885 | assertTrue (da.size() == 1); |
886 | assertTrue (da[0]); |
887 | } |
888 | |
889 | |
890 | void JSONTest::testFalseElement() |
891 | { |
892 | std::string json = "[ false ]" ; |
893 | Parser parser; |
894 | Var result; |
895 | |
896 | try |
897 | { |
898 | result = parser.parse(json); |
899 | } |
900 | catch(JSONException& jsone) |
901 | { |
902 | std::cout << jsone.message() << std::endl; |
903 | assertTrue (false); |
904 | } |
905 | |
906 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
907 | |
908 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
909 | Var test = array->get(0); |
910 | assertTrue (test.type() == typeid(bool)); |
911 | bool value = test; |
912 | assertTrue (!value); |
913 | |
914 | Poco::Dynamic::Array da = *array; |
915 | assertTrue (da.size() == 1); |
916 | assertTrue (!da[0]); |
917 | } |
918 | |
919 | |
920 | void JSONTest::testNumberElement() |
921 | { |
922 | std::string json = "[ 1969 ]" ; |
923 | Parser parser; |
924 | Var result; |
925 | |
926 | try |
927 | { |
928 | result = parser.parse(json); |
929 | } |
930 | catch(JSONException& jsone) |
931 | { |
932 | std::cout << jsone.message() << std::endl; |
933 | assertTrue (false); |
934 | } |
935 | |
936 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
937 | |
938 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
939 | Var test = array->get(0); |
940 | assertTrue (test.isInteger()); |
941 | int value = test; |
942 | assertTrue (value == 1969); |
943 | |
944 | Poco::Dynamic::Array da = *array; |
945 | assertTrue (da.size() == 1); |
946 | assertTrue (da[0] == 1969); |
947 | } |
948 | |
949 | |
950 | void JSONTest::testStringElement() |
951 | { |
952 | std::string json = "[ \"value\" ]" ; |
953 | Parser parser; |
954 | Var result; |
955 | |
956 | try |
957 | { |
958 | result = parser.parse(json); |
959 | } |
960 | catch(JSONException& jsone) |
961 | { |
962 | std::cout << jsone.message() << std::endl; |
963 | assertTrue (false); |
964 | } |
965 | |
966 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
967 | |
968 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
969 | Var test = array->get(0); |
970 | assertTrue (test.isString()); |
971 | std::string value = test.convert<std::string>(); |
972 | assertTrue (value.compare("value" ) == 0); |
973 | |
974 | Poco::Dynamic::Array da = *array; |
975 | assertTrue (da.size() == 1); |
976 | assertTrue (da[0] == "value" ); |
977 | |
978 | std::stringstream s; |
979 | json = "[ \"\\u0017\" ]" ; |
980 | Var v = Parser().parse(json); |
981 | Stringifier::condense(v, s); |
982 | std::string ss = s.str(); |
983 | assertTrue (s.str() == "[\"\\u0017\"]" ); |
984 | } |
985 | |
986 | |
987 | void JSONTest::testEmptyObjectElement() |
988 | { |
989 | std::string json = "[{}]" ; |
990 | Parser parser; |
991 | Var result; |
992 | |
993 | try |
994 | { |
995 | result = parser.parse(json); |
996 | } |
997 | catch(JSONException& jsone) |
998 | { |
999 | std::cout << jsone.message() << std::endl; |
1000 | assertTrue (false); |
1001 | } |
1002 | |
1003 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
1004 | |
1005 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
1006 | Object::Ptr object = array->getObject(0); |
1007 | assertTrue (object->size() == 0); |
1008 | |
1009 | Poco::Dynamic::Array da = *array; |
1010 | assertTrue (da.size() == 1); |
1011 | assertTrue (da[0].isStruct()); |
1012 | assertTrue (da[0].size() == 0); |
1013 | } |
1014 | |
1015 | |
1016 | void JSONTest::testDoubleElement() |
1017 | { |
1018 | std::string json = "[ 1234.5 ]" ; |
1019 | Parser parser; |
1020 | Var result; |
1021 | |
1022 | try |
1023 | { |
1024 | result = parser.parse(json); |
1025 | } |
1026 | catch(JSONException& jsone) |
1027 | { |
1028 | std::cout << jsone.message() << std::endl; |
1029 | assertTrue (false); |
1030 | } |
1031 | |
1032 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
1033 | |
1034 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
1035 | Var test = array->get(0); |
1036 | assertTrue (test.isNumeric()); |
1037 | double value = test; |
1038 | assertTrue (value == 1234.5); |
1039 | |
1040 | Poco::Dynamic::Array da = *array; |
1041 | assertTrue (da.size() == 1); |
1042 | assertTrue (da[0] == 1234.5); |
1043 | } |
1044 | |
1045 | |
1046 | void JSONTest::testSetArrayElement() |
1047 | { |
1048 | std::string json = "[]" ; |
1049 | Parser parser; |
1050 | Var result = parser.parse(json); |
1051 | Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); |
1052 | |
1053 | Poco::Dynamic::Array dynArray = *array; |
1054 | assertTrue (dynArray.size() == 0); |
1055 | |
1056 | // array[0] = 7 |
1057 | array->set(0, 7); |
1058 | assertTrue (array->size() == 1); |
1059 | assertTrue (array->getElement<int>(0) == 7); |
1060 | |
1061 | dynArray = *array; |
1062 | assertTrue (dynArray.size() == 1); |
1063 | assertTrue (dynArray[0] == 7); |
1064 | |
1065 | // array[2] = "foo" |
1066 | array->set(2, std::string("foo" )); |
1067 | assertTrue (array->size() == 3); |
1068 | assertTrue (array->getElement<int>(0) == 7); |
1069 | assertTrue (array->isNull(1)); |
1070 | assertTrue (array->getElement<std::string>(2) == "foo" ); |
1071 | |
1072 | dynArray = *array; |
1073 | assertTrue (dynArray.size() == 3); |
1074 | assertTrue (dynArray[0] == 7); |
1075 | assertTrue (dynArray[1].isEmpty()); |
1076 | assertTrue (dynArray[2] == "foo" ); |
1077 | |
1078 | // array[1] = 13 |
1079 | array->set(1, 13); |
1080 | assertTrue (array->size() == 3); |
1081 | assertTrue (array->getElement<int>(0) == 7); |
1082 | assertTrue (array->getElement<int>(1) == 13); |
1083 | assertTrue (array->getElement<std::string>(2) == "foo" ); |
1084 | |
1085 | dynArray = std::move(*array); |
1086 | assertTrue (dynArray.size() == 3); |
1087 | assertTrue (dynArray[0] == 7); |
1088 | assertTrue (dynArray[1] == 13); |
1089 | assertTrue (dynArray[2] == "foo" ); |
1090 | |
1091 | dynArray.clear(); |
1092 | assertTrue (dynArray.size() == 0); |
1093 | } |
1094 | |
1095 | |
1096 | void JSONTest::testOptValue() |
1097 | { |
1098 | std::string json = "{ }" ; |
1099 | Parser parser; |
1100 | Var result; |
1101 | |
1102 | try |
1103 | { |
1104 | result = parser.parse(json); |
1105 | } |
1106 | catch(JSONException& jsone) |
1107 | { |
1108 | std::cout << jsone.message() << std::endl; |
1109 | assertTrue (false); |
1110 | } |
1111 | |
1112 | assertTrue (result.type() == typeid(Object::Ptr)); |
1113 | |
1114 | Object::Ptr object = result.extract<Object::Ptr>(); |
1115 | int n = object->optValue("test" , 123); |
1116 | assertTrue (n == 123); |
1117 | } |
1118 | |
1119 | |
1120 | void JSONTest::testQuery() |
1121 | { |
1122 | std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ], \"address\": { \"street\": \"A Street\", \"number\": 123, \"city\":\"The City\"} }" ; |
1123 | Parser parser; |
1124 | Var result; |
1125 | |
1126 | try |
1127 | { |
1128 | result = parser.parse(json); |
1129 | } |
1130 | catch(JSONException& jsone) |
1131 | { |
1132 | std::cout << jsone.message() << std::endl; |
1133 | assertTrue (false); |
1134 | } |
1135 | |
1136 | assertTrue (result.type() == typeid(Object::Ptr)); |
1137 | |
1138 | Query query(result); |
1139 | |
1140 | std::string firstChild = query.findValue("children[0]" , "" ); |
1141 | assertTrue (firstChild.compare("Jonas" ) == 0); |
1142 | |
1143 | Poco::DynamicStruct ds = *result.extract<Object::Ptr>(); |
1144 | assertTrue (ds["name" ] == "Franky" ); |
1145 | assertTrue (ds["children" ].size() == 2); |
1146 | assertTrue (ds["children" ][0] == "Jonas" ); |
1147 | assertTrue (ds["children" ][1] == "Ellen" ); |
1148 | |
1149 | Object::Ptr pAddress = query.findObject("address" ); |
1150 | assertTrue (pAddress->getValue<std::string>("street" ) == "A Street" ); |
1151 | pAddress = query.findObject("bad address" ); |
1152 | assertTrue (pAddress.isNull()); |
1153 | |
1154 | Object address; |
1155 | address.set("dummy" , 123); |
1156 | query.findObject("bad address" , address); |
1157 | assertTrue (!address.has("dummy" )); |
1158 | Object& rAddress = query.findObject("address" , address); |
1159 | assertTrue (rAddress.getValue<int>("number" ) == 123); |
1160 | |
1161 | Var badAddr = query.find("address.street.anotherObject" ); |
1162 | assertTrue (badAddr.isEmpty()); |
1163 | |
1164 | using Poco::JSON::Array; |
1165 | |
1166 | Array::Ptr pChildren = query.findArray("children" ); |
1167 | assertTrue (pChildren->getElement<std::string>(0) == "Jonas" ); |
1168 | pChildren = query.findArray("no children" ); |
1169 | assertTrue (pChildren.isNull()); |
1170 | |
1171 | Array children; |
1172 | children.add("dummy" ); |
1173 | query.findArray("no children" , children); |
1174 | assertTrue (children.size() == 0); |
1175 | Array& rChildren = query.findArray("children" , children); |
1176 | assertTrue (rChildren.getElement<std::string>(1) == "Ellen" ); |
1177 | |
1178 | Object::Ptr pObj = new Poco::JSON::Object; |
1179 | pObj->set("Id" , 22); |
1180 | |
1181 | Query queryPointer(pObj); |
1182 | Var idQueryPointer = queryPointer.find("Id" ); |
1183 | assertTrue (22 == idQueryPointer); |
1184 | |
1185 | Query queryObj(*pObj); |
1186 | Var idQueryObj = queryObj.find("Id" ); |
1187 | assertTrue (22 == idQueryObj); |
1188 | |
1189 | Var bad = 1; |
1190 | try |
1191 | { |
1192 | Query badQuery(bad); |
1193 | fail ("must throw" ); |
1194 | } |
1195 | catch (Poco::InvalidArgumentException&) { } |
1196 | } |
1197 | |
1198 | |
1199 | void JSONTest::() |
1200 | { |
1201 | std::string json = "{ \"name\" : \"Franky\" /* father */, \"children\" : [ \"Jonas\" /* son */ , \"Ellen\" /* daughter */ ] }" ; |
1202 | Parser parser; |
1203 | Var result; |
1204 | |
1205 | try |
1206 | { |
1207 | parser.parse(json); |
1208 | fail ("must fail" ); |
1209 | } |
1210 | catch(Poco::SyntaxException&) |
1211 | { |
1212 | } |
1213 | catch (JSONException&) |
1214 | { |
1215 | } |
1216 | |
1217 | parser.reset(); |
1218 | parser.setAllowComments(true); |
1219 | try |
1220 | { |
1221 | result = parser.parse(json); |
1222 | } |
1223 | catch(JSONException& jsone) |
1224 | { |
1225 | std::cout << jsone.message() << std::endl; |
1226 | assertTrue (false); |
1227 | } |
1228 | |
1229 | assertTrue (result.type() == typeid(Object::Ptr)); |
1230 | |
1231 | Query query(result); |
1232 | |
1233 | std::string firstChild = query.findValue("children[0]" , "" ); |
1234 | assertTrue (firstChild.compare("Jonas" ) == 0); |
1235 | } |
1236 | |
1237 | |
1238 | void JSONTest::testPrintHandler() |
1239 | { |
1240 | std::string json = "{ \"name\" : \"Homer\", \"age\" : 38, \"wife\" : \"Marge\", \"age\" : 36, \"children\" : [ \"Bart\", \"Lisa\", \"Maggie\" ] }" ; |
1241 | Parser parser; |
1242 | std::ostringstream ostr; |
1243 | PrintHandler::Ptr pHandler = new PrintHandler(ostr); |
1244 | parser.setHandler(pHandler); |
1245 | parser.parse(json); |
1246 | assertTrue (ostr.str() == "{\"name\":\"Homer\",\"age\":38,\"wife\":\"Marge\",\"age\":36,\"children\":[\"Bart\",\"Lisa\",\"Maggie\"]}" ); |
1247 | |
1248 | pHandler->setIndent(1); |
1249 | ostr.str("" ); |
1250 | parser.reset(); |
1251 | parser.parse(json); |
1252 | assertTrue (ostr.str() == "{\n" |
1253 | " \"name\" : \"Homer\",\n" |
1254 | " \"age\" : 38,\n" |
1255 | " \"wife\" : \"Marge\",\n" |
1256 | " \"age\" : 36,\n" |
1257 | " \"children\" : [\n" |
1258 | " \"Bart\",\n" |
1259 | " \"Lisa\",\n" |
1260 | " \"Maggie\"\n" |
1261 | " ]\n" |
1262 | "}" |
1263 | ); |
1264 | |
1265 | pHandler->setIndent(2); |
1266 | ostr.str("" ); |
1267 | parser.reset(); |
1268 | parser.parse(json); |
1269 | assertTrue (ostr.str() == "{\n" |
1270 | " \"name\" : \"Homer\",\n" |
1271 | " \"age\" : 38,\n" |
1272 | " \"wife\" : \"Marge\",\n" |
1273 | " \"age\" : 36,\n" |
1274 | " \"children\" : [\n" |
1275 | " \"Bart\",\n" |
1276 | " \"Lisa\",\n" |
1277 | " \"Maggie\"\n" |
1278 | " ]\n" |
1279 | "}" |
1280 | ); |
1281 | |
1282 | pHandler->setIndent(4); |
1283 | ostr.str("" ); |
1284 | parser.reset(); |
1285 | parser.parse(json); |
1286 | assertTrue (ostr.str() == "{\n" |
1287 | " \"name\" : \"Homer\",\n" |
1288 | " \"age\" : 38,\n" |
1289 | " \"wife\" : \"Marge\",\n" |
1290 | " \"age\" : 36,\n" |
1291 | " \"children\" : [\n" |
1292 | " \"Bart\",\n" |
1293 | " \"Lisa\",\n" |
1294 | " \"Maggie\"\n" |
1295 | " ]\n" |
1296 | "}" |
1297 | ); |
1298 | |
1299 | json = |
1300 | "{" |
1301 | "\"array\":" |
1302 | "[" |
1303 | "{" |
1304 | "\"key1\":" |
1305 | "[" |
1306 | "1,2,3," |
1307 | "{" |
1308 | "\"subkey\":" |
1309 | "\"test\"" |
1310 | "}" |
1311 | "]" |
1312 | "}," |
1313 | "{" |
1314 | "\"key2\":" |
1315 | "{" |
1316 | "\"anotherSubKey\":" |
1317 | "[" |
1318 | "1," |
1319 | "{" |
1320 | "\"subSubKey\":" |
1321 | "[" |
1322 | "4,5,6" |
1323 | "]" |
1324 | "}" |
1325 | "]" |
1326 | "}" |
1327 | "}" |
1328 | "]" |
1329 | "}" ; |
1330 | |
1331 | |
1332 | ostr.str("" ); |
1333 | pHandler->setIndent(0); |
1334 | parser.reset(); |
1335 | parser.parse(json); |
1336 | assertTrue (json == ostr.str()); |
1337 | |
1338 | json="[[\"a\"],[\"b\"],[[\"c\"],[\"d\"]]]" ; |
1339 | ostr.str("" ); |
1340 | pHandler->setIndent(0); |
1341 | parser.reset(); |
1342 | parser.parse(json); |
1343 | assertTrue (json == ostr.str()); |
1344 | |
1345 | json="[{\"1\":\"one\",\"0\":[\"zero\",\"nil\"]}]" ; |
1346 | ostr.str("" ); |
1347 | pHandler->setIndent(0); |
1348 | parser.reset(); |
1349 | parser.parse(json); |
1350 | assertTrue (json == ostr.str()); |
1351 | |
1352 | } |
1353 | |
1354 | |
1355 | void JSONTest::testStringify() |
1356 | { |
1357 | std::ostringstream os; |
1358 | Var i = 123; |
1359 | Stringifier::stringify(i, os); |
1360 | assertTrue (os.str() == "123" ); |
1361 | |
1362 | os.str("" ); |
1363 | Var f = 123.456; |
1364 | Stringifier::stringify(f, os); |
1365 | assertTrue (os.str() == "123.456" ); |
1366 | |
1367 | os.str("" ); |
1368 | Var s = "abcdef" ; |
1369 | Stringifier::stringify(s, os); |
1370 | assertTrue (os.str() == "\"abcdef\"" ); |
1371 | |
1372 | os.str("" ); |
1373 | DateTime dt; |
1374 | Var d = dt; |
1375 | Stringifier::stringify(d, os); |
1376 | assertTrue (os.str() == std::string("\"" + DateTimeFormatter::format(dt, Poco::DateTimeFormat::ISO8601_FORMAT) + "\"" )); |
1377 | |
1378 | std::string str1 = "\r" ; |
1379 | std::string str2 = "\n" ; |
1380 | Object obj1, obj2; |
1381 | obj1.set("payload" , str1); |
1382 | obj2.set("payload" , str2); |
1383 | std::ostringstream oss1, oss2; |
1384 | Poco::JSON::Stringifier::stringify(obj1, oss1); |
1385 | Poco::JSON::Stringifier::stringify(obj2, oss2); |
1386 | assertTrue (oss1.str() == "{\"payload\":\"\\r\"}" ); |
1387 | std::cout << "\"" << oss1.str() << "\"" << std::endl; |
1388 | assertTrue (oss2.str() == "{\"payload\":\"\\n\"}" ); |
1389 | |
1390 | Object jObj(false); |
1391 | jObj.set("foo\\" , 0); |
1392 | jObj.set("bar/" , 0); |
1393 | jObj.set("baz" , 0); |
1394 | jObj.set("q\"uote\"d" , 0); |
1395 | jObj.set("backspace" , "bs\b" ); |
1396 | jObj.set("newline" , "nl\n" ); |
1397 | jObj.set("tab" , "tb\t" ); |
1398 | |
1399 | std::stringstream ss; |
1400 | jObj.stringify(ss); |
1401 | |
1402 | assertTrue (ss.str() == "{\"backspace\":\"bs\\b\",\"bar\\/\":0,\"baz\":0,\"foo\\\\\":0," |
1403 | "\"newline\":\"nl\\n\",\"q\\\"uote\\\"d\":0,\"tab\":\"tb\\t\"}" ); |
1404 | |
1405 | std::string json = "{ \"Simpsons\" : { \"husband\" : { \"name\" : \"Homer\" , \"age\" : 38 }, \"wife\" : { \"name\" : \"Marge\", \"age\" : 36 }, " |
1406 | "\"children\" : [ \"Bart\", \"Lisa\", \"Maggie\" ], " |
1407 | "\"address\" : { \"number\" : 742, \"street\" : \"Evergreen Terrace\", \"town\" : \"Springfield\" } } }" ; |
1408 | Parser parser; |
1409 | Var result; |
1410 | |
1411 | try |
1412 | { |
1413 | result = parser.parse(json); |
1414 | } |
1415 | catch(JSONException& jsone) |
1416 | { |
1417 | std::cout << jsone.message() << std::endl; |
1418 | assertTrue (false); |
1419 | } |
1420 | |
1421 | assertTrue (result.type() == typeid(Object::Ptr)); |
1422 | std::ostringstream ostr; |
1423 | |
1424 | Stringifier::condense(result, ostr); |
1425 | std::string str = "{" |
1426 | "\"Simpsons\":{" |
1427 | "\"address\":{" |
1428 | "\"number\":742," |
1429 | "\"street\":\"Evergreen Terrace\"," |
1430 | "\"town\":\"Springfield\"" |
1431 | "}," |
1432 | "\"children\":[" |
1433 | "\"Bart\"," |
1434 | "\"Lisa\"," |
1435 | "\"Maggie\"]," |
1436 | "\"husband\":{" |
1437 | "\"age\":38," |
1438 | "\"name\":\"Homer\"" |
1439 | "}," |
1440 | "\"wife\":{" |
1441 | "\"age\":36,\"name\":\"Marge\"" |
1442 | "}}}" ; |
1443 | |
1444 | assertTrue (ostr.str() == str); |
1445 | |
1446 | ostr.str("" ); |
1447 | Stringifier::stringify(result, ostr); |
1448 | assertTrue (ostr.str() == str); |
1449 | |
1450 | ostr.str("" ); |
1451 | Stringifier::stringify(result, ostr, 1); |
1452 | str = "{\n" |
1453 | " \"Simpsons\" : {\n" |
1454 | " \"address\" : {\n" |
1455 | " \"number\" : 742,\n" |
1456 | " \"street\" : \"Evergreen Terrace\",\n" |
1457 | " \"town\" : \"Springfield\"\n" |
1458 | " },\n" |
1459 | " \"children\" : [\n" |
1460 | " \"Bart\",\n" |
1461 | " \"Lisa\",\n" |
1462 | " \"Maggie\"\n" |
1463 | " ],\n" |
1464 | " \"husband\" : {\n" |
1465 | " \"age\" : 38,\n" |
1466 | " \"name\" : \"Homer\"\n" |
1467 | " },\n" |
1468 | " \"wife\" : {\n" |
1469 | " \"age\" : 36,\n" |
1470 | " \"name\" : \"Marge\"\n" |
1471 | " }\n" |
1472 | " }\n" |
1473 | "}" ; |
1474 | assertTrue (ostr.str() == str); |
1475 | |
1476 | ostr.str("" ); |
1477 | Stringifier::stringify(result, ostr, 2); |
1478 | str = "{\n" |
1479 | " \"Simpsons\" : {\n" |
1480 | " \"address\" : {\n" |
1481 | " \"number\" : 742,\n" |
1482 | " \"street\" : \"Evergreen Terrace\",\n" |
1483 | " \"town\" : \"Springfield\"\n" |
1484 | " },\n" |
1485 | " \"children\" : [\n" |
1486 | " \"Bart\",\n" |
1487 | " \"Lisa\",\n" |
1488 | " \"Maggie\"\n" |
1489 | " ],\n" |
1490 | " \"husband\" : {\n" |
1491 | " \"age\" : 38,\n" |
1492 | " \"name\" : \"Homer\"\n" |
1493 | " },\n" |
1494 | " \"wife\" : {\n" |
1495 | " \"age\" : 36,\n" |
1496 | " \"name\" : \"Marge\"\n" |
1497 | " }\n" |
1498 | " }\n" |
1499 | "}" ; |
1500 | assertTrue (ostr.str() == str); |
1501 | |
1502 | ostr.str("" ); |
1503 | Stringifier::stringify(result, ostr, 4); |
1504 | str = "{\n" |
1505 | " \"Simpsons\" : {\n" |
1506 | " \"address\" : {\n" |
1507 | " \"number\" : 742,\n" |
1508 | " \"street\" : \"Evergreen Terrace\",\n" |
1509 | " \"town\" : \"Springfield\"\n" |
1510 | " },\n" |
1511 | " \"children\" : [\n" |
1512 | " \"Bart\",\n" |
1513 | " \"Lisa\",\n" |
1514 | " \"Maggie\"\n" |
1515 | " ],\n" |
1516 | " \"husband\" : {\n" |
1517 | " \"age\" : 38,\n" |
1518 | " \"name\" : \"Homer\"\n" |
1519 | " },\n" |
1520 | " \"wife\" : {\n" |
1521 | " \"age\" : 36,\n" |
1522 | " \"name\" : \"Marge\"\n" |
1523 | " }\n" |
1524 | " }\n" |
1525 | "}" ; |
1526 | assertTrue (ostr.str() == str); |
1527 | } |
1528 | |
1529 | |
1530 | void JSONTest::testStringifyPreserveOrder() |
1531 | { |
1532 | Object presObj(Poco::JSON_PRESERVE_KEY_ORDER); |
1533 | presObj.set("foo" , 0); |
1534 | presObj.set("bar" , 0); |
1535 | presObj.set("baz" , 0); |
1536 | std::stringstream ss; |
1537 | presObj.stringify(ss); |
1538 | assertTrue (ss.str() == "{\"foo\":0,\"bar\":0,\"baz\":0}" ); |
1539 | Object::NameList nl = presObj.getNames(); |
1540 | assertTrue (nl.size() == 3); |
1541 | assertTrue (nl[0] == "foo" ); |
1542 | assertTrue (nl[1] == "bar" ); |
1543 | assertTrue (nl[2] == "baz" ); |
1544 | |
1545 | ss.str("" ); |
1546 | Stringifier::stringify(presObj, ss); |
1547 | assertTrue (ss.str() == "{\"foo\":0,\"bar\":0,\"baz\":0}" ); |
1548 | |
1549 | Object noPresObj; |
1550 | noPresObj.set("foo" , 0); |
1551 | noPresObj.set("bar" , 0); |
1552 | noPresObj.set("baz" , 0); |
1553 | ss.str("" ); |
1554 | noPresObj.stringify(ss); |
1555 | assertTrue (ss.str() == "{\"bar\":0,\"baz\":0,\"foo\":0}" ); |
1556 | ss.str("" ); |
1557 | Stringifier::stringify(noPresObj, ss); |
1558 | assertTrue (ss.str() == "{\"bar\":0,\"baz\":0,\"foo\":0}" ); |
1559 | |
1560 | std::string json = "{ \"Simpsons\" : { \"husband\" : { \"name\" : \"Homer\" , \"age\" : 38 }, \"wife\" : { \"name\" : \"Marge\", \"age\" : 36 }, " |
1561 | "\"children\" : [ \"Bart\", \"Lisa\", \"Maggie\" ], " |
1562 | "\"address\" : { \"number\" : 742, \"street\" : \"Evergreen Terrace\", \"town\" : \"Springfield\" } } }" ; |
1563 | |
1564 | ParseHandler::Ptr pHandler = new ParseHandler(true); |
1565 | Parser parser(pHandler); |
1566 | Var result; |
1567 | |
1568 | try |
1569 | { |
1570 | result = parser.parse(json); |
1571 | } |
1572 | catch(JSONException& jsone) |
1573 | { |
1574 | std::cout << jsone.message() << std::endl; |
1575 | assertTrue (false); |
1576 | } |
1577 | |
1578 | assertTrue (result.type() == typeid(Object::Ptr)); |
1579 | std::ostringstream ostr; |
1580 | |
1581 | Stringifier::condense(result, ostr); |
1582 | assertTrue (ostr.str() == "{\"Simpsons\":{\"husband\":{\"name\":\"Homer\",\"age\":38},\"wife\":{\"name\":\"Marge\",\"age\":36}," |
1583 | "\"children\":[\"Bart\",\"Lisa\",\"Maggie\"]," |
1584 | "\"address\":{\"number\":742,\"street\":\"Evergreen Terrace\",\"town\":\"Springfield\"}}}" ); |
1585 | |
1586 | ostr.str("" ); |
1587 | Stringifier::stringify(result, ostr); |
1588 | assertTrue (ostr.str() == "{\"Simpsons\":{\"husband\":{\"name\":\"Homer\",\"age\":38},\"wife\":{\"name\":\"Marge\",\"age\":36}," |
1589 | "\"children\":[\"Bart\",\"Lisa\",\"Maggie\"]," |
1590 | "\"address\":{\"number\":742,\"street\":\"Evergreen Terrace\",\"town\":\"Springfield\"}}}" ); |
1591 | |
1592 | ostr.str("" ); |
1593 | Stringifier::stringify(result, ostr, 1); |
1594 | assertTrue (ostr.str() == "{\n" |
1595 | " \"Simpsons\" : {\n" |
1596 | " \"husband\" : {\n" |
1597 | " \"name\" : \"Homer\",\n" |
1598 | " \"age\" : 38\n" |
1599 | " },\n" |
1600 | " \"wife\" : {\n" |
1601 | " \"name\" : \"Marge\",\n" |
1602 | " \"age\" : 36\n" |
1603 | " },\n" |
1604 | " \"children\" : [\n" |
1605 | " \"Bart\",\n" |
1606 | " \"Lisa\",\n" |
1607 | " \"Maggie\"\n" |
1608 | " ],\n" |
1609 | " \"address\" : {\n" |
1610 | " \"number\" : 742,\n" |
1611 | " \"street\" : \"Evergreen Terrace\",\n" |
1612 | " \"town\" : \"Springfield\"\n" |
1613 | " }\n" |
1614 | " }\n" |
1615 | "}" ); |
1616 | |
1617 | ostr.str("" ); |
1618 | Stringifier::stringify(result, ostr, 2); |
1619 | assertTrue (ostr.str() == "{\n" |
1620 | " \"Simpsons\" : {\n" |
1621 | " \"husband\" : {\n" |
1622 | " \"name\" : \"Homer\",\n" |
1623 | " \"age\" : 38\n" |
1624 | " },\n" |
1625 | " \"wife\" : {\n" |
1626 | " \"name\" : \"Marge\",\n" |
1627 | " \"age\" : 36\n" |
1628 | " },\n" |
1629 | " \"children\" : [\n" |
1630 | " \"Bart\",\n" |
1631 | " \"Lisa\",\n" |
1632 | " \"Maggie\"\n" |
1633 | " ],\n" |
1634 | " \"address\" : {\n" |
1635 | " \"number\" : 742,\n" |
1636 | " \"street\" : \"Evergreen Terrace\",\n" |
1637 | " \"town\" : \"Springfield\"\n" |
1638 | " }\n" |
1639 | " }\n" |
1640 | "}" ); |
1641 | |
1642 | ostr.str("" ); |
1643 | Stringifier::stringify(result, ostr, 4); |
1644 | assertTrue (ostr.str() == "{\n" |
1645 | " \"Simpsons\" : {\n" |
1646 | " \"husband\" : {\n" |
1647 | " \"name\" : \"Homer\",\n" |
1648 | " \"age\" : 38\n" |
1649 | " },\n" |
1650 | " \"wife\" : {\n" |
1651 | " \"name\" : \"Marge\",\n" |
1652 | " \"age\" : 36\n" |
1653 | " },\n" |
1654 | " \"children\" : [\n" |
1655 | " \"Bart\",\n" |
1656 | " \"Lisa\",\n" |
1657 | " \"Maggie\"\n" |
1658 | " ],\n" |
1659 | " \"address\" : {\n" |
1660 | " \"number\" : 742,\n" |
1661 | " \"street\" : \"Evergreen Terrace\",\n" |
1662 | " \"town\" : \"Springfield\"\n" |
1663 | " }\n" |
1664 | " }\n" |
1665 | "}" ); |
1666 | |
1667 | Poco::DynamicStruct ds = *result.extract<Object::Ptr>(); |
1668 | assertTrue(ds.toString() == "{ \"Simpsons\" : { \"address\" : { \"number\" : 742, \"street\" : \"Evergreen Terrace\", \"town\" : \"Springfield\" }, " |
1669 | "\"children\" : [ \"Bart\", \"Lisa\", \"Maggie\" ], " |
1670 | "\"husband\" : { \"age\" : 38, \"name\" : \"Homer\" }, " |
1671 | "\"wife\" : { \"age\" : 36, \"name\" : \"Marge\" } } }" ); |
1672 | assertTrue (ds["Simpsons" ].isStruct()); |
1673 | assertFalse(ds["Simpsons" ].isOrdered()); |
1674 | assertTrue (ds["Simpsons" ]["husband" ].isStruct()); |
1675 | assertTrue (ds["Simpsons" ]["husband" ]["name" ] == "Homer" ); |
1676 | assertTrue (ds["Simpsons" ]["husband" ]["age" ] == 38); |
1677 | |
1678 | assertTrue (ds["Simpsons" ]["wife" ].isStruct()); |
1679 | assertTrue (ds["Simpsons" ]["wife" ]["name" ] == "Marge" ); |
1680 | assertTrue (ds["Simpsons" ]["wife" ]["age" ] == 36); |
1681 | |
1682 | assertTrue (ds["Simpsons" ]["children" ].isArray()); |
1683 | assertTrue (ds["Simpsons" ]["children" ][0] == "Bart" ); |
1684 | assertTrue (ds["Simpsons" ]["children" ][1] == "Lisa" ); |
1685 | assertTrue (ds["Simpsons" ]["children" ][2] == "Maggie" ); |
1686 | |
1687 | assertTrue (ds["Simpsons" ]["address" ].isStruct()); |
1688 | assertTrue (ds["Simpsons" ]["address" ]["number" ] == 742); |
1689 | assertTrue (ds["Simpsons" ]["address" ]["street" ] == "Evergreen Terrace" ); |
1690 | assertTrue (ds["Simpsons" ]["address" ]["town" ] == "Springfield" ); |
1691 | |
1692 | #ifdef POCO_ENABLE_CPP11 |
1693 | Poco::OrderedDynamicStruct ods = *result.extract<Object::Ptr>(); |
1694 | assertTrue(ods["Simpsons" ].isStruct()); |
1695 | assertTrue(ods["Simpsons" ].isOrdered()); |
1696 | assertTrue(ods.toString() == "{ \"Simpsons\" : { \"husband\" : { \"name\" : \"Homer\", \"age\" : 38 }, " |
1697 | "\"wife\" : { \"name\" : \"Marge\", \"age\" : 36 }, " |
1698 | "\"children\" : [ \"Bart\", \"Lisa\", \"Maggie\" ], " |
1699 | "\"address\" : { \"number\" : 742, \"street\" : \"Evergreen Terrace\", " |
1700 | "\"town\" : \"Springfield\" } } }" ); |
1701 | #endif // POCO_ENABLE_CPP11 |
1702 | } |
1703 | |
1704 | |
1705 | void JSONTest::testValidJanssonFiles() |
1706 | { |
1707 | Poco::Path pathPattern(getTestFilesPath("valid" )); |
1708 | |
1709 | std::set<std::string> paths; |
1710 | Poco::Glob::glob(pathPattern, paths); |
1711 | |
1712 | for(std::set<std::string>::iterator it = paths.begin(); it != paths.end(); ++it) |
1713 | { |
1714 | Poco::Path filePath(*it, "input" ); |
1715 | |
1716 | if ( filePath.isFile() ) |
1717 | { |
1718 | Poco::File inputFile(filePath); |
1719 | if ( inputFile.exists() ) |
1720 | { |
1721 | Poco::FileInputStream fis(filePath.toString()); |
1722 | std::cout << filePath.toString() << " ... " ; |
1723 | |
1724 | Parser parser; |
1725 | Var result; |
1726 | |
1727 | try |
1728 | { |
1729 | parser.parse(fis); |
1730 | result = parser.asVar(); |
1731 | std::cout << "Ok!" << std::endl; |
1732 | } |
1733 | catch(JSONException& jsone) |
1734 | { |
1735 | std::string err = jsone.displayText(); |
1736 | std::cout << "Failed:" << err << std::endl; |
1737 | fail (err); |
1738 | } |
1739 | catch(Poco::Exception& e) |
1740 | { |
1741 | std::string err = e.displayText(); |
1742 | std::cout << "Failed:" << err << std::endl; |
1743 | fail (err); |
1744 | } |
1745 | } |
1746 | } |
1747 | } |
1748 | } |
1749 | |
1750 | |
1751 | void JSONTest::testInvalidJanssonFiles() |
1752 | { |
1753 | Poco::Path pathPattern(getTestFilesPath("invalid" )); |
1754 | |
1755 | std::set<std::string> paths; |
1756 | Poco::Glob::glob(pathPattern, paths); |
1757 | |
1758 | for(std::set<std::string>::iterator it = paths.begin(); it != paths.end(); ++it) |
1759 | { |
1760 | Poco::Path filePath(*it, "input" ); |
1761 | |
1762 | if (filePath.isFile()) |
1763 | { |
1764 | Poco::File inputFile(filePath); |
1765 | if (inputFile.exists()) |
1766 | { |
1767 | Poco::FileInputStream fis(filePath.toString()); |
1768 | std::cout << filePath.toString() << " ... " ; |
1769 | |
1770 | Parser parser; |
1771 | parser.setAllowNullByte(false); |
1772 | Var result; |
1773 | |
1774 | try |
1775 | { |
1776 | parser.parse(fis); |
1777 | result = parser.asVar(); |
1778 | // We shouldn't get here. |
1779 | std::cout << "We didn't get an exception. This is the result: " << result.convert<std::string>() << std::endl; |
1780 | fail(result.convert<std::string>()); |
1781 | } |
1782 | catch(Poco::Exception& /*ex*/) |
1783 | { |
1784 | std::cout << /*" (" << ex.displayText() << ") " <<*/ "Ok!" << std::endl; |
1785 | } |
1786 | } |
1787 | } |
1788 | } |
1789 | } |
1790 | |
1791 | |
1792 | void JSONTest::testInvalidUnicodeJanssonFiles() |
1793 | { |
1794 | Poco::Path pathPattern(getTestFilesPath("invalid-unicode" )); |
1795 | |
1796 | std::set<std::string> paths; |
1797 | Poco::Glob::glob(pathPattern, paths); |
1798 | |
1799 | for(std::set<std::string>::iterator it = paths.begin(); it != paths.end(); ++it) |
1800 | { |
1801 | Poco::Path filePath(*it, "input" ); |
1802 | |
1803 | if ( filePath.isFile() ) |
1804 | { |
1805 | Poco::File inputFile(filePath); |
1806 | if ( inputFile.exists() ) |
1807 | { |
1808 | Poco::FileInputStream fis(filePath.toString()); |
1809 | std::cout << filePath.toString() << " ... " ; |
1810 | |
1811 | Parser parser; |
1812 | parser.setAllowNullByte(false); |
1813 | Var result; |
1814 | |
1815 | try |
1816 | { |
1817 | parser.parse(fis); |
1818 | result = parser.asVar(); |
1819 | // We shouldn't get here. |
1820 | std::cout << "We didn't get an exception. This is the result: " << result.convert<std::string>() << std::endl; |
1821 | fail(result.convert<std::string>()); |
1822 | } |
1823 | catch(Poco::Exception& /*ex*/) |
1824 | { |
1825 | std::cout << /*" (" << ex.displayText() << ") " <<*/ "Ok!" << std::endl; |
1826 | } |
1827 | } |
1828 | } |
1829 | } |
1830 | } |
1831 | |
1832 | |
1833 | void JSONTest::testTemplate() |
1834 | { |
1835 | Template tpl; |
1836 | tpl.parse("Hello world! From <?= person.name?>.\n<?if person.tooOld?>You're too old.<?endif?>" ); |
1837 | |
1838 | Object::Ptr data = new Object(); |
1839 | Object::Ptr person = new Object(); |
1840 | data->set("person" , person); |
1841 | person->set("name" , "Franky" ); |
1842 | person->set("tooOld" , true); |
1843 | std::ostringstream ostr; |
1844 | tpl.render(data, ostr); |
1845 | std::cout << ostr.str(); |
1846 | assertTrue (ostr.str() == "Hello world! From Franky.\nYou're too old." ); |
1847 | } |
1848 | |
1849 | |
1850 | void JSONTest::testUnicode() |
1851 | { |
1852 | const unsigned char supp[] = {0x61, 0xE1, 0xE9, 0x78, 0xED, 0xF3, 0xFA, 0x0}; |
1853 | std::string text((const char*) supp); |
1854 | |
1855 | std::string json = "{ \"test\" : \"a\\u00E1\\u00E9x\\u00ED\\u00F3\\u00FA\" }" ; |
1856 | Parser parser; |
1857 | |
1858 | Var result; |
1859 | parser.parse(json); |
1860 | result = parser.asVar(); |
1861 | |
1862 | assertTrue (result.type() == typeid(Object::Ptr)); |
1863 | |
1864 | Object::Ptr object = result.extract<Object::Ptr>(); |
1865 | Var test = object->get("test" ); |
1866 | |
1867 | Poco::Latin1Encoding latin1; |
1868 | Poco::UTF8Encoding utf8; |
1869 | Poco::TextConverter converter(latin1, utf8); |
1870 | std::string original; |
1871 | converter.convert(text, original); |
1872 | |
1873 | assertTrue (test.convert<std::string>() == original); |
1874 | |
1875 | parser.reset(); |
1876 | std::ostringstream os; |
1877 | os << '[' << (char) 0x92 << ']'; |
1878 | try |
1879 | { |
1880 | parser.parse(os.str()); |
1881 | fail("Invalid Unicode sequence, must fail." ); |
1882 | } |
1883 | catch (JSONException&) {} |
1884 | |
1885 | parser.reset(); |
1886 | os.str("" ); |
1887 | os << "[\"" << (char)0xC2 << (char)0x92 << "\"]" ; |
1888 | result = parser.parse(os.str()); |
1889 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
1890 | |
1891 | parser.reset(); |
1892 | os.str("" ); |
1893 | os << "[\"" << (char)0xAC << "\"]" ; |
1894 | try |
1895 | { |
1896 | parser.parse(os.str()); |
1897 | fail("Invalid Unicode sequence, must fail." ); |
1898 | } |
1899 | catch (JSONException&) {} |
1900 | |
1901 | parser.reset(); |
1902 | os.str("" ); |
1903 | os << "[\"" << (char)0xE2 << (char)0x82 << (char)0xAC << "\"]" ; |
1904 | result = parser.parse(os.str()); |
1905 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
1906 | |
1907 | parser.reset(); |
1908 | os.str("" ); |
1909 | os << "[\"" << (char)0xA2 << "\"]" ; |
1910 | try |
1911 | { |
1912 | parser.parse(os.str()); |
1913 | fail("Invalid Unicode sequence, must fail." ); |
1914 | } |
1915 | catch (JSONException&){} |
1916 | |
1917 | parser.reset(); |
1918 | os.str("" ); |
1919 | os << "[\"" << (char)0xF0 << (char)0xA4 << (char)0xAD << (char)0xAD << "\"]" ; |
1920 | result = parser.parse(os.str()); |
1921 | assertTrue (result.type() == typeid(Poco::JSON::Array::Ptr)); |
1922 | } |
1923 | |
1924 | |
1925 | void JSONTest::testSmallBuffer() |
1926 | { |
1927 | Poco::JSON::Parser parser(new Poco::JSON::ParseHandler(), 4); |
1928 | std::string jsonStr = "{ \"x\" : \"123456789012345678901234567890123456789012345678901234567890\" }" ; |
1929 | parser.parse(jsonStr); |
1930 | } |
1931 | |
1932 | void JSONTest::testEscape0() |
1933 | { |
1934 | Poco::JSON::Object::Ptr json = new Poco::JSON::Object(); |
1935 | |
1936 | std::string nullString("B\0b" , 3); |
1937 | json->set("name" , nullString); |
1938 | |
1939 | std::stringstream ss; |
1940 | json->stringify(ss); |
1941 | |
1942 | assertTrue (ss.str().compare("{\"name\":\"B\\u0000b\"}" ) == 0); |
1943 | } |
1944 | |
1945 | |
1946 | void JSONTest::testNonEscapeUnicode() |
1947 | { |
1948 | std::string chinese("{\"arr\":[{ \"name\" : \"\\u4e2d\" }]}" ); |
1949 | Poco::JSON::Parser parser(new Poco::JSON::ParseHandler()); |
1950 | Var result = parser.parse(chinese); |
1951 | |
1952 | assertTrue (result.type() == typeid(Object::Ptr)); |
1953 | |
1954 | Object::Ptr object = result.extract<Object::Ptr>(); |
1955 | object->setEscapeUnicode(false); |
1956 | |
1957 | std::stringstream ss; |
1958 | object->stringify(ss); |
1959 | assertTrue (ss.str().compare("{\"arr\":[{\"name\":\"\xE4\xB8\xAD\"}]}" ) == 0); |
1960 | |
1961 | const unsigned char utf8Chars[] = {'{', '"', 'a', 'r', 'r', '"', ':', '[', '{', '"', 'n', 'a', 'm', 'e', '"', ':', |
1962 | '"', 'g', 0xC3, 0xBC, 'n', 't', 'e', 'r', '"', '}', ']', '}', 0}; |
1963 | std::string utf8Text((const char*) utf8Chars); |
1964 | parser.reset(); |
1965 | result = parser.parse(utf8Text); |
1966 | object = result.extract<Object::Ptr>(); |
1967 | object->setEscapeUnicode(false); |
1968 | |
1969 | ss.str("" ); object->stringify(ss); |
1970 | assertTrue (ss.str() == "{\"arr\":[{\"name\":\"g\xC3\xBCnter\"}]}" ); |
1971 | |
1972 | Poco::JSON::Object obj1; |
1973 | std::string shortEscapeStr("String with \t" ); |
1974 | std::string longEscapeStr("String with \a and \v plus \t for good measure" ); |
1975 | obj1.set("shortEscape" , shortEscapeStr); |
1976 | obj1.set("longEscape" , longEscapeStr); |
1977 | |
1978 | ss.str("" ); |
1979 | obj1.stringify(ss); |
1980 | |
1981 | parser.reset(); |
1982 | parser.parse(ss.str()); |
1983 | result = parser.asVar(); |
1984 | |
1985 | assertTrue (result.type() == typeid(Object::Ptr)); |
1986 | |
1987 | object = result.extract<Object::Ptr>(); |
1988 | Var shortEscape = object->get("shortEscape" ); |
1989 | Var longEscape = object->get("longEscape" ); |
1990 | assertTrue (shortEscape.convert<std::string>() == shortEscapeStr); |
1991 | assertTrue (longEscape.convert<std::string>() == longEscapeStr); |
1992 | } |
1993 | |
1994 | |
1995 | void JSONTest::testEscapeUnicode() |
1996 | { |
1997 | Poco::JSON::Object::Ptr json = new Poco::JSON::Object(); |
1998 | std::string chinese("{ \"name\" : \"\\u4e2d\" }" ); |
1999 | Poco::JSON::Parser parser(new Poco::JSON::ParseHandler()); |
2000 | Var result = parser.parse(chinese); |
2001 | |
2002 | assertTrue (result.type() == typeid(Object::Ptr)); |
2003 | |
2004 | Object::Ptr object = result.extract<Object::Ptr>(); |
2005 | object->setEscapeUnicode(true); |
2006 | |
2007 | std::stringstream ss; |
2008 | object->stringify(ss, 0, -1); |
2009 | assertTrue (ss.str().compare("{\"name\":\"\\u4E2D\"}" ) == 0); |
2010 | |
2011 | const unsigned char utf8Chars[] = {'{', '"', 'n', 'a', 'm', 'e', '"', ':', |
2012 | '"', 'g', 0xC3, 0xBC, 'n', 't', 'e', 'r', '"', '}', 0}; |
2013 | std::string utf8Text((const char*) utf8Chars); |
2014 | parser.reset(); |
2015 | result = parser.parse(utf8Text); |
2016 | object = result.extract<Object::Ptr>(); |
2017 | object->setEscapeUnicode(true); |
2018 | ss.str("" ); object->stringify(ss, 0, -1); |
2019 | assertTrue (ss.str() == "{\"name\":\"g\\u00FCnter\"}" ); |
2020 | } |
2021 | |
2022 | |
2023 | std::string JSONTest::getTestFilesPath(const std::string& type) |
2024 | { |
2025 | std::ostringstream ostr; |
2026 | ostr << "data/" << type << '/'; |
2027 | std::string validDir(ostr.str()); |
2028 | Poco::Path pathPattern(validDir); |
2029 | if (Poco::File(pathPattern).exists()) |
2030 | { |
2031 | validDir += '*'; |
2032 | return validDir; |
2033 | } |
2034 | |
2035 | ostr.str("" ); |
2036 | ostr << "/JSON/testsuite/data/" << type << '/'; |
2037 | validDir = Poco::Environment::get("POCO_BASE" ) + ostr.str(); |
2038 | std::cout << validDir << std::endl; |
2039 | pathPattern = validDir; |
2040 | |
2041 | if (Poco::File(pathPattern).exists()) |
2042 | validDir += '*'; |
2043 | else |
2044 | { |
2045 | std::cout << "Can't find " << validDir << std::endl; |
2046 | throw Poco::NotFoundException("cannot locate directory containing valid JSON test files" ); |
2047 | } |
2048 | return validDir; |
2049 | } |
2050 | |
2051 | |
2052 | void JSONTest::testCopy() |
2053 | { |
2054 | Object obj1(Poco::JSON_PRESERVE_KEY_ORDER); |
2055 | obj1.set("foo" , 0); |
2056 | obj1.set("bar" , 0); |
2057 | obj1.set("baz" , 0); |
2058 | |
2059 | Object::NameList nl = obj1.getNames(); |
2060 | assertTrue (nl.size() == 3); |
2061 | assertTrue (nl[0] == "foo" ); |
2062 | assertTrue (nl[1] == "bar" ); |
2063 | assertTrue (nl[2] == "baz" ); |
2064 | |
2065 | Object obj2; |
2066 | obj2 = obj1; |
2067 | nl = obj2.getNames(); |
2068 | assertTrue (nl.size() == 3); |
2069 | assertTrue (nl[0] == "foo" ); |
2070 | assertTrue (nl[1] == "bar" ); |
2071 | assertTrue (nl[2] == "baz" ); |
2072 | |
2073 | Object obj3; |
2074 | obj3.set("foo" , 0); |
2075 | obj3.set("bar" , 0); |
2076 | obj3.set("baz" , 0); |
2077 | nl = obj3.getNames(); |
2078 | assertTrue (nl.size() == 3); |
2079 | assertTrue (nl[0] == "bar" ); |
2080 | assertTrue (nl[1] == "baz" ); |
2081 | assertTrue (nl[2] == "foo" ); |
2082 | |
2083 | Object obj4; |
2084 | obj4 = obj3; |
2085 | nl = obj4.getNames(); |
2086 | assertTrue (nl.size() == 3); |
2087 | assertTrue (nl[0] == "bar" ); |
2088 | assertTrue (nl[1] == "baz" ); |
2089 | assertTrue (nl[2] == "foo" ); |
2090 | |
2091 | obj4 = obj1; |
2092 | nl = obj4.getNames(); |
2093 | assertTrue (nl.size() == 3); |
2094 | assertTrue (nl[0] == "foo" ); |
2095 | assertTrue (nl[1] == "bar" ); |
2096 | assertTrue (nl[2] == "baz" ); |
2097 | |
2098 | Object obj5(obj1); |
2099 | nl = obj5.getNames(); |
2100 | assertTrue (nl.size() == 3); |
2101 | assertTrue (nl[0] == "foo" ); |
2102 | assertTrue (nl[1] == "bar" ); |
2103 | assertTrue (nl[2] == "baz" ); |
2104 | } |
2105 | |
2106 | |
2107 | void JSONTest::testMove() |
2108 | { |
2109 | Object obj1(Poco::JSON_PRESERVE_KEY_ORDER); |
2110 | obj1.set("foo" , 0); |
2111 | obj1.set("bar" , 0); |
2112 | obj1.set("baz" , 0); |
2113 | |
2114 | Object::NameList nl = obj1.getNames(); |
2115 | assertTrue (nl.size() == 3); |
2116 | assertTrue (nl[0] == "foo" ); |
2117 | assertTrue (nl[1] == "bar" ); |
2118 | assertTrue (nl[2] == "baz" ); |
2119 | |
2120 | Object obj2; |
2121 | obj2 = std::move(obj1); |
2122 | assertTrue (obj1.getNames().size() == 0); |
2123 | |
2124 | nl = obj2.getNames(); |
2125 | assertTrue (nl.size() == 3); |
2126 | assertTrue (nl[0] == "foo" ); |
2127 | assertTrue (nl[1] == "bar" ); |
2128 | assertTrue (nl[2] == "baz" ); |
2129 | |
2130 | Object obj3; |
2131 | obj3.set("foo" , 0); |
2132 | obj3.set("bar" , 0); |
2133 | obj3.set("baz" , 0); |
2134 | nl = obj3.getNames(); |
2135 | assertTrue (nl.size() == 3); |
2136 | assertTrue (nl[0] == "bar" ); |
2137 | assertTrue (nl[1] == "baz" ); |
2138 | assertTrue (nl[2] == "foo" ); |
2139 | |
2140 | Object obj4; |
2141 | obj4 = std::move(obj3); |
2142 | assertTrue (obj3.getNames().size() == 0); |
2143 | |
2144 | nl = obj4.getNames(); |
2145 | assertTrue (nl.size() == 3); |
2146 | assertTrue (nl[0] == "bar" ); |
2147 | assertTrue (nl[1] == "baz" ); |
2148 | assertTrue (nl[2] == "foo" ); |
2149 | |
2150 | Object obj5(Poco::JSON_PRESERVE_KEY_ORDER); |
2151 | obj5.set("foo" , 0); |
2152 | obj5.set("bar" , 0); |
2153 | obj5.set("baz" , 0); |
2154 | nl = obj5.getNames(); |
2155 | assertTrue (nl.size() == 3); |
2156 | assertTrue (nl[0] == "foo" ); |
2157 | assertTrue (nl[1] == "bar" ); |
2158 | assertTrue (nl[2] == "baz" ); |
2159 | |
2160 | obj4 = std::move(obj5); |
2161 | assertTrue (obj5.getNames().size() == 0); |
2162 | |
2163 | nl = obj4.getNames(); |
2164 | assertTrue (nl.size() == 3); |
2165 | assertTrue (nl[0] == "foo" ); |
2166 | assertTrue (nl[1] == "bar" ); |
2167 | assertTrue (nl[2] == "baz" ); |
2168 | } |
2169 | |
2170 | |
2171 | CppUnit::Test* JSONTest::suite() |
2172 | { |
2173 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONTest" ); |
2174 | |
2175 | CppUnit_addTest(pSuite, JSONTest, testNullProperty); |
2176 | CppUnit_addTest(pSuite, JSONTest, testTrueProperty); |
2177 | CppUnit_addTest(pSuite, JSONTest, testFalseProperty); |
2178 | CppUnit_addTest(pSuite, JSONTest, testNumberProperty); |
2179 | CppUnit_addTest(pSuite, JSONTest, testUnsignedNumberProperty); |
2180 | #if defined(POCO_HAVE_INT64) |
2181 | CppUnit_addTest(pSuite, JSONTest, testNumber64Property); |
2182 | CppUnit_addTest(pSuite, JSONTest, testUnsignedNumber64Property); |
2183 | #endif |
2184 | CppUnit_addTest(pSuite, JSONTest, testStringProperty); |
2185 | CppUnit_addTest(pSuite, JSONTest, testEmptyObject); |
2186 | CppUnit_addTest(pSuite, JSONTest, testEmptyPropertyName); |
2187 | CppUnit_addTest(pSuite, JSONTest, testComplexObject); |
2188 | CppUnit_addTest(pSuite, JSONTest, testDoubleProperty); |
2189 | CppUnit_addTest(pSuite, JSONTest, testDouble2Property); |
2190 | CppUnit_addTest(pSuite, JSONTest, testDouble3Property); |
2191 | CppUnit_addTest(pSuite, JSONTest, testObjectProperty); |
2192 | CppUnit_addTest(pSuite, JSONTest, testObjectArray); |
2193 | CppUnit_addTest(pSuite, JSONTest, testArrayOfObjects); |
2194 | CppUnit_addTest(pSuite, JSONTest, testEmptyArray); |
2195 | CppUnit_addTest(pSuite, JSONTest, testNestedArray); |
2196 | CppUnit_addTest(pSuite, JSONTest, testNullElement); |
2197 | CppUnit_addTest(pSuite, JSONTest, testTrueElement); |
2198 | CppUnit_addTest(pSuite, JSONTest, testFalseElement); |
2199 | CppUnit_addTest(pSuite, JSONTest, testNumberElement); |
2200 | CppUnit_addTest(pSuite, JSONTest, testStringElement); |
2201 | CppUnit_addTest(pSuite, JSONTest, testEmptyObjectElement); |
2202 | CppUnit_addTest(pSuite, JSONTest, testDoubleElement); |
2203 | CppUnit_addTest(pSuite, JSONTest, testSetArrayElement); |
2204 | CppUnit_addTest(pSuite, JSONTest, testOptValue); |
2205 | CppUnit_addTest(pSuite, JSONTest, testQuery); |
2206 | CppUnit_addTest(pSuite, JSONTest, testComment); |
2207 | CppUnit_addTest(pSuite, JSONTest, testPrintHandler); |
2208 | CppUnit_addTest(pSuite, JSONTest, testStringify); |
2209 | CppUnit_addTest(pSuite, JSONTest, testStringifyPreserveOrder); |
2210 | CppUnit_addTest(pSuite, JSONTest, testValidJanssonFiles); |
2211 | CppUnit_addTest(pSuite, JSONTest, testInvalidJanssonFiles); |
2212 | CppUnit_addTest(pSuite, JSONTest, testInvalidUnicodeJanssonFiles); |
2213 | CppUnit_addTest(pSuite, JSONTest, testTemplate); |
2214 | CppUnit_addTest(pSuite, JSONTest, testUnicode); |
2215 | CppUnit_addTest(pSuite, JSONTest, testSmallBuffer); |
2216 | CppUnit_addTest(pSuite, JSONTest, testEscape0); |
2217 | CppUnit_addTest(pSuite, JSONTest, testNonEscapeUnicode); |
2218 | CppUnit_addTest(pSuite, JSONTest, testEscapeUnicode); |
2219 | CppUnit_addTest(pSuite, JSONTest, testCopy); |
2220 | CppUnit_addTest(pSuite, JSONTest, testMove); |
2221 | |
2222 | return pSuite; |
2223 | } |
2224 | |