simplewriter.cpp 1.01 KB
Newer Older
1 2 3 4 5 6 7
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;
using namespace std;

8
int main() {
9 10 11
    StringBuffer s;
    Writer<StringBuffer> writer(s);
    
12 13 14 15 16 17
    writer.StartObject();               // Between StartObject()/EndObject(), 
    writer.Key("hello");                // output a key,
    writer.String("world");             // follow by a value.
    writer.Key("t");
    writer.Bool(true);
    writer.Key("f");
18
    writer.Bool(false);
19
    writer.Key("n");
20
    writer.Null();
21
    writer.Key("i");
22
    writer.Uint(123);
23
    writer.Key("pi");
24
    writer.Double(3.1416);
25 26
    writer.Key("a");
    writer.StartArray();                // Between StartArray()/EndArray(),
27
    for (unsigned i = 0; i < 4; i++)
28
        writer.Uint(i);                 // all values are elements of the array.
29 30 31
    writer.EndArray();
    writer.EndObject();

32
    // {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
33
    cout << s.GetString() << endl;
34

35
    return 0;
36
}