rippled
Loading...
Searching...
No Matches
Value.cpp
1#include <xrpl/beast/core/LexicalCast.h>
2#include <xrpl/json/json_errors.h>
3#include <xrpl/json/json_reader.h>
4#include <xrpl/json/json_value.h>
5#include <xrpl/json/json_writer.h>
6
7#include <doctest/doctest.h>
8
9#include <algorithm>
10#include <cmath>
11#include <regex>
12#include <sstream>
13#include <string>
14
15namespace ripple {
16
17TEST_SUITE_BEGIN("json_value");
18
19TEST_CASE("limits")
20{
21 using namespace Json;
22 static_assert(Value::minInt == Int(~(UInt(-1) / 2)));
23 static_assert(Value::maxInt == Int(UInt(-1) / 2));
24 static_assert(Value::maxUInt == UInt(-1));
25}
26
27TEST_CASE("construct and compare Json::StaticString")
28{
29 static constexpr char sample[]{"Contents of a Json::StaticString"};
30
31 static constexpr Json::StaticString test1(sample);
32 char const* addrTest1{test1};
33
34 CHECK(addrTest1 == &sample[0]);
35 CHECK(test1.c_str() == &sample[0]);
36
37 static constexpr Json::StaticString test2{
38 "Contents of a Json::StaticString"};
39 static constexpr Json::StaticString test3{"Another StaticString"};
40
41 CHECK(test1 == test2);
42 CHECK(test1 != test3);
43
44 std::string str{sample};
45 CHECK(str == test2);
46 CHECK(str != test3);
47 CHECK(test2 == str);
48 CHECK(test3 != str);
49}
50
51TEST_CASE("different types")
52{
53 // Exercise ValueType constructor
54 static constexpr Json::StaticString staticStr{"staticStr"};
55
56 auto testCopy = [](Json::ValueType typ) {
57 Json::Value val{typ};
58 Json::Value cpy{val};
59 CHECK(val.type() == typ);
60 CHECK(cpy.type() == typ);
61 return val;
62 };
63 {
64 Json::Value const nullV{testCopy(Json::nullValue)};
65 CHECK(nullV.isNull());
66 CHECK(!nullV.isBool());
67 CHECK(!nullV.isInt());
68 CHECK(!nullV.isUInt());
69 CHECK(!nullV.isIntegral());
70 CHECK(!nullV.isDouble());
71 CHECK(!nullV.isNumeric());
72 CHECK(!nullV.isString());
73 CHECK(!nullV.isArray());
74 CHECK(nullV.isArrayOrNull());
75 CHECK(!nullV.isObject());
76 CHECK(nullV.isObjectOrNull());
77 }
78 {
79 Json::Value const intV{testCopy(Json::intValue)};
80 CHECK(!intV.isNull());
81 CHECK(!intV.isBool());
82 CHECK(intV.isInt());
83 CHECK(!intV.isUInt());
84 CHECK(intV.isIntegral());
85 CHECK(!intV.isDouble());
86 CHECK(intV.isNumeric());
87 CHECK(!intV.isString());
88 CHECK(!intV.isArray());
89 CHECK(!intV.isArrayOrNull());
90 CHECK(!intV.isObject());
91 CHECK(!intV.isObjectOrNull());
92 }
93 {
94 Json::Value const uintV{testCopy(Json::uintValue)};
95 CHECK(!uintV.isNull());
96 CHECK(!uintV.isBool());
97 CHECK(!uintV.isInt());
98 CHECK(uintV.isUInt());
99 CHECK(uintV.isIntegral());
100 CHECK(!uintV.isDouble());
101 CHECK(uintV.isNumeric());
102 CHECK(!uintV.isString());
103 CHECK(!uintV.isArray());
104 CHECK(!uintV.isArrayOrNull());
105 CHECK(!uintV.isObject());
106 CHECK(!uintV.isObjectOrNull());
107 }
108 {
109 Json::Value const realV{testCopy(Json::realValue)};
110 CHECK(!realV.isNull());
111 CHECK(!realV.isBool());
112 CHECK(!realV.isInt());
113 CHECK(!realV.isUInt());
114 CHECK(!realV.isIntegral());
115 CHECK(realV.isDouble());
116 CHECK(realV.isNumeric());
117 CHECK(!realV.isString());
118 CHECK(!realV.isArray());
119 CHECK(!realV.isArrayOrNull());
120 CHECK(!realV.isObject());
121 CHECK(!realV.isObjectOrNull());
122 }
123 {
124 Json::Value const stringV{testCopy(Json::stringValue)};
125 CHECK(!stringV.isNull());
126 CHECK(!stringV.isBool());
127 CHECK(!stringV.isInt());
128 CHECK(!stringV.isUInt());
129 CHECK(!stringV.isIntegral());
130 CHECK(!stringV.isDouble());
131 CHECK(!stringV.isNumeric());
132 CHECK(stringV.isString());
133 CHECK(!stringV.isArray());
134 CHECK(!stringV.isArrayOrNull());
135 CHECK(!stringV.isObject());
136 CHECK(!stringV.isObjectOrNull());
137 }
138 {
139 Json::Value const staticStrV{staticStr};
140 {
141 Json::Value cpy{staticStrV};
142 CHECK(staticStrV.type() == Json::stringValue);
143 CHECK(cpy.type() == Json::stringValue);
144 }
145 CHECK(!staticStrV.isNull());
146 CHECK(!staticStrV.isBool());
147 CHECK(!staticStrV.isInt());
148 CHECK(!staticStrV.isUInt());
149 CHECK(!staticStrV.isIntegral());
150 CHECK(!staticStrV.isDouble());
151 CHECK(!staticStrV.isNumeric());
152 CHECK(staticStrV.isString());
153 CHECK(!staticStrV.isArray());
154 CHECK(!staticStrV.isArrayOrNull());
155 CHECK(!staticStrV.isObject());
156 CHECK(!staticStrV.isObjectOrNull());
157 }
158 {
159 Json::Value const boolV{testCopy(Json::booleanValue)};
160 CHECK(!boolV.isNull());
161 CHECK(boolV.isBool());
162 CHECK(!boolV.isInt());
163 CHECK(!boolV.isUInt());
164 CHECK(boolV.isIntegral());
165 CHECK(!boolV.isDouble());
166 CHECK(boolV.isNumeric());
167 CHECK(!boolV.isString());
168 CHECK(!boolV.isArray());
169 CHECK(!boolV.isArrayOrNull());
170 CHECK(!boolV.isObject());
171 CHECK(!boolV.isObjectOrNull());
172 }
173 {
174 Json::Value const arrayV{testCopy(Json::arrayValue)};
175 CHECK(!arrayV.isNull());
176 CHECK(!arrayV.isBool());
177 CHECK(!arrayV.isInt());
178 CHECK(!arrayV.isUInt());
179 CHECK(!arrayV.isIntegral());
180 CHECK(!arrayV.isDouble());
181 CHECK(!arrayV.isNumeric());
182 CHECK(!arrayV.isString());
183 CHECK(arrayV.isArray());
184 CHECK(arrayV.isArrayOrNull());
185 CHECK(!arrayV.isObject());
186 CHECK(!arrayV.isObjectOrNull());
187 }
188 {
189 Json::Value const objectV{testCopy(Json::objectValue)};
190 CHECK(!objectV.isNull());
191 CHECK(!objectV.isBool());
192 CHECK(!objectV.isInt());
193 CHECK(!objectV.isUInt());
194 CHECK(!objectV.isIntegral());
195 CHECK(!objectV.isDouble());
196 CHECK(!objectV.isNumeric());
197 CHECK(!objectV.isString());
198 CHECK(!objectV.isArray());
199 CHECK(!objectV.isArrayOrNull());
200 CHECK(objectV.isObject());
201 CHECK(objectV.isObjectOrNull());
202 }
203}
204
205TEST_CASE("compare strings")
206{
207 auto doCompare = [&](Json::Value const& lhs,
208 Json::Value const& rhs,
209 bool lhsEqRhs,
210 bool lhsLtRhs,
211 int line) {
212 CAPTURE(line);
213 CHECK((lhs == rhs) == lhsEqRhs);
214 CHECK((lhs != rhs) != lhsEqRhs);
215 CHECK((lhs < rhs) == (!(lhsEqRhs || !lhsLtRhs)));
216 CHECK((lhs <= rhs) == (lhsEqRhs || lhsLtRhs));
217 CHECK((lhs >= rhs) == (lhsEqRhs || !lhsLtRhs));
218 CHECK((lhs > rhs) == (!(lhsEqRhs || lhsLtRhs)));
219 };
220
221 Json::Value const null0;
222 Json::Value const intNeg1{-1};
223 Json::Value const int0{Json::intValue};
224 Json::Value const intPos1{1};
225 Json::Value const uint0{Json::uintValue};
226 Json::Value const uint1{1u};
227 Json::Value const realNeg1{-1.0};
228 Json::Value const real0{Json::realValue};
229 Json::Value const realPos1{1.0};
230 Json::Value const str0{Json::stringValue};
231 Json::Value const str1{"1"};
232 Json::Value const boolF{false};
233 Json::Value const boolT{true};
234 Json::Value const array0{Json::arrayValue};
235 Json::Value const array1{[]() {
236 Json::Value array1;
237 array1[0u] = 1;
238 return array1;
239 }()};
240 Json::Value const obj0{Json::objectValue};
241 Json::Value const obj1{[]() {
242 Json::Value obj1;
243 obj1["one"] = 1;
244 return obj1;
245 }()};
246
247#pragma push_macro("DO_COMPARE")
248 // DO_COMPARE(lhs, rhs, lhsEqualsToRhs lhsLessThanRhs)
249#define DO_COMPARE(lhs, rhs, eq, lt) doCompare(lhs, rhs, eq, lt, __LINE__)
250 DO_COMPARE(null0, Json::Value{}, true, false);
251 DO_COMPARE(null0, intNeg1, false, true);
252 DO_COMPARE(null0, int0, false, true);
253 DO_COMPARE(null0, intPos1, false, true);
254 DO_COMPARE(null0, uint0, false, true);
255 DO_COMPARE(null0, uint1, false, true);
256 DO_COMPARE(null0, realNeg1, false, true);
257 DO_COMPARE(null0, real0, false, true);
258 DO_COMPARE(null0, realPos1, false, true);
259 DO_COMPARE(null0, str0, false, true);
260 DO_COMPARE(null0, str1, false, true);
261 DO_COMPARE(null0, boolF, false, true);
262 DO_COMPARE(null0, boolT, false, true);
263 DO_COMPARE(null0, array0, false, true);
264 DO_COMPARE(null0, array1, false, true);
265 DO_COMPARE(null0, obj0, false, true);
266 DO_COMPARE(null0, obj1, false, true);
267
268 DO_COMPARE(intNeg1, null0, false, false);
269 DO_COMPARE(intNeg1, intNeg1, true, false);
270 DO_COMPARE(intNeg1, int0, false, true);
271 DO_COMPARE(intNeg1, intPos1, false, true);
272 DO_COMPARE(intNeg1, uint0, false, true);
273 DO_COMPARE(intNeg1, uint1, false, true);
274 DO_COMPARE(intNeg1, realNeg1, false, true);
275 DO_COMPARE(intNeg1, real0, false, true);
276 DO_COMPARE(intNeg1, realPos1, false, true);
277 DO_COMPARE(intNeg1, str0, false, true);
278 DO_COMPARE(intNeg1, str1, false, true);
279 DO_COMPARE(intNeg1, boolF, false, true);
280 DO_COMPARE(intNeg1, boolT, false, true);
281 DO_COMPARE(intNeg1, array0, false, true);
282 DO_COMPARE(intNeg1, array1, false, true);
283 DO_COMPARE(intNeg1, obj0, false, true);
284 DO_COMPARE(intNeg1, obj1, false, true);
285
286 DO_COMPARE(int0, null0, false, false);
287 DO_COMPARE(int0, intNeg1, false, false);
288 DO_COMPARE(int0, int0, true, false);
289 DO_COMPARE(int0, intPos1, false, true);
290 DO_COMPARE(int0, uint0, true, false);
291 DO_COMPARE(int0, uint1, false, true);
292 DO_COMPARE(int0, realNeg1, false, true);
293 DO_COMPARE(int0, real0, false, true);
294 DO_COMPARE(int0, realPos1, false, true);
295 DO_COMPARE(int0, str0, false, true);
296 DO_COMPARE(int0, str1, false, true);
297 DO_COMPARE(int0, boolF, false, true);
298 DO_COMPARE(int0, boolT, false, true);
299 DO_COMPARE(int0, array0, false, true);
300 DO_COMPARE(int0, array1, false, true);
301 DO_COMPARE(int0, obj0, false, true);
302 DO_COMPARE(int0, obj1, false, true);
303
304 DO_COMPARE(intPos1, null0, false, false);
305 DO_COMPARE(intPos1, intNeg1, false, false);
306 DO_COMPARE(intPos1, int0, false, false);
307 DO_COMPARE(intPos1, intPos1, true, false);
308 DO_COMPARE(intPos1, uint0, false, false);
309 DO_COMPARE(intPos1, uint1, true, false);
310 DO_COMPARE(intPos1, realNeg1, false, true);
311 DO_COMPARE(intPos1, real0, false, true);
312 DO_COMPARE(intPos1, realPos1, false, true);
313 DO_COMPARE(intPos1, str0, false, true);
314 DO_COMPARE(intPos1, str1, false, true);
315 DO_COMPARE(intPos1, boolF, false, true);
316 DO_COMPARE(intPos1, boolT, false, true);
317 DO_COMPARE(intPos1, array0, false, true);
318 DO_COMPARE(intPos1, array1, false, true);
319 DO_COMPARE(intPos1, obj0, false, true);
320 DO_COMPARE(intPos1, obj1, false, true);
321
322 DO_COMPARE(uint0, null0, false, false);
323 DO_COMPARE(uint0, intNeg1, false, false);
324 DO_COMPARE(uint0, int0, true, false);
325 DO_COMPARE(uint0, intPos1, false, true);
326 DO_COMPARE(uint0, uint0, true, false);
327 DO_COMPARE(uint0, uint1, false, true);
328 DO_COMPARE(uint0, realNeg1, false, true);
329 DO_COMPARE(uint0, real0, false, true);
330 DO_COMPARE(uint0, realPos1, false, true);
331 DO_COMPARE(uint0, str0, false, true);
332 DO_COMPARE(uint0, str1, false, true);
333 DO_COMPARE(uint0, boolF, false, true);
334 DO_COMPARE(uint0, boolT, false, true);
335 DO_COMPARE(uint0, array0, false, true);
336 DO_COMPARE(uint0, array1, false, true);
337 DO_COMPARE(uint0, obj0, false, true);
338 DO_COMPARE(uint0, obj1, false, true);
339
340 DO_COMPARE(uint1, null0, false, false);
341 DO_COMPARE(uint1, intNeg1, false, false);
342 DO_COMPARE(uint1, int0, false, false);
343 DO_COMPARE(uint1, intPos1, true, false);
344 DO_COMPARE(uint1, uint0, false, false);
345 DO_COMPARE(uint1, uint1, true, false);
346 DO_COMPARE(uint1, realNeg1, false, true);
347 DO_COMPARE(uint1, real0, false, true);
348 DO_COMPARE(uint1, realPos1, false, true);
349 DO_COMPARE(uint1, str0, false, true);
350 DO_COMPARE(uint1, str1, false, true);
351 DO_COMPARE(uint1, boolF, false, true);
352 DO_COMPARE(uint1, boolT, false, true);
353 DO_COMPARE(uint1, array0, false, true);
354 DO_COMPARE(uint1, array1, false, true);
355 DO_COMPARE(uint1, obj0, false, true);
356 DO_COMPARE(uint1, obj1, false, true);
357
358 DO_COMPARE(realNeg1, null0, false, false);
359 DO_COMPARE(realNeg1, intNeg1, false, false);
360 DO_COMPARE(realNeg1, int0, false, false);
361 DO_COMPARE(realNeg1, intPos1, false, false);
362 DO_COMPARE(realNeg1, uint0, false, false);
363 DO_COMPARE(realNeg1, uint1, false, false);
364 DO_COMPARE(realNeg1, realNeg1, true, false);
365 DO_COMPARE(realNeg1, real0, false, true);
366 DO_COMPARE(realNeg1, realPos1, false, true);
367 DO_COMPARE(realNeg1, str0, false, true);
368 DO_COMPARE(realNeg1, str1, false, true);
369 DO_COMPARE(realNeg1, boolF, false, true);
370 DO_COMPARE(realNeg1, boolT, false, true);
371 DO_COMPARE(realNeg1, array0, false, true);
372 DO_COMPARE(realNeg1, array1, false, true);
373 DO_COMPARE(realNeg1, obj0, false, true);
374 DO_COMPARE(realNeg1, obj1, false, true);
375
376 DO_COMPARE(real0, null0, false, false);
377 DO_COMPARE(real0, intNeg1, false, false);
378 DO_COMPARE(real0, int0, false, false);
379 DO_COMPARE(real0, intPos1, false, false);
380 DO_COMPARE(real0, uint0, false, false);
381 DO_COMPARE(real0, uint1, false, false);
382 DO_COMPARE(real0, realNeg1, false, false);
383 DO_COMPARE(real0, real0, true, false);
384 DO_COMPARE(real0, realPos1, false, true);
385 DO_COMPARE(real0, str0, false, true);
386 DO_COMPARE(real0, str1, false, true);
387 DO_COMPARE(real0, boolF, false, true);
388 DO_COMPARE(real0, boolT, false, true);
389 DO_COMPARE(real0, array0, false, true);
390 DO_COMPARE(real0, array1, false, true);
391 DO_COMPARE(real0, obj0, false, true);
392 DO_COMPARE(real0, obj1, false, true);
393
394 DO_COMPARE(realPos1, null0, false, false);
395 DO_COMPARE(realPos1, intNeg1, false, false);
396 DO_COMPARE(realPos1, int0, false, false);
397 DO_COMPARE(realPos1, intPos1, false, false);
398 DO_COMPARE(realPos1, uint0, false, false);
399 DO_COMPARE(realPos1, uint1, false, false);
400 DO_COMPARE(realPos1, realNeg1, false, false);
401 DO_COMPARE(realPos1, real0, false, false);
402 DO_COMPARE(realPos1, realPos1, true, false);
403 DO_COMPARE(realPos1, str0, false, true);
404 DO_COMPARE(realPos1, str1, false, true);
405 DO_COMPARE(realPos1, boolF, false, true);
406 DO_COMPARE(realPos1, boolT, false, true);
407 DO_COMPARE(realPos1, array0, false, true);
408 DO_COMPARE(realPos1, array1, false, true);
409 DO_COMPARE(realPos1, obj0, false, true);
410 DO_COMPARE(realPos1, obj1, false, true);
411
412 DO_COMPARE(str0, null0, false, false);
413 DO_COMPARE(str0, intNeg1, false, false);
414 DO_COMPARE(str0, int0, false, false);
415 DO_COMPARE(str0, intPos1, false, false);
416 DO_COMPARE(str0, uint0, false, false);
417 DO_COMPARE(str0, uint1, false, false);
418 DO_COMPARE(str0, realNeg1, false, false);
419 DO_COMPARE(str0, real0, false, false);
420 DO_COMPARE(str0, realPos1, false, false);
421 DO_COMPARE(str0, str0, true, false);
422 DO_COMPARE(str0, str1, false, true);
423 DO_COMPARE(str0, boolF, false, true);
424 DO_COMPARE(str0, boolT, false, true);
425 DO_COMPARE(str0, array0, false, true);
426 DO_COMPARE(str0, array1, false, true);
427 DO_COMPARE(str0, obj0, false, true);
428 DO_COMPARE(str0, obj1, false, true);
429
430 DO_COMPARE(str1, null0, false, false);
431 DO_COMPARE(str1, intNeg1, false, false);
432 DO_COMPARE(str1, int0, false, false);
433 DO_COMPARE(str1, intPos1, false, false);
434 DO_COMPARE(str1, uint0, false, false);
435 DO_COMPARE(str1, uint1, false, false);
436 DO_COMPARE(str1, realNeg1, false, false);
437 DO_COMPARE(str1, real0, false, false);
438 DO_COMPARE(str1, realPos1, false, false);
439 DO_COMPARE(str1, str0, false, false);
440 DO_COMPARE(str1, str1, true, false);
441 DO_COMPARE(str1, boolF, false, true);
442 DO_COMPARE(str1, boolT, false, true);
443 DO_COMPARE(str1, array0, false, true);
444 DO_COMPARE(str1, array1, false, true);
445 DO_COMPARE(str1, obj0, false, true);
446 DO_COMPARE(str1, obj1, false, true);
447
448 DO_COMPARE(boolF, null0, false, false);
449 DO_COMPARE(boolF, intNeg1, false, false);
450 DO_COMPARE(boolF, int0, false, false);
451 DO_COMPARE(boolF, intPos1, false, false);
452 DO_COMPARE(boolF, uint0, false, false);
453 DO_COMPARE(boolF, uint1, false, false);
454 DO_COMPARE(boolF, realNeg1, false, false);
455 DO_COMPARE(boolF, real0, false, false);
456 DO_COMPARE(boolF, realPos1, false, false);
457 DO_COMPARE(boolF, str0, false, false);
458 DO_COMPARE(boolF, str1, false, false);
459 DO_COMPARE(boolF, boolF, true, false);
460 DO_COMPARE(boolF, boolT, false, true);
461 DO_COMPARE(boolF, array0, false, true);
462 DO_COMPARE(boolF, array1, false, true);
463 DO_COMPARE(boolF, obj0, false, true);
464 DO_COMPARE(boolF, obj1, false, true);
465
466 DO_COMPARE(boolT, null0, false, false);
467 DO_COMPARE(boolT, intNeg1, false, false);
468 DO_COMPARE(boolT, int0, false, false);
469 DO_COMPARE(boolT, intPos1, false, false);
470 DO_COMPARE(boolT, uint0, false, false);
471 DO_COMPARE(boolT, uint1, false, false);
472 DO_COMPARE(boolT, realNeg1, false, false);
473 DO_COMPARE(boolT, real0, false, false);
474 DO_COMPARE(boolT, realPos1, false, false);
475 DO_COMPARE(boolT, str0, false, false);
476 DO_COMPARE(boolT, str1, false, false);
477 DO_COMPARE(boolT, boolF, false, false);
478 DO_COMPARE(boolT, boolT, true, false);
479 DO_COMPARE(boolT, array0, false, true);
480 DO_COMPARE(boolT, array1, false, true);
481 DO_COMPARE(boolT, obj0, false, true);
482 DO_COMPARE(boolT, obj1, false, true);
483
484 DO_COMPARE(array0, null0, false, false);
485 DO_COMPARE(array0, intNeg1, false, false);
486 DO_COMPARE(array0, int0, false, false);
487 DO_COMPARE(array0, intPos1, false, false);
488 DO_COMPARE(array0, uint0, false, false);
489 DO_COMPARE(array0, uint1, false, false);
490 DO_COMPARE(array0, realNeg1, false, false);
491 DO_COMPARE(array0, real0, false, false);
492 DO_COMPARE(array0, realPos1, false, false);
493 DO_COMPARE(array0, str0, false, false);
494 DO_COMPARE(array0, str1, false, false);
495 DO_COMPARE(array0, boolF, false, false);
496 DO_COMPARE(array0, boolT, false, false);
497 DO_COMPARE(array0, array0, true, false);
498 DO_COMPARE(array0, array1, false, true);
499 DO_COMPARE(array0, obj0, false, true);
500 DO_COMPARE(array0, obj1, false, true);
501
502 DO_COMPARE(array1, null0, false, false);
503 DO_COMPARE(array1, intNeg1, false, false);
504 DO_COMPARE(array1, int0, false, false);
505 DO_COMPARE(array1, intPos1, false, false);
506 DO_COMPARE(array1, uint0, false, false);
507 DO_COMPARE(array1, uint1, false, false);
508 DO_COMPARE(array1, realNeg1, false, false);
509 DO_COMPARE(array1, real0, false, false);
510 DO_COMPARE(array1, realPos1, false, false);
511 DO_COMPARE(array1, str0, false, false);
512 DO_COMPARE(array1, str1, false, false);
513 DO_COMPARE(array1, boolF, false, false);
514 DO_COMPARE(array1, boolT, false, false);
515 DO_COMPARE(array1, array0, false, false);
516 DO_COMPARE(array1, array1, true, false);
517 DO_COMPARE(array1, obj0, false, true);
518 DO_COMPARE(array1, obj1, false, true);
519
520 DO_COMPARE(obj0, null0, false, false);
521 DO_COMPARE(obj0, intNeg1, false, false);
522 DO_COMPARE(obj0, int0, false, false);
523 DO_COMPARE(obj0, intPos1, false, false);
524 DO_COMPARE(obj0, uint0, false, false);
525 DO_COMPARE(obj0, uint1, false, false);
526 DO_COMPARE(obj0, realNeg1, false, false);
527 DO_COMPARE(obj0, real0, false, false);
528 DO_COMPARE(obj0, realPos1, false, false);
529 DO_COMPARE(obj0, str0, false, false);
530 DO_COMPARE(obj0, str1, false, false);
531 DO_COMPARE(obj0, boolF, false, false);
532 DO_COMPARE(obj0, boolT, false, false);
533 DO_COMPARE(obj0, array0, false, false);
534 DO_COMPARE(obj0, array1, false, false);
535 DO_COMPARE(obj0, obj0, true, false);
536 DO_COMPARE(obj0, obj1, false, true);
537
538 DO_COMPARE(obj1, null0, false, false);
539 DO_COMPARE(obj1, intNeg1, false, false);
540 DO_COMPARE(obj1, int0, false, false);
541 DO_COMPARE(obj1, intPos1, false, false);
542 DO_COMPARE(obj1, uint0, false, false);
543 DO_COMPARE(obj1, uint1, false, false);
544 DO_COMPARE(obj1, realNeg1, false, false);
545 DO_COMPARE(obj1, real0, false, false);
546 DO_COMPARE(obj1, realPos1, false, false);
547 DO_COMPARE(obj1, str0, false, false);
548 DO_COMPARE(obj1, str1, false, false);
549 DO_COMPARE(obj1, boolF, false, false);
550 DO_COMPARE(obj1, boolT, false, false);
551 DO_COMPARE(obj1, array0, false, false);
552 DO_COMPARE(obj1, array1, false, false);
553 DO_COMPARE(obj1, obj0, false, false);
554 DO_COMPARE(obj1, obj1, true, false);
555#undef DO_COMPARE
556#pragma pop_macro("DO_COMPARE")
557}
558
560{
561 CHECK(!Json::Value());
562
563 CHECK(!Json::Value(""));
564
565 CHECK(bool(Json::Value("empty")));
566 CHECK(bool(Json::Value(false)));
567 CHECK(bool(Json::Value(true)));
568 CHECK(bool(Json::Value(0)));
569 CHECK(bool(Json::Value(1)));
570
572 CHECK(!array);
573 array.append(0);
574 CHECK(bool(array));
575
577 CHECK(!object);
578 object[""] = false;
579 CHECK(bool(object));
580}
581
582TEST_CASE("bad json")
583{
584 char const* s(R"({"method":"ledger","params":[{"ledger_index":1e300}]})");
585
586 Json::Value j;
587 Json::Reader r;
588
589 CHECK(r.parse(s, j));
590}
591
592TEST_CASE("edge cases")
593{
597
598 std::uint32_t a_uint = max_uint - 1978;
599 std::int32_t a_large_int = max_int - 1978;
600 std::int32_t a_small_int = min_int + 1978;
601
602 {
603 std::string json = "{\"max_uint\":" + std::to_string(max_uint);
604 json += ",\"max_int\":" + std::to_string(max_int);
605 json += ",\"min_int\":" + std::to_string(min_int);
606 json += ",\"a_uint\":" + std::to_string(a_uint);
607 json += ",\"a_large_int\":" + std::to_string(a_large_int);
608 json += ",\"a_small_int\":" + std::to_string(a_small_int);
609 json += "}";
610
611 Json::Value j1;
612 Json::Reader r1;
613
614 CHECK(r1.parse(json, j1));
615 CHECK(j1["max_uint"].asUInt() == max_uint);
616 CHECK(j1["max_uint"].asAbsUInt() == max_uint);
617 CHECK(j1["max_int"].asInt() == max_int);
618 CHECK(j1["max_int"].asAbsUInt() == max_int);
619 CHECK(j1["min_int"].asInt() == min_int);
620 CHECK(
621 j1["min_int"].asAbsUInt() ==
622 static_cast<std::int64_t>(min_int) * -1);
623 CHECK(j1["a_uint"].asUInt() == a_uint);
624 CHECK(j1["a_uint"].asAbsUInt() == a_uint);
625 CHECK(j1["a_uint"] > a_large_int);
626 CHECK(j1["a_uint"] > a_small_int);
627 CHECK(j1["a_large_int"].asInt() == a_large_int);
628 CHECK(j1["a_large_int"].asAbsUInt() == a_large_int);
629 CHECK(j1["a_large_int"].asUInt() == a_large_int);
630 CHECK(j1["a_large_int"] < a_uint);
631 CHECK(j1["a_small_int"].asInt() == a_small_int);
632 CHECK(
633 j1["a_small_int"].asAbsUInt() ==
634 static_cast<std::int64_t>(a_small_int) * -1);
635 CHECK(j1["a_small_int"] < a_uint);
636 }
637
638 std::uint64_t overflow = std::uint64_t(max_uint) + 1;
639 {
640 std::string json = "{\"overflow\":";
641 json += std::to_string(overflow);
642 json += "}";
643
644 Json::Value j2;
645 Json::Reader r2;
646
647 CHECK(!r2.parse(json, j2));
648 }
649
650 std::int64_t underflow = std::int64_t(min_int) - 1;
651 {
652 std::string json = "{\"underflow\":";
653 json += std::to_string(underflow);
654 json += "}";
655
656 Json::Value j3;
657 Json::Reader r3;
658
659 CHECK(!r3.parse(json, j3));
660 }
661
662 {
663 Json::Value intString{std::to_string(overflow)};
664 CHECK_THROWS_AS(intString.asUInt(), beast::BadLexicalCast);
665 CHECK_THROWS_AS(intString.asAbsUInt(), Json::error);
666
667 intString = "4294967295";
668 CHECK(intString.asUInt() == 4294967295u);
669 CHECK(intString.asAbsUInt() == 4294967295u);
670
671 intString = "0";
672 CHECK(intString.asUInt() == 0);
673 CHECK(intString.asAbsUInt() == 0);
674
675 intString = "-1";
676 CHECK_THROWS_AS(intString.asUInt(), beast::BadLexicalCast);
677 CHECK(intString.asAbsUInt() == 1);
678
679 intString = "-4294967295";
680 CHECK(intString.asAbsUInt() == 4294967295);
681
682 intString = "-4294967296";
683 CHECK_THROWS_AS(intString.asAbsUInt(), Json::error);
684
685 intString = "2147483648";
686 CHECK_THROWS_AS(intString.asInt(), beast::BadLexicalCast);
687 CHECK(intString.asAbsUInt() == 2147483648);
688
689 intString = "2147483647";
690 CHECK(intString.asInt() == 2147483647);
691 CHECK(intString.asAbsUInt() == 2147483647);
692
693 intString = "-2147483648";
694 CHECK(intString.asInt() == -2147483648LL); // MSVC wants the LL
695 CHECK(intString.asAbsUInt() == 2147483648LL);
696
697 intString = "-2147483649";
698 CHECK_THROWS_AS(intString.asInt(), beast::BadLexicalCast);
699 CHECK(intString.asAbsUInt() == 2147483649);
700 }
701
702 {
703 Json::Value intReal{4294967297.0};
704 CHECK_THROWS_AS(intReal.asUInt(), Json::error);
705 CHECK_THROWS_AS(intReal.asAbsUInt(), Json::error);
706
707 intReal = 4294967295.0;
708 CHECK(intReal.asUInt() == 4294967295u);
709 CHECK(intReal.asAbsUInt() == 4294967295u);
710
711 intReal = 0.0;
712 CHECK(intReal.asUInt() == 0);
713 CHECK(intReal.asAbsUInt() == 0);
714
715 intReal = -1.0;
716 CHECK_THROWS_AS(intReal.asUInt(), Json::error);
717 CHECK(intReal.asAbsUInt() == 1);
718
719 intReal = -4294967295.0;
720 CHECK(intReal.asAbsUInt() == 4294967295);
721
722 intReal = -4294967296.0;
723 CHECK_THROWS_AS(intReal.asAbsUInt(), Json::error);
724
725 intReal = 2147483648.0;
726 CHECK_THROWS_AS(intReal.asInt(), Json::error);
727 CHECK(intReal.asAbsUInt() == 2147483648);
728
729 intReal = 2147483647.0;
730 CHECK(intReal.asInt() == 2147483647);
731 CHECK(intReal.asAbsUInt() == 2147483647);
732
733 intReal = -2147483648.0;
734 CHECK(intReal.asInt() == -2147483648LL); // MSVC wants the LL
735 CHECK(intReal.asAbsUInt() == 2147483648LL);
736
737 intReal = -2147483649.0;
738 CHECK_THROWS_AS(intReal.asInt(), Json::error);
739 CHECK(intReal.asAbsUInt() == 2147483649);
740 }
741}
742
744{
745 Json::Value v1{2.5};
746 CHECK(v1.isDouble());
747 CHECK(v1.asDouble() == 2.5);
748
749 Json::Value v2 = v1;
750 CHECK(v1.isDouble());
751 CHECK(v1.asDouble() == 2.5);
752 CHECK(v2.isDouble());
753 CHECK(v2.asDouble() == 2.5);
754 CHECK(v1 == v2);
755
756 v1 = v2;
757 CHECK(v1.isDouble());
758 CHECK(v1.asDouble() == 2.5);
759 CHECK(v2.isDouble());
760 CHECK(v2.asDouble() == 2.5);
761 CHECK(v1 == v2);
762}
763
765{
766 Json::Value v1{2.5};
767 CHECK(v1.isDouble());
768 CHECK(v1.asDouble() == 2.5);
769
770 Json::Value v2 = std::move(v1);
771 CHECK(!v1);
772 CHECK(v2.isDouble());
773 CHECK(v2.asDouble() == 2.5);
774 CHECK(v1 != v2);
775
776 v1 = std::move(v2);
777 CHECK(v1.isDouble());
778 CHECK(v1.asDouble() == 2.5);
779 CHECK(!v2);
780 CHECK(v1 != v2);
781}
782
783TEST_CASE("comparisons")
784{
785 Json::Value a, b;
786 auto testEquals = [&](std::string const& name) {
787 CHECK(a == b);
788 CHECK(a <= b);
789 CHECK(a >= b);
790
791 CHECK(!(a != b));
792 CHECK(!(a < b));
793 CHECK(!(a > b));
794
795 CHECK(b == a);
796 CHECK(b <= a);
797 CHECK(b >= a);
798
799 CHECK(!(b != a));
800 CHECK(!(b < a));
801 CHECK(!(b > a));
802 };
803
804 auto testGreaterThan = [&](std::string const& name) {
805 CHECK(!(a == b));
806 CHECK(!(a <= b));
807 CHECK(a >= b);
808
809 CHECK(a != b);
810 CHECK(!(a < b));
811 CHECK(a > b);
812
813 CHECK(!(b == a));
814 CHECK(b <= a);
815 CHECK(!(b >= a));
816
817 CHECK(b != a);
818 CHECK(b < a);
819 CHECK(!(b > a));
820 };
821
822 a["a"] = Json::UInt(0);
823 b["a"] = Json::Int(0);
824 testEquals("zero");
825
826 b["a"] = Json::Int(-1);
827 testGreaterThan("negative");
828
830 Json::UInt bigger = big;
831 bigger++;
832
833 a["a"] = bigger;
834 b["a"] = big;
835 testGreaterThan("big");
836}
837
838TEST_CASE("compact")
839{
840 Json::Value j;
841 Json::Reader r;
842 char const* s("{\"array\":[{\"12\":23},{},null,false,0.5]}");
843
844 auto countLines = [](std::string const& str) {
845 return 1 + std::count_if(str.begin(), str.end(), [](char c) {
846 return c == '\n';
847 });
848 };
849
850 CHECK(r.parse(s, j));
851 {
853 ss << j;
854 CHECK(countLines(ss.str()) > 1);
855 }
856 {
858 ss << Json::Compact(std::move(j));
859 CHECK(countLines(ss.str()) == 1);
860 }
861}
862
863TEST_CASE("conversions")
864{
865 // We have Json::Int, but not Json::Double or Json::Real.
866 // We have Json::Int, Json::Value::Int, and Json::ValueType::intValue.
867 // We have Json::ValueType::realValue but Json::Value::asDouble.
868 // TODO: What's the thinking here?
869 {
870 // null
871 Json::Value val;
872 CHECK(val.isNull());
873 // val.asCString() should trigger an assertion failure
874 CHECK(val.asString() == "");
875 CHECK(val.asInt() == 0);
876 CHECK(val.asUInt() == 0);
877 CHECK(val.asAbsUInt() == 0);
878 CHECK(val.asDouble() == 0.0);
879 CHECK(val.asBool() == false);
880
889 }
890 {
891 // int
892 Json::Value val = -1234;
893 CHECK(val.isInt());
894 // val.asCString() should trigger an assertion failure
895 CHECK(val.asString() == "-1234");
896 CHECK(val.asInt() == -1234);
897 CHECK_THROWS_AS(val.asUInt(), Json::error);
898 CHECK(val.asAbsUInt() == 1234u);
899 CHECK(val.asDouble() == -1234.0);
900 CHECK(val.asBool() == true);
901
910 }
911 {
912 // uint
913 Json::Value val = 1234U;
914 CHECK(val.isUInt());
915 // val.asCString() should trigger an assertion failure
916 CHECK(val.asString() == "1234");
917 CHECK(val.asInt() == 1234);
918 CHECK(val.asUInt() == 1234u);
919 CHECK(val.asAbsUInt() == 1234u);
920 CHECK(val.asDouble() == 1234.0);
921 CHECK(val.asBool() == true);
922
931 }
932 {
933 // real
934 Json::Value val = 2.0;
935 CHECK(val.isDouble());
936 // val.asCString() should trigger an assertion failure
937 CHECK(std::regex_match(val.asString(), std::regex("^2\\.0*$")));
938 CHECK(val.asInt() == 2);
939 CHECK(val.asUInt() == 2u);
940 CHECK(val.asAbsUInt() == 2u);
941 CHECK(val.asDouble() == 2.0);
942 CHECK(val.asBool() == true);
943
952 }
953 {
954 // numeric string
955 Json::Value val = "54321";
956 CHECK(val.isString());
957 CHECK(strcmp(val.asCString(), "54321") == 0);
958 CHECK(val.asString() == "54321");
959 CHECK(val.asInt() == 54321);
960 CHECK(val.asUInt() == 54321u);
961 CHECK(val.asAbsUInt() == 54321);
962 CHECK_THROWS_AS(val.asDouble(), Json::error);
963 CHECK(val.asBool() == true);
964
973 }
974 {
975 // non-numeric string
977 CHECK(val.isString());
978 CHECK(val.asCString() == nullptr);
979 CHECK(val.asString() == "");
980 CHECK_THROWS_AS(val.asInt(), std::exception);
981 CHECK_THROWS_AS(val.asUInt(), std::exception);
982 CHECK_THROWS_AS(val.asAbsUInt(), std::exception);
983 CHECK_THROWS_AS(val.asDouble(), std::exception);
984 CHECK(val.asBool() == false);
985
994 }
995 {
996 // bool false
997 Json::Value val = false;
998 CHECK(val.isBool());
999 // val.asCString() should trigger an assertion failure
1000 CHECK(val.asString() == "false");
1001 CHECK(val.asInt() == 0);
1002 CHECK(val.asUInt() == 0);
1003 CHECK(val.asAbsUInt() == 0);
1004 CHECK(val.asDouble() == 0.0);
1005 CHECK(val.asBool() == false);
1006
1015 }
1016 {
1017 // bool true
1018 Json::Value val = true;
1019 CHECK(val.isBool());
1020 // val.asCString() should trigger an assertion failure
1021 CHECK(val.asString() == "true");
1022 CHECK(val.asInt() == 1);
1023 CHECK(val.asUInt() == 1);
1024 CHECK(val.asAbsUInt() == 1);
1025 CHECK(val.asDouble() == 1.0);
1026 CHECK(val.asBool() == true);
1027
1036 }
1037 {
1038 // array type
1040 CHECK(val.isArray());
1041 // val.asCString should trigger an assertion failure
1042 CHECK_THROWS_AS(val.asString(), Json::error);
1043 CHECK_THROWS_AS(val.asInt(), Json::error);
1044 CHECK_THROWS_AS(val.asUInt(), Json::error);
1045 CHECK_THROWS_AS(val.asAbsUInt(), Json::error);
1046 CHECK_THROWS_AS(val.asDouble(), Json::error);
1047 CHECK(val.asBool() == false); // empty or not
1048
1057 }
1058 {
1059 // object type
1061 CHECK(val.isObject());
1062 // val.asCString should trigger an assertion failure
1063 CHECK_THROWS_AS(val.asString(), Json::error);
1064 CHECK_THROWS_AS(val.asInt(), Json::error);
1065 CHECK_THROWS_AS(val.asUInt(), Json::error);
1066 CHECK_THROWS_AS(val.asAbsUInt(), Json::error);
1067 CHECK_THROWS_AS(val.asDouble(), Json::error);
1068 CHECK(val.asBool() == false); // empty or not
1069
1078 }
1079}
1080
1081TEST_CASE("access members")
1082{
1083 Json::Value val;
1084 CHECK(val.type() == Json::nullValue);
1085 CHECK(val.size() == 0);
1086 CHECK(!val.isValidIndex(0));
1087 CHECK(!val.isMember("key"));
1088 {
1089 Json::Value const constVal = val;
1090 CHECK(constVal[7u].type() == Json::nullValue);
1091 CHECK(!constVal.isMember("key"));
1092 CHECK(constVal["key"].type() == Json::nullValue);
1093 CHECK(constVal.getMemberNames().empty());
1094 CHECK(constVal.get(1u, "default0") == "default0");
1095 CHECK(constVal.get(std::string("not"), "oh") == "oh");
1096 CHECK(constVal.get("missing", "default2") == "default2");
1097 }
1098
1099 val = -7;
1100 CHECK(val.type() == Json::intValue);
1101 CHECK(val.size() == 0);
1102 CHECK(!val.isValidIndex(0));
1103 CHECK(!val.isMember("key"));
1104
1105 val = 42u;
1106 CHECK(val.type() == Json::uintValue);
1107 CHECK(val.size() == 0);
1108 CHECK(!val.isValidIndex(0));
1109 CHECK(!val.isMember("key"));
1110
1111 val = 3.14159;
1112 CHECK(val.type() == Json::realValue);
1113 CHECK(val.size() == 0);
1114 CHECK(!val.isValidIndex(0));
1115 CHECK(!val.isMember("key"));
1116
1117 val = true;
1118 CHECK(val.type() == Json::booleanValue);
1119 CHECK(val.size() == 0);
1120 CHECK(!val.isValidIndex(0));
1121 CHECK(!val.isMember("key"));
1122
1123 val = "string";
1124 CHECK(val.type() == Json::stringValue);
1125 CHECK(val.size() == 0);
1126 CHECK(!val.isValidIndex(0));
1127 CHECK(!val.isMember("key"));
1128
1130 CHECK(val.type() == Json::objectValue);
1131 CHECK(val.size() == 0);
1132 static Json::StaticString const staticThree("three");
1133 val[staticThree] = 3;
1134 val["two"] = 2;
1135 CHECK(val.size() == 2);
1136 CHECK(val.isValidIndex(1));
1137 CHECK(!val.isValidIndex(2));
1138 CHECK(val[staticThree] == 3);
1139 CHECK(val.isMember("two"));
1140 CHECK(val.isMember(staticThree));
1141 CHECK(!val.isMember("key"));
1142 {
1143 Json::Value const constVal = val;
1144 CHECK(constVal["two"] == 2);
1145 CHECK(constVal["four"].type() == Json::nullValue);
1146 CHECK(constVal[staticThree] == 3);
1147 CHECK(constVal.isMember("two"));
1148 CHECK(constVal.isMember(staticThree));
1149 CHECK(!constVal.isMember("key"));
1150 CHECK(val.get(std::string("two"), "backup") == 2);
1151 CHECK(val.get("missing", "default2") == "default2");
1152 }
1153
1155 CHECK(val.type() == Json::arrayValue);
1156 CHECK(val.size() == 0);
1157 val[0u] = "zero";
1158 val[1u] = "one";
1159 CHECK(val.size() == 2);
1160 CHECK(val.isValidIndex(1));
1161 CHECK(!val.isValidIndex(2));
1162 CHECK(val[20u].type() == Json::nullValue);
1163 CHECK(!val.isMember("key"));
1164 {
1165 Json::Value const constVal = val;
1166 CHECK(constVal[0u] == "zero");
1167 CHECK(constVal[2u].type() == Json::nullValue);
1168 CHECK(!constVal.isMember("key"));
1169 CHECK(val.get(1u, "default0") == "one");
1170 CHECK(val.get(3u, "default1") == "default1");
1171 }
1172}
1173
1174TEST_CASE("remove members")
1175{
1176 Json::Value val;
1177 CHECK(val.removeMember(std::string("member")).type() == Json::nullValue);
1178
1180 static Json::StaticString const staticThree("three");
1181 val[staticThree] = 3;
1182 val["two"] = 2;
1183 CHECK(val.size() == 2);
1184
1186 CHECK(val.size() == 2);
1187
1188 CHECK(val.removeMember(staticThree) == 3);
1189 CHECK(val.size() == 1);
1190
1191 CHECK(val.removeMember(staticThree).type() == Json::nullValue);
1192 CHECK(val.size() == 1);
1193
1194 CHECK(val.removeMember(std::string("two")) == 2);
1195 CHECK(val.size() == 0);
1196
1198 CHECK(val.size() == 0);
1199}
1200
1201TEST_CASE("iterator")
1202{
1203 {
1204 // Iterating an array.
1206 arr[0u] = "zero";
1207 arr[1u] = "one";
1208 arr[2u] = "two";
1209 arr[3u] = "three";
1210
1211 Json::ValueIterator const b{arr.begin()};
1212 Json::ValueIterator const e{arr.end()};
1213
1214 Json::ValueIterator i1 = b;
1215 Json::ValueIterator i2 = e;
1216 --i2;
1217
1218 // key(), index(), and memberName() on an object iterator.
1219 CHECK(b != e);
1220 CHECK(!(b == e));
1221 CHECK(i1.key() == 0);
1222 CHECK(i2.key() == 3);
1223 CHECK(i1.index() == 0);
1224 CHECK(i2.index() == 3);
1225 CHECK(std::strcmp(i1.memberName(), "") == 0);
1226 CHECK(std::strcmp(i2.memberName(), "") == 0);
1227
1228 // Pre and post increment and decrement.
1229 *i1++ = "0";
1230 CHECK(*i1 == "one");
1231 *i1 = "1";
1232 ++i1;
1233
1234 *i2-- = "3";
1235 CHECK(*i2 == "two");
1236 CHECK(i1 == i2);
1237 *i2 = "2";
1238 CHECK(*i1 == "2");
1239 }
1240 {
1241 // Iterating a const object.
1242 Json::Value const obj{[]() {
1244 obj["0"] = 0;
1245 obj["1"] = 1;
1246 obj["2"] = 2;
1247 obj["3"] = 3;
1248 return obj;
1249 }()};
1250
1251 Json::ValueConstIterator i1{obj.begin()};
1252 Json::ValueConstIterator i2{obj.end()};
1253 --i2;
1254
1255 // key(), index(), and memberName() on an object iterator.
1256 CHECK(i1 != i2);
1257 CHECK(!(i1 == i2));
1258 CHECK(i1.key() == "0");
1259 CHECK(i2.key() == "3");
1260 CHECK(i1.index() == -1);
1261 CHECK(i2.index() == -1);
1262 CHECK(std::strcmp(i1.memberName(), "0") == 0);
1263 CHECK(std::strcmp(i2.memberName(), "3") == 0);
1264
1265 // Pre and post increment and decrement.
1266 CHECK(*i1++ == 0);
1267 CHECK(*i1 == 1);
1268 ++i1;
1269
1270 CHECK(*i2-- == 3);
1271 CHECK(*i2 == 2);
1272 CHECK(i1 == i2);
1273 CHECK(*i1 == 2);
1274 }
1275 {
1276 // Iterating a non-const null object.
1277 Json::Value nul{};
1278 CHECK(nul.begin() == nul.end());
1279 }
1280 {
1281 // Iterating a const Int.
1282 Json::Value const i{-3};
1283 CHECK(i.begin() == i.end());
1284 }
1285}
1286
1287TEST_CASE("nest limits")
1288{
1289 Json::Reader r;
1290 {
1291 auto nest = [](std::uint32_t depth) -> std::string {
1292 std::string s = "{";
1293 for (std::uint32_t i{1}; i <= depth; ++i)
1294 s += "\"obj\":{";
1295 for (std::uint32_t i{1}; i <= depth; ++i)
1296 s += "}";
1297 s += "}";
1298 return s;
1299 };
1300
1301 {
1302 // Within object nest limit
1303 auto json{nest(std::min(10u, Json::Reader::nest_limit))};
1304 Json::Value j;
1305 CHECK(r.parse(json, j));
1306 }
1307
1308 {
1309 // Exceed object nest limit
1310 auto json{nest(Json::Reader::nest_limit + 1)};
1311 Json::Value j;
1312 CHECK(!r.parse(json, j));
1313 }
1314 }
1315
1316 auto nest = [](std::uint32_t depth) -> std::string {
1317 std::string s = "{";
1318 for (std::uint32_t i{1}; i <= depth; ++i)
1319 s += "\"array\":[{";
1320 for (std::uint32_t i{1}; i <= depth; ++i)
1321 s += "]}";
1322 s += "}";
1323 return s;
1324 };
1325 {
1326 // Exceed array nest limit
1327 auto json{nest(Json::Reader::nest_limit + 1)};
1328 Json::Value j;
1329 CHECK(!r.parse(json, j));
1330 }
1331}
1332
1333TEST_CASE("memory leak")
1334{
1335 // When run with the address sanitizer, this test confirms there is no
1336 // memory leak with the scenarios below.
1337 {
1338 Json::Value a;
1339 a[0u] = 1;
1340 CHECK(a.type() == Json::arrayValue);
1341 CHECK(a[0u].type() == Json::intValue);
1342 a = std::move(a[0u]);
1343 CHECK(a.type() == Json::intValue);
1344 }
1345 {
1346 Json::Value b;
1347 Json::Value temp;
1348 temp["a"] = "Probably avoids the small string optimization";
1349 temp["b"] = "Also probably avoids the small string optimization";
1350 CHECK(temp.type() == Json::objectValue);
1351 b.append(temp);
1352 CHECK(temp.type() == Json::objectValue);
1353 CHECK(b.size() == 1);
1354
1355 b.append(std::move(temp));
1356 CHECK(b.size() == 2);
1357
1358 // Note that the type() == nullValue check is implementation
1359 // specific and not guaranteed to be valid in the future.
1360 CHECK(temp.type() == Json::nullValue);
1361 }
1362}
1363
1365
1366} // namespace ripple
Decorator for streaming out compact json.
Unserialize a JSON document into a Value.
Definition json_reader.h:20
static constexpr unsigned nest_limit
Definition json_reader.h:73
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Lightweight wrapper to tag static string.
Definition json_value.h:45
constexpr char const * c_str() const
Definition json_value.h:58
const iterator for object and array value.
Definition json_value.h:559
Value key() const
Return either the index or the member name of the referenced value as a Value.
char const * memberName() const
Return the member name of the referenced Value.
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
Iterator for object and array value.
Definition json_value.h:620
Represents a JSON value.
Definition json_value.h:131
bool isArray() const
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
bool isDouble() const
char const * asCString() const
Int asInt() const
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
bool isString() const
UInt asUInt() const
Members getMemberNames() const
Return a list of the member names.
ValueType type() const
bool isObject() const
Value removeMember(char const *key)
Remove and return the named member.
bool isValidIndex(UInt index) const
Return true if index < size().
std::string asString() const
Returns the unquoted string value.
bool isBool() const
bool asBool() const
bool isUInt() const
bool isNull() const
isNull() tests to see if this field is null.
bool isMember(char const *key) const
Return true if the object has a member named key.
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool isConvertibleTo(ValueType other) const
double asDouble() const
bool isInt() const
T count_if(T... args)
T empty(T... args)
T max(T... args)
T min(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:6
ValueType
Type of the value held by a Value object.
Definition json_value.h:19
@ booleanValue
bool value
Definition json_value.h:25
@ nullValue
'null' value
Definition json_value.h:20
@ stringValue
UTF-8 string value.
Definition json_value.h:24
@ realValue
double value
Definition json_value.h:23
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ intValue
signed integer value
Definition json_value.h:21
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
@ uintValue
unsigned integer value
Definition json_value.h:22
int Int
unsigned int UInt
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
TEST_SUITE_END()
TEST_CASE("limits")
Definition Value.cpp:19
TEST_SUITE_BEGIN("json_value")
T regex_match(T... args)
T str(T... args)
T strcmp(T... args)
Thrown when a conversion is not possible with LexicalCast.
T to_string(T... args)