11 {
13
14
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);
17
19
20#if 0
21
23 return 1;
24#else
25
26 char buffer[
sizeof(
json)];
29 return 1;
30#endif
31
32 printf("\nParsing to document succeeded.\n");
33
35
36
37 printf("\nAccess values in document:\n");
38 assert(document.IsObject());
39
40 assert(document.HasMember("hello"));
41 assert(document["hello"].IsString());
42 printf("hello = %s\n", document["hello"].GetString());
43
44
46 assert(hello != document.MemberEnd());
47 assert(hello->value.IsString());
48 assert(strcmp("world", hello->value.GetString()) == 0);
49 (void)hello;
50
51 assert(document["t"].IsBool());
52 printf("t = %s\n", document["t"].GetBool() ? "true" : "false");
53
54 assert(document["f"].IsBool());
55 printf("f = %s\n", document["f"].GetBool() ? "true" : "false");
56
57 printf(
"n = %s\n", document[
"n"].
IsNull() ?
"null" :
"?");
58
59 assert(document["i"].IsNumber());
60 assert(document["i"].IsInt());
61 printf("i = %d\n", document["i"].GetInt());
62
63 assert(document["pi"].IsNumber());
64 assert(document["pi"].IsDouble());
65 printf("pi = %g\n", document["pi"].GetDouble());
66
67 {
68 const Value&
a = document[
"a"];
71 printf(
"a[%d] = %d\n", i,
a[i].GetInt());
72
73 int y =
a[0].GetInt();
74 (void)y;
75
76
77 printf("a = ");
79 printf("%d ", itr->GetInt());
80 printf("\n");
81 }
82
83
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()]);
87
89
90
91
92 {
95 f20 *= j;
96 document["i"] = f20;
97 assert(!document["i"].IsInt());
98 }
99
100
101 {
104 for (int i = 5; i <= 10; i++)
105 a.PushBack(i, allocator);
106
107
108 a.PushBack(
"Lua", allocator).PushBack(
"Mio", allocator);
109 }
110
111
112
113
114
115 {
116 document["hello"] = "rapidjson";
117
118
119 }
120
121
123 {
124 char buffer2[10];
125 int len = sprintf(buffer2, "%s %s", "Milo", "Yip");
126
128
129
130
131
132
133
134 memset(buffer2, 0, sizeof(buffer2));
135 }
136
137 document.AddMember(
"author", author, document.
GetAllocator());
138
139 assert(author.IsNull());
140
142
143
144 printf("\nModified JSON with reformatting:\n");
147 document.Accept(writer);
149
150 return 0;
151}
Allocator & GetAllocator()
Get the allocator of this document.
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string.
MemoryPoolAllocator<> AllocatorType
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).
const Ch * GetString() const
GenericMemberIterator< false, UTF8<>, MemoryPoolAllocator<> >::Iterator MemberIterator
const GenericValue * ConstValueIterator
GenericMemberIterator< true, UTF8<>, MemoryPoolAllocator<> >::Iterator ConstMemberIterator
Writer with indentation and spacing.
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
GenericStringBuffer< UTF8< char >, CrtAllocator > StringBuffer
void * memcpy(void *a, const void *b, size_t c)
PolymorphicMatcher< internal::IsNullMatcher > IsNull()
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).
unsigned __int64 uint64_t