26 RAPIDJSON_DIAG_OFF(c++98-compat)
27 RAPIDJSON_DIAG_OFF(missing-variable-declarations)
32 template <
typename DocumentType>
34 typedef typename DocumentType::ValueType ValueType;
37 if (doc.HasParseError())
38 printf(
"Error: %d at %zu\n", static_cast<int>(doc.GetParseError()), doc.GetErrorOffset());
44 const ValueType& hello = doc[
"hello"];
49 const ValueType& t = doc[
"t"];
53 const ValueType& f = doc[
"f"];
57 const ValueType& n = doc[
"n"];
61 const ValueType& i = doc[
"i"];
66 const ValueType& pi = doc[
"pi"];
71 const ValueType&
a = doc[
"a"];
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));
103 memset(buffer + length,
'X', length);
104 #if RAPIDJSON_HAS_STDSTRING 108 doc.Parse(buffer, length);
112 #if RAPIDJSON_HAS_STDSTRING 123 ParseTest<CrtAllocator, MemoryPoolAllocator<> >();
124 ParseTest<CrtAllocator, CrtAllocator>();
144 err = doc.
Parse(
"{}");
156 static FILE* OpenEncodedFile(
const char* filename) {
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));
189 memset(buffer + length,
'X', length);
190 #if RAPIDJSON_HAS_STDSTRING 197 if (doc.HasParseError())
198 printf(
"Error: %d at %zu\n", static_cast<int>(doc.GetParseError()), doc.GetErrorOffset());
201 #if RAPIDJSON_HAS_STDSTRING 205 #if defined(_MSC_VER) && _MSC_VER < 1800 217 FILE* fp = OpenEncodedFile(
"utf8.json");
228 wchar_t expected[] = L
"I can eat glass and it doesn't hurt me.";
231 EXPECT_EQ(
sizeof(expected) /
sizeof(
wchar_t) - 1, v.GetStringLength());
237 OutputStream eos(bos,
false);
244 fp = OpenEncodedFile(
"utf8.json");
249 reader.
Parse(is, writer2);
258 FILE* fp = OpenEncodedFile(
"utf32be.json");
269 char expected[] =
"I can eat glass and it doesn't hurt me.";
272 EXPECT_EQ(
sizeof(expected) - 1, v.GetStringLength());
281 fp = OpenEncodedFile(
"utf8.json");
286 reader.
Parse(is, writer2);
297 d1.SetArray().PushBack(1,
a).PushBack(2,
a);
300 o.SetObject().AddMember(
"a", 1,
a);
317 d2.SetArray().PushBack(3,
a);
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];
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] } ");
392 doc.FindMember(
"a")->name.SetNull();
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>
461 typedef ::testing::Types< CrtAllocator, MemoryPoolAllocator<> > MoveAllocatorTypes;
462 TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes);
464 TYPED_TEST(DocumentMove, MoveConstructor) {
470 a.Parse(
"[\"one\", \"two\", \"three\"]");
482 EXPECT_EQ(&b.GetAllocator(), &allocator);
484 b.Parse(
"{\"Foo\": \"Bar\", \"Baz\": 42}");
495 EXPECT_EQ(&c.GetAllocator(), &allocator);
498 TYPED_TEST(DocumentMove, MoveConstructorParseError) {
535 TYPED_TEST(DocumentMove, MoveConstructorStack) {
541 size_t defaultCapacity =
a.GetStackCapacity();
545 Reader reader(&
a.GetAllocator());
547 reader.template Parse<kParseDefaultFlags>(is,
a);
548 size_t capacity =
a.GetStackCapacity();
552 EXPECT_EQ(
a.GetStackCapacity(), defaultCapacity);
553 EXPECT_EQ(b.GetStackCapacity(), capacity);
556 EXPECT_EQ(b.GetStackCapacity(), defaultCapacity);
557 EXPECT_EQ(c.GetStackCapacity(), capacity);
561 TYPED_TEST(DocumentMove, MoveAssignment) {
567 a.Parse(
"[\"one\", \"two\", \"three\"]");
580 EXPECT_EQ(&b.GetAllocator(), &allocator);
582 b.Parse(
"{\"Foo\": \"Bar\", \"Baz\": 42}");
594 EXPECT_EQ(&c.GetAllocator(), &allocator);
597 TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
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();
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
GenericDocument & Swap(GenericDocument &rhs) RAPIDJSON_NOEXCEPT
Exchange the contents of this document with those of another.
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
#define EXPECT_GT(val1, val2)
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
Output byte stream wrapper with statically bound encoding.
size_t GetSize() const
Get the size of string in bytes in the string buffer.
C-runtime library allocator.
size_t Size() const
Computes the memory blocks allocated.
#define EXPECT_THROW(statement, expected_exception)
ParseErrorCode Code() const
Get the error code.
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
#define EXPECT_LE(val1, val2)
Allocator & GetAllocator()
Get the allocator of this document.
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS. ...
File byte stream for input using fread().
Concept for encoding of Unicode characters.
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
#define EXPECT_NE(val1, val2)
#define ASSERT_THROW(statement, expected_exception)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
int StrCmp(const Ch *s1, const Ch *s2)
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
void ParseCheck(DocumentType &doc)
const T & move(const T &t)
const GenericPointer< typename T::ValueType > T2 value
void * memcpy(void *a, const void *b, size_t c)
#define ASSERT_TRUE(condition)
Validate encoding of JSON strings.
const Ch * GetString() const
Result of parsing (wraps ParseErrorCode)
virtual ~OutputStringStream()
#define EXPECT_DOUBLE_EQ(val1, val2)
#define EXPECT_FALSE(condition)
Concept for allocating, resizing and freeing memory block.
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
error
Tracks LMDB error codes.
#define EXPECT_EQ(val1, val2)
A document for parsing JSON text as DOM.
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.