26RAPIDJSON_DIAG_OFF(c++98-compat)
27RAPIDJSON_DIAG_OFF(missing-variable-declarations)
32template <
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"];
78template <
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
144 err = doc.
Parse(
"{}");
156static 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));
188 memcpy(buffer, json, length);
189 memset(buffer + length,
'X', length);
190#if RAPIDJSON_HAS_STDSTRING
191 std::string s2(buffer, length);
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");
223 d.ParseStream<0,
UTF8<> >(eis);
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
418#include <type_traits>
421 static_assert(std::is_constructible<Document>::value,
"");
422 static_assert(std::is_default_constructible<Document>::value,
"");
424 static_assert(!std::is_copy_constructible<Document>::value,
"");
426 static_assert(std::is_move_constructible<Document>::value,
"");
428 static_assert(!std::is_nothrow_constructible<Document>::value,
"");
429 static_assert(!std::is_nothrow_default_constructible<Document>::value,
"");
431 static_assert(!std::is_nothrow_copy_constructible<Document>::value,
"");
432 static_assert(std::is_nothrow_move_constructible<Document>::value,
"");
435 static_assert(std::is_assignable<Document,Document>::value,
"");
437 static_assert(!std::is_copy_assignable<Document>::value,
"");
439 static_assert(std::is_move_assignable<Document>::value,
"");
442 static_assert(std::is_nothrow_assignable<Document, Document>::value,
"");
444 static_assert(!std::is_nothrow_copy_assignable<Document>::value,
"");
446 static_assert(std::is_nothrow_move_assignable<Document>::value,
"");
449 static_assert( std::is_destructible<Document>::value,
"");
451 static_assert(std::is_nothrow_destructible<Document>::value,
"");
457template <
typename Allocator>
461typedef ::testing::Types< CrtAllocator, MemoryPoolAllocator<> > MoveAllocatorTypes;
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);
498TYPED_TEST(DocumentMove, MoveConstructorParseError) {
519 EXPECT_EQ(b.GetParseError(), error.Code());
520 EXPECT_EQ(b.GetErrorOffset(), error.Offset());
526 EXPECT_EQ(c.GetParseError(), error.Code());
528 EXPECT_EQ(c.GetErrorOffset(), error.Offset());
535TYPED_TEST(DocumentMove, MoveConstructorStack) {
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);
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);
597TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
614 EXPECT_EQ(b.GetParseError(), error.Code());
616 EXPECT_EQ(b.GetErrorOffset(), error.Offset());
623 EXPECT_EQ(c.GetParseError(), error.Code());
625 EXPECT_EQ(c.GetErrorOffset(), error.Offset());
632TYPED_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);
C-runtime library allocator.
Output byte stream wrapper with statically bound encoding.
File byte stream for input using fread().
A document for parsing JSON text as DOM.
Allocator & GetAllocator()
Get the allocator of this document.
GenericDocument & Swap(GenericDocument &rhs) RAPIDJSON_NOEXCEPT
Exchange the contents of this document with those of another.
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
size_t GetStackCapacity() const
Get the capacity of stack in bytes.
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
const Ch * GetString() const
size_t GetSize() const
Get the size of string in bytes in the string buffer.
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Default memory allocator used by the parser and DOM.
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
size_t Size() const
Computes the memory blocks allocated.
Concept for allocating, resizing and freeing memory block.
Concept for encoding of Unicode characters.
#define D(var, file, col, who, lev,...)
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
void ParseCheck(DocumentType &doc)
GenericReader< UTF8< char >, UTF8< char >, CrtAllocator > Reader
#define EXPECT_EQ(val1, val2)
#define EXPECT_NE(val1, val2)
#define EXPECT_THROW(statement, expected_exception)
#define EXPECT_GT(val1, val2)
#define EXPECT_DOUBLE_EQ(val1, val2)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define TEST(test_case_name, test_name)
#define EXPECT_LE(val1, val2)
#define ASSERT_TRUE(condition)
#define EXPECT_FALSE(condition)
#define ASSERT_THROW(statement, expected_exception)
TYPED_TEST(TypedTest, TestA)
TYPED_TEST_CASE(TypedTest, MyTypes)
bip::allocator< T, pinnable_mapped_file::segment_manager > allocator
void swap(picojson::value &x, picojson::value &y)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
@ kParseDefaultFlags
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS.
@ kParseValidateEncodingFlag
Validate encoding of JSON strings.
Dynamically select encoding according to stream's runtime-specified UTF encoding type.
virtual ~OutputStringStream()
Result of parsing (wraps ParseErrorCode)
ParseErrorCode Code() const
Get the error code.
bool IsError() const
Whether the result is an error.
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
int StrCmp(const Ch *s1, const Ch *s2)
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)