1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
6 #if defined(__GNUC__)
7 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
9 #elif defined(_MSC_VER)
10 #pragma warning(disable : 4996)
11 #endif
12
13 #include "fuzz.h"
14 #include "jsontest.h"
15 #include <cmath>
16 #include <cstring>
17 #include <functional>
18 #include <iomanip>
19 #include <iostream>
20 #include <iterator>
21 #include <json/config.h>
22 #include <json/json.h>
23 #include <limits>
24 #include <memory>
25 #include <sstream>
26 #include <string>
27
28 typedef Json::CharReader* CharReaderPtr;
29
30 // Make numeric limits more convenient to talk about.
31 // Assumes int type in 32 bits.
32 #define kint32max Json::Value::maxInt
33 #define kint32min Json::Value::minInt
34 #define kuint32max Json::Value::maxUInt
35 #define kint64max Json::Value::maxInt64
36 #define kint64min Json::Value::minInt64
37 #define kuint64max Json::Value::maxUInt64
38
39 // static const double kdint64max = double(kint64max);
40 // static const float kfint64max = float(kint64max);
41 static const float kfint32max = float(kint32max);
42 static const float kfuint32max = float(kuint32max);
43
44 // //////////////////////////////////////////////////////////////////
45 // //////////////////////////////////////////////////////////////////
46 // Json Library test cases
47 // //////////////////////////////////////////////////////////////////
48 // //////////////////////////////////////////////////////////////////
49
50 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
uint64ToDouble(Json::UInt64 value)51 static inline double uint64ToDouble(Json::UInt64 value) {
52 return static_cast<double>(value);
53 }
54 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
uint64ToDouble(Json::UInt64 value)55 static inline double uint64ToDouble(Json::UInt64 value) {
56 return static_cast<double>(Json::Int64(value / 2)) * 2.0 +
57 static_cast<double>(Json::Int64(value & 1));
58 }
59 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
60
61 // local_ is the collection for the testcases in this code file.
62 static std::deque<JsonTest::TestCaseFactory> local_;
63 #define JSONTEST_FIXTURE_LOCAL(FixtureType, name) \
64 JSONTEST_FIXTURE_V2(FixtureType, name, local_)
65
66 struct ValueTest : JsonTest::TestCase {
67 Json::Value null_;
68 Json::Value emptyArray_;
69 Json::Value emptyObject_;
70 Json::Value integer_;
71 Json::Value unsignedInteger_;
72 Json::Value smallUnsignedInteger_;
73 Json::Value real_;
74 Json::Value float_;
75 Json::Value array1_;
76 Json::Value object1_;
77 Json::Value emptyString_;
78 Json::Value string1_;
79 Json::Value string_;
80 Json::Value true_;
81 Json::Value false_;
82
ValueTestValueTest83 ValueTest()
84 : emptyArray_(Json::arrayValue), emptyObject_(Json::objectValue),
85 integer_(123456789), unsignedInteger_(34567890u),
86 smallUnsignedInteger_(Json::Value::UInt(Json::Value::maxInt)),
87 real_(1234.56789), float_(0.00390625f), emptyString_(""), string1_("a"),
88 string_("sometext with space"), true_(true), false_(false) {
89 array1_.append(1234);
90 object1_["id"] = 1234;
91 }
92
93 struct IsCheck {
94 /// Initialize all checks to \c false by default.
95 IsCheck();
96
97 bool isObject_;
98 bool isArray_;
99 bool isBool_;
100 bool isString_;
101 bool isNull_;
102
103 bool isInt_;
104 bool isInt64_;
105 bool isUInt_;
106 bool isUInt64_;
107 bool isIntegral_;
108 bool isDouble_;
109 bool isNumeric_;
110 };
111
112 void checkConstMemberCount(const Json::Value& value,
113 unsigned int expectedCount);
114
115 void checkMemberCount(Json::Value& value, unsigned int expectedCount);
116
117 void checkIs(const Json::Value& value, const IsCheck& check);
118
119 void checkIsLess(const Json::Value& x, const Json::Value& y);
120
121 void checkIsEqual(const Json::Value& x, const Json::Value& y);
122
123 /// Normalize the representation of floating-point number by stripped leading
124 /// 0 in exponent.
125 static Json::String normalizeFloatingPointStr(const Json::String& s);
126 };
127
normalizeFloatingPointStr(const Json::String & s)128 Json::String ValueTest::normalizeFloatingPointStr(const Json::String& s) {
129 std::string::size_type index = s.find_last_of("eE");
130 if (index == s.npos)
131 return s;
132 std::size_t signWidth = (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
133 std::string::size_type exponentStartIndex = index + 1 + signWidth;
134 Json::String normalized = s.substr(0, exponentStartIndex);
135 std::string::size_type indexDigit =
136 s.find_first_not_of('0', exponentStartIndex);
137 Json::String exponent = "0";
138 if (indexDigit != s.npos) { // nonzero exponent
139 exponent = s.substr(indexDigit);
140 }
141 return normalized + exponent;
142 }
143
JSONTEST_FIXTURE_LOCAL(ValueTest,checkNormalizeFloatingPointStr)144 JSONTEST_FIXTURE_LOCAL(ValueTest, checkNormalizeFloatingPointStr) {
145 struct TestData {
146 std::string in;
147 std::string out;
148 } const testData[] = {
149 {"0.0", "0.0"},
150 {"0e0", "0e0"},
151 {"1234.0", "1234.0"},
152 {"1234.0e0", "1234.0e0"},
153 {"1234.0e-1", "1234.0e-1"},
154 {"1234.0e+0", "1234.0e+0"},
155 {"1234.0e+001", "1234.0e+1"},
156 {"1234e-1", "1234e-1"},
157 {"1234e+000", "1234e+0"},
158 {"1234e+001", "1234e+1"},
159 {"1234e10", "1234e10"},
160 {"1234e010", "1234e10"},
161 {"1234e+010", "1234e+10"},
162 {"1234e-010", "1234e-10"},
163 {"1234e+100", "1234e+100"},
164 {"1234e-100", "1234e-100"},
165 };
166 for (unsigned int index = 0; index < sizeof(testData) / sizeof(testData[0]);
167 ++index) {
168 const struct TestData td = testData[index];
169 JSONTEST_ASSERT_STRING_EQUAL(normalizeFloatingPointStr(td.in), td.out);
170 }
171 }
172
JSONTEST_FIXTURE_LOCAL(ValueTest,memberCount)173 JSONTEST_FIXTURE_LOCAL(ValueTest, memberCount) {
174 JSONTEST_ASSERT_PRED(checkMemberCount(emptyArray_, 0));
175 JSONTEST_ASSERT_PRED(checkMemberCount(emptyObject_, 0));
176 JSONTEST_ASSERT_PRED(checkMemberCount(array1_, 1));
177 JSONTEST_ASSERT_PRED(checkMemberCount(object1_, 1));
178 JSONTEST_ASSERT_PRED(checkMemberCount(null_, 0));
179 JSONTEST_ASSERT_PRED(checkMemberCount(integer_, 0));
180 JSONTEST_ASSERT_PRED(checkMemberCount(unsignedInteger_, 0));
181 JSONTEST_ASSERT_PRED(checkMemberCount(smallUnsignedInteger_, 0));
182 JSONTEST_ASSERT_PRED(checkMemberCount(real_, 0));
183 JSONTEST_ASSERT_PRED(checkMemberCount(emptyString_, 0));
184 JSONTEST_ASSERT_PRED(checkMemberCount(string_, 0));
185 JSONTEST_ASSERT_PRED(checkMemberCount(true_, 0));
186 JSONTEST_ASSERT_PRED(checkMemberCount(false_, 0));
187 JSONTEST_ASSERT_PRED(checkMemberCount(string1_, 0));
188 JSONTEST_ASSERT_PRED(checkMemberCount(float_, 0));
189 }
190
JSONTEST_FIXTURE_LOCAL(ValueTest,objects)191 JSONTEST_FIXTURE_LOCAL(ValueTest, objects) {
192 // Types
193 IsCheck checks;
194 checks.isObject_ = true;
195 JSONTEST_ASSERT_PRED(checkIs(emptyObject_, checks));
196 JSONTEST_ASSERT_PRED(checkIs(object1_, checks));
197
198 JSONTEST_ASSERT_EQUAL(Json::objectValue, emptyObject_.type());
199
200 // Empty object okay
201 JSONTEST_ASSERT(emptyObject_.isConvertibleTo(Json::nullValue));
202
203 // Non-empty object not okay
204 JSONTEST_ASSERT(!object1_.isConvertibleTo(Json::nullValue));
205
206 // Always okay
207 JSONTEST_ASSERT(emptyObject_.isConvertibleTo(Json::objectValue));
208
209 // Never okay
210 JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::arrayValue));
211 JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::intValue));
212 JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::uintValue));
213 JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::realValue));
214 JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::booleanValue));
215 JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::stringValue));
216
217 // Access through const reference
218 const Json::Value& constObject = object1_;
219
220 JSONTEST_ASSERT_EQUAL(Json::Value(1234), constObject["id"]);
221 JSONTEST_ASSERT_EQUAL(Json::Value(), constObject["unknown id"]);
222
223 // Access through find()
224 const char idKey[] = "id";
225 const Json::Value* foundId = object1_.find(idKey, idKey + strlen(idKey));
226 JSONTEST_ASSERT(foundId != JSONCPP_NULL);
227 JSONTEST_ASSERT_EQUAL(Json::Value(1234), *foundId);
228
229 const char unknownIdKey[] = "unknown id";
230 const Json::Value* foundUnknownId =
231 object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey));
232 JSONTEST_ASSERT(JSONCPP_NULL == foundUnknownId);
233
234 // Access through demand()
235 const char yetAnotherIdKey[] = "yet another id";
236 const Json::Value* foundYetAnotherId =
237 object1_.find(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey));
238 JSONTEST_ASSERT(JSONCPP_NULL == foundYetAnotherId);
239 Json::Value* demandedYetAnotherId = object1_.demand(
240 yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey));
241 JSONTEST_ASSERT(demandedYetAnotherId != JSONCPP_NULL);
242 *demandedYetAnotherId = "baz";
243
244 JSONTEST_ASSERT_EQUAL(Json::Value("baz"), object1_["yet another id"]);
245
246 // Access through non-const reference
247 JSONTEST_ASSERT_EQUAL(Json::Value(1234), object1_["id"]);
248 JSONTEST_ASSERT_EQUAL(Json::Value(), object1_["unknown id"]);
249
250 object1_["some other id"] = "foo";
251 JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["some other id"]);
252 JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["some other id"]);
253
254 // Remove.
255 Json::Value got;
256 bool did;
257 did = object1_.removeMember("some other id", &got);
258 JSONTEST_ASSERT_EQUAL(Json::Value("foo"), got);
259 JSONTEST_ASSERT_EQUAL(true, did);
260 got = Json::Value("bar");
261 did = object1_.removeMember("some other id", &got);
262 JSONTEST_ASSERT_EQUAL(Json::Value("bar"), got);
263 JSONTEST_ASSERT_EQUAL(false, did);
264
265 object1_["some other id"] = "foo";
266 Json::Value* gotPtr = JSONCPP_NULL;
267 did = object1_.removeMember("some other id", gotPtr);
268 JSONTEST_ASSERT(JSONCPP_NULL == gotPtr);
269 JSONTEST_ASSERT_EQUAL(true, did);
270
271 // Using other removeMember interfaces, the test idea is the same as above.
272 object1_["some other id"] = "foo";
273 const Json::String key("some other id");
274 did = object1_.removeMember(key, &got);
275 JSONTEST_ASSERT_EQUAL(Json::Value("foo"), got);
276 JSONTEST_ASSERT_EQUAL(true, did);
277 got = Json::Value("bar");
278 did = object1_.removeMember(key, &got);
279 JSONTEST_ASSERT_EQUAL(Json::Value("bar"), got);
280 JSONTEST_ASSERT_EQUAL(false, did);
281
282 object1_["some other id"] = "foo";
283 object1_.removeMember(key);
284 JSONTEST_ASSERT_EQUAL(Json::nullValue, object1_[key]);
285 }
286
JSONTEST_FIXTURE_LOCAL(ValueTest,arrays)287 JSONTEST_FIXTURE_LOCAL(ValueTest, arrays) {
288 const unsigned int index0 = 0;
289
290 // Types
291 IsCheck checks;
292 checks.isArray_ = true;
293 JSONTEST_ASSERT_PRED(checkIs(emptyArray_, checks));
294 JSONTEST_ASSERT_PRED(checkIs(array1_, checks));
295
296 JSONTEST_ASSERT_EQUAL(Json::arrayValue, array1_.type());
297
298 // Empty array okay
299 JSONTEST_ASSERT(emptyArray_.isConvertibleTo(Json::nullValue));
300
301 // Non-empty array not okay
302 JSONTEST_ASSERT(!array1_.isConvertibleTo(Json::nullValue));
303
304 // Always okay
305 JSONTEST_ASSERT(emptyArray_.isConvertibleTo(Json::arrayValue));
306
307 // Never okay
308 JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::objectValue));
309 JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::intValue));
310 JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::uintValue));
311 JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::realValue));
312 JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::booleanValue));
313 JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::stringValue));
314
315 // Access through const reference
316 const Json::Value& constArray = array1_;
317 JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[index0]);
318 JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[0]);
319
320 // Access through non-const reference
321 JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[index0]);
322 JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[0]);
323
324 array1_[2] = Json::Value(17);
325 JSONTEST_ASSERT_EQUAL(Json::Value(), array1_[1]);
326 JSONTEST_ASSERT_EQUAL(Json::Value(17), array1_[2]);
327 Json::Value got;
328 JSONTEST_ASSERT_EQUAL(true, array1_.removeIndex(2, &got));
329 JSONTEST_ASSERT_EQUAL(Json::Value(17), got);
330 JSONTEST_ASSERT_EQUAL(false, array1_.removeIndex(2, &got)); // gone now
331 }
JSONTEST_FIXTURE_LOCAL(ValueTest,resizeArray)332 JSONTEST_FIXTURE_LOCAL(ValueTest, resizeArray) {
333 Json::Value array;
334 {
335 for (Json::ArrayIndex i = 0; i < 10; i++)
336 array[i] = i;
337 JSONTEST_ASSERT_EQUAL(array.size(), 10);
338 // The length set is greater than the length of the array.
339 array.resize(15);
340 JSONTEST_ASSERT_EQUAL(array.size(), 15);
341
342 // The length set is less than the length of the array.
343 array.resize(5);
344 JSONTEST_ASSERT_EQUAL(array.size(), 5);
345
346 // The length of the array is set to 0.
347 array.resize(0);
348 JSONTEST_ASSERT_EQUAL(array.size(), 0);
349 }
350 {
351 for (Json::ArrayIndex i = 0; i < 10; i++)
352 array[i] = i;
353 JSONTEST_ASSERT_EQUAL(array.size(), 10);
354 array.clear();
355 JSONTEST_ASSERT_EQUAL(array.size(), 0);
356 }
357 }
JSONTEST_FIXTURE_LOCAL(ValueTest,getArrayValue)358 JSONTEST_FIXTURE_LOCAL(ValueTest, getArrayValue) {
359 Json::Value array;
360 for (Json::ArrayIndex i = 0; i < 5; i++)
361 array[i] = i;
362
363 JSONTEST_ASSERT_EQUAL(array.size(), 5);
364 const Json::Value defaultValue(10);
365 Json::ArrayIndex index = 0;
366 for (; index <= 4; index++)
367 JSONTEST_ASSERT_EQUAL(index, array.get(index, defaultValue).asInt());
368
369 index = 4;
370 JSONTEST_ASSERT_EQUAL(array.isValidIndex(index), true);
371 index = 5;
372 JSONTEST_ASSERT_EQUAL(array.isValidIndex(index), false);
373 JSONTEST_ASSERT_EQUAL(defaultValue, array.get(index, defaultValue));
374 JSONTEST_ASSERT_EQUAL(array.isValidIndex(index), false);
375 }
JSONTEST_FIXTURE_LOCAL(ValueTest,arrayIssue252)376 JSONTEST_FIXTURE_LOCAL(ValueTest, arrayIssue252) {
377 int count = 5;
378 Json::Value root;
379 Json::Value item;
380 root["array"] = Json::Value::nullSingleton();
381 for (int i = 0; i < count; i++) {
382 item["a"] = i;
383 item["b"] = i;
384 root["array"][i] = item;
385 }
386 // JSONTEST_ASSERT_EQUAL(5, root["array"].size());
387 }
388
JSONTEST_FIXTURE_LOCAL(ValueTest,arrayInsertAtRandomIndex)389 JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
390 Json::Value array;
391 const Json::Value str0("index2");
392 const Json::Value str1("index3");
393 array.append("index0"); // append rvalue
394 array.append("index1");
395 array.append(str0); // append lvalue
396
397 std::vector<Json::Value*> vec; // storage value address for checking
398 for (Json::ArrayIndex i = 0; i < 3; i++) {
399 vec.push_back(&array[i]);
400 }
401 JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append
402 JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
403 JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
404
405 // insert lvalue at the head
406 JSONTEST_ASSERT(array.insert(0, str1));
407 JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
408 JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
409 JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
410 JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]);
411 // checking address
412 for (Json::ArrayIndex i = 0; i < 3; i++) {
413 JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
414 }
415 vec.push_back(&array[3]);
416 // insert rvalue at middle
417 JSONTEST_ASSERT(array.insert(2, "index4"));
418 JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
419 JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
420 JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
421 JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
422 JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
423 // checking address
424 for (Json::ArrayIndex i = 0; i < 4; i++) {
425 JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
426 }
427 vec.push_back(&array[4]);
428 // insert rvalue at the tail
429 JSONTEST_ASSERT(array.insert(5, "index5"));
430 JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
431 JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
432 JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
433 JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
434 JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
435 JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
436 // checking address
437 for (Json::ArrayIndex i = 0; i < 5; i++) {
438 JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
439 }
440 vec.push_back(&array[5]);
441 // beyond max array size, it should not be allowed to insert into its tail
442 JSONTEST_ASSERT(!array.insert(10, "index10"));
443 }
444
JSONTEST_FIXTURE_LOCAL(ValueTest,null)445 JSONTEST_FIXTURE_LOCAL(ValueTest, null) {
446 JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());
447
448 IsCheck checks;
449 checks.isNull_ = true;
450 JSONTEST_ASSERT_PRED(checkIs(null_, checks));
451
452 JSONTEST_ASSERT(null_.isConvertibleTo(Json::nullValue));
453 JSONTEST_ASSERT(null_.isConvertibleTo(Json::intValue));
454 JSONTEST_ASSERT(null_.isConvertibleTo(Json::uintValue));
455 JSONTEST_ASSERT(null_.isConvertibleTo(Json::realValue));
456 JSONTEST_ASSERT(null_.isConvertibleTo(Json::booleanValue));
457 JSONTEST_ASSERT(null_.isConvertibleTo(Json::stringValue));
458 JSONTEST_ASSERT(null_.isConvertibleTo(Json::arrayValue));
459 JSONTEST_ASSERT(null_.isConvertibleTo(Json::objectValue));
460
461 JSONTEST_ASSERT_EQUAL(Json::Int(0), null_.asInt());
462 JSONTEST_ASSERT_EQUAL(Json::LargestInt(0), null_.asLargestInt());
463 JSONTEST_ASSERT_EQUAL(Json::UInt(0), null_.asUInt());
464 JSONTEST_ASSERT_EQUAL(Json::LargestUInt(0), null_.asLargestUInt());
465 JSONTEST_ASSERT_EQUAL(0.0, null_.asDouble());
466 JSONTEST_ASSERT_EQUAL(0.0, null_.asFloat());
467 JSONTEST_ASSERT_STRING_EQUAL("", null_.asString());
468
469 JSONTEST_ASSERT_EQUAL(Json::Value::nullSingleton(), null_);
470
471 // Test using a Value in a boolean context (false iff null)
472 JSONTEST_ASSERT_EQUAL(null_, false);
473 JSONTEST_ASSERT_EQUAL(object1_, true);
474 JSONTEST_ASSERT_EQUAL(!null_, true);
475 JSONTEST_ASSERT_EQUAL(!object1_, false);
476 }
477
JSONTEST_FIXTURE_LOCAL(ValueTest,strings)478 JSONTEST_FIXTURE_LOCAL(ValueTest, strings) {
479 JSONTEST_ASSERT_EQUAL(Json::stringValue, string1_.type());
480
481 IsCheck checks;
482 checks.isString_ = true;
483 JSONTEST_ASSERT_PRED(checkIs(emptyString_, checks));
484 JSONTEST_ASSERT_PRED(checkIs(string_, checks));
485 JSONTEST_ASSERT_PRED(checkIs(string1_, checks));
486
487 // Empty string okay
488 JSONTEST_ASSERT(emptyString_.isConvertibleTo(Json::nullValue));
489
490 // Non-empty string not okay
491 JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::nullValue));
492
493 // Always okay
494 JSONTEST_ASSERT(string1_.isConvertibleTo(Json::stringValue));
495
496 // Never okay
497 JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::objectValue));
498 JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::arrayValue));
499 JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::intValue));
500 JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::uintValue));
501 JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::realValue));
502
503 JSONTEST_ASSERT_STRING_EQUAL("a", string1_.asString());
504 JSONTEST_ASSERT_STRING_EQUAL("a", string1_.asCString());
505 }
506
JSONTEST_FIXTURE_LOCAL(ValueTest,bools)507 JSONTEST_FIXTURE_LOCAL(ValueTest, bools) {
508 JSONTEST_ASSERT_EQUAL(Json::booleanValue, false_.type());
509
510 IsCheck checks;
511 checks.isBool_ = true;
512 JSONTEST_ASSERT_PRED(checkIs(false_, checks));
513 JSONTEST_ASSERT_PRED(checkIs(true_, checks));
514
515 // False okay
516 JSONTEST_ASSERT(false_.isConvertibleTo(Json::nullValue));
517
518 // True not okay
519 JSONTEST_ASSERT(!true_.isConvertibleTo(Json::nullValue));
520
521 // Always okay
522 JSONTEST_ASSERT(true_.isConvertibleTo(Json::intValue));
523 JSONTEST_ASSERT(true_.isConvertibleTo(Json::uintValue));
524 JSONTEST_ASSERT(true_.isConvertibleTo(Json::realValue));
525 JSONTEST_ASSERT(true_.isConvertibleTo(Json::booleanValue));
526 JSONTEST_ASSERT(true_.isConvertibleTo(Json::stringValue));
527
528 // Never okay
529 JSONTEST_ASSERT(!true_.isConvertibleTo(Json::arrayValue));
530 JSONTEST_ASSERT(!true_.isConvertibleTo(Json::objectValue));
531
532 JSONTEST_ASSERT_EQUAL(true, true_.asBool());
533 JSONTEST_ASSERT_EQUAL(1, true_.asInt());
534 JSONTEST_ASSERT_EQUAL(1, true_.asLargestInt());
535 JSONTEST_ASSERT_EQUAL(1, true_.asUInt());
536 JSONTEST_ASSERT_EQUAL(1, true_.asLargestUInt());
537 JSONTEST_ASSERT_EQUAL(1.0, true_.asDouble());
538 JSONTEST_ASSERT_EQUAL(1.0, true_.asFloat());
539
540 JSONTEST_ASSERT_EQUAL(false, false_.asBool());
541 JSONTEST_ASSERT_EQUAL(0, false_.asInt());
542 JSONTEST_ASSERT_EQUAL(0, false_.asLargestInt());
543 JSONTEST_ASSERT_EQUAL(0, false_.asUInt());
544 JSONTEST_ASSERT_EQUAL(0, false_.asLargestUInt());
545 JSONTEST_ASSERT_EQUAL(0.0, false_.asDouble());
546 JSONTEST_ASSERT_EQUAL(0.0, false_.asFloat());
547 }
548
JSONTEST_FIXTURE_LOCAL(ValueTest,integers)549 JSONTEST_FIXTURE_LOCAL(ValueTest, integers) {
550 IsCheck checks;
551 Json::Value val;
552
553 // Conversions that don't depend on the value.
554 JSONTEST_ASSERT(Json::Value(17).isConvertibleTo(Json::realValue));
555 JSONTEST_ASSERT(Json::Value(17).isConvertibleTo(Json::stringValue));
556 JSONTEST_ASSERT(Json::Value(17).isConvertibleTo(Json::booleanValue));
557 JSONTEST_ASSERT(!Json::Value(17).isConvertibleTo(Json::arrayValue));
558 JSONTEST_ASSERT(!Json::Value(17).isConvertibleTo(Json::objectValue));
559
560 JSONTEST_ASSERT(Json::Value(17U).isConvertibleTo(Json::realValue));
561 JSONTEST_ASSERT(Json::Value(17U).isConvertibleTo(Json::stringValue));
562 JSONTEST_ASSERT(Json::Value(17U).isConvertibleTo(Json::booleanValue));
563 JSONTEST_ASSERT(!Json::Value(17U).isConvertibleTo(Json::arrayValue));
564 JSONTEST_ASSERT(!Json::Value(17U).isConvertibleTo(Json::objectValue));
565
566 JSONTEST_ASSERT(Json::Value(17.0).isConvertibleTo(Json::realValue));
567 JSONTEST_ASSERT(Json::Value(17.0).isConvertibleTo(Json::stringValue));
568 JSONTEST_ASSERT(Json::Value(17.0).isConvertibleTo(Json::booleanValue));
569 JSONTEST_ASSERT(!Json::Value(17.0).isConvertibleTo(Json::arrayValue));
570 JSONTEST_ASSERT(!Json::Value(17.0).isConvertibleTo(Json::objectValue));
571
572 // Default int
573 val = Json::Value(Json::intValue);
574
575 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
576
577 checks = IsCheck();
578 checks.isInt_ = true;
579 checks.isInt64_ = true;
580 checks.isUInt_ = true;
581 checks.isUInt64_ = true;
582 checks.isIntegral_ = true;
583 checks.isDouble_ = true;
584 checks.isNumeric_ = true;
585 JSONTEST_ASSERT_PRED(checkIs(val, checks));
586
587 JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue));
588 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
589 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
590
591 JSONTEST_ASSERT_EQUAL(0, val.asInt());
592 JSONTEST_ASSERT_EQUAL(0, val.asLargestInt());
593 JSONTEST_ASSERT_EQUAL(0, val.asUInt());
594 JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());
595 JSONTEST_ASSERT_EQUAL(0.0, val.asDouble());
596 JSONTEST_ASSERT_EQUAL(0.0, val.asFloat());
597 JSONTEST_ASSERT_EQUAL(false, val.asBool());
598 JSONTEST_ASSERT_STRING_EQUAL("0", val.asString());
599
600 // Default uint
601 val = Json::Value(Json::uintValue);
602
603 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
604
605 checks = IsCheck();
606 checks.isInt_ = true;
607 checks.isInt64_ = true;
608 checks.isUInt_ = true;
609 checks.isUInt64_ = true;
610 checks.isIntegral_ = true;
611 checks.isDouble_ = true;
612 checks.isNumeric_ = true;
613 JSONTEST_ASSERT_PRED(checkIs(val, checks));
614
615 JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue));
616 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
617 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
618
619 JSONTEST_ASSERT_EQUAL(0, val.asInt());
620 JSONTEST_ASSERT_EQUAL(0, val.asLargestInt());
621 JSONTEST_ASSERT_EQUAL(0, val.asUInt());
622 JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());
623 JSONTEST_ASSERT_EQUAL(0.0, val.asDouble());
624 JSONTEST_ASSERT_EQUAL(0.0, val.asFloat());
625 JSONTEST_ASSERT_EQUAL(false, val.asBool());
626 JSONTEST_ASSERT_STRING_EQUAL("0", val.asString());
627
628 // Default real
629 val = Json::Value(Json::realValue);
630
631 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
632
633 JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue));
634 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
635 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
636
637 checks = IsCheck();
638 checks.isInt_ = true;
639 checks.isInt64_ = true;
640 checks.isUInt_ = true;
641 checks.isUInt64_ = true;
642 checks.isIntegral_ = true;
643 checks.isDouble_ = true;
644 checks.isNumeric_ = true;
645 JSONTEST_ASSERT_PRED(checkIs(val, checks));
646
647 JSONTEST_ASSERT_EQUAL(0, val.asInt());
648 JSONTEST_ASSERT_EQUAL(0, val.asLargestInt());
649 JSONTEST_ASSERT_EQUAL(0, val.asUInt());
650 JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());
651 JSONTEST_ASSERT_EQUAL(0.0, val.asDouble());
652 JSONTEST_ASSERT_EQUAL(0.0, val.asFloat());
653 JSONTEST_ASSERT_EQUAL(false, val.asBool());
654 JSONTEST_ASSERT_STRING_EQUAL("0.0", val.asString());
655
656 // Zero (signed constructor arg)
657 val = Json::Value(0);
658
659 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
660
661 checks = IsCheck();
662 checks.isInt_ = true;
663 checks.isInt64_ = true;
664 checks.isUInt_ = true;
665 checks.isUInt64_ = true;
666 checks.isIntegral_ = true;
667 checks.isDouble_ = true;
668 checks.isNumeric_ = true;
669 JSONTEST_ASSERT_PRED(checkIs(val, checks));
670
671 JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue));
672 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
673 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
674
675 JSONTEST_ASSERT_EQUAL(0, val.asInt());
676 JSONTEST_ASSERT_EQUAL(0, val.asLargestInt());
677 JSONTEST_ASSERT_EQUAL(0, val.asUInt());
678 JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());
679 JSONTEST_ASSERT_EQUAL(0.0, val.asDouble());
680 JSONTEST_ASSERT_EQUAL(0.0, val.asFloat());
681 JSONTEST_ASSERT_EQUAL(false, val.asBool());
682 JSONTEST_ASSERT_STRING_EQUAL("0", val.asString());
683
684 // Zero (unsigned constructor arg)
685 val = Json::Value(0u);
686
687 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
688
689 checks = IsCheck();
690 checks.isInt_ = true;
691 checks.isInt64_ = true;
692 checks.isUInt_ = true;
693 checks.isUInt64_ = true;
694 checks.isIntegral_ = true;
695 checks.isDouble_ = true;
696 checks.isNumeric_ = true;
697 JSONTEST_ASSERT_PRED(checkIs(val, checks));
698
699 JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue));
700 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
701 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
702
703 JSONTEST_ASSERT_EQUAL(0, val.asInt());
704 JSONTEST_ASSERT_EQUAL(0, val.asLargestInt());
705 JSONTEST_ASSERT_EQUAL(0, val.asUInt());
706 JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());
707 JSONTEST_ASSERT_EQUAL(0.0, val.asDouble());
708 JSONTEST_ASSERT_EQUAL(0.0, val.asFloat());
709 JSONTEST_ASSERT_EQUAL(false, val.asBool());
710 JSONTEST_ASSERT_STRING_EQUAL("0", val.asString());
711
712 // Zero (floating-point constructor arg)
713 val = Json::Value(0.0);
714
715 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
716
717 checks = IsCheck();
718 checks.isInt_ = true;
719 checks.isInt64_ = true;
720 checks.isUInt_ = true;
721 checks.isUInt64_ = true;
722 checks.isIntegral_ = true;
723 checks.isDouble_ = true;
724 checks.isNumeric_ = true;
725 JSONTEST_ASSERT_PRED(checkIs(val, checks));
726
727 JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue));
728 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
729 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
730
731 JSONTEST_ASSERT_EQUAL(0, val.asInt());
732 JSONTEST_ASSERT_EQUAL(0, val.asLargestInt());
733 JSONTEST_ASSERT_EQUAL(0, val.asUInt());
734 JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());
735 JSONTEST_ASSERT_EQUAL(0.0, val.asDouble());
736 JSONTEST_ASSERT_EQUAL(0.0, val.asFloat());
737 JSONTEST_ASSERT_EQUAL(false, val.asBool());
738 JSONTEST_ASSERT_STRING_EQUAL("0.0", val.asString());
739
740 // 2^20 (signed constructor arg)
741 val = Json::Value(1 << 20);
742
743 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
744 checks = IsCheck();
745 checks.isInt_ = true;
746 checks.isInt64_ = true;
747 checks.isUInt_ = true;
748 checks.isUInt64_ = true;
749 checks.isIntegral_ = true;
750 checks.isDouble_ = true;
751 checks.isNumeric_ = true;
752 JSONTEST_ASSERT_PRED(checkIs(val, checks));
753
754 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
755 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
756 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
757
758 JSONTEST_ASSERT_EQUAL((1 << 20), val.asInt());
759 JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestInt());
760 JSONTEST_ASSERT_EQUAL((1 << 20), val.asUInt());
761 JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestUInt());
762 JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble());
763 JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat());
764 JSONTEST_ASSERT_EQUAL(true, val.asBool());
765 JSONTEST_ASSERT_STRING_EQUAL("1048576", val.asString());
766
767 // 2^20 (unsigned constructor arg)
768 val = Json::Value(Json::UInt(1 << 20));
769
770 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
771
772 checks = IsCheck();
773 checks.isInt_ = true;
774 checks.isInt64_ = true;
775 checks.isUInt_ = true;
776 checks.isUInt64_ = true;
777 checks.isIntegral_ = true;
778 checks.isDouble_ = true;
779 checks.isNumeric_ = true;
780 JSONTEST_ASSERT_PRED(checkIs(val, checks));
781
782 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
783 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
784 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
785
786 JSONTEST_ASSERT_EQUAL((1 << 20), val.asInt());
787 JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestInt());
788 JSONTEST_ASSERT_EQUAL((1 << 20), val.asUInt());
789 JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestUInt());
790 JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble());
791 JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat());
792 JSONTEST_ASSERT_EQUAL(true, val.asBool());
793 JSONTEST_ASSERT_STRING_EQUAL("1048576", val.asString());
794
795 // 2^20 (floating-point constructor arg)
796 val = Json::Value((1 << 20) / 1.0);
797
798 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
799
800 checks = IsCheck();
801 checks.isInt_ = true;
802 checks.isInt64_ = true;
803 checks.isUInt_ = true;
804 checks.isUInt64_ = true;
805 checks.isIntegral_ = true;
806 checks.isDouble_ = true;
807 checks.isNumeric_ = true;
808 JSONTEST_ASSERT_PRED(checkIs(val, checks));
809
810 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
811 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
812 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
813
814 JSONTEST_ASSERT_EQUAL((1 << 20), val.asInt());
815 JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestInt());
816 JSONTEST_ASSERT_EQUAL((1 << 20), val.asUInt());
817 JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestUInt());
818 JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble());
819 JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat());
820 JSONTEST_ASSERT_EQUAL(true, val.asBool());
821 JSONTEST_ASSERT_STRING_EQUAL(
822 "1048576.0",
823 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
824
825 // -2^20
826 val = Json::Value(-(1 << 20));
827
828 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
829
830 checks = IsCheck();
831 checks.isInt_ = true;
832 checks.isInt64_ = true;
833 checks.isIntegral_ = true;
834 checks.isDouble_ = true;
835 checks.isNumeric_ = true;
836 JSONTEST_ASSERT_PRED(checkIs(val, checks));
837
838 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
839 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
840 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
841
842 JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asInt());
843 JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asLargestInt());
844 JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asDouble());
845 JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asFloat());
846 JSONTEST_ASSERT_EQUAL(true, val.asBool());
847 JSONTEST_ASSERT_STRING_EQUAL("-1048576", val.asString());
848
849 // int32 max
850 val = Json::Value(kint32max);
851
852 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
853
854 checks = IsCheck();
855 checks.isInt_ = true;
856 checks.isInt64_ = true;
857 checks.isUInt_ = true;
858 checks.isUInt64_ = true;
859 checks.isIntegral_ = true;
860 checks.isDouble_ = true;
861 checks.isNumeric_ = true;
862 JSONTEST_ASSERT_PRED(checkIs(val, checks));
863
864 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
865 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
866 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
867
868 JSONTEST_ASSERT_EQUAL(kint32max, val.asInt());
869 JSONTEST_ASSERT_EQUAL(kint32max, val.asLargestInt());
870 JSONTEST_ASSERT_EQUAL(kint32max, val.asUInt());
871 JSONTEST_ASSERT_EQUAL(kint32max, val.asLargestUInt());
872 JSONTEST_ASSERT_EQUAL(kint32max, val.asDouble());
873 JSONTEST_ASSERT_EQUAL(kfint32max, val.asFloat());
874 JSONTEST_ASSERT_EQUAL(true, val.asBool());
875 JSONTEST_ASSERT_STRING_EQUAL("2147483647", val.asString());
876
877 // int32 min
878 val = Json::Value(kint32min);
879
880 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
881
882 checks = IsCheck();
883 checks.isInt_ = true;
884 checks.isInt64_ = true;
885 checks.isIntegral_ = true;
886 checks.isDouble_ = true;
887 checks.isNumeric_ = true;
888 JSONTEST_ASSERT_PRED(checkIs(val, checks));
889
890 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
891 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
892 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
893
894 JSONTEST_ASSERT_EQUAL(kint32min, val.asInt());
895 JSONTEST_ASSERT_EQUAL(kint32min, val.asLargestInt());
896 JSONTEST_ASSERT_EQUAL(kint32min, val.asDouble());
897 JSONTEST_ASSERT_EQUAL(kint32min, val.asFloat());
898 JSONTEST_ASSERT_EQUAL(true, val.asBool());
899 JSONTEST_ASSERT_STRING_EQUAL("-2147483648", val.asString());
900
901 // uint32 max
902 val = Json::Value(kuint32max);
903
904 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
905
906 checks = IsCheck();
907 checks.isInt64_ = true;
908 checks.isUInt_ = true;
909 checks.isUInt64_ = true;
910 checks.isIntegral_ = true;
911 checks.isDouble_ = true;
912 checks.isNumeric_ = true;
913 JSONTEST_ASSERT_PRED(checkIs(val, checks));
914
915 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
916 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
917 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
918
919 #ifndef JSON_NO_INT64
920 JSONTEST_ASSERT_EQUAL(kuint32max, val.asLargestInt());
921 #endif
922 JSONTEST_ASSERT_EQUAL(kuint32max, val.asUInt());
923 JSONTEST_ASSERT_EQUAL(kuint32max, val.asLargestUInt());
924 JSONTEST_ASSERT_EQUAL(kuint32max, val.asDouble());
925 JSONTEST_ASSERT_EQUAL(kfuint32max, val.asFloat());
926 JSONTEST_ASSERT_EQUAL(true, val.asBool());
927 JSONTEST_ASSERT_STRING_EQUAL("4294967295", val.asString());
928
929 #ifdef JSON_NO_INT64
930 // int64 max
931 val = Json::Value(double(kint64max));
932
933 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
934
935 checks = IsCheck();
936 checks.isDouble_ = true;
937 checks.isNumeric_ = true;
938 JSONTEST_ASSERT_PRED(checkIs(val, checks));
939
940 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
941 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
942 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
943
944 JSONTEST_ASSERT_EQUAL(double(kint64max), val.asDouble());
945 JSONTEST_ASSERT_EQUAL(float(kint64max), val.asFloat());
946 JSONTEST_ASSERT_EQUAL(true, val.asBool());
947 JSONTEST_ASSERT_STRING_EQUAL("9.22337e+18", val.asString());
948
949 // int64 min
950 val = Json::Value(double(kint64min));
951
952 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
953
954 checks = IsCheck();
955 checks.isDouble_ = true;
956 checks.isNumeric_ = true;
957 JSONTEST_ASSERT_PRED(checkIs(val, checks));
958
959 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
960 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
961 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
962
963 JSONTEST_ASSERT_EQUAL(double(kint64min), val.asDouble());
964 JSONTEST_ASSERT_EQUAL(float(kint64min), val.asFloat());
965 JSONTEST_ASSERT_EQUAL(true, val.asBool());
966 JSONTEST_ASSERT_STRING_EQUAL("-9.22337e+18", val.asString());
967
968 // uint64 max
969 val = Json::Value(double(kuint64max));
970
971 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
972
973 checks = IsCheck();
974 checks.isDouble_ = true;
975 checks.isNumeric_ = true;
976 JSONTEST_ASSERT_PRED(checkIs(val, checks));
977
978 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
979 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
980 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
981
982 JSONTEST_ASSERT_EQUAL(double(kuint64max), val.asDouble());
983 JSONTEST_ASSERT_EQUAL(float(kuint64max), val.asFloat());
984 JSONTEST_ASSERT_EQUAL(true, val.asBool());
985 JSONTEST_ASSERT_STRING_EQUAL("1.84467e+19", val.asString());
986 #else // ifdef JSON_NO_INT64
987 // 2^40 (signed constructor arg)
988 val = Json::Value(Json::Int64(1) << 40);
989
990 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
991
992 checks = IsCheck();
993 checks.isInt64_ = true;
994 checks.isUInt64_ = true;
995 checks.isIntegral_ = true;
996 checks.isDouble_ = true;
997 checks.isNumeric_ = true;
998 JSONTEST_ASSERT_PRED(checkIs(val, checks));
999
1000 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1001 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1002 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1003
1004 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asInt64());
1005 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestInt());
1006 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asUInt64());
1007 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestUInt());
1008 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble());
1009 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat());
1010 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1011 JSONTEST_ASSERT_STRING_EQUAL("1099511627776", val.asString());
1012
1013 // 2^40 (unsigned constructor arg)
1014 val = Json::Value(Json::UInt64(1) << 40);
1015
1016 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
1017
1018 checks = IsCheck();
1019 checks.isInt64_ = true;
1020 checks.isUInt64_ = true;
1021 checks.isIntegral_ = true;
1022 checks.isDouble_ = true;
1023 checks.isNumeric_ = true;
1024 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1025
1026 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1027 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1028 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1029
1030 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asInt64());
1031 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestInt());
1032 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asUInt64());
1033 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestUInt());
1034 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble());
1035 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat());
1036 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1037 JSONTEST_ASSERT_STRING_EQUAL("1099511627776", val.asString());
1038
1039 // 2^40 (floating-point constructor arg)
1040 val = Json::Value((Json::Int64(1) << 40) / 1.0);
1041
1042 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1043
1044 checks = IsCheck();
1045 checks.isInt64_ = true;
1046 checks.isUInt64_ = true;
1047 checks.isIntegral_ = true;
1048 checks.isDouble_ = true;
1049 checks.isNumeric_ = true;
1050 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1051
1052 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1053 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1054 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1055
1056 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asInt64());
1057 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestInt());
1058 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asUInt64());
1059 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestUInt());
1060 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble());
1061 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat());
1062 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1063 JSONTEST_ASSERT_STRING_EQUAL(
1064 "1099511627776.0",
1065 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1066
1067 // -2^40
1068 val = Json::Value(-(Json::Int64(1) << 40));
1069
1070 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
1071
1072 checks = IsCheck();
1073 checks.isInt64_ = true;
1074 checks.isIntegral_ = true;
1075 checks.isDouble_ = true;
1076 checks.isNumeric_ = true;
1077 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1078
1079 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1080 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1081 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1082
1083 JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asInt64());
1084 JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asLargestInt());
1085 JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asDouble());
1086 JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asFloat());
1087 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1088 JSONTEST_ASSERT_STRING_EQUAL("-1099511627776", val.asString());
1089
1090 // int64 max
1091 val = Json::Value(Json::Int64(kint64max));
1092
1093 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
1094
1095 checks = IsCheck();
1096 checks.isInt64_ = true;
1097 checks.isUInt64_ = true;
1098 checks.isIntegral_ = true;
1099 checks.isDouble_ = true;
1100 checks.isNumeric_ = true;
1101 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1102
1103 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1104 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1105 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1106
1107 JSONTEST_ASSERT_EQUAL(kint64max, val.asInt64());
1108 JSONTEST_ASSERT_EQUAL(kint64max, val.asLargestInt());
1109 JSONTEST_ASSERT_EQUAL(kint64max, val.asUInt64());
1110 JSONTEST_ASSERT_EQUAL(kint64max, val.asLargestUInt());
1111 JSONTEST_ASSERT_EQUAL(double(kint64max), val.asDouble());
1112 JSONTEST_ASSERT_EQUAL(float(kint64max), val.asFloat());
1113 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1114 JSONTEST_ASSERT_STRING_EQUAL("9223372036854775807", val.asString());
1115
1116 // int64 max (floating point constructor). Note that kint64max is not exactly
1117 // representable as a double, and will be rounded up to be higher.
1118 val = Json::Value(double(kint64max));
1119
1120 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1121
1122 checks = IsCheck();
1123 checks.isUInt64_ = true;
1124 checks.isIntegral_ = true;
1125 checks.isDouble_ = true;
1126 checks.isNumeric_ = true;
1127 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1128
1129 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1130 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1131 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1132
1133 JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asUInt64());
1134 JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asLargestUInt());
1135 JSONTEST_ASSERT_EQUAL(uint64ToDouble(Json::UInt64(1) << 63), val.asDouble());
1136 JSONTEST_ASSERT_EQUAL(float(Json::UInt64(1) << 63), val.asFloat());
1137
1138 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1139 JSONTEST_ASSERT_STRING_EQUAL(
1140 "9.2233720368547758e+18",
1141 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1142
1143 // int64 min
1144 val = Json::Value(Json::Int64(kint64min));
1145
1146 JSONTEST_ASSERT_EQUAL(Json::intValue, val.type());
1147
1148 checks = IsCheck();
1149 checks.isInt64_ = true;
1150 checks.isIntegral_ = true;
1151 checks.isDouble_ = true;
1152 checks.isNumeric_ = true;
1153 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1154
1155 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1156 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1157 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1158
1159 JSONTEST_ASSERT_EQUAL(kint64min, val.asInt64());
1160 JSONTEST_ASSERT_EQUAL(kint64min, val.asLargestInt());
1161 JSONTEST_ASSERT_EQUAL(double(kint64min), val.asDouble());
1162 JSONTEST_ASSERT_EQUAL(float(kint64min), val.asFloat());
1163 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1164 JSONTEST_ASSERT_STRING_EQUAL("-9223372036854775808", val.asString());
1165
1166 // int64 min (floating point constructor). Note that kint64min *is* exactly
1167 // representable as a double.
1168 val = Json::Value(double(kint64min));
1169
1170 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1171
1172 checks = IsCheck();
1173 checks.isInt64_ = true;
1174 checks.isIntegral_ = true;
1175 checks.isDouble_ = true;
1176 checks.isNumeric_ = true;
1177 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1178
1179 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1180 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1181 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1182
1183 JSONTEST_ASSERT_EQUAL(kint64min, val.asInt64());
1184 JSONTEST_ASSERT_EQUAL(kint64min, val.asLargestInt());
1185 JSONTEST_ASSERT_EQUAL(-9223372036854775808.0, val.asDouble());
1186 JSONTEST_ASSERT_EQUAL(-9223372036854775808.0, val.asFloat());
1187 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1188 JSONTEST_ASSERT_STRING_EQUAL(
1189 "-9.2233720368547758e+18",
1190 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1191
1192 // 10^19
1193 const Json::UInt64 ten_to_19 = static_cast<Json::UInt64>(1e19);
1194 val = Json::Value(Json::UInt64(ten_to_19));
1195
1196 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
1197
1198 checks = IsCheck();
1199 checks.isUInt64_ = true;
1200 checks.isIntegral_ = true;
1201 checks.isDouble_ = true;
1202 checks.isNumeric_ = true;
1203 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1204
1205 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1206 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1207 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1208
1209 JSONTEST_ASSERT_EQUAL(ten_to_19, val.asUInt64());
1210 JSONTEST_ASSERT_EQUAL(ten_to_19, val.asLargestUInt());
1211 JSONTEST_ASSERT_EQUAL(uint64ToDouble(ten_to_19), val.asDouble());
1212 JSONTEST_ASSERT_EQUAL(float(uint64ToDouble(ten_to_19)), val.asFloat());
1213 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1214 JSONTEST_ASSERT_STRING_EQUAL("10000000000000000000", val.asString());
1215
1216 // 10^19 (double constructor). Note that 10^19 is not exactly representable
1217 // as a double.
1218 val = Json::Value(uint64ToDouble(ten_to_19));
1219
1220 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1221
1222 checks = IsCheck();
1223 checks.isUInt64_ = true;
1224 checks.isIntegral_ = true;
1225 checks.isDouble_ = true;
1226 checks.isNumeric_ = true;
1227 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1228
1229 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1230 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1231 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1232
1233 JSONTEST_ASSERT_EQUAL(1e19, val.asDouble());
1234 JSONTEST_ASSERT_EQUAL(1e19, val.asFloat());
1235 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1236 JSONTEST_ASSERT_STRING_EQUAL(
1237 "1e+19",
1238 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1239
1240 // uint64 max
1241 val = Json::Value(Json::UInt64(kuint64max));
1242
1243 JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type());
1244
1245 checks = IsCheck();
1246 checks.isUInt64_ = true;
1247 checks.isIntegral_ = true;
1248 checks.isDouble_ = true;
1249 checks.isNumeric_ = true;
1250 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1251
1252 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1253 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1254 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1255
1256 JSONTEST_ASSERT_EQUAL(kuint64max, val.asUInt64());
1257 JSONTEST_ASSERT_EQUAL(kuint64max, val.asLargestUInt());
1258 JSONTEST_ASSERT_EQUAL(uint64ToDouble(kuint64max), val.asDouble());
1259 JSONTEST_ASSERT_EQUAL(float(uint64ToDouble(kuint64max)), val.asFloat());
1260 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1261 JSONTEST_ASSERT_STRING_EQUAL("18446744073709551615", val.asString());
1262
1263 // uint64 max (floating point constructor). Note that kuint64max is not
1264 // exactly representable as a double, and will be rounded up to be higher.
1265 val = Json::Value(uint64ToDouble(kuint64max));
1266
1267 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1268
1269 checks = IsCheck();
1270 checks.isDouble_ = true;
1271 checks.isNumeric_ = true;
1272 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1273
1274 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1275 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1276 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1277
1278 JSONTEST_ASSERT_EQUAL(18446744073709551616.0, val.asDouble());
1279 JSONTEST_ASSERT_EQUAL(18446744073709551616.0, val.asFloat());
1280 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1281 JSONTEST_ASSERT_STRING_EQUAL(
1282 "1.8446744073709552e+19",
1283 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1284 #endif
1285 }
1286
JSONTEST_FIXTURE_LOCAL(ValueTest,nonIntegers)1287 JSONTEST_FIXTURE_LOCAL(ValueTest, nonIntegers) {
1288 IsCheck checks;
1289 Json::Value val;
1290
1291 // Small positive number
1292 val = Json::Value(1.5);
1293
1294 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1295
1296 checks = IsCheck();
1297 checks.isDouble_ = true;
1298 checks.isNumeric_ = true;
1299 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1300
1301 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
1302 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
1303 JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue));
1304 JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue));
1305 JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue));
1306 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1307 JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue));
1308 JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue));
1309
1310 JSONTEST_ASSERT_EQUAL(1.5, val.asDouble());
1311 JSONTEST_ASSERT_EQUAL(1.5, val.asFloat());
1312 JSONTEST_ASSERT_EQUAL(1, val.asInt());
1313 JSONTEST_ASSERT_EQUAL(1, val.asLargestInt());
1314 JSONTEST_ASSERT_EQUAL(1, val.asUInt());
1315 JSONTEST_ASSERT_EQUAL(1, val.asLargestUInt());
1316 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1317 JSONTEST_ASSERT_EQUAL("1.5", val.asString());
1318
1319 // Small negative number
1320 val = Json::Value(-1.5);
1321
1322 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1323
1324 checks = IsCheck();
1325 checks.isDouble_ = true;
1326 checks.isNumeric_ = true;
1327 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1328
1329 JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue));
1330 JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue));
1331 JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue));
1332 JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue));
1333 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1334 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1335 JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue));
1336 JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue));
1337
1338 JSONTEST_ASSERT_EQUAL(-1.5, val.asDouble());
1339 JSONTEST_ASSERT_EQUAL(-1.5, val.asFloat());
1340 JSONTEST_ASSERT_EQUAL(-1, val.asInt());
1341 JSONTEST_ASSERT_EQUAL(-1, val.asLargestInt());
1342 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1343 JSONTEST_ASSERT_EQUAL("-1.5", val.asString());
1344
1345 // A bit over int32 max
1346 val = Json::Value(kint32max + 0.5);
1347
1348 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1349
1350 checks = IsCheck();
1351 checks.isDouble_ = true;
1352 checks.isNumeric_ = true;
1353 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1354
1355 JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
1356 JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue));
1357 JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue));
1358 JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue));
1359 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1360 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1361 JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue));
1362 JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue));
1363
1364 JSONTEST_ASSERT_EQUAL(2147483647.5, val.asDouble());
1365 JSONTEST_ASSERT_EQUAL(float(2147483647.5), val.asFloat());
1366 JSONTEST_ASSERT_EQUAL(2147483647U, val.asUInt());
1367 #ifdef JSON_HAS_INT64
1368 JSONTEST_ASSERT_EQUAL(2147483647L, val.asLargestInt());
1369 JSONTEST_ASSERT_EQUAL(2147483647U, val.asLargestUInt());
1370 #endif
1371 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1372 JSONTEST_ASSERT_EQUAL(
1373 "2147483647.5",
1374 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1375
1376 // A bit under int32 min
1377 val = Json::Value(kint32min - 0.5);
1378
1379 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1380
1381 checks = IsCheck();
1382 checks.isDouble_ = true;
1383 checks.isNumeric_ = true;
1384 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1385
1386 JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue));
1387 JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue));
1388 JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue));
1389 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1390 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1391 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1392 JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue));
1393 JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue));
1394
1395 JSONTEST_ASSERT_EQUAL(-2147483648.5, val.asDouble());
1396 JSONTEST_ASSERT_EQUAL(float(-2147483648.5), val.asFloat());
1397 #ifdef JSON_HAS_INT64
1398 JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 31), val.asLargestInt());
1399 #endif
1400 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1401 JSONTEST_ASSERT_EQUAL(
1402 "-2147483648.5",
1403 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1404
1405 // A bit over uint32 max
1406 val = Json::Value(kuint32max + 0.5);
1407
1408 JSONTEST_ASSERT_EQUAL(Json::realValue, val.type());
1409
1410 checks = IsCheck();
1411 checks.isDouble_ = true;
1412 checks.isNumeric_ = true;
1413 JSONTEST_ASSERT_PRED(checkIs(val, checks));
1414
1415 JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue));
1416 JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue));
1417 JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue));
1418 JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue));
1419 JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue));
1420 JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue));
1421 JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue));
1422 JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue));
1423
1424 JSONTEST_ASSERT_EQUAL(4294967295.5, val.asDouble());
1425 JSONTEST_ASSERT_EQUAL(float(4294967295.5), val.asFloat());
1426 #ifdef JSON_HAS_INT64
1427 JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 32) - 1, val.asLargestInt());
1428 JSONTEST_ASSERT_EQUAL((Json::UInt64(1) << 32) - Json::UInt64(1),
1429 val.asLargestUInt());
1430 #endif
1431 JSONTEST_ASSERT_EQUAL(true, val.asBool());
1432 JSONTEST_ASSERT_EQUAL(
1433 "4294967295.5",
1434 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1435
1436 val = Json::Value(1.2345678901234);
1437 JSONTEST_ASSERT_STRING_EQUAL(
1438 "1.2345678901234001",
1439 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1440
1441 // A 16-digit floating point number.
1442 val = Json::Value(2199023255552000.0f);
1443 JSONTEST_ASSERT_EQUAL(float(2199023255552000.0f), val.asFloat());
1444 JSONTEST_ASSERT_STRING_EQUAL(
1445 "2199023255552000.0",
1446 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1447
1448 // A very large floating point number.
1449 val = Json::Value(3.402823466385289e38);
1450 JSONTEST_ASSERT_EQUAL(float(3.402823466385289e38), val.asFloat());
1451 JSONTEST_ASSERT_STRING_EQUAL(
1452 "3.402823466385289e+38",
1453 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1454
1455 // An even larger floating point number.
1456 val = Json::Value(1.2345678e300);
1457 JSONTEST_ASSERT_EQUAL(double(1.2345678e300), val.asDouble());
1458 JSONTEST_ASSERT_STRING_EQUAL(
1459 "1.2345678e+300",
1460 normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
1461 }
1462
checkConstMemberCount(const Json::Value & value,unsigned int expectedCount)1463 void ValueTest::checkConstMemberCount(const Json::Value& value,
1464 unsigned int expectedCount) {
1465 unsigned int count = 0;
1466 Json::Value::const_iterator itEnd = value.end();
1467 for (Json::Value::const_iterator it = value.begin(); it != itEnd; ++it) {
1468 ++count;
1469 }
1470 JSONTEST_ASSERT_EQUAL(expectedCount, count) << "Json::Value::const_iterator";
1471 }
1472
checkMemberCount(Json::Value & value,unsigned int expectedCount)1473 void ValueTest::checkMemberCount(Json::Value& value,
1474 unsigned int expectedCount) {
1475 JSONTEST_ASSERT_EQUAL(expectedCount, value.size());
1476
1477 unsigned int count = 0;
1478 Json::Value::iterator itEnd = value.end();
1479 for (Json::Value::iterator it = value.begin(); it != itEnd; ++it) {
1480 ++count;
1481 }
1482 JSONTEST_ASSERT_EQUAL(expectedCount, count) << "Json::Value::iterator";
1483
1484 JSONTEST_ASSERT_PRED(checkConstMemberCount(value, expectedCount));
1485 }
1486
IsCheck()1487 ValueTest::IsCheck::IsCheck()
1488 : isObject_(false), isArray_(false), isBool_(false), isString_(false),
1489 isNull_(false), isInt_(false), isInt64_(false), isUInt_(false),
1490 isUInt64_(false), isIntegral_(false), isDouble_(false),
1491 isNumeric_(false) {}
1492
checkIs(const Json::Value & value,const IsCheck & check)1493 void ValueTest::checkIs(const Json::Value& value, const IsCheck& check) {
1494 JSONTEST_ASSERT_EQUAL(check.isObject_, value.isObject());
1495 JSONTEST_ASSERT_EQUAL(check.isArray_, value.isArray());
1496 JSONTEST_ASSERT_EQUAL(check.isBool_, value.isBool());
1497 JSONTEST_ASSERT_EQUAL(check.isDouble_, value.isDouble());
1498 JSONTEST_ASSERT_EQUAL(check.isInt_, value.isInt());
1499 JSONTEST_ASSERT_EQUAL(check.isUInt_, value.isUInt());
1500 JSONTEST_ASSERT_EQUAL(check.isIntegral_, value.isIntegral());
1501 JSONTEST_ASSERT_EQUAL(check.isNumeric_, value.isNumeric());
1502 JSONTEST_ASSERT_EQUAL(check.isString_, value.isString());
1503 JSONTEST_ASSERT_EQUAL(check.isNull_, value.isNull());
1504
1505 #ifdef JSON_HAS_INT64
1506 JSONTEST_ASSERT_EQUAL(check.isInt64_, value.isInt64());
1507 JSONTEST_ASSERT_EQUAL(check.isUInt64_, value.isUInt64());
1508 #else
1509 JSONTEST_ASSERT_EQUAL(false, value.isInt64());
1510 JSONTEST_ASSERT_EQUAL(false, value.isUInt64());
1511 #endif
1512 }
1513
JSONTEST_FIXTURE_LOCAL(ValueTest,compareNull)1514 JSONTEST_FIXTURE_LOCAL(ValueTest, compareNull) {
1515 JSONTEST_ASSERT_PRED(checkIsEqual(Json::Value(), Json::Value()));
1516 JSONTEST_ASSERT_PRED(
1517 checkIsEqual(Json::Value::nullSingleton(), Json::Value()));
1518 JSONTEST_ASSERT_PRED(
1519 checkIsEqual(Json::Value::nullSingleton(), Json::Value::nullSingleton()));
1520 }
1521
JSONTEST_FIXTURE_LOCAL(ValueTest,compareInt)1522 JSONTEST_FIXTURE_LOCAL(ValueTest, compareInt) {
1523 JSONTEST_ASSERT_PRED(checkIsLess(0, 10));
1524 JSONTEST_ASSERT_PRED(checkIsEqual(10, 10));
1525 JSONTEST_ASSERT_PRED(checkIsEqual(-10, -10));
1526 JSONTEST_ASSERT_PRED(checkIsLess(-10, 0));
1527 }
1528
JSONTEST_FIXTURE_LOCAL(ValueTest,compareUInt)1529 JSONTEST_FIXTURE_LOCAL(ValueTest, compareUInt) {
1530 JSONTEST_ASSERT_PRED(checkIsLess(0u, 10u));
1531 JSONTEST_ASSERT_PRED(checkIsLess(0u, Json::Value::maxUInt));
1532 JSONTEST_ASSERT_PRED(checkIsEqual(10u, 10u));
1533 }
1534
JSONTEST_FIXTURE_LOCAL(ValueTest,compareDouble)1535 JSONTEST_FIXTURE_LOCAL(ValueTest, compareDouble) {
1536 JSONTEST_ASSERT_PRED(checkIsLess(0.0, 10.0));
1537 JSONTEST_ASSERT_PRED(checkIsEqual(10.0, 10.0));
1538 JSONTEST_ASSERT_PRED(checkIsEqual(-10.0, -10.0));
1539 JSONTEST_ASSERT_PRED(checkIsLess(-10.0, 0.0));
1540 }
1541
JSONTEST_FIXTURE_LOCAL(ValueTest,compareString)1542 JSONTEST_FIXTURE_LOCAL(ValueTest, compareString) {
1543 JSONTEST_ASSERT_PRED(checkIsLess("", " "));
1544 JSONTEST_ASSERT_PRED(checkIsLess("", "a"));
1545 JSONTEST_ASSERT_PRED(checkIsLess("abcd", "zyui"));
1546 JSONTEST_ASSERT_PRED(checkIsLess("abc", "abcd"));
1547 JSONTEST_ASSERT_PRED(checkIsEqual("abcd", "abcd"));
1548 JSONTEST_ASSERT_PRED(checkIsEqual(" ", " "));
1549 JSONTEST_ASSERT_PRED(checkIsLess("ABCD", "abcd"));
1550 JSONTEST_ASSERT_PRED(checkIsEqual("ABCD", "ABCD"));
1551 }
1552
JSONTEST_FIXTURE_LOCAL(ValueTest,compareBoolean)1553 JSONTEST_FIXTURE_LOCAL(ValueTest, compareBoolean) {
1554 JSONTEST_ASSERT_PRED(checkIsLess(false, true));
1555 JSONTEST_ASSERT_PRED(checkIsEqual(false, false));
1556 JSONTEST_ASSERT_PRED(checkIsEqual(true, true));
1557 }
1558
JSONTEST_FIXTURE_LOCAL(ValueTest,compareArray)1559 JSONTEST_FIXTURE_LOCAL(ValueTest, compareArray) {
1560 // array compare size then content
1561 Json::Value emptyArray(Json::arrayValue);
1562 Json::Value l1aArray;
1563 l1aArray.append(0);
1564 Json::Value l1bArray;
1565 l1bArray.append(10);
1566 Json::Value l2aArray;
1567 l2aArray.append(0);
1568 l2aArray.append(0);
1569 Json::Value l2bArray;
1570 l2bArray.append(0);
1571 l2bArray.append(10);
1572 JSONTEST_ASSERT_PRED(checkIsLess(emptyArray, l1aArray));
1573 JSONTEST_ASSERT_PRED(checkIsLess(emptyArray, l2aArray));
1574 JSONTEST_ASSERT_PRED(checkIsLess(l1aArray, l1bArray));
1575 JSONTEST_ASSERT_PRED(checkIsLess(l1bArray, l2aArray));
1576 JSONTEST_ASSERT_PRED(checkIsLess(l2aArray, l2bArray));
1577 JSONTEST_ASSERT_PRED(checkIsEqual(emptyArray, Json::Value(emptyArray)));
1578 JSONTEST_ASSERT_PRED(checkIsEqual(l1aArray, Json::Value(l1aArray)));
1579 JSONTEST_ASSERT_PRED(checkIsEqual(l1bArray, Json::Value(l1bArray)));
1580 JSONTEST_ASSERT_PRED(checkIsEqual(l2aArray, Json::Value(l2aArray)));
1581 JSONTEST_ASSERT_PRED(checkIsEqual(l2bArray, Json::Value(l2bArray)));
1582 }
1583
JSONTEST_FIXTURE_LOCAL(ValueTest,compareObject)1584 JSONTEST_FIXTURE_LOCAL(ValueTest, compareObject) {
1585 // object compare size then content
1586 Json::Value emptyObject(Json::objectValue);
1587 Json::Value l1aObject;
1588 l1aObject["key1"] = 0;
1589 Json::Value l1bObject;
1590 l1bObject["key1"] = 10;
1591 Json::Value l2aObject;
1592 l2aObject["key1"] = 0;
1593 l2aObject["key2"] = 0;
1594 Json::Value l2bObject;
1595 l2bObject["key1"] = 10;
1596 l2bObject["key2"] = 0;
1597 JSONTEST_ASSERT_PRED(checkIsLess(emptyObject, l1aObject));
1598 JSONTEST_ASSERT_PRED(checkIsLess(l1aObject, l1bObject));
1599 JSONTEST_ASSERT_PRED(checkIsLess(l1bObject, l2aObject));
1600 JSONTEST_ASSERT_PRED(checkIsLess(l2aObject, l2bObject));
1601 JSONTEST_ASSERT_PRED(checkIsEqual(emptyObject, Json::Value(emptyObject)));
1602 JSONTEST_ASSERT_PRED(checkIsEqual(l1aObject, Json::Value(l1aObject)));
1603 JSONTEST_ASSERT_PRED(checkIsEqual(l1bObject, Json::Value(l1bObject)));
1604 JSONTEST_ASSERT_PRED(checkIsEqual(l2aObject, Json::Value(l2aObject)));
1605 JSONTEST_ASSERT_PRED(checkIsEqual(l2bObject, Json::Value(l2bObject)));
1606 {
1607 Json::Value aObject;
1608 aObject["a"] = 10;
1609 Json::Value bObject;
1610 bObject["b"] = 0;
1611 Json::Value cObject;
1612 cObject["c"] = 20;
1613 cObject["f"] = 15;
1614 Json::Value dObject;
1615 dObject["d"] = -2;
1616 dObject["e"] = 10;
1617 JSONTEST_ASSERT_PRED(checkIsLess(aObject, bObject));
1618 JSONTEST_ASSERT_PRED(checkIsLess(bObject, cObject));
1619 JSONTEST_ASSERT_PRED(checkIsLess(cObject, dObject));
1620 JSONTEST_ASSERT_PRED(checkIsEqual(aObject, Json::Value(aObject)));
1621 JSONTEST_ASSERT_PRED(checkIsEqual(bObject, Json::Value(bObject)));
1622 JSONTEST_ASSERT_PRED(checkIsEqual(cObject, Json::Value(cObject)));
1623 JSONTEST_ASSERT_PRED(checkIsEqual(dObject, Json::Value(dObject)));
1624 }
1625 }
1626
JSONTEST_FIXTURE_LOCAL(ValueTest,compareType)1627 JSONTEST_FIXTURE_LOCAL(ValueTest, compareType) {
1628 // object of different type are ordered according to their type
1629 JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(), Json::Value(1)));
1630 JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(1), Json::Value(1u)));
1631 JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(1u), Json::Value(1.0)));
1632 JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(1.0), Json::Value("a")));
1633 JSONTEST_ASSERT_PRED(checkIsLess(Json::Value("a"), Json::Value(true)));
1634 JSONTEST_ASSERT_PRED(
1635 checkIsLess(Json::Value(true), Json::Value(Json::arrayValue)));
1636 JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(Json::arrayValue),
1637 Json::Value(Json::objectValue)));
1638 }
1639
JSONTEST_FIXTURE_LOCAL(ValueTest,CopyObject)1640 JSONTEST_FIXTURE_LOCAL(ValueTest, CopyObject) {
1641 Json::Value arrayVal;
1642 arrayVal.append("val1");
1643 arrayVal.append("val2");
1644 arrayVal.append("val3");
1645 Json::Value stringVal("string value");
1646 Json::Value copy1, copy2;
1647 {
1648 Json::Value arrayCopy, stringCopy;
1649 arrayCopy.copy(arrayVal);
1650 stringCopy.copy(stringVal);
1651 JSONTEST_ASSERT_PRED(checkIsEqual(arrayCopy, arrayVal));
1652 JSONTEST_ASSERT_PRED(checkIsEqual(stringCopy, stringVal));
1653 arrayCopy.append("val4");
1654 JSONTEST_ASSERT(arrayCopy.size() == 4);
1655 arrayVal.append("new4");
1656 arrayVal.append("new5");
1657 JSONTEST_ASSERT(arrayVal.size() == 5);
1658 JSONTEST_ASSERT(!(arrayCopy == arrayVal));
1659 stringCopy = "another string";
1660 JSONTEST_ASSERT(!(stringCopy == stringVal));
1661 copy1.copy(arrayCopy);
1662 copy2.copy(stringCopy);
1663 }
1664 JSONTEST_ASSERT(arrayVal.size() == 5);
1665 JSONTEST_ASSERT(stringVal == "string value");
1666 JSONTEST_ASSERT(copy1.size() == 4);
1667 JSONTEST_ASSERT(copy2 == "another string");
1668 copy1.copy(stringVal);
1669 JSONTEST_ASSERT(copy1 == "string value");
1670 copy2.copy(arrayVal);
1671 JSONTEST_ASSERT(copy2.size() == 5);
1672 {
1673 Json::Value srcObject, objectCopy, otherObject;
1674 srcObject["key0"] = 10;
1675 objectCopy.copy(srcObject);
1676 JSONTEST_ASSERT(srcObject["key0"].asInt() == 10);
1677 JSONTEST_ASSERT(objectCopy["key0"].asInt() == 10);
1678 JSONTEST_ASSERT(srcObject.getMemberNames().size() == 1);
1679 JSONTEST_ASSERT(objectCopy.getMemberNames().size() == 1);
1680 otherObject["key1"] = 15;
1681 otherObject["key2"] = 16;
1682 JSONTEST_ASSERT(otherObject.getMemberNames().size() == 2);
1683 objectCopy.copy(otherObject);
1684 JSONTEST_ASSERT(objectCopy["key1"].asInt() == 15);
1685 JSONTEST_ASSERT(objectCopy["key2"].asInt() == 16);
1686 JSONTEST_ASSERT(objectCopy.getMemberNames().size() == 2);
1687 otherObject["key1"] = 20;
1688 JSONTEST_ASSERT(objectCopy["key1"].asInt() == 15);
1689 }
1690 }
1691
checkIsLess(const Json::Value & x,const Json::Value & y)1692 void ValueTest::checkIsLess(const Json::Value& x, const Json::Value& y) {
1693 JSONTEST_ASSERT(x < y);
1694 JSONTEST_ASSERT(y > x);
1695 JSONTEST_ASSERT(x <= y);
1696 JSONTEST_ASSERT(y >= x);
1697 JSONTEST_ASSERT(!(x == y));
1698 JSONTEST_ASSERT(!(y == x));
1699 JSONTEST_ASSERT(!(x >= y));
1700 JSONTEST_ASSERT(!(y <= x));
1701 JSONTEST_ASSERT(!(x > y));
1702 JSONTEST_ASSERT(!(y < x));
1703 JSONTEST_ASSERT(x.compare(y) < 0);
1704 JSONTEST_ASSERT(y.compare(x) >= 0);
1705 }
1706
checkIsEqual(const Json::Value & x,const Json::Value & y)1707 void ValueTest::checkIsEqual(const Json::Value& x, const Json::Value& y) {
1708 JSONTEST_ASSERT(x == y);
1709 JSONTEST_ASSERT(y == x);
1710 JSONTEST_ASSERT(x <= y);
1711 JSONTEST_ASSERT(y <= x);
1712 JSONTEST_ASSERT(x >= y);
1713 JSONTEST_ASSERT(y >= x);
1714 JSONTEST_ASSERT(!(x < y));
1715 JSONTEST_ASSERT(!(y < x));
1716 JSONTEST_ASSERT(!(x > y));
1717 JSONTEST_ASSERT(!(y > x));
1718 JSONTEST_ASSERT(x.compare(y) == 0);
1719 JSONTEST_ASSERT(y.compare(x) == 0);
1720 }
1721
JSONTEST_FIXTURE_LOCAL(ValueTest,typeChecksThrowExceptions)1722 JSONTEST_FIXTURE_LOCAL(ValueTest, typeChecksThrowExceptions) {
1723 #if JSON_USE_EXCEPTION
1724
1725 Json::Value intVal(1);
1726 Json::Value strVal("Test");
1727 Json::Value objVal(Json::objectValue);
1728 Json::Value arrVal(Json::arrayValue);
1729
1730 JSONTEST_ASSERT_THROWS(intVal["test"]);
1731 JSONTEST_ASSERT_THROWS(strVal["test"]);
1732 JSONTEST_ASSERT_THROWS(arrVal["test"]);
1733
1734 JSONTEST_ASSERT_THROWS(intVal.removeMember("test"));
1735 JSONTEST_ASSERT_THROWS(strVal.removeMember("test"));
1736 JSONTEST_ASSERT_THROWS(arrVal.removeMember("test"));
1737
1738 JSONTEST_ASSERT_THROWS(intVal.getMemberNames());
1739 JSONTEST_ASSERT_THROWS(strVal.getMemberNames());
1740 JSONTEST_ASSERT_THROWS(arrVal.getMemberNames());
1741
1742 JSONTEST_ASSERT_THROWS(intVal[0]);
1743 JSONTEST_ASSERT_THROWS(objVal[0]);
1744 JSONTEST_ASSERT_THROWS(strVal[0]);
1745
1746 JSONTEST_ASSERT_THROWS(intVal.clear());
1747
1748 JSONTEST_ASSERT_THROWS(intVal.resize(1));
1749 JSONTEST_ASSERT_THROWS(strVal.resize(1));
1750 JSONTEST_ASSERT_THROWS(objVal.resize(1));
1751
1752 JSONTEST_ASSERT_THROWS(intVal.asCString());
1753
1754 JSONTEST_ASSERT_THROWS(objVal.asString());
1755 JSONTEST_ASSERT_THROWS(arrVal.asString());
1756
1757 JSONTEST_ASSERT_THROWS(strVal.asInt());
1758 JSONTEST_ASSERT_THROWS(objVal.asInt());
1759 JSONTEST_ASSERT_THROWS(arrVal.asInt());
1760
1761 JSONTEST_ASSERT_THROWS(strVal.asUInt());
1762 JSONTEST_ASSERT_THROWS(objVal.asUInt());
1763 JSONTEST_ASSERT_THROWS(arrVal.asUInt());
1764
1765 JSONTEST_ASSERT_THROWS(strVal.asInt64());
1766 JSONTEST_ASSERT_THROWS(objVal.asInt64());
1767 JSONTEST_ASSERT_THROWS(arrVal.asInt64());
1768
1769 JSONTEST_ASSERT_THROWS(strVal.asUInt64());
1770 JSONTEST_ASSERT_THROWS(objVal.asUInt64());
1771 JSONTEST_ASSERT_THROWS(arrVal.asUInt64());
1772
1773 JSONTEST_ASSERT_THROWS(strVal.asDouble());
1774 JSONTEST_ASSERT_THROWS(objVal.asDouble());
1775 JSONTEST_ASSERT_THROWS(arrVal.asDouble());
1776
1777 JSONTEST_ASSERT_THROWS(strVal.asFloat());
1778 JSONTEST_ASSERT_THROWS(objVal.asFloat());
1779 JSONTEST_ASSERT_THROWS(arrVal.asFloat());
1780
1781 JSONTEST_ASSERT_THROWS(strVal.asBool());
1782 JSONTEST_ASSERT_THROWS(objVal.asBool());
1783 JSONTEST_ASSERT_THROWS(arrVal.asBool());
1784
1785 #endif
1786 }
1787
JSONTEST_FIXTURE_LOCAL(ValueTest,offsetAccessors)1788 JSONTEST_FIXTURE_LOCAL(ValueTest, offsetAccessors) {
1789 Json::Value x;
1790 JSONTEST_ASSERT(x.getOffsetStart() == 0);
1791 JSONTEST_ASSERT(x.getOffsetLimit() == 0);
1792 x.setOffsetStart(10);
1793 x.setOffsetLimit(20);
1794 JSONTEST_ASSERT(x.getOffsetStart() == 10);
1795 JSONTEST_ASSERT(x.getOffsetLimit() == 20);
1796 Json::Value y(x);
1797 JSONTEST_ASSERT(y.getOffsetStart() == 10);
1798 JSONTEST_ASSERT(y.getOffsetLimit() == 20);
1799 Json::Value z;
1800 z.swap(y);
1801 JSONTEST_ASSERT(z.getOffsetStart() == 10);
1802 JSONTEST_ASSERT(z.getOffsetLimit() == 20);
1803 JSONTEST_ASSERT(y.getOffsetStart() == 0);
1804 JSONTEST_ASSERT(y.getOffsetLimit() == 0);
1805 }
1806
JSONTEST_FIXTURE_LOCAL(ValueTest,StaticString)1807 JSONTEST_FIXTURE_LOCAL(ValueTest, StaticString) {
1808 char mutant[] = "hello";
1809 Json::StaticString ss(mutant);
1810 Json::String regular(mutant);
1811 mutant[1] = 'a';
1812 JSONTEST_ASSERT_STRING_EQUAL("hallo", ss.c_str());
1813 JSONTEST_ASSERT_STRING_EQUAL("hello", regular.c_str());
1814 {
1815 Json::Value root;
1816 root["top"] = ss;
1817 JSONTEST_ASSERT_STRING_EQUAL("hallo", root["top"].asString());
1818 mutant[1] = 'u';
1819 JSONTEST_ASSERT_STRING_EQUAL("hullo", root["top"].asString());
1820 }
1821 {
1822 Json::Value root;
1823 root["top"] = regular;
1824 JSONTEST_ASSERT_STRING_EQUAL("hello", root["top"].asString());
1825 mutant[1] = 'u';
1826 JSONTEST_ASSERT_STRING_EQUAL("hello", root["top"].asString());
1827 }
1828 }
1829
JSONTEST_FIXTURE_LOCAL(ValueTest,WideString)1830 JSONTEST_FIXTURE_LOCAL(ValueTest, WideString) {
1831 // https://github.com/open-source-parsers/jsoncpp/issues/756
1832 const std::string uni = "\u5f0f\uff0c\u8fdb"; // "式,进"
1833 std::string styled;
1834 {
1835 Json::Value v;
1836 v["abc"] = uni;
1837 styled = v.toStyledString();
1838 }
1839 Json::Value root;
1840 {
1841 JSONCPP_STRING errs;
1842 std::istringstream iss(styled);
1843 bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs);
1844 JSONTEST_ASSERT(ok);
1845 if (!ok) {
1846 std::cerr << "errs: " << errs << std::endl;
1847 }
1848 }
1849 JSONTEST_ASSERT_STRING_EQUAL(root["abc"].asString(), uni);
1850 }
1851
JSONTEST_FIXTURE_LOCAL(ValueTest,CommentBefore)1852 JSONTEST_FIXTURE_LOCAL(ValueTest, CommentBefore) {
1853 Json::Value val; // fill val
1854 val.setComment(Json::String("// this comment should appear before"),
1855 Json::commentBefore);
1856 Json::StreamWriterBuilder wbuilder;
1857 wbuilder.settings_["commentStyle"] = "All";
1858 {
1859 char const expected[] = "// this comment should appear before\nnull";
1860 Json::String result = Json::writeString(wbuilder, val);
1861 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1862 Json::String res2 = val.toStyledString();
1863 Json::String exp2 = "\n";
1864 exp2 += expected;
1865 exp2 += "\n";
1866 JSONTEST_ASSERT_STRING_EQUAL(exp2, res2);
1867 }
1868 Json::Value other = "hello";
1869 val.swapPayload(other);
1870 {
1871 char const expected[] = "// this comment should appear before\n\"hello\"";
1872 Json::String result = Json::writeString(wbuilder, val);
1873 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1874 Json::String res2 = val.toStyledString();
1875 Json::String exp2 = "\n";
1876 exp2 += expected;
1877 exp2 += "\n";
1878 JSONTEST_ASSERT_STRING_EQUAL(exp2, res2);
1879 JSONTEST_ASSERT_STRING_EQUAL("null\n", other.toStyledString());
1880 }
1881 val = "hello";
1882 // val.setComment("// this comment should appear before",
1883 // Json::CommentPlacement::commentBefore); Assignment over-writes comments.
1884 {
1885 char const expected[] = "\"hello\"";
1886 Json::String result = Json::writeString(wbuilder, val);
1887 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1888 Json::String res2 = val.toStyledString();
1889 Json::String exp2;
1890 exp2 += expected;
1891 exp2 += "\n";
1892 JSONTEST_ASSERT_STRING_EQUAL(exp2, res2);
1893 }
1894 }
1895
JSONTEST_FIXTURE_LOCAL(ValueTest,zeroes)1896 JSONTEST_FIXTURE_LOCAL(ValueTest, zeroes) {
1897 char const cstr[] = "h\0i";
1898 Json::String binary(cstr, sizeof(cstr)); // include trailing 0
1899 JSONTEST_ASSERT_EQUAL(4U, binary.length());
1900 Json::StreamWriterBuilder b;
1901 {
1902 Json::Value root;
1903 root = binary;
1904 JSONTEST_ASSERT_STRING_EQUAL(binary, root.asString());
1905 }
1906 {
1907 char const top[] = "top";
1908 Json::Value root;
1909 root[top] = binary;
1910 JSONTEST_ASSERT_STRING_EQUAL(binary, root[top].asString());
1911 Json::Value removed;
1912 bool did;
1913 did = root.removeMember(top, top + sizeof(top) - 1U, &removed);
1914 JSONTEST_ASSERT(did);
1915 JSONTEST_ASSERT_STRING_EQUAL(binary, removed.asString());
1916 did = root.removeMember(top, top + sizeof(top) - 1U, &removed);
1917 JSONTEST_ASSERT(!did);
1918 JSONTEST_ASSERT_STRING_EQUAL(binary, removed.asString()); // still
1919 }
1920 }
1921
JSONTEST_FIXTURE_LOCAL(ValueTest,zeroesInKeys)1922 JSONTEST_FIXTURE_LOCAL(ValueTest, zeroesInKeys) {
1923 char const cstr[] = "h\0i";
1924 Json::String binary(cstr, sizeof(cstr)); // include trailing 0
1925 JSONTEST_ASSERT_EQUAL(4U, binary.length());
1926 {
1927 Json::Value root;
1928 root[binary] = "there";
1929 JSONTEST_ASSERT_STRING_EQUAL("there", root[binary].asString());
1930 JSONTEST_ASSERT(!root.isMember("h"));
1931 JSONTEST_ASSERT(root.isMember(binary));
1932 JSONTEST_ASSERT_STRING_EQUAL(
1933 "there", root.get(binary, Json::Value::nullSingleton()).asString());
1934 Json::Value removed;
1935 bool did;
1936 did = root.removeMember(binary.data(), binary.data() + binary.length(),
1937 &removed);
1938 JSONTEST_ASSERT(did);
1939 JSONTEST_ASSERT_STRING_EQUAL("there", removed.asString());
1940 did = root.removeMember(binary.data(), binary.data() + binary.length(),
1941 &removed);
1942 JSONTEST_ASSERT(!did);
1943 JSONTEST_ASSERT_STRING_EQUAL("there", removed.asString()); // still
1944 JSONTEST_ASSERT(!root.isMember(binary));
1945 JSONTEST_ASSERT_STRING_EQUAL(
1946 "", root.get(binary, Json::Value::nullSingleton()).asString());
1947 }
1948 }
1949
JSONTEST_FIXTURE_LOCAL(ValueTest,specialFloats)1950 JSONTEST_FIXTURE_LOCAL(ValueTest, specialFloats) {
1951 Json::StreamWriterBuilder b;
1952 b.settings_["useSpecialFloats"] = true;
1953
1954 Json::Value v = std::numeric_limits<double>::quiet_NaN();
1955 Json::String expected = "NaN";
1956 Json::String result = Json::writeString(b, v);
1957 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1958
1959 v = std::numeric_limits<double>::infinity();
1960 expected = "Infinity";
1961 result = Json::writeString(b, v);
1962 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1963
1964 v = -std::numeric_limits<double>::infinity();
1965 expected = "-Infinity";
1966 result = Json::writeString(b, v);
1967 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1968 }
1969
JSONTEST_FIXTURE_LOCAL(ValueTest,precision)1970 JSONTEST_FIXTURE_LOCAL(ValueTest, precision) {
1971 Json::StreamWriterBuilder b;
1972 b.settings_["precision"] = 5;
1973
1974 Json::Value v = 100.0 / 3;
1975 Json::String expected = "33.333";
1976 Json::String result = Json::writeString(b, v);
1977 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1978
1979 v = 0.25000000;
1980 expected = "0.25";
1981 result = Json::writeString(b, v);
1982 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1983
1984 v = 0.2563456;
1985 expected = "0.25635";
1986 result = Json::writeString(b, v);
1987 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1988
1989 b.settings_["precision"] = 1;
1990 expected = "0.3";
1991 result = Json::writeString(b, v);
1992 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1993
1994 b.settings_["precision"] = 17;
1995 v = 1234857476305.256345694873740545068;
1996 expected = "1234857476305.2563";
1997 result = Json::writeString(b, v);
1998 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
1999
2000 b.settings_["precision"] = 24;
2001 v = 0.256345694873740545068;
2002 expected = "0.25634569487374054";
2003 result = Json::writeString(b, v);
2004 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2005
2006 b.settings_["precision"] = 5;
2007 b.settings_["precisionType"] = "decimal";
2008 v = 0.256345694873740545068;
2009 expected = "0.25635";
2010 result = Json::writeString(b, v);
2011 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2012
2013 b.settings_["precision"] = 1;
2014 b.settings_["precisionType"] = "decimal";
2015 v = 0.256345694873740545068;
2016 expected = "0.3";
2017 result = Json::writeString(b, v);
2018 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2019
2020 b.settings_["precision"] = 10;
2021 b.settings_["precisionType"] = "decimal";
2022 v = 0.23300000;
2023 expected = "0.233";
2024 result = Json::writeString(b, v);
2025 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2026 }
JSONTEST_FIXTURE_LOCAL(ValueTest,searchValueByPath)2027 JSONTEST_FIXTURE_LOCAL(ValueTest, searchValueByPath) {
2028 Json::Value root, subroot;
2029 root["property1"][0] = 0;
2030 root["property1"][1] = 1;
2031 subroot["object"] = "object";
2032 root["property2"] = subroot;
2033
2034 const Json::Value defaultValue("error");
2035 Json::FastWriter writer;
2036
2037 {
2038 const Json::String expected("{"
2039 "\"property1\":[0,1],"
2040 "\"property2\":{\"object\":\"object\"}"
2041 "}\n");
2042 Json::String outcome = writer.write(root);
2043 JSONTEST_ASSERT_STRING_EQUAL(expected, outcome);
2044
2045 // Array member exists.
2046 const Json::Path path1(".property1.[%]", 1);
2047 Json::Value result = path1.resolve(root);
2048 JSONTEST_ASSERT_EQUAL(Json::Value(1), result);
2049 result = path1.resolve(root, defaultValue);
2050 JSONTEST_ASSERT_EQUAL(Json::Value(1), result);
2051
2052 // Array member does not exist.
2053 const Json::Path path2(".property1.[2]");
2054 result = path2.resolve(root);
2055 JSONTEST_ASSERT_EQUAL(Json::nullValue, result);
2056 result = path2.resolve(root, defaultValue);
2057 JSONTEST_ASSERT_EQUAL(defaultValue, result);
2058
2059 // Access array path form error
2060 const Json::Path path3(".property1.0");
2061 result = path3.resolve(root);
2062 JSONTEST_ASSERT_EQUAL(Json::nullValue, result);
2063 result = path3.resolve(root, defaultValue);
2064 JSONTEST_ASSERT_EQUAL(defaultValue, result);
2065
2066 // Object member exists.
2067 const Json::Path path4(".property2.%", "object");
2068 result = path4.resolve(root);
2069 JSONTEST_ASSERT_EQUAL(Json::Value("object"), result);
2070 result = path4.resolve(root, defaultValue);
2071 JSONTEST_ASSERT_EQUAL(Json::Value("object"), result);
2072
2073 // Object member does not exist.
2074 const Json::Path path5(".property2.hello");
2075 result = path5.resolve(root);
2076 JSONTEST_ASSERT_EQUAL(Json::nullValue, result);
2077 result = path5.resolve(root, defaultValue);
2078 JSONTEST_ASSERT_EQUAL(defaultValue, result);
2079
2080 // Access object path form error
2081 const Json::Path path6(".property2.[0]");
2082 result = path5.resolve(root);
2083 JSONTEST_ASSERT_EQUAL(Json::nullValue, result);
2084 result = path6.resolve(root, defaultValue);
2085 JSONTEST_ASSERT_EQUAL(defaultValue, result);
2086
2087 // resolve will not change the value
2088 outcome = writer.write(root);
2089 JSONTEST_ASSERT_STRING_EQUAL(expected, outcome);
2090 }
2091 {
2092 const Json::String expected("{"
2093 "\"property1\":[0,1,null],"
2094 "\"property2\":{"
2095 "\"hello\":null,"
2096 "\"object\":\"object\"}}\n");
2097 Json::Path path1(".property1.[%]", 2);
2098 Json::Value& value1 = path1.make(root);
2099 JSONTEST_ASSERT_EQUAL(Json::nullValue, value1);
2100
2101 Json::Path path2(".property2.%", "hello");
2102 Json::Value& value2 = path2.make(root);
2103 JSONTEST_ASSERT_EQUAL(Json::nullValue, value2);
2104
2105 // make will change the value
2106 const Json::String outcome = writer.write(root);
2107 JSONTEST_ASSERT_STRING_EQUAL(expected, outcome);
2108 }
2109 }
2110 struct FastWriterTest : JsonTest::TestCase {};
2111
JSONTEST_FIXTURE_LOCAL(FastWriterTest,dropNullPlaceholders)2112 JSONTEST_FIXTURE_LOCAL(FastWriterTest, dropNullPlaceholders) {
2113 Json::FastWriter writer;
2114 Json::Value nullValue;
2115 JSONTEST_ASSERT(writer.write(nullValue) == "null\n");
2116
2117 writer.dropNullPlaceholders();
2118 JSONTEST_ASSERT(writer.write(nullValue) == "\n");
2119 }
2120
JSONTEST_FIXTURE_LOCAL(FastWriterTest,enableYAMLCompatibility)2121 JSONTEST_FIXTURE_LOCAL(FastWriterTest, enableYAMLCompatibility) {
2122 Json::FastWriter writer;
2123 Json::Value root;
2124 root["hello"] = "world";
2125
2126 JSONTEST_ASSERT(writer.write(root) == "{\"hello\":\"world\"}\n");
2127
2128 writer.enableYAMLCompatibility();
2129 JSONTEST_ASSERT(writer.write(root) == "{\"hello\": \"world\"}\n");
2130 }
2131
JSONTEST_FIXTURE_LOCAL(FastWriterTest,omitEndingLineFeed)2132 JSONTEST_FIXTURE_LOCAL(FastWriterTest, omitEndingLineFeed) {
2133 Json::FastWriter writer;
2134 Json::Value nullValue;
2135
2136 JSONTEST_ASSERT(writer.write(nullValue) == "null\n");
2137
2138 writer.omitEndingLineFeed();
2139 JSONTEST_ASSERT(writer.write(nullValue) == "null");
2140 }
2141
JSONTEST_FIXTURE_LOCAL(FastWriterTest,writeNumericValue)2142 JSONTEST_FIXTURE_LOCAL(FastWriterTest, writeNumericValue) {
2143 Json::FastWriter writer;
2144 const Json::String expected("{"
2145 "\"emptyValue\":null,"
2146 "\"false\":false,"
2147 "\"null\":\"null\","
2148 "\"number\":-6200000000000000.0,"
2149 "\"real\":1.256,"
2150 "\"uintValue\":17"
2151 "}\n");
2152 Json::Value root;
2153 root["emptyValue"] = Json::nullValue;
2154 root["false"] = false;
2155 root["null"] = "null";
2156 root["number"] = -6.2e+15;
2157 root["real"] = 1.256;
2158 root["uintValue"] = Json::Value(17U);
2159
2160 const Json::String result = writer.write(root);
2161 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2162 }
2163
JSONTEST_FIXTURE_LOCAL(FastWriterTest,writeArrays)2164 JSONTEST_FIXTURE_LOCAL(FastWriterTest, writeArrays) {
2165 Json::FastWriter writer;
2166 const Json::String expected("{"
2167 "\"property1\":[\"value1\",\"value2\"],"
2168 "\"property2\":[]"
2169 "}\n");
2170 Json::Value root;
2171 root["property1"][0] = "value1";
2172 root["property1"][1] = "value2";
2173 root["property2"] = Json::arrayValue;
2174
2175 const Json::String result = writer.write(root);
2176 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2177 }
2178
JSONTEST_FIXTURE_LOCAL(FastWriterTest,writeNestedObjects)2179 JSONTEST_FIXTURE_LOCAL(FastWriterTest, writeNestedObjects) {
2180 Json::FastWriter writer;
2181 const Json::String expected("{"
2182 "\"object1\":{"
2183 "\"bool\":true,"
2184 "\"nested\":123"
2185 "},"
2186 "\"object2\":{}"
2187 "}\n");
2188 Json::Value root, child;
2189 child["nested"] = 123;
2190 child["bool"] = true;
2191 root["object1"] = child;
2192 root["object2"] = Json::objectValue;
2193
2194 const Json::String result = writer.write(root);
2195 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2196 }
2197
2198 struct StyledWriterTest : JsonTest::TestCase {};
2199
JSONTEST_FIXTURE_LOCAL(StyledWriterTest,writeNumericValue)2200 JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeNumericValue) {
2201 Json::StyledWriter writer;
2202 const Json::String expected("{\n"
2203 " \"emptyValue\" : null,\n"
2204 " \"false\" : false,\n"
2205 " \"null\" : \"null\",\n"
2206 " \"number\" : -6200000000000000.0,\n"
2207 " \"real\" : 1.256,\n"
2208 " \"uintValue\" : 17\n"
2209 "}\n");
2210 Json::Value root;
2211 root["emptyValue"] = Json::nullValue;
2212 root["false"] = false;
2213 root["null"] = "null";
2214 root["number"] = -6.2e+15;
2215 root["real"] = 1.256;
2216 root["uintValue"] = Json::Value(17U);
2217
2218 const Json::String result = writer.write(root);
2219 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2220 }
2221
JSONTEST_FIXTURE_LOCAL(StyledWriterTest,writeArrays)2222 JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeArrays) {
2223 Json::StyledWriter writer;
2224 const Json::String expected("{\n"
2225 " \"property1\" : [ \"value1\", \"value2\" ],\n"
2226 " \"property2\" : []\n"
2227 "}\n");
2228 Json::Value root;
2229 root["property1"][0] = "value1";
2230 root["property1"][1] = "value2";
2231 root["property2"] = Json::arrayValue;
2232
2233 const Json::String result = writer.write(root);
2234 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2235 }
2236
JSONTEST_FIXTURE_LOCAL(StyledWriterTest,writeNestedObjects)2237 JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeNestedObjects) {
2238 Json::StyledWriter writer;
2239 const Json::String expected("{\n"
2240 " \"object1\" : {\n"
2241 " \"bool\" : true,\n"
2242 " \"nested\" : 123\n"
2243 " },\n"
2244 " \"object2\" : {}\n"
2245 "}\n");
2246 Json::Value root, child;
2247 child["nested"] = 123;
2248 child["bool"] = true;
2249 root["object1"] = child;
2250 root["object2"] = Json::objectValue;
2251
2252 const Json::String result = writer.write(root);
2253 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2254 }
2255
JSONTEST_FIXTURE_LOCAL(StyledWriterTest,multiLineArray)2256 JSONTEST_FIXTURE_LOCAL(StyledWriterTest, multiLineArray) {
2257 Json::StyledWriter writer;
2258 {
2259 // Array member has more than 20 print effect rendering lines
2260 const Json::String expected("[\n "
2261 "0,\n 1,\n 2,\n "
2262 "3,\n 4,\n 5,\n "
2263 "6,\n 7,\n 8,\n "
2264 "9,\n 10,\n 11,\n "
2265 "12,\n 13,\n 14,\n "
2266 "15,\n 16,\n 17,\n "
2267 "18,\n 19,\n 20\n]\n");
2268 Json::Value root;
2269 for (Json::ArrayIndex i = 0; i < 21; i++)
2270 root[i] = i;
2271 const Json::String result = writer.write(root);
2272 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2273 }
2274 {
2275 // Array members do not exceed 21 print effects to render a single line
2276 const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n");
2277 Json::Value root;
2278 for (Json::ArrayIndex i = 0; i < 10; i++)
2279 root[i] = i;
2280 const Json::String result = writer.write(root);
2281 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2282 }
2283 }
2284
JSONTEST_FIXTURE_LOCAL(StyledWriterTest,writeValueWithComment)2285 JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeValueWithComment) {
2286 Json::StyledWriter writer;
2287 {
2288 const Json::String expected("\n//commentBeforeValue\n\"hello\"\n");
2289 Json::Value root = "hello";
2290 root.setComment(Json::String("//commentBeforeValue"), Json::commentBefore);
2291 const Json::String result = writer.write(root);
2292 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2293 }
2294 {
2295 const Json::String expected("\"hello\" //commentAfterValueOnSameLine\n");
2296 Json::Value root = "hello";
2297 root.setComment(Json::String("//commentAfterValueOnSameLine"),
2298 Json::commentAfterOnSameLine);
2299 const Json::String result = writer.write(root);
2300 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2301 }
2302 {
2303 const Json::String expected("\"hello\"\n//commentAfter\n\n");
2304 Json::Value root = "hello";
2305 root.setComment(Json::String("//commentAfter"), Json::commentAfter);
2306 const Json::String result = writer.write(root);
2307 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2308 }
2309 }
2310
2311 struct StyledStreamWriterTest : JsonTest::TestCase {};
2312
JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest,writeNumericValue)2313 JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeNumericValue) {
2314 Json::StyledStreamWriter writer;
2315 const Json::String expected("{\n"
2316 "\t\"emptyValue\" : null,\n"
2317 "\t\"false\" : false,\n"
2318 "\t\"null\" : \"null\",\n"
2319 "\t\"number\" : -6200000000000000.0,\n"
2320 "\t\"real\" : 1.256,\n"
2321 "\t\"uintValue\" : 17\n"
2322 "}\n");
2323
2324 Json::Value root;
2325 root["emptyValue"] = Json::nullValue;
2326 root["false"] = false;
2327 root["null"] = "null";
2328 root["number"] = -6.2e+15; // big float number
2329 root["real"] = 1.256; // float number
2330 root["uintValue"] = Json::Value(17U);
2331
2332 Json::OStringStream sout;
2333 writer.write(sout, root);
2334 const Json::String result = sout.str();
2335 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2336 }
2337
JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest,writeArrays)2338 JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeArrays) {
2339 Json::StyledStreamWriter writer;
2340 const Json::String expected("{\n"
2341 "\t\"property1\" : [ \"value1\", \"value2\" ],\n"
2342 "\t\"property2\" : []\n"
2343 "}\n");
2344 Json::Value root;
2345 root["property1"][0] = "value1";
2346 root["property1"][1] = "value2";
2347 root["property2"] = Json::arrayValue;
2348
2349 Json::OStringStream sout;
2350 writer.write(sout, root);
2351 const Json::String result = sout.str();
2352 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2353 }
2354
JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest,writeNestedObjects)2355 JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeNestedObjects) {
2356 Json::StyledStreamWriter writer;
2357 const Json::String expected("{\n"
2358 "\t\"object1\" : \n"
2359 "\t"
2360 "{\n"
2361 "\t\t\"bool\" : true,\n"
2362 "\t\t\"nested\" : 123\n"
2363 "\t},\n"
2364 "\t\"object2\" : {}\n"
2365 "}\n");
2366 Json::Value root, child;
2367 child["nested"] = 123;
2368 child["bool"] = true;
2369 root["object1"] = child;
2370 root["object2"] = Json::objectValue;
2371
2372 Json::OStringStream sout;
2373 writer.write(sout, root);
2374 const Json::String result = sout.str();
2375 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2376 }
2377
JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest,multiLineArray)2378 JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, multiLineArray) {
2379 {
2380 // Array member has more than 20 print effect rendering lines
2381 const Json::String expected("[\n\t0,"
2382 "\n\t1,"
2383 "\n\t2,"
2384 "\n\t3,"
2385 "\n\t4,"
2386 "\n\t5,"
2387 "\n\t6,"
2388 "\n\t7,"
2389 "\n\t8,"
2390 "\n\t9,"
2391 "\n\t10,"
2392 "\n\t11,"
2393 "\n\t12,"
2394 "\n\t13,"
2395 "\n\t14,"
2396 "\n\t15,"
2397 "\n\t16,"
2398 "\n\t17,"
2399 "\n\t18,"
2400 "\n\t19,"
2401 "\n\t20\n]\n");
2402 Json::StyledStreamWriter writer;
2403 Json::Value root;
2404 for (Json::ArrayIndex i = 0; i < 21; i++)
2405 root[i] = i;
2406 Json::OStringStream sout;
2407 writer.write(sout, root);
2408 const Json::String result = sout.str();
2409 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2410 }
2411 {
2412 Json::StyledStreamWriter writer;
2413 // Array members do not exceed 21 print effects to render a single line
2414 const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n");
2415 Json::Value root;
2416 for (Json::ArrayIndex i = 0; i < 10; i++)
2417 root[i] = i;
2418 Json::OStringStream sout;
2419 writer.write(sout, root);
2420 const Json::String result = sout.str();
2421 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2422 }
2423 }
2424
JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest,writeValueWithComment)2425 JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeValueWithComment) {
2426 Json::StyledStreamWriter writer("\t");
2427 {
2428 const Json::String expected("//commentBeforeValue\n\"hello\"\n");
2429 Json::Value root = "hello";
2430 Json::OStringStream sout;
2431 root.setComment(Json::String("//commentBeforeValue"), Json::commentBefore);
2432 writer.write(sout, root);
2433 const Json::String result = sout.str();
2434 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2435 }
2436 {
2437 const Json::String expected("\"hello\" //commentAfterValueOnSameLine\n");
2438 Json::Value root = "hello";
2439 Json::OStringStream sout;
2440 root.setComment(Json::String("//commentAfterValueOnSameLine"),
2441 Json::commentAfterOnSameLine);
2442 writer.write(sout, root);
2443 const Json::String result = sout.str();
2444 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2445 }
2446 {
2447 const Json::String expected("\"hello\"\n//commentAfter\n");
2448 Json::Value root = "hello";
2449 Json::OStringStream sout;
2450 root.setComment(Json::String("//commentAfter"), Json::commentAfter);
2451 writer.write(sout, root);
2452 const Json::String result = sout.str();
2453 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2454 }
2455 }
2456
2457 struct StreamWriterTest : JsonTest::TestCase {};
2458
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,writeNumericValue)2459 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeNumericValue) {
2460 Json::StreamWriterBuilder writer;
2461 const Json::String expected("{\n"
2462 "\t\"emptyValue\" : null,\n"
2463 "\t\"false\" : false,\n"
2464 "\t\"null\" : \"null\",\n"
2465 "\t\"number\" : -6200000000000000.0,\n"
2466 "\t\"real\" : 1.256,\n"
2467 "\t\"uintValue\" : 17\n"
2468 "}");
2469 Json::Value root;
2470 root["emptyValue"] = Json::nullValue;
2471 root["false"] = false;
2472 root["null"] = "null";
2473 root["number"] = -6.2e+15;
2474 root["real"] = 1.256;
2475 root["uintValue"] = Json::Value(17U);
2476
2477 const Json::String result = Json::writeString(writer, root);
2478 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2479 }
2480
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,writeArrays)2481 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeArrays) {
2482 Json::StreamWriterBuilder writer;
2483 const Json::String expected("{\n"
2484 "\t\"property1\" : \n"
2485 "\t[\n"
2486 "\t\t\"value1\",\n"
2487 "\t\t\"value2\"\n"
2488 "\t],\n"
2489 "\t\"property2\" : []\n"
2490 "}");
2491
2492 Json::Value root;
2493 root["property1"][0] = "value1";
2494 root["property1"][1] = "value2";
2495 root["property2"] = Json::arrayValue;
2496
2497 const Json::String result = Json::writeString(writer, root);
2498 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2499 }
2500
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,writeNestedObjects)2501 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeNestedObjects) {
2502 Json::StreamWriterBuilder writer;
2503 const Json::String expected("{\n"
2504 "\t\"object1\" : \n"
2505 "\t{\n"
2506 "\t\t\"bool\" : true,\n"
2507 "\t\t\"nested\" : 123\n"
2508 "\t},\n"
2509 "\t\"object2\" : {}\n"
2510 "}");
2511
2512 Json::Value root, child;
2513 child["nested"] = 123;
2514 child["bool"] = true;
2515 root["object1"] = child;
2516 root["object2"] = Json::objectValue;
2517
2518 const Json::String result = Json::writeString(writer, root);
2519 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2520 }
2521
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,multiLineArray)2522 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, multiLineArray) {
2523 Json::StreamWriterBuilder wb;
2524 wb.settings_["commentStyle"] = "None";
2525 {
2526 // When wb.settings_["commentStyle"] = "None", the effect of
2527 // printing multiple lines will be displayed when there are
2528 // more than 20 array members.
2529 const Json::String expected("[\n\t0,"
2530 "\n\t1,"
2531 "\n\t2,"
2532 "\n\t3,"
2533 "\n\t4,"
2534 "\n\t5,"
2535 "\n\t6,"
2536 "\n\t7,"
2537 "\n\t8,"
2538 "\n\t9,"
2539 "\n\t10,"
2540 "\n\t11,"
2541 "\n\t12,"
2542 "\n\t13,"
2543 "\n\t14,"
2544 "\n\t15,"
2545 "\n\t16,"
2546 "\n\t17,"
2547 "\n\t18,"
2548 "\n\t19,"
2549 "\n\t20\n]");
2550 Json::Value root;
2551 for (Json::ArrayIndex i = 0; i < 21; i++)
2552 root[i] = i;
2553 const Json::String result = Json::writeString(wb, root);
2554 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2555 }
2556 {
2557 // Array members do not exceed 21 print effects to render a single line
2558 const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]");
2559 Json::Value root;
2560 for (Json::ArrayIndex i = 0; i < 10; i++)
2561 root[i] = i;
2562 const Json::String result = Json::writeString(wb, root);
2563 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
2564 }
2565 }
2566
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,dropNullPlaceholders)2567 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, dropNullPlaceholders) {
2568 Json::StreamWriterBuilder b;
2569 Json::Value nullValue;
2570 b.settings_["dropNullPlaceholders"] = false;
2571 JSONTEST_ASSERT(Json::writeString(b, nullValue) == "null");
2572 b.settings_["dropNullPlaceholders"] = true;
2573 JSONTEST_ASSERT(Json::writeString(b, nullValue).empty());
2574 }
2575
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,enableYAMLCompatibility)2576 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, enableYAMLCompatibility) {
2577 Json::StreamWriterBuilder b;
2578 Json::Value root;
2579 root["hello"] = "world";
2580
2581 b.settings_["indentation"] = "";
2582 JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}");
2583
2584 b.settings_["enableYAMLCompatibility"] = true;
2585 JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\": \"world\"}");
2586
2587 b.settings_["enableYAMLCompatibility"] = false;
2588 JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}");
2589 }
2590
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,indentation)2591 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, indentation) {
2592 Json::StreamWriterBuilder b;
2593 Json::Value root;
2594 root["hello"] = "world";
2595
2596 b.settings_["indentation"] = "";
2597 JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}");
2598
2599 b.settings_["indentation"] = "\t";
2600 JSONTEST_ASSERT(Json::writeString(b, root) ==
2601 "{\n\t\"hello\" : \"world\"\n}");
2602 }
2603
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,writeZeroes)2604 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeZeroes) {
2605 Json::String binary("hi", 3); // include trailing 0
2606 JSONTEST_ASSERT_EQUAL(3, binary.length());
2607 Json::String expected("\"hi\\u0000\""); // unicoded zero
2608 Json::StreamWriterBuilder b;
2609 {
2610 Json::Value root;
2611 root = binary;
2612 JSONTEST_ASSERT_STRING_EQUAL(binary, root.asString());
2613 Json::String out = Json::writeString(b, root);
2614 JSONTEST_ASSERT_EQUAL(expected.size(), out.size());
2615 JSONTEST_ASSERT_STRING_EQUAL(expected, out);
2616 }
2617 {
2618 Json::Value root;
2619 root["top"] = binary;
2620 JSONTEST_ASSERT_STRING_EQUAL(binary, root["top"].asString());
2621 Json::String out = Json::writeString(b, root["top"]);
2622 JSONTEST_ASSERT_STRING_EQUAL(expected, out);
2623 }
2624 }
2625
JSONTEST_FIXTURE_LOCAL(StreamWriterTest,unicode)2626 JSONTEST_FIXTURE_LOCAL(StreamWriterTest, unicode) {
2627 // Create a Json value containing UTF-8 string with some chars that need
2628 // escape (tab,newline).
2629 Json::Value root;
2630 root["test"] = "\t\n\xF0\x91\xA2\xA1\x3D\xC4\xB3\xF0\x9B\x84\x9B\xEF\xBD\xA7";
2631
2632 Json::StreamWriterBuilder b;
2633
2634 // Default settings - should be unicode escaped.
2635 JSONTEST_ASSERT(Json::writeString(b, root) ==
2636 "{\n\t\"test\" : "
2637 "\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}");
2638
2639 b.settings_["emitUTF8"] = true;
2640
2641 // Should not be unicode escaped.
2642 JSONTEST_ASSERT(
2643 Json::writeString(b, root) ==
2644 "{\n\t\"test\" : "
2645 "\"\\t\\n\xF0\x91\xA2\xA1=\xC4\xB3\xF0\x9B\x84\x9B\xEF\xBD\xA7\"\n}");
2646
2647 b.settings_["emitUTF8"] = false;
2648
2649 // Should be unicode escaped.
2650 JSONTEST_ASSERT(Json::writeString(b, root) ==
2651 "{\n\t\"test\" : "
2652 "\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}");
2653 }
2654 #if JSONCPP_CXX_STD_11
2655 struct ReaderTest : JsonTest::TestCase {
setStrictModeReaderTest2656 void setStrictMode() {
2657 reader = std::unique_ptr<Json::Reader>(
2658 new Json::Reader(Json::Features{}.strictMode()));
2659 }
2660
setFeaturesReaderTest2661 void setFeatures(Json::Features& features) {
2662 reader = std::unique_ptr<Json::Reader>(new Json::Reader(features));
2663 }
2664
checkStructuredErrorsReaderTest2665 void checkStructuredErrors(
2666 const std::vector<Json::Reader::StructuredError>& actual,
2667 const std::vector<Json::Reader::StructuredError>& expected) {
2668 JSONTEST_ASSERT_EQUAL(expected.size(), actual.size());
2669 for (size_t i = 0; i < actual.size(); ++i) {
2670 const auto& a = actual[i];
2671 const auto& e = expected[i];
2672 JSONTEST_ASSERT_EQUAL(e.offset_start, a.offset_start) << i;
2673 JSONTEST_ASSERT_EQUAL(e.offset_limit, a.offset_limit) << i;
2674 JSONTEST_ASSERT_EQUAL(e.message, a.message) << i;
2675 }
2676 }
2677
checkParseReaderTest2678 template <typename Input> void checkParse(Input&& input) {
2679 JSONTEST_ASSERT(reader->parse(input, root));
2680 }
2681
2682 template <typename Input>
2683 void
checkParseReaderTest2684 checkParse(Input&& input,
2685 const std::vector<Json::Reader::StructuredError>& structured) {
2686 JSONTEST_ASSERT(!reader->parse(input, root));
2687 checkStructuredErrors(reader->getStructuredErrors(), structured);
2688 }
2689
2690 template <typename Input>
checkParseReaderTest2691 void checkParse(Input&& input,
2692 const std::vector<Json::Reader::StructuredError>& structured,
2693 const std::string& formatted) {
2694 checkParse(input, structured);
2695 JSONTEST_ASSERT_EQUAL(formatted, reader->getFormattedErrorMessages());
2696 }
2697
2698 std::unique_ptr<Json::Reader> reader{new Json::Reader()};
2699 Json::Value root;
2700 };
2701
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseWithNoErrors)2702 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrors) {
2703 checkParse("{ \"property\" : \"value\" }");
2704 }
2705
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseObject)2706 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseObject) {
2707 checkParse("{\"property\"}",
2708 {{11, 12, "Missing ':' after object member name"}},
2709 "* Line 1, Column 12\n Missing ':' after object member name\n");
2710 checkParse(
2711 "{\"property\" : \"value\" ",
2712 {{22, 22, "Missing ',' or '}' in object declaration"}},
2713 "* Line 1, Column 23\n Missing ',' or '}' in object declaration\n");
2714 checkParse("{\"property\" : \"value\", ",
2715 {{23, 23, "Missing '}' or object member name"}},
2716 "* Line 1, Column 24\n Missing '}' or object member name\n");
2717 }
2718
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseArray)2719 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseArray) {
2720 checkParse(
2721 "[ \"value\" ", {{10, 10, "Missing ',' or ']' in array declaration"}},
2722 "* Line 1, Column 11\n Missing ',' or ']' in array declaration\n");
2723 checkParse(
2724 "[ \"value1\" \"value2\" ] ",
2725 {{11, 19, "Missing ',' or ']' in array declaration"}},
2726 "* Line 1, Column 12\n Missing ',' or ']' in array declaration\n");
2727 }
2728
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseString)2729 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) {
2730 checkParse("[ \"\u8a2a\" ]");
2731 checkParse(
2732 "[ \"\\ud801\" ]",
2733 {{2, 10,
2734 "additional six characters expected to parse unicode surrogate "
2735 "pair."}},
2736 "* Line 1, Column 3\n"
2737 " additional six characters expected to parse unicode surrogate pair.\n"
2738 "See Line 1, Column 10 for detail.\n");
2739 checkParse("[ \"\\ud801\\d1234\" ]",
2740 {{2, 16,
2741 "expecting another \\u token to begin the "
2742 "second half of a unicode surrogate pair"}},
2743 "* Line 1, Column 3\n"
2744 " expecting another \\u token to begin the "
2745 "second half of a unicode surrogate pair\n"
2746 "See Line 1, Column 12 for detail.\n");
2747 checkParse("[ \"\\ua3t@\" ]",
2748 {{2, 10,
2749 "Bad unicode escape sequence in string: "
2750 "hexadecimal digit expected."}},
2751 "* Line 1, Column 3\n"
2752 " Bad unicode escape sequence in string: "
2753 "hexadecimal digit expected.\n"
2754 "See Line 1, Column 9 for detail.\n");
2755 checkParse(
2756 "[ \"\\ua3t\" ]",
2757 {{2, 9, "Bad unicode escape sequence in string: four digits expected."}},
2758 "* Line 1, Column 3\n"
2759 " Bad unicode escape sequence in string: four digits expected.\n"
2760 "See Line 1, Column 6 for detail.\n");
2761 }
2762
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseComment)2763 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseComment) {
2764 checkParse(
2765 "{ /*commentBeforeValue*/ \"property\" : \"value\" }//commentAfterValue"
2766 "\n");
2767 checkParse(" true //comment1\n//comment2\r//comment3\r\n");
2768 }
2769
JSONTEST_FIXTURE_LOCAL(ReaderTest,streamParseWithNoErrors)2770 JSONTEST_FIXTURE_LOCAL(ReaderTest, streamParseWithNoErrors) {
2771 std::string styled = "{ \"property\" : \"value\" }";
2772 std::istringstream iss(styled);
2773 checkParse(iss);
2774 }
2775
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseWithNoErrorsTestingOffsets)2776 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrorsTestingOffsets) {
2777 checkParse("{"
2778 " \"property\" : [\"value\", \"value2\"],"
2779 " \"obj\" : { \"nested\" : -6.2e+15, \"bool\" : true},"
2780 " \"null\" : null,"
2781 " \"false\" : false"
2782 "}");
2783 auto checkOffsets = [&](const Json::Value& v, int start, int limit) {
2784 JSONTEST_ASSERT_EQUAL(start, v.getOffsetStart());
2785 JSONTEST_ASSERT_EQUAL(limit, v.getOffsetLimit());
2786 };
2787 checkOffsets(root, 0, 114);
2788 checkOffsets(root["property"], 15, 34);
2789 checkOffsets(root["property"][0], 16, 23);
2790 checkOffsets(root["property"][1], 25, 33);
2791 checkOffsets(root["obj"], 44, 81);
2792 checkOffsets(root["obj"]["nested"], 57, 65);
2793 checkOffsets(root["obj"]["bool"], 76, 80);
2794 checkOffsets(root["null"], 92, 96);
2795 checkOffsets(root["false"], 108, 113);
2796 }
2797
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseWithOneError)2798 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneError) {
2799 checkParse("{ \"property\" :: \"value\" }",
2800 {{14, 15, "Syntax error: value, object or array expected."}},
2801 "* Line 1, Column 15\n Syntax error: value, object or array "
2802 "expected.\n");
2803 checkParse("s", {{0, 1, "Syntax error: value, object or array expected."}},
2804 "* Line 1, Column 1\n Syntax error: value, object or array "
2805 "expected.\n");
2806 }
2807
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseSpecialFloat)2808 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) {
2809 checkParse("{ \"a\" : Infi }",
2810 {{8, 9, "Syntax error: value, object or array expected."}},
2811 "* Line 1, Column 9\n Syntax error: value, object or array "
2812 "expected.\n");
2813 checkParse("{ \"a\" : Infiniaa }",
2814 {{8, 9, "Syntax error: value, object or array expected."}},
2815 "* Line 1, Column 9\n Syntax error: value, object or array "
2816 "expected.\n");
2817 }
2818
JSONTEST_FIXTURE_LOCAL(ReaderTest,strictModeParseNumber)2819 JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) {
2820 setStrictMode();
2821 checkParse(
2822 "123",
2823 {{0, 3,
2824 "A valid JSON document must be either an array or an object value."}},
2825 "* Line 1, Column 1\n"
2826 " A valid JSON document must be either an array or an object value.\n");
2827 }
2828
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseChineseWithOneError)2829 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseChineseWithOneError) {
2830 checkParse("{ \"pr"
2831 #if JSONCPP_CXX_STD_11
2832 u8"\u4f50\u85e4" // 佐藤
2833 #else
2834 "\u4f50\u85e4" // 佐藤
2835 #endif
2836 "erty\" :: \"value\" }",
2837 {{18, 19, "Syntax error: value, object or array expected."}},
2838 "* Line 1, Column 19\n Syntax error: value, object or array "
2839 "expected.\n");
2840 }
2841
JSONTEST_FIXTURE_LOCAL(ReaderTest,parseWithDetailError)2842 JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithDetailError) {
2843 checkParse("{ \"property\" : \"v\\alue\" }",
2844 {{15, 23, "Bad escape sequence in string"}},
2845 "* Line 1, Column 16\n"
2846 " Bad escape sequence in string\n"
2847 "See Line 1, Column 20 for detail.\n");
2848 }
2849
JSONTEST_FIXTURE_LOCAL(ReaderTest,pushErrorTest)2850 JSONTEST_FIXTURE_LOCAL(ReaderTest, pushErrorTest) {
2851 checkParse("{ \"AUTHOR\" : 123 }");
2852 if (!root["AUTHOR"].isString()) {
2853 JSONTEST_ASSERT(
2854 reader->pushError(root["AUTHOR"], "AUTHOR must be a string"));
2855 }
2856 JSONTEST_ASSERT_STRING_EQUAL(reader->getFormattedErrorMessages(),
2857 "* Line 1, Column 14\n"
2858 " AUTHOR must be a string\n");
2859
2860 checkParse("{ \"AUTHOR\" : 123 }");
2861 if (!root["AUTHOR"].isString()) {
2862 JSONTEST_ASSERT(reader->pushError(root["AUTHOR"], "AUTHOR must be a string",
2863 root["AUTHOR"]));
2864 }
2865 JSONTEST_ASSERT_STRING_EQUAL(reader->getFormattedErrorMessages(),
2866 "* Line 1, Column 14\n"
2867 " AUTHOR must be a string\n"
2868 "See Line 1, Column 14 for detail.\n");
2869 }
2870
JSONTEST_FIXTURE_LOCAL(ReaderTest,allowNumericKeysTest)2871 JSONTEST_FIXTURE_LOCAL(ReaderTest, allowNumericKeysTest) {
2872 Json::Features features;
2873 features.allowNumericKeys_ = true;
2874 setFeatures(features);
2875 checkParse("{ 123 : \"abc\" }");
2876 }
2877 #endif // JSONCPP_CXX_STD_11
2878 struct CharReaderTest : JsonTest::TestCase {};
2879
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseWithNoErrors)2880 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrors) {
2881 Json::CharReaderBuilder b;
2882 CharReaderPtr reader(b.newCharReader());
2883 Json::String errs;
2884 Json::Value root;
2885 char const doc[] = "{ \"property\" : \"value\" }";
2886 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2887 JSONTEST_ASSERT(ok);
2888 JSONTEST_ASSERT(errs.empty());
2889 delete reader;
2890 }
2891
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseWithNoErrorsTestingOffsets)2892 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrorsTestingOffsets) {
2893 Json::CharReaderBuilder b;
2894 CharReaderPtr reader(b.newCharReader());
2895 Json::String errs;
2896 Json::Value root;
2897 char const doc[] = "{ \"property\" : [\"value\", \"value2\"], \"obj\" : "
2898 "{ \"nested\" : -6.2e+15, \"num\" : +123, \"bool\" : "
2899 "true}, \"null\" : null, \"false\" : false }";
2900 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2901 JSONTEST_ASSERT(ok);
2902 JSONTEST_ASSERT(errs.empty());
2903 delete reader;
2904 }
2905
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseNumber)2906 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseNumber) {
2907 Json::CharReaderBuilder b;
2908 CharReaderPtr reader(b.newCharReader());
2909 Json::String errs;
2910 Json::Value root;
2911 {
2912 // if intvalue > threshold, treat the number as a double.
2913 // 21 digits
2914 char const doc[] = "[111111111111111111111]";
2915 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2916 JSONTEST_ASSERT(ok);
2917 JSONTEST_ASSERT(errs.empty());
2918 JSONTEST_ASSERT_EQUAL(1.1111111111111111e+020, root[0]);
2919 }
2920 delete reader;
2921 }
2922
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseString)2923 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) {
2924 Json::CharReaderBuilder b;
2925 CharReaderPtr reader(b.newCharReader());
2926 Json::Value root;
2927 Json::String errs;
2928 {
2929 char const doc[] = "[\"\"]";
2930 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2931 JSONTEST_ASSERT(ok);
2932 JSONTEST_ASSERT(errs.empty());
2933 JSONTEST_ASSERT_EQUAL("", root[0]);
2934 }
2935 {
2936 char const doc[] = "[\"\\u8A2a\"]";
2937 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2938 JSONTEST_ASSERT(ok);
2939 JSONTEST_ASSERT(errs.empty());
2940 #if JSONCPP_CXX_STD_11
2941 JSONTEST_ASSERT_EQUAL(u8"\u8A2a", root[0].asString()); // "訪"
2942 #else
2943 JSONTEST_ASSERT_EQUAL("\u8A2a", root[0].asString()); // "訪"
2944 #endif // JSONCPP_CXX_STD_11
2945 }
2946 {
2947 char const doc[] = "[ \"\\uD801\" ]";
2948 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2949 JSONTEST_ASSERT(!ok);
2950 JSONTEST_ASSERT(errs == "* Line 1, Column 3\n"
2951 " additional six characters expected to "
2952 "parse unicode surrogate pair.\n"
2953 "See Line 1, Column 10 for detail.\n");
2954 }
2955 {
2956 char const doc[] = "[ \"\\uD801\\d1234\" ]";
2957 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2958 JSONTEST_ASSERT(!ok);
2959 JSONTEST_ASSERT(errs == "* Line 1, Column 3\n"
2960 " expecting another \\u token to begin the "
2961 "second half of a unicode surrogate pair\n"
2962 "See Line 1, Column 12 for detail.\n");
2963 }
2964 {
2965 char const doc[] = "[ \"\\ua3t@\" ]";
2966 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2967 JSONTEST_ASSERT(!ok);
2968 JSONTEST_ASSERT(errs == "* Line 1, Column 3\n"
2969 " Bad unicode escape sequence in string: "
2970 "hexadecimal digit expected.\n"
2971 "See Line 1, Column 9 for detail.\n");
2972 }
2973 {
2974 char const doc[] = "[ \"\\ua3t\" ]";
2975 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
2976 JSONTEST_ASSERT(!ok);
2977 JSONTEST_ASSERT(
2978 errs ==
2979 "* Line 1, Column 3\n"
2980 " Bad unicode escape sequence in string: four digits expected.\n"
2981 "See Line 1, Column 6 for detail.\n");
2982 }
2983 delete reader;
2984 {
2985 b.settings_["allowSingleQuotes"] = true;
2986 CharReaderPtr charReader(b.newCharReader());
2987 char const doc[] = "{'a': 'x\\ty', \"b\":'x\\\\y'}";
2988 bool ok = charReader->parse(doc, doc + std::strlen(doc), &root, &errs);
2989 JSONTEST_ASSERT(ok);
2990 JSONTEST_ASSERT_STRING_EQUAL("", errs);
2991 JSONTEST_ASSERT_EQUAL(2u, root.size());
2992 JSONTEST_ASSERT_STRING_EQUAL("x\ty", root["a"].asString());
2993 JSONTEST_ASSERT_STRING_EQUAL("x\\y", root["b"].asString());
2994 delete charReader;
2995 }
2996 }
2997
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseComment)2998 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseComment) {
2999 Json::CharReaderBuilder b;
3000 CharReaderPtr reader(b.newCharReader());
3001 Json::Value root;
3002 Json::String errs;
3003 {
3004 char const doc[] = "//comment1\n { //comment2\n \"property\" :"
3005 " \"value\" //comment3\n } //comment4\n";
3006 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3007 JSONTEST_ASSERT(ok);
3008 JSONTEST_ASSERT(errs.empty());
3009 JSONTEST_ASSERT_EQUAL("value", root["property"]);
3010 }
3011 {
3012 char const doc[] = "{ \"property\" //comment\n : \"value\" }";
3013 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3014 JSONTEST_ASSERT(!ok);
3015 JSONTEST_ASSERT(errs == "* Line 1, Column 14\n"
3016 " Missing ':' after object member name\n");
3017 }
3018 {
3019 char const doc[] = "//comment1\n [ //comment2\n \"value\" //comment3\n,"
3020 " //comment4\n true //comment5\n ] //comment6\n";
3021 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3022 JSONTEST_ASSERT(ok);
3023 JSONTEST_ASSERT(errs.empty());
3024 JSONTEST_ASSERT_EQUAL("value", root[0]);
3025 JSONTEST_ASSERT_EQUAL(true, root[1]);
3026 }
3027 delete reader;
3028 }
3029
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseObjectWithErrors)3030 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseObjectWithErrors) {
3031 Json::CharReaderBuilder b;
3032 CharReaderPtr reader(b.newCharReader());
3033 Json::Value root;
3034 Json::String errs;
3035 {
3036 char const doc[] = "{ \"property\" : \"value\" ";
3037 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3038 JSONTEST_ASSERT(!ok);
3039 JSONTEST_ASSERT(errs == "* Line 1, Column 24\n"
3040 " Missing ',' or '}' in object declaration\n");
3041 JSONTEST_ASSERT_EQUAL("value", root["property"]);
3042 }
3043 {
3044 char const doc[] = "{ \"property\" : \"value\" ,";
3045 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3046 JSONTEST_ASSERT(!ok);
3047 JSONTEST_ASSERT(errs == "* Line 1, Column 25\n"
3048 " Missing '}' or object member name\n");
3049 JSONTEST_ASSERT_EQUAL("value", root["property"]);
3050 }
3051 delete reader;
3052 }
3053
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseArrayWithErrors)3054 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseArrayWithErrors) {
3055 Json::CharReaderBuilder b;
3056 CharReaderPtr reader(b.newCharReader());
3057 Json::Value root;
3058 Json::String errs;
3059 {
3060 char const doc[] = "[ \"value\" ";
3061 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3062 JSONTEST_ASSERT(!ok);
3063 JSONTEST_ASSERT(errs == "* Line 1, Column 11\n"
3064 " Missing ',' or ']' in array declaration\n");
3065 JSONTEST_ASSERT_EQUAL("value", root[0]);
3066 }
3067 {
3068 char const doc[] = "[ \"value1\" \"value2\" ]";
3069 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3070 JSONTEST_ASSERT(!ok);
3071 JSONTEST_ASSERT(errs == "* Line 1, Column 12\n"
3072 " Missing ',' or ']' in array declaration\n");
3073 JSONTEST_ASSERT_EQUAL("value1", root[0]);
3074 }
3075 delete reader;
3076 }
3077
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseWithOneError)3078 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithOneError) {
3079 Json::CharReaderBuilder b;
3080 CharReaderPtr reader(b.newCharReader());
3081 Json::String errs;
3082 Json::Value root;
3083 char const doc[] = "{ \"property\" :: \"value\" }";
3084 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3085 JSONTEST_ASSERT(!ok);
3086 JSONTEST_ASSERT(errs ==
3087 "* Line 1, Column 15\n Syntax error: value, object or array "
3088 "expected.\n");
3089 delete reader;
3090 }
3091
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseChineseWithOneError)3092 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseChineseWithOneError) {
3093 Json::CharReaderBuilder b;
3094 CharReaderPtr reader(b.newCharReader());
3095 Json::String errs;
3096 Json::Value root;
3097 char const doc[] = "{ \"pr佐藤erty\" :: \"value\" }";
3098 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3099 JSONTEST_ASSERT(!ok);
3100 JSONTEST_ASSERT(errs ==
3101 "* Line 1, Column 19\n Syntax error: value, object or array "
3102 "expected.\n");
3103 delete reader;
3104 }
3105
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseWithDetailError)3106 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithDetailError) {
3107 Json::CharReaderBuilder b;
3108 CharReaderPtr reader(b.newCharReader());
3109 Json::String errs;
3110 Json::Value root;
3111 char const doc[] = "{ \"property\" : \"v\\alue\" }";
3112 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3113 JSONTEST_ASSERT(!ok);
3114 JSONTEST_ASSERT(errs ==
3115 "* Line 1, Column 16\n Bad escape sequence in string\nSee "
3116 "Line 1, Column 20 for detail.\n");
3117 delete reader;
3118 }
3119
JSONTEST_FIXTURE_LOCAL(CharReaderTest,parseWithStackLimit)3120 JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
3121 Json::CharReaderBuilder b;
3122 Json::Value root;
3123 char const doc[] = "{ \"property\" : \"value\" }";
3124 {
3125 b.settings_["stackLimit"] = 2;
3126 CharReaderPtr reader(b.newCharReader());
3127 Json::String errs;
3128 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3129 JSONTEST_ASSERT(ok);
3130 JSONTEST_ASSERT(errs.empty());
3131 JSONTEST_ASSERT_EQUAL("value", root["property"]);
3132 delete reader;
3133 }
3134 {
3135 b.settings_["stackLimit"] = 1;
3136 CharReaderPtr reader(b.newCharReader());
3137 Json::String errs;
3138 JSONTEST_ASSERT_THROWS(
3139 reader->parse(doc, doc + std::strlen(doc), &root, &errs));
3140 delete reader;
3141 }
3142 }
3143
JSONTEST_FIXTURE_LOCAL(CharReaderTest,testOperator)3144 JSONTEST_FIXTURE_LOCAL(CharReaderTest, testOperator) {
3145 const std::string styled = "{ \"property\" : \"value\" }";
3146 std::istringstream iss(styled);
3147 Json::Value root;
3148 iss >> root;
3149 JSONTEST_ASSERT_EQUAL("value", root["property"]);
3150 }
3151
3152 struct CharReaderStrictModeTest : JsonTest::TestCase {};
3153
JSONTEST_FIXTURE_LOCAL(CharReaderStrictModeTest,dupKeys)3154 JSONTEST_FIXTURE_LOCAL(CharReaderStrictModeTest, dupKeys) {
3155 Json::CharReaderBuilder b;
3156 Json::Value root;
3157 char const doc[] =
3158 "{ \"property\" : \"value\", \"key\" : \"val1\", \"key\" : \"val2\" }";
3159 {
3160 b.strictMode(&b.settings_);
3161 CharReaderPtr reader(b.newCharReader());
3162 Json::String errs;
3163 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3164 JSONTEST_ASSERT(!ok);
3165 JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 41\n"
3166 " Duplicate key: 'key'\n",
3167 errs);
3168 JSONTEST_ASSERT_EQUAL("val1", root["key"]); // so far
3169 delete reader;
3170 }
3171 }
3172 struct CharReaderFailIfExtraTest : JsonTest::TestCase {};
3173
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest,issue164)3174 JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue164) {
3175 // This is interpreted as a string value followed by a colon.
3176 Json::CharReaderBuilder b;
3177 Json::Value root;
3178 char const doc[] = " \"property\" : \"value\" }";
3179 {
3180 b.settings_["failIfExtra"] = false;
3181 CharReaderPtr reader(b.newCharReader());
3182 Json::String errs;
3183 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3184 JSONTEST_ASSERT(ok);
3185 JSONTEST_ASSERT(errs.empty());
3186 JSONTEST_ASSERT_EQUAL("property", root);
3187 delete reader;
3188 }
3189 {
3190 b.settings_["failIfExtra"] = true;
3191 CharReaderPtr reader(b.newCharReader());
3192 Json::String errs;
3193 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3194 JSONTEST_ASSERT(!ok);
3195 JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 13\n"
3196 " Extra non-whitespace after JSON value.\n",
3197 errs);
3198 JSONTEST_ASSERT_EQUAL("property", root);
3199 delete reader;
3200 }
3201 {
3202 b.strictMode(&b.settings_);
3203 CharReaderPtr reader(b.newCharReader());
3204 Json::String errs;
3205 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3206 JSONTEST_ASSERT(!ok);
3207 JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 13\n"
3208 " Extra non-whitespace after JSON value.\n",
3209 errs);
3210 JSONTEST_ASSERT_EQUAL("property", root);
3211 delete reader;
3212 }
3213 {
3214 b.strictMode(&b.settings_);
3215 b.settings_["failIfExtra"] = false;
3216 CharReaderPtr reader(b.newCharReader());
3217 Json::String errs;
3218 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3219 JSONTEST_ASSERT(!ok);
3220 JSONTEST_ASSERT_STRING_EQUAL(
3221 "* Line 1, Column 1\n"
3222 " A valid JSON document must be either an array or an object value.\n",
3223 errs);
3224 JSONTEST_ASSERT_EQUAL("property", root);
3225 delete reader;
3226 }
3227 }
3228
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest,issue107)3229 JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue107) {
3230 // This is interpreted as an int value followed by a colon.
3231 Json::CharReaderBuilder b;
3232 Json::Value root;
3233 char const doc[] = "1:2:3";
3234 b.settings_["failIfExtra"] = true;
3235 CharReaderPtr reader(b.newCharReader());
3236 Json::String errs;
3237 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3238 JSONTEST_ASSERT(!ok);
3239 JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 2\n"
3240 " Extra non-whitespace after JSON value.\n",
3241 errs);
3242 JSONTEST_ASSERT_EQUAL(1, root.asInt());
3243 delete reader;
3244 }
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest,commentAfterObject)3245 JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterObject) {
3246 Json::CharReaderBuilder b;
3247 Json::Value root;
3248 {
3249 char const doc[] = "{ \"property\" : \"value\" } //trailing\n//comment\n";
3250 b.settings_["failIfExtra"] = true;
3251 CharReaderPtr reader(b.newCharReader());
3252 Json::String errs;
3253 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3254 JSONTEST_ASSERT(ok);
3255 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3256 JSONTEST_ASSERT_EQUAL("value", root["property"]);
3257 delete reader;
3258 }
3259 }
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest,commentAfterArray)3260 JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterArray) {
3261 Json::CharReaderBuilder b;
3262 Json::Value root;
3263 char const doc[] = "[ \"property\" , \"value\" ] //trailing\n//comment\n";
3264 b.settings_["failIfExtra"] = true;
3265 CharReaderPtr reader(b.newCharReader());
3266 Json::String errs;
3267 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3268 JSONTEST_ASSERT(ok);
3269 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3270 JSONTEST_ASSERT_EQUAL("value", root[1u]);
3271 delete reader;
3272 }
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest,commentAfterBool)3273 JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterBool) {
3274 Json::CharReaderBuilder b;
3275 Json::Value root;
3276 char const doc[] = " true /*trailing\ncomment*/";
3277 b.settings_["failIfExtra"] = true;
3278 CharReaderPtr reader(b.newCharReader());
3279 Json::String errs;
3280 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3281 JSONTEST_ASSERT(ok);
3282 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3283 JSONTEST_ASSERT_EQUAL(true, root.asBool());
3284 delete reader;
3285 }
3286
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest,parseComment)3287 JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, parseComment) {
3288 Json::CharReaderBuilder b;
3289 b.settings_["failIfExtra"] = true;
3290 CharReaderPtr reader(b.newCharReader());
3291 Json::Value root;
3292 Json::String errs;
3293 {
3294 char const doc[] = " true //comment1\n//comment2\r//comment3\r\n";
3295 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3296 JSONTEST_ASSERT(ok);
3297 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3298 JSONTEST_ASSERT_EQUAL(true, root.asBool());
3299 }
3300 {
3301 char const doc[] = " true //com\rment";
3302 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3303 JSONTEST_ASSERT(!ok);
3304 JSONTEST_ASSERT_STRING_EQUAL("* Line 2, Column 1\n"
3305 " Extra non-whitespace after JSON value.\n",
3306 errs);
3307 JSONTEST_ASSERT_EQUAL(true, root.asBool());
3308 }
3309 {
3310 char const doc[] = " true //com\nment";
3311 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3312 JSONTEST_ASSERT(!ok);
3313 JSONTEST_ASSERT_STRING_EQUAL("* Line 2, Column 1\n"
3314 " Extra non-whitespace after JSON value.\n",
3315 errs);
3316 JSONTEST_ASSERT_EQUAL(true, root.asBool());
3317 }
3318 delete reader;
3319 }
3320 #if JSONCPP_CXX_STD_11
3321 struct CharReaderAllowDropNullTest : JsonTest::TestCase {
3322 typedef Json::Value Value;
3323 typedef std::function<void(const Value&)> ValueCheck;
3324
3325 Value nullValue = Value{Json::nullValue};
3326 Value emptyArray = Value{Json::arrayValue};
3327
checkEqCharReaderAllowDropNullTest3328 ValueCheck checkEq(const Value& v) {
3329 return [=](const Value& root) { JSONTEST_ASSERT_EQUAL(root, v); };
3330 }
3331
objGetAndCharReaderAllowDropNullTest3332 ValueCheck objGetAnd(std::string idx, ValueCheck f) {
3333 return [=](const Value& root) { f(root.get(idx, true)); };
3334 }
3335
arrGetAndCharReaderAllowDropNullTest3336 ValueCheck arrGetAnd(int idx, ValueCheck f) {
3337 return [=](const Value& root) { f(root[idx]); };
3338 }
3339 };
3340
JSONTEST_FIXTURE_LOCAL(CharReaderAllowDropNullTest,issue178)3341 JSONTEST_FIXTURE_LOCAL(CharReaderAllowDropNullTest, issue178) {
3342 struct TestSpec {
3343 int line;
3344 std::string doc;
3345 size_t rootSize;
3346 ValueCheck onRoot;
3347 };
3348 const TestSpec specs[] = {
3349 {__LINE__, "{\"a\":,\"b\":true}", 2, objGetAnd("a", checkEq(nullValue))},
3350 {__LINE__, "{\"a\":,\"b\":true}", 2, objGetAnd("a", checkEq(nullValue))},
3351 {__LINE__, "{\"a\":}", 1, objGetAnd("a", checkEq(nullValue))},
3352 {__LINE__, "[]", 0, checkEq(emptyArray)},
3353 {__LINE__, "[null]", 1, JSONCPP_NULL},
3354 {__LINE__, "[,]", 2, JSONCPP_NULL},
3355 {__LINE__, "[,,,]", 4, JSONCPP_NULL},
3356 {__LINE__, "[null,]", 2, JSONCPP_NULL},
3357 {__LINE__, "[,null]", 2, JSONCPP_NULL},
3358 {__LINE__, "[,,]", 3, JSONCPP_NULL},
3359 {__LINE__, "[null,,]", 3, JSONCPP_NULL},
3360 {__LINE__, "[,null,]", 3, JSONCPP_NULL},
3361 {__LINE__, "[,,null]", 3, JSONCPP_NULL},
3362 {__LINE__, "[[],,,]", 4, arrGetAnd(0, checkEq(emptyArray))},
3363 {__LINE__, "[,[],,]", 4, arrGetAnd(1, checkEq(emptyArray))},
3364 {__LINE__, "[,,,[]]", 4, arrGetAnd(3, checkEq(emptyArray))},
3365 };
3366 for (const auto& spec : specs) {
3367 Json::CharReaderBuilder b;
3368 b.settings_["allowDroppedNullPlaceholders"] = true;
3369 std::unique_ptr<Json::CharReader> reader(b.newCharReader());
3370
3371 Json::Value root;
3372 Json::String errs;
3373 bool ok = reader->parse(spec.doc.data(), spec.doc.data() + spec.doc.size(),
3374 &root, &errs);
3375 JSONTEST_ASSERT(ok);
3376 JSONTEST_ASSERT_STRING_EQUAL(errs, "");
3377 if (spec.onRoot) {
3378 spec.onRoot(root);
3379 }
3380 }
3381 }
3382 #endif // JSONCPP_CXX_STD_11
3383 struct CharReaderAllowNumericKeysTest : JsonTest::TestCase {};
3384
JSONTEST_FIXTURE_LOCAL(CharReaderAllowNumericKeysTest,allowNumericKeys)3385 JSONTEST_FIXTURE_LOCAL(CharReaderAllowNumericKeysTest, allowNumericKeys) {
3386 Json::CharReaderBuilder b;
3387 b.settings_["allowNumericKeys"] = true;
3388 Json::Value root;
3389 Json::String errs;
3390 CharReaderPtr reader(b.newCharReader());
3391 char const doc[] = "{15:true,-16:true,12.01:true}";
3392 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3393 JSONTEST_ASSERT(ok);
3394 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3395 JSONTEST_ASSERT_EQUAL(3u, root.size());
3396 JSONTEST_ASSERT_EQUAL(true, root.get("15", false));
3397 JSONTEST_ASSERT_EQUAL(true, root.get("-16", false));
3398 JSONTEST_ASSERT_EQUAL(true, root.get("12.01", false));
3399 delete reader;
3400 }
3401
3402 struct CharReaderAllowSingleQuotesTest : JsonTest::TestCase {};
3403
JSONTEST_FIXTURE_LOCAL(CharReaderAllowSingleQuotesTest,issue182)3404 JSONTEST_FIXTURE_LOCAL(CharReaderAllowSingleQuotesTest, issue182) {
3405 Json::CharReaderBuilder b;
3406 b.settings_["allowSingleQuotes"] = true;
3407 Json::Value root;
3408 Json::String errs;
3409 CharReaderPtr reader(b.newCharReader());
3410 {
3411 char const doc[] = "{'a':true,\"b\":true}";
3412 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3413 JSONTEST_ASSERT(ok);
3414 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3415 JSONTEST_ASSERT_EQUAL(2u, root.size());
3416 JSONTEST_ASSERT_EQUAL(true, root.get("a", false));
3417 JSONTEST_ASSERT_EQUAL(true, root.get("b", false));
3418 }
3419 {
3420 char const doc[] = "{'a': 'x', \"b\":'y'}";
3421 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3422 JSONTEST_ASSERT(ok);
3423 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3424 JSONTEST_ASSERT_EQUAL(2u, root.size());
3425 JSONTEST_ASSERT_STRING_EQUAL("x", root["a"].asString());
3426 JSONTEST_ASSERT_STRING_EQUAL("y", root["b"].asString());
3427 }
3428 delete reader;
3429 }
3430
3431 struct CharReaderAllowZeroesTest : JsonTest::TestCase {};
3432
JSONTEST_FIXTURE_LOCAL(CharReaderAllowZeroesTest,issue176)3433 JSONTEST_FIXTURE_LOCAL(CharReaderAllowZeroesTest, issue176) {
3434 Json::CharReaderBuilder b;
3435 b.settings_["allowSingleQuotes"] = true;
3436 Json::Value root;
3437 Json::String errs;
3438 CharReaderPtr reader(b.newCharReader());
3439 {
3440 char const doc[] = "{'a':true,\"b\":true}";
3441 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3442 JSONTEST_ASSERT(ok);
3443 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3444 JSONTEST_ASSERT_EQUAL(2u, root.size());
3445 JSONTEST_ASSERT_EQUAL(true, root.get("a", false));
3446 JSONTEST_ASSERT_EQUAL(true, root.get("b", false));
3447 }
3448 {
3449 char const doc[] = "{'a': 'x', \"b\":'y'}";
3450 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3451 JSONTEST_ASSERT(ok);
3452 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3453 JSONTEST_ASSERT_EQUAL(2u, root.size());
3454 JSONTEST_ASSERT_STRING_EQUAL("x", root["a"].asString());
3455 JSONTEST_ASSERT_STRING_EQUAL("y", root["b"].asString());
3456 }
3457 delete reader;
3458 }
3459
3460 struct CharReaderAllowSpecialFloatsTest : JsonTest::TestCase {};
3461
JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest,specialFloat)3462 JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, specialFloat) {
3463 Json::CharReaderBuilder b;
3464 CharReaderPtr reader(b.newCharReader());
3465 Json::Value root;
3466 Json::String errs;
3467 {
3468 char const doc[] = "{\"a\": NaN}";
3469 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3470 JSONTEST_ASSERT(!ok);
3471 JSONTEST_ASSERT_STRING_EQUAL(
3472 "* Line 1, Column 7\n"
3473 " Syntax error: value, object or array expected.\n",
3474 errs);
3475 }
3476 {
3477 char const doc[] = "{\"a\": Infinity}";
3478 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3479 JSONTEST_ASSERT(!ok);
3480 JSONTEST_ASSERT_STRING_EQUAL(
3481 "* Line 1, Column 7\n"
3482 " Syntax error: value, object or array expected.\n",
3483 errs);
3484 }
3485 delete reader;
3486 }
3487
JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest,issue209)3488 JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, issue209) {
3489 Json::CharReaderBuilder b;
3490 b.settings_["allowSpecialFloats"] = true;
3491 Json::Value root;
3492 Json::String errs;
3493 CharReaderPtr reader(b.newCharReader());
3494 {
3495 char const doc[] =
3496 "{\"a\":NaN,\"b\":Infinity,\"c\":-Infinity,\"d\":+Infinity}";
3497 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3498 JSONTEST_ASSERT(ok);
3499 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3500 JSONTEST_ASSERT_EQUAL(4u, root.size());
3501 double n = root["a"].asDouble();
3502 JSONTEST_ASSERT(std::isnan(n));
3503 JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(),
3504 root.get("b", 0.0));
3505 JSONTEST_ASSERT_EQUAL(-std::numeric_limits<double>::infinity(),
3506 root.get("c", 0.0));
3507 JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(),
3508 root.get("d", 0.0));
3509 }
3510
3511 struct TestData {
3512 int line;
3513 bool ok;
3514 Json::String in;
3515 };
3516 const TestData test_data[] = {
3517 {__LINE__, true, "{\"a\":9}"}, //
3518 {__LINE__, false, "{\"a\":0Infinity}"}, //
3519 {__LINE__, false, "{\"a\":1Infinity}"}, //
3520 {__LINE__, false, "{\"a\":9Infinity}"}, //
3521 {__LINE__, false, "{\"a\":0nfinity}"}, //
3522 {__LINE__, false, "{\"a\":1nfinity}"}, //
3523 {__LINE__, false, "{\"a\":9nfinity}"}, //
3524 {__LINE__, false, "{\"a\":nfinity}"}, //
3525 {__LINE__, false, "{\"a\":.nfinity}"}, //
3526 {__LINE__, false, "{\"a\":9nfinity}"}, //
3527 {__LINE__, false, "{\"a\":-nfinity}"}, //
3528 {__LINE__, true, "{\"a\":Infinity}"}, //
3529 {__LINE__, false, "{\"a\":.Infinity}"}, //
3530 {__LINE__, false, "{\"a\":_Infinity}"}, //
3531 {__LINE__, false, "{\"a\":_nfinity}"}, //
3532 {__LINE__, true, "{\"a\":-Infinity}"}, //
3533 {__LINE__, true, "{\"a\":+Infinity}"} //
3534 };
3535 for (unsigned int index = 0; index < sizeof(test_data) / sizeof(test_data[0]);
3536 ++index) {
3537 const struct TestData td = test_data[index];
3538 bool ok = reader->parse(&*td.in.begin(), &*td.in.begin() + td.in.size(),
3539 &root, &errs);
3540 JSONTEST_ASSERT(td.ok == ok) << "line:" << td.line << "\n"
3541 << " expected: {"
3542 << "ok:" << td.ok << ", in:\'" << td.in << "\'"
3543 << "}\n"
3544 << " actual: {"
3545 << "ok:" << ok << "}\n";
3546 }
3547
3548 {
3549 char const doc[] = "{\"posInf\": +Infinity, \"NegInf\": -Infinity}";
3550 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3551 JSONTEST_ASSERT(ok);
3552 JSONTEST_ASSERT_STRING_EQUAL("", errs);
3553 JSONTEST_ASSERT_EQUAL(2u, root.size());
3554 JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(),
3555 root["posInf"].asDouble());
3556 JSONTEST_ASSERT_EQUAL(-std::numeric_limits<double>::infinity(),
3557 root["NegInf"].asDouble());
3558 }
3559 delete reader;
3560 }
3561
3562 struct EscapeSequenceTest : JsonTest::TestCase {};
3563
JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest,readerParseEscapeSequence)3564 JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, readerParseEscapeSequence) {
3565 Json::Reader reader;
3566 Json::Value root;
3567 bool ok = reader.parse("[\"\\\"\",\"\\/\",\"\\\\\",\"\\b\","
3568 "\"\\f\",\"\\n\",\"\\r\",\"\\t\","
3569 "\"\\u0278\",\"\\ud852\\udf62\"]\n",
3570 root);
3571 JSONTEST_ASSERT(ok);
3572 JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
3573 JSONTEST_ASSERT(reader.getStructuredErrors().empty());
3574 }
3575
JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest,charReaderParseEscapeSequence)3576 JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, charReaderParseEscapeSequence) {
3577 Json::CharReaderBuilder b;
3578 CharReaderPtr reader(b.newCharReader());
3579 Json::Value root;
3580 Json::String errs;
3581 char const doc[] = "[\"\\\"\",\"\\/\",\"\\\\\",\"\\b\","
3582 "\"\\f\",\"\\n\",\"\\r\",\"\\t\","
3583 "\"\\u0278\",\"\\ud852\\udf62\"]";
3584 bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3585 JSONTEST_ASSERT(ok);
3586 JSONTEST_ASSERT(errs.empty());
3587 delete reader;
3588 }
3589
JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest,writeEscapeSequence)3590 JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, writeEscapeSequence) {
3591 Json::FastWriter writer;
3592 const Json::String expected("[\"\\\"\",\"\\\\\",\"\\b\","
3593 "\"\\f\",\"\\n\",\"\\r\",\"\\t\","
3594 "\"\\u0278\",\"\\ud852\\udf62\"]\n");
3595 Json::Value root;
3596 root[0] = "\"";
3597 root[1] = "\\";
3598 root[2] = "\b";
3599 root[3] = "\f";
3600 root[4] = "\n";
3601 root[5] = "\r";
3602 root[6] = "\t";
3603 root[7] = "ɸ";
3604 root[8] = "";
3605 const Json::String result = writer.write(root);
3606 JSONTEST_ASSERT_STRING_EQUAL(expected, result);
3607 }
3608
3609 struct BuilderTest : JsonTest::TestCase {};
3610
JSONTEST_FIXTURE_LOCAL(BuilderTest,settings)3611 JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
3612 {
3613 Json::Value errs;
3614 Json::CharReaderBuilder rb;
3615 JSONTEST_ASSERT_EQUAL(false, rb.settings_.isMember("foo"));
3616 JSONTEST_ASSERT_EQUAL(true, rb.validate(&errs));
3617 rb["foo"] = "bar";
3618 JSONTEST_ASSERT_EQUAL(true, rb.settings_.isMember("foo"));
3619 JSONTEST_ASSERT_EQUAL(false, rb.validate(&errs));
3620 }
3621 {
3622 Json::Value errs;
3623 Json::StreamWriterBuilder wb;
3624 JSONTEST_ASSERT_EQUAL(false, wb.settings_.isMember("foo"));
3625 JSONTEST_ASSERT_EQUAL(true, wb.validate(&errs));
3626 wb["foo"] = "bar";
3627 JSONTEST_ASSERT_EQUAL(true, wb.settings_.isMember("foo"));
3628 JSONTEST_ASSERT_EQUAL(false, wb.validate(&errs));
3629 }
3630 }
3631
3632 struct BomTest : JsonTest::TestCase {};
3633
JSONTEST_FIXTURE_LOCAL(BomTest,skipBom)3634 JSONTEST_FIXTURE_LOCAL(BomTest, skipBom) {
3635 const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}";
3636 Json::Value root;
3637 JSONCPP_STRING errs;
3638 std::istringstream iss(with_bom);
3639 bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs);
3640 // The default behavior is to skip the BOM, so we can parse it normally.
3641 JSONTEST_ASSERT(ok);
3642 JSONTEST_ASSERT(errs.empty());
3643 JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value");
3644 }
JSONTEST_FIXTURE_LOCAL(BomTest,notSkipBom)3645 JSONTEST_FIXTURE_LOCAL(BomTest, notSkipBom) {
3646 const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}";
3647 Json::Value root;
3648 JSONCPP_STRING errs;
3649 std::istringstream iss(with_bom);
3650 Json::CharReaderBuilder b;
3651 b.settings_["skipBom"] = false;
3652 bool ok = parseFromStream(b, iss, &root, &errs);
3653 // Detect the BOM, and failed on it.
3654 JSONTEST_ASSERT(!ok);
3655 JSONTEST_ASSERT(!errs.empty());
3656 }
3657
3658 struct IteratorTest : JsonTest::TestCase {};
3659
JSONTEST_FIXTURE_LOCAL(IteratorTest,convert)3660 JSONTEST_FIXTURE_LOCAL(IteratorTest, convert) {
3661 Json::Value j;
3662 const Json::Value& cj = j;
3663 Json::Value::const_iterator it = j.begin();
3664 Json::Value::const_iterator cit;
3665 cit = it;
3666 JSONTEST_ASSERT(cit == cj.begin());
3667 }
3668
JSONTEST_FIXTURE_LOCAL(IteratorTest,decrement)3669 JSONTEST_FIXTURE_LOCAL(IteratorTest, decrement) {
3670 Json::Value json;
3671 json["k1"] = "a";
3672 json["k2"] = "b";
3673 std::vector<std::string> values;
3674 std::vector<std::string> expected;
3675 expected.push_back("b");
3676 expected.push_back("a");
3677 for (Json::Value::const_iterator it = json.end(); it != json.begin();) {
3678 --it;
3679 values.push_back(it->asString());
3680 }
3681 JSONTEST_ASSERT(values.size() == expected.size());
3682 for (unsigned int i = 0; i < expected.size(); i++) {
3683 JSONTEST_ASSERT(values.at(i) == expected.at(i));
3684 }
3685 }
3686
JSONTEST_FIXTURE_LOCAL(IteratorTest,reverseIterator)3687 JSONTEST_FIXTURE_LOCAL(IteratorTest, reverseIterator) {
3688 Json::Value json;
3689 json["k1"] = "a";
3690 json["k2"] = "b";
3691 std::vector<std::string> values;
3692 typedef Json::Value::const_iterator Iter;
3693 std::reverse_iterator<Iter> re = std::reverse_iterator<Iter>(json.begin());
3694 for (std::reverse_iterator<Iter> it = std::reverse_iterator<Iter>(json.end());
3695 it != re; ++it) {
3696 values.push_back(it->asString());
3697 }
3698 std::vector<std::string> expected;
3699 expected.push_back("b");
3700 expected.push_back("a");
3701 JSONTEST_ASSERT(values.size() == expected.size());
3702 for (unsigned int i = 0; i < expected.size(); i++) {
3703 JSONTEST_ASSERT(values.at(i) == expected.at(i));
3704 }
3705 }
3706
JSONTEST_FIXTURE_LOCAL(IteratorTest,distance)3707 JSONTEST_FIXTURE_LOCAL(IteratorTest, distance) {
3708 {
3709 Json::Value json;
3710 json["k1"] = "a";
3711 json["k2"] = "b";
3712 int i = 0;
3713 Json::Value::const_iterator it = json.begin();
3714 for (;; ++it, ++i) {
3715 Json::ValueIteratorBase::difference_type dist = it - json.begin();
3716 JSONTEST_ASSERT_EQUAL(i, dist);
3717 if (it == json.end())
3718 break;
3719 }
3720 }
3721 {
3722 Json::Value empty;
3723 JSONTEST_ASSERT_EQUAL(0, empty.end() - empty.end());
3724 JSONTEST_ASSERT_EQUAL(0, empty.end() - empty.begin());
3725 }
3726 }
3727
JSONTEST_FIXTURE_LOCAL(IteratorTest,nullValues)3728 JSONTEST_FIXTURE_LOCAL(IteratorTest, nullValues) {
3729 {
3730 Json::Value json;
3731 Json::Value::const_iterator end = json.end();
3732 Json::Value::const_iterator endCopy = end;
3733 JSONTEST_ASSERT(endCopy == end);
3734 endCopy = end;
3735 JSONTEST_ASSERT(endCopy == end);
3736 }
3737 {
3738 // Same test, now with const Value.
3739 const Json::Value json;
3740 Json::Value::const_iterator end = json.end();
3741 Json::Value::const_iterator endCopy = end;
3742 JSONTEST_ASSERT(endCopy == end);
3743 endCopy = end;
3744 JSONTEST_ASSERT(endCopy == end);
3745 }
3746 }
3747
JSONTEST_FIXTURE_LOCAL(IteratorTest,staticStringKey)3748 JSONTEST_FIXTURE_LOCAL(IteratorTest, staticStringKey) {
3749 Json::Value json;
3750 json[Json::StaticString("k1")] = "a";
3751 JSONTEST_ASSERT_EQUAL(Json::Value("k1"), json.begin().key());
3752 }
3753
JSONTEST_FIXTURE_LOCAL(IteratorTest,names)3754 JSONTEST_FIXTURE_LOCAL(IteratorTest, names) {
3755 Json::Value json;
3756 json["k1"] = "a";
3757 json["k2"] = "b";
3758 Json::ValueIterator it = json.begin();
3759 JSONTEST_ASSERT(it != json.end());
3760 JSONTEST_ASSERT_EQUAL(Json::Value("k1"), it.key());
3761 JSONTEST_ASSERT_STRING_EQUAL("k1", it.name());
3762 JSONTEST_ASSERT_STRING_EQUAL("k1", it.memberName());
3763 JSONTEST_ASSERT_EQUAL(-1, it.index());
3764 ++it;
3765 JSONTEST_ASSERT(it != json.end());
3766 JSONTEST_ASSERT_EQUAL(Json::Value("k2"), it.key());
3767 JSONTEST_ASSERT_STRING_EQUAL("k2", it.name());
3768 JSONTEST_ASSERT_STRING_EQUAL("k2", it.memberName());
3769 JSONTEST_ASSERT_EQUAL(-1, it.index());
3770 ++it;
3771 JSONTEST_ASSERT(it == json.end());
3772 }
3773
JSONTEST_FIXTURE_LOCAL(IteratorTest,indexes)3774 JSONTEST_FIXTURE_LOCAL(IteratorTest, indexes) {
3775 Json::Value json;
3776 json[0] = "a";
3777 json[1] = "b";
3778 Json::ValueIterator it = json.begin();
3779 JSONTEST_ASSERT(it != json.end());
3780 JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(0)), it.key());
3781 JSONTEST_ASSERT_STRING_EQUAL("", it.name());
3782 JSONTEST_ASSERT_EQUAL(0, it.index());
3783 ++it;
3784 JSONTEST_ASSERT(it != json.end());
3785 JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(1)), it.key());
3786 JSONTEST_ASSERT_STRING_EQUAL("", it.name());
3787 JSONTEST_ASSERT_EQUAL(1, it.index());
3788 ++it;
3789 JSONTEST_ASSERT(it == json.end());
3790 }
3791
JSONTEST_FIXTURE_LOCAL(IteratorTest,constness)3792 JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) {
3793 Json::Value const v;
3794 JSONTEST_ASSERT_THROWS(
3795 Json::Value::iterator it(v.begin())); // Compile, but throw.
3796
3797 Json::Value value;
3798
3799 for (int i = 9; i < 12; ++i) {
3800 Json::OStringStream out;
3801 out << std::setw(2) << i;
3802 Json::String str = out.str();
3803 value[str] = str;
3804 }
3805
3806 Json::OStringStream out;
3807 // in old code, this will get a compile error
3808 Json::Value::const_iterator iter = value.begin();
3809 for (; iter != value.end(); ++iter) {
3810 out << *iter << ',';
3811 }
3812 Json::String expected = "\" 9\",\"10\",\"11\",";
3813 JSONTEST_ASSERT_STRING_EQUAL(expected, out.str());
3814 }
3815 #if JSONCPP_CXX_STD_11
3816 struct RValueTest : JsonTest::TestCase {};
3817
JSONTEST_FIXTURE_LOCAL(RValueTest,moveConstruction)3818 JSONTEST_FIXTURE_LOCAL(RValueTest, moveConstruction) {
3819 Json::Value json;
3820 json["key"] = "value";
3821 Json::Value moved = std::move(json);
3822 JSONTEST_ASSERT(moved != json); // Possibly not nullValue; definitely not
3823 // equal.
3824 JSONTEST_ASSERT_EQUAL(Json::objectValue, moved.type());
3825 JSONTEST_ASSERT_EQUAL(Json::stringValue, moved["key"].type());
3826 }
3827 #endif // JSONCPP_CXX_STD_11
3828 struct FuzzTest : JsonTest::TestCase {};
3829
3830 // Build and run the fuzz test without any fuzzer, so that it's guaranteed not
3831 // go out of date, even if it's never run as an actual fuzz test.
JSONTEST_FIXTURE_LOCAL(FuzzTest,fuzzDoesntCrash)3832 JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) {
3833 const std::string example = "{}";
3834 JSONTEST_ASSERT_EQUAL(
3835 0,
3836 LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(example.c_str()),
3837 example.size()));
3838 }
3839
main(int argc,const char * argv[])3840 int main(int argc, const char* argv[]) {
3841 JsonTest::Runner runner;
3842
3843 for (unsigned int index = 0; index < local_.size(); ++index) {
3844 JsonTest::TestCaseFactory local = local_[index];
3845 runner.add(local);
3846 }
3847
3848 return runner.runCommandLine(argc, argv);
3849 }
3850 #if JSONCPP_CXX_STD_11
3851 struct MemberTemplateAs : JsonTest::TestCase {
3852 template <typename T, typename F>
EqEvalMemberTemplateAs3853 JsonTest::TestResult& EqEval(T v, F f) const {
3854 const Json::Value j = v;
3855 return JSONTEST_ASSERT_EQUAL(j.as<T>(), f(j));
3856 }
3857 };
3858
JSONTEST_FIXTURE_LOCAL(MemberTemplateAs,BehavesSameAsNamedAs)3859 JSONTEST_FIXTURE_LOCAL(MemberTemplateAs, BehavesSameAsNamedAs) {
3860 const Json::Value jstr = "hello world";
3861 JSONTEST_ASSERT_STRING_EQUAL(jstr.as<const char*>(), jstr.asCString());
3862 JSONTEST_ASSERT_STRING_EQUAL(jstr.as<Json::String>(), jstr.asString());
3863 EqEval(Json::Int(64), [](const Json::Value& j) { return j.asInt(); });
3864 EqEval(Json::UInt(64), [](const Json::Value& j) { return j.asUInt(); });
3865 #if defined(JSON_HAS_INT64)
3866 EqEval(Json::Int64(64), [](const Json::Value& j) { return j.asInt64(); });
3867 EqEval(Json::UInt64(64), [](const Json::Value& j) { return j.asUInt64(); });
3868 #endif // if defined(JSON_HAS_INT64)
3869 EqEval(Json::LargestInt(64),
3870 [](const Json::Value& j) { return j.asLargestInt(); });
3871 EqEval(Json::LargestUInt(64),
3872 [](const Json::Value& j) { return j.asLargestUInt(); });
3873
3874 EqEval(69.69f, [](const Json::Value& j) { return j.asFloat(); });
3875 EqEval(69.69, [](const Json::Value& j) { return j.asDouble(); });
3876 EqEval(false, [](const Json::Value& j) { return j.asBool(); });
3877 EqEval(true, [](const Json::Value& j) { return j.asBool(); });
3878 }
3879 #endif // JSONCPP_CXX_STD_11
3880 class MemberTemplateIs : public JsonTest::TestCase {};
3881
JSONTEST_FIXTURE_LOCAL(MemberTemplateIs,BehavesSameAsNamedIs)3882 JSONTEST_FIXTURE_LOCAL(MemberTemplateIs, BehavesSameAsNamedIs) {
3883 const Json::Value values[] = {true, 142, 40.63, "hello world"};
3884 for (size_t index = 0; index < sizeof(values) / sizeof(values[0]); index++) {
3885 const Json::Value& j = values[index];
3886 JSONTEST_ASSERT_EQUAL(j.is<bool>(), j.isBool());
3887 JSONTEST_ASSERT_EQUAL(j.is<Json::Int>(), j.isInt());
3888 JSONTEST_ASSERT_EQUAL(j.is<Json::Int64>(), j.isInt64());
3889 JSONTEST_ASSERT_EQUAL(j.is<Json::UInt>(), j.isUInt());
3890 JSONTEST_ASSERT_EQUAL(j.is<Json::UInt64>(), j.isUInt64());
3891 JSONTEST_ASSERT_EQUAL(j.is<double>(), j.isDouble());
3892 JSONTEST_ASSERT_EQUAL(j.is<Json::String>(), j.isString());
3893 }
3894 }
3895
3896 #if defined(__GNUC__)
3897 #pragma GCC diagnostic pop
3898 #endif
3899