simplewriter.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "rapidjson/writer.h"
  2. #include "rapidjson/stringbuffer.h"
  3. #include <iostream>
  4. using namespace rapidjson;
  5. using namespace std;
  6. int main() {
  7. StringBuffer s;
  8. Writer<StringBuffer> writer(s);
  9. writer.StartObject(); // Between StartObject()/EndObject(),
  10. writer.Key("hello"); // output a key,
  11. writer.String("world"); // follow by a value.
  12. writer.Key("t");
  13. writer.Bool(true);
  14. writer.Key("f");
  15. writer.Bool(false);
  16. writer.Key("n");
  17. writer.Null();
  18. writer.Key("i");
  19. writer.Uint(123);
  20. writer.Key("pi");
  21. writer.Double(3.1416);
  22. writer.Key("a");
  23. writer.StartArray(); // Between StartArray()/EndArray(),
  24. for (unsigned i = 0; i < 4; i++)
  25. writer.Uint(i); // all values are elements of the array.
  26. writer.EndArray();
  27. writer.EndObject();
  28. // {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
  29. cout << s.GetString() << endl;
  30. return 0;
  31. }