MCPcopy Create free account
hub / github.com/Tencent/rapidjson / main

Function main

example/tutorial/tutorial.cpp:11–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9using namespace std;
10
11int main(int, char*[]) {
12 ////////////////////////////////////////////////////////////////////////////
13 // 1. Parse a JSON text string to a document.
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
18 Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
19
20#if 0
21 // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
22 if (document.Parse(json).HasParseError())
23 return 1;
24#else
25 // In-situ parsing, decode strings directly in the source string. Source must be string.
26 char buffer[sizeof(json)];
27 memcpy(buffer, json, sizeof(json));
28 if (document.ParseInsitu(buffer).HasParseError())
29 return 1;
30#endif
31
32 printf("\nParsing to document succeeded.\n");
33
34 ////////////////////////////////////////////////////////////////////////////
35 // 2. Access values in document.
36
37 printf("\nAccess values in document:\n");
38 assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
39
40 assert(document.HasMember("hello"));
41 assert(document["hello"].IsString());
42 printf("hello = %s\n", document["hello"].GetString());
43
44 // Since version 0.2, you can use single lookup to check the existing of member and its value:
45 Value::MemberIterator hello = document.FindMember("hello");
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()); // JSON true/false are bool. Can also uses more specific function IsTrue().
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()); // Number is a JSON type, but C++ needs more specific type.
60 assert(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
61 printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"]
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"]; // Using a reference for consecutive access is handy and faster.

Callers

nothing calls this directly

Calls 12

HasMemberMethod · 0.80
FindMemberMethod · 0.80
MemberEndMethod · 0.80
BeginMethod · 0.80
MemberBeginMethod · 0.80
AddMemberMethod · 0.80
HasParseErrorMethod · 0.45
ParseMethod · 0.45
SizeMethod · 0.45
EndMethod · 0.45
GetTypeMethod · 0.45
PushBackMethod · 0.45

Tested by

no test coverage detected