simplewriter.cpp 1.1 KB

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