26 RAPIDJSON_DIAG_OFF(c++98-compat)
27 RAPIDJSON_DIAG_OFF(missing-variable-declarations)
32 template <
typename DocumentType>
34 typedef typename DocumentType::ValueType
ValueType;
36 EXPECT_FALSE(doc.HasParseError());
37 if (doc.HasParseError())
38 printf(
"Error: %d at %zu\n", static_cast<int>(doc.GetParseError()), doc.GetErrorOffset());
39 EXPECT_TRUE(static_cast<ParseResult>(doc));
41 EXPECT_TRUE(doc.IsObject());
43 EXPECT_TRUE(doc.HasMember(
"hello"));
44 const ValueType& hello = doc[
"hello"];
45 EXPECT_TRUE(hello.IsString());
46 EXPECT_STREQ(
"world", hello.GetString());
48 EXPECT_TRUE(doc.HasMember(
"t"));
49 const ValueType& t = doc[
"t"];
50 EXPECT_TRUE(t.IsTrue());
52 EXPECT_TRUE(doc.HasMember(
"f"));
53 const ValueType&
f = doc[
"f"];
54 EXPECT_TRUE(f.IsFalse());
56 EXPECT_TRUE(doc.HasMember(
"n"));
57 const ValueType& n = doc[
"n"];
58 EXPECT_TRUE(n.IsNull());
60 EXPECT_TRUE(doc.HasMember(
"i"));
61 const ValueType& i = doc[
"i"];
62 EXPECT_TRUE(i.IsNumber());
63 EXPECT_EQ(123, i.GetInt());
65 EXPECT_TRUE(doc.HasMember(
"pi"));
66 const ValueType& pi = doc[
"pi"];
67 EXPECT_TRUE(pi.IsNumber());
68 EXPECT_DOUBLE_EQ(3.1416, pi.GetDouble());
70 EXPECT_TRUE(doc.HasMember(
"a"));
71 const ValueType&
a = doc[
"a"];
72 EXPECT_TRUE(a.IsArray());
73 EXPECT_EQ(4u, a.Size());
75 EXPECT_EQ(j + 1, a[j].GetUint());
78 template <
typename Allocator,
typename StackAllocator>
83 const char*
json =
" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
90 doc.template ParseStream<0>(s);
94 char *buffer = strdup(json);
95 doc.ParseInsitu(buffer);
100 size_t length = strlen(json);
101 buffer =
reinterpret_cast<char*
>(malloc(length * 2));
102 memcpy(buffer, json, length);
103 memset(buffer + length,
'X', length);
104 #if RAPIDJSON_HAS_STDSTRING 105 std::string s2(buffer, length);
108 doc.Parse(buffer, length);
112 #if RAPIDJSON_HAS_STDSTRING 123 ParseTest<CrtAllocator, MemoryPoolAllocator<> >();
124 ParseTest<CrtAllocator, CrtAllocator>();
132 EXPECT_TRUE(noError);
136 EXPECT_NE(err, noError);
137 EXPECT_NE(err.
Code(), noError);
141 EXPECT_TRUE(doc.IsArray());
142 EXPECT_EQ(doc.Size(), 1u);
144 err = doc.
Parse(
"{}");
146 EXPECT_FALSE(err.IsError());
148 EXPECT_EQ(err, noError);
149 EXPECT_EQ(err.Code(), noError);
152 EXPECT_TRUE(doc.IsObject());
153 EXPECT_EQ(doc.MemberCount(), 0u);
157 const char *paths[] = {
161 "../../bin/encodings",
162 "../../../bin/encodings" 165 for (
size_t i = 0; i <
sizeof(paths) /
sizeof(paths[0]); i++) {
166 sprintf(buffer,
"%s/%s", paths[i], filename);
167 FILE *fp = fopen(buffer,
"rb");
175 const char*
json =
" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
186 size_t length = strlen(json);
187 char* buffer =
reinterpret_cast<char*
>(malloc(length * 2));
188 memcpy(buffer, json, length);
189 memset(buffer + length,
'X', length);
190 #if RAPIDJSON_HAS_STDSTRING 191 std::string s2(buffer, length);
196 EXPECT_FALSE(doc.HasParseError());
197 if (doc.HasParseError())
198 printf(
"Error: %d at %zu\n", static_cast<int>(doc.GetParseError()), doc.GetErrorOffset());
199 EXPECT_EQ(0,
StrCmp(doc[L
"hello"].GetString(), L
"world"));
201 #if RAPIDJSON_HAS_STDSTRING 205 #if defined(_MSC_VER) && _MSC_VER < 1800 210 EXPECT_FALSE(doc.HasParseError());
211 EXPECT_EQ(0,
StrCmp(doc[L
"hello"].GetString(), L
"world"));
228 wchar_t expected[] = L
"I can eat glass and it doesn't hurt me.";
230 EXPECT_TRUE(v.IsString());
231 EXPECT_EQ(
sizeof(expected) /
sizeof(
wchar_t) - 1, v.GetStringLength());
232 EXPECT_EQ(0,
StrCmp(expected, v.GetString()));
237 OutputStream eos(bos,
false);
249 reader.
Parse(is, writer2);
269 char expected[] =
"I can eat glass and it doesn't hurt me.";
271 EXPECT_TRUE(v.IsString());
272 EXPECT_EQ(
sizeof(expected) - 1, v.GetStringLength());
273 EXPECT_EQ(0,
StrCmp(expected, v.GetString()));
286 reader.
Parse(is, writer2);
297 d1.SetArray().PushBack(1, a).PushBack(2, a);
300 o.SetObject().AddMember(
"a", 1, a);
304 EXPECT_TRUE(d1.IsObject());
305 EXPECT_TRUE(o.IsArray());
308 EXPECT_TRUE(d1.IsArray());
309 EXPECT_TRUE(o.IsObject());
312 EXPECT_TRUE(d1.IsObject());
313 EXPECT_TRUE(o.IsArray());
317 d2.SetArray().PushBack(3, a);
319 EXPECT_TRUE(d1.IsArray());
320 EXPECT_TRUE(d2.IsObject());
321 EXPECT_EQ(&d2.GetAllocator(), &
a);
325 EXPECT_TRUE(d1.IsNull());
329 EXPECT_TRUE(d2.IsNull());
330 EXPECT_NE(&d2.GetAllocator(), &
a);
336 EXPECT_TRUE(d1.IsNull());
337 EXPECT_TRUE(d2.IsTrue());
340 EXPECT_TRUE(o.IsTrue());
341 EXPECT_TRUE(d2.IsArray());
361 doc.
Parse(
" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
367 EXPECT_EQ(
"{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str());
372 char valueBuffer[4096];
373 char parseBuffer[1024];
374 MemoryPoolAllocator<> valueAllocator(valueBuffer,
sizeof(valueBuffer));
375 MemoryPoolAllocator<> parseAllocator(parseBuffer,
sizeof(parseBuffer));
376 DocumentType doc(&valueAllocator,
sizeof(parseBuffer) / 2, &parseAllocator);
377 doc.Parse(
" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
378 EXPECT_FALSE(doc.HasParseError());
379 EXPECT_LE(valueAllocator.
Size(),
sizeof(valueBuffer));
380 EXPECT_LE(parseAllocator.
Size(),
sizeof(parseBuffer));
383 EXPECT_LE(valueAllocator.
Size(), valueAllocator.
Capacity());
384 EXPECT_LE(parseAllocator.
Size(), parseAllocator.
Capacity());
392 doc.FindMember(
"a")->name.SetNull();
404 ASSERT_TRUE(json.IsArray());
406 ASSERT_TRUE(v.IsObject());
409 ASSERT_TRUE(s.IsString());
411 EXPECT_EQ(0, memcmp(L
"Wed Oct 30 17:13:20 +0000 2012", s.GetString(), (s.GetStringLength() + 1) *
sizeof(
wchar_t)));
414 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 416 #if 0 // Many old compiler does not support these. Turn it off temporaily. 418 #include <type_traits> 457 template <
typename Allocator>
458 struct DocumentMove:
public ::testing::Test {
461 typedef ::testing::Types< CrtAllocator, MemoryPoolAllocator<> > MoveAllocatorTypes;
462 TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes);
464 TYPED_TEST(DocumentMove, MoveConstructor) {
470 a.Parse(
"[\"one\", \"two\", \"three\"]");
471 EXPECT_FALSE(a.HasParseError());
472 EXPECT_TRUE(a.IsArray());
473 EXPECT_EQ(3u, a.Size());
474 EXPECT_EQ(&a.GetAllocator(), &allocator);
478 EXPECT_TRUE(a.IsNull());
479 EXPECT_TRUE(b.IsArray());
480 EXPECT_EQ(3u, b.Size());
482 EXPECT_EQ(&b.GetAllocator(), &allocator);
484 b.Parse(
"{\"Foo\": \"Bar\", \"Baz\": 42}");
485 EXPECT_FALSE(b.HasParseError());
486 EXPECT_TRUE(b.IsObject());
487 EXPECT_EQ(2u, b.MemberCount());
491 EXPECT_TRUE(b.IsNull());
492 EXPECT_TRUE(c.IsObject());
493 EXPECT_EQ(2u, c.MemberCount());
495 EXPECT_EQ(&c.GetAllocator(), &allocator);
498 TYPED_TEST(DocumentMove, MoveConstructorParseError) {
505 ParseResult error(a.GetParseError(), a.GetErrorOffset());
506 EXPECT_TRUE(a.HasParseError());
507 EXPECT_NE(error, noError);
508 EXPECT_NE(error.Code(), noError);
509 EXPECT_NE(error.Code(), noError.
Code());
510 EXPECT_NE(error.Offset(), noError.
Offset());
513 EXPECT_FALSE(a.HasParseError());
514 EXPECT_TRUE(b.HasParseError());
515 EXPECT_EQ(a.GetParseError(), noError);
516 EXPECT_EQ(a.GetParseError(), noError.
Code());
517 EXPECT_EQ(a.GetErrorOffset(), noError.
Offset());
518 EXPECT_EQ(b.GetParseError(), error);
519 EXPECT_EQ(b.GetParseError(), error.Code());
520 EXPECT_EQ(b.GetErrorOffset(), error.Offset());
523 EXPECT_FALSE(b.HasParseError());
524 EXPECT_TRUE(c.HasParseError());
525 EXPECT_EQ(b.GetParseError(), noError.
Code());
526 EXPECT_EQ(c.GetParseError(), error.Code());
527 EXPECT_EQ(b.GetErrorOffset(), noError.
Offset());
528 EXPECT_EQ(c.GetErrorOffset(), error.Offset());
535 TYPED_TEST(DocumentMove, MoveConstructorStack) {
547 reader.template Parse<kParseDefaultFlags>(is,
a);
549 EXPECT_GT(capacity, 0u);
551 Document b(std::move(a));
555 Document c = std::move(b);
557 EXPECT_EQ(c.GetStackCapacity(), capacity);
561 TYPED_TEST(DocumentMove, MoveAssignment) {
567 a.Parse(
"[\"one\", \"two\", \"three\"]");
568 EXPECT_FALSE(a.HasParseError());
569 EXPECT_TRUE(a.IsArray());
570 EXPECT_EQ(3u, a.Size());
571 EXPECT_EQ(&a.GetAllocator(), &allocator);
576 EXPECT_TRUE(a.IsNull());
577 EXPECT_TRUE(b.IsArray());
578 EXPECT_EQ(3u, b.Size());
580 EXPECT_EQ(&b.GetAllocator(), &allocator);
582 b.Parse(
"{\"Foo\": \"Bar\", \"Baz\": 42}");
583 EXPECT_FALSE(b.HasParseError());
584 EXPECT_TRUE(b.IsObject());
585 EXPECT_EQ(2u, b.MemberCount());
590 EXPECT_TRUE(b.IsNull());
591 EXPECT_TRUE(c.IsObject());
592 EXPECT_EQ(2u, c.MemberCount());
594 EXPECT_EQ(&c.GetAllocator(), &allocator);
597 TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
604 ParseResult error(a.GetParseError(), a.GetErrorOffset());
605 EXPECT_TRUE(a.HasParseError());
606 EXPECT_NE(error.Code(), noError.
Code());
607 EXPECT_NE(error.Offset(), noError.
Offset());
611 EXPECT_FALSE(a.HasParseError());
612 EXPECT_TRUE(b.HasParseError());
613 EXPECT_EQ(a.GetParseError(), noError.
Code());
614 EXPECT_EQ(b.GetParseError(), error.Code());
615 EXPECT_EQ(a.GetErrorOffset(), noError.
Offset());
616 EXPECT_EQ(b.GetErrorOffset(), error.Offset());
620 EXPECT_FALSE(b.HasParseError());
621 EXPECT_TRUE(c.HasParseError());
622 EXPECT_EQ(b.GetParseError(), noError.
Code());
623 EXPECT_EQ(c.GetParseError(), error.Code());
624 EXPECT_EQ(b.GetErrorOffset(), noError.
Offset());
625 EXPECT_EQ(c.GetErrorOffset(), error.Offset());
632 TYPED_TEST(DocumentMove, MoveAssignmentStack) {
638 size_t defaultCapacity = a.GetStackCapacity();
642 Reader reader(&a.GetAllocator());
644 reader.template Parse<kParseDefaultFlags>(is,
a);
645 size_t capacity = a.GetStackCapacity();
646 EXPECT_GT(capacity, 0u);
650 EXPECT_EQ(a.GetStackCapacity(), defaultCapacity);
651 EXPECT_EQ(b.GetStackCapacity(), capacity);
655 EXPECT_EQ(b.GetStackCapacity(), defaultCapacity);
656 EXPECT_EQ(c.GetStackCapacity(), capacity);
660 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
GenericReader< UTF8< char >, UTF8< char >, CrtAllocator > Reader
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
size_t GetSize() const
Get the size of string in bytes in the string buffer.
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
const Ch * GetString() const
virtual ~OutputStringStream()
Output byte stream wrapper with statically bound encoding.
size_t Size() const
Computes the memory blocks allocated.
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Concept for encoding of Unicode characters.
Result of parsing (wraps ParseErrorCode)
A document for parsing JSON text as DOM.
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS. ...
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
ParseErrorCode Code() const
Get the error code.
Concept for allocating, resizing and freeing memory block.
Allocator & GetAllocator()
Get the allocator of this document.
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
GenericValue< Encoding, Allocator > ValueType
Value type of itself.
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
int StrCmp(const Ch *s1, const Ch *s2)
void ParseCheck(DocumentType &doc)
C-runtime library allocator.
File byte stream for input using fread().
const GenericPointer< typename T::ValueType > T2 value
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Validate encoding of JSON strings.
static FILE * OpenEncodedFile(const char *filename)
GenericDocument & Swap(GenericDocument &rhs) RAPIDJSON_NOEXCEPT
Exchange the contents of this document with those of another.
size_t GetStackCapacity() const
Get the capacity of stack in bytes.
bool HasParseError() const
Whether a parse error has occured in the last parsing.
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)