Home | History | Annotate | Download | only in tutorial
      1 // Hello World example
      2 // This example shows basic usage of DOM-style API.
      3 
      4 #include "rapidjson/document.h"     // rapidjson's DOM-style API
      5 #include "rapidjson/prettywriter.h" // for stringify JSON
      6 #include <cstdio>
      7 
      8 using namespace rapidjson;
      9 using namespace std;
     10 
     11 int 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.
     69         assert(a.IsArray());
     70         for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
     71             printf("a[%d] = %d\n", i, a[i].GetInt());
     72 
     73         int y = a[0].GetInt();
     74         (void)y;
     75 
     76         // Iterating array with iterators
     77         printf("a = ");
     78         for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
     79             printf("%d ", itr->GetInt());
     80         printf("\n");
     81     }
     82 
     83     // Iterating object members
     84     static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
     85     for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)
     86         printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);
     87 
     88     ////////////////////////////////////////////////////////////////////////////
     89     // 3. Modify values in document.
     90 
     91     // Change i to a bigger number
     92     {
     93         uint64_t f20 = 1;   // compute factorial of 20
     94         for (uint64_t j = 1; j <= 20; j++)
     95             f20 *= j;
     96         document["i"] = f20;    // Alternate form: document["i"].SetUint64(f20)
     97         assert(!document["i"].IsInt()); // No longer can be cast as int or uint.
     98     }
     99 
    100     // Adding values to array.
    101     {
    102         Value& a = document["a"];   // This time we uses non-const reference.
    103         Document::AllocatorType& allocator = document.GetAllocator();
    104         for (int i = 5; i <= 10; i++)
    105             a.PushBack(i, allocator);   // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.
    106 
    107         // Fluent API
    108         a.PushBack("Lua", allocator).PushBack("Mio", allocator);
    109     }
    110 
    111     // Making string values.
    112 
    113     // This version of SetString() just store the pointer to the string.
    114     // So it is for literal and string that exists within value's life-cycle.
    115     {
    116         document["hello"] = "rapidjson";    // This will invoke strlen()
    117         // Faster version:
    118         // document["hello"].SetString("rapidjson", 9);
    119     }
    120 
    121     // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
    122     Value author;
    123     {
    124         char buffer[10];
    125         int len = sprintf(buffer, "%s %s", "Milo", "Yip");  // synthetic example of dynamically created string.
    126 
    127         author.SetString(buffer, static_cast<size_t>(len), document.GetAllocator());
    128         // Shorter but slower version:
    129         // document["hello"].SetString(buffer, document.GetAllocator());
    130 
    131         // Constructor version:
    132         // Value author(buffer, len, document.GetAllocator());
    133         // Value author(buffer, document.GetAllocator());
    134         memset(buffer, 0, sizeof(buffer)); // For demonstration purpose.
    135     }
    136     // Variable 'buffer' is unusable now but 'author' has already made a copy.
    137     document.AddMember("author", author, document.GetAllocator());
    138 
    139     assert(author.IsNull());        // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.
    140 
    141     ////////////////////////////////////////////////////////////////////////////
    142     // 4. Stringify JSON
    143 
    144     printf("\nModified JSON with reformatting:\n");
    145     StringBuffer sb;
    146     PrettyWriter<StringBuffer> writer(sb);
    147     document.Accept(writer);    // Accept() traverses the DOM and generates Handler events.
    148     puts(sb.GetString());
    149 
    150     return 0;
    151 }
    152