15 const char json[] =
" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
16 printf(
"Original JSON:\n %s\n", json);
26 char buffer[
sizeof(
json)];
27 memcpy(buffer, json,
sizeof(json));
32 printf(
"\nParsing to document succeeded.\n");
37 printf(
"\nAccess values in document:\n");
38 assert(document.IsObject());
40 assert(document.HasMember(
"hello"));
41 assert(document[
"hello"].IsString());
42 printf(
"hello = %s\n", document[
"hello"].GetString());
46 assert(hello != document.MemberEnd());
47 assert(hello->value.IsString());
48 assert(strcmp(
"world", hello->value.GetString()) == 0);
51 assert(document[
"t"].IsBool());
52 printf(
"t = %s\n", document[
"t"].GetBool() ?
"true" :
"false");
54 assert(document[
"f"].IsBool());
55 printf(
"f = %s\n", document[
"f"].GetBool() ?
"true" :
"false");
57 printf(
"n = %s\n", document[
"n"].IsNull() ?
"null" :
"?");
59 assert(document[
"i"].IsNumber());
60 assert(document[
"i"].IsInt());
61 printf(
"i = %d\n", document[
"i"].GetInt());
63 assert(document[
"pi"].IsNumber());
64 assert(document[
"pi"].IsDouble());
65 printf(
"pi = %g\n", document[
"pi"].GetDouble());
68 const Value&
a = document[
"a"];
70 for (
SizeType i = 0; i < a.Size(); i++)
71 printf(
"a[%d] = %d\n", i, a[i].GetInt());
73 int y = a[0].GetInt();
79 printf(
"%d ", itr->GetInt());
84 static const char* kTypeNames[] = {
"Null",
"False",
"True",
"Object",
"Array",
"String",
"Number" };
86 printf(
"Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);
97 assert(!document[
"i"].IsInt());
104 for (
int i = 5; i <= 10; i++)
105 a.PushBack(i, allocator);
108 a.PushBack(
"Lua", allocator).PushBack(
"Mio", allocator);
116 document[
"hello"] =
"rapidjson";
125 int len = sprintf(buffer2,
"%s %s",
"Milo",
"Yip");
127 author.SetString(buffer2, static_cast<SizeType>(len), document.
GetAllocator());
134 memset(buffer2, 0,
sizeof(buffer2));
137 document.AddMember(
"author", author, document.
GetAllocator());
139 assert(author.IsNull());
144 printf(
"\nModified JSON with reformatting:\n");
147 document.Accept(writer);
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
const Ch * GetString() const
(Constant) member iterator for a JSON object value
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string.
unsigned __int64 uint64_t
Allocator & GetAllocator()
Get the allocator of this document.
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
Writer with indentation and spacing.
bool HasParseError() const
Whether a parse error has occured in the last parsing.