simpledom.cpp 772 B

12345678910111213141516171819202122232425262728293031
  1. // JSON simple example
  2. // This example does not handle errors.
  3. #include <rtthread.h>
  4. #include "rapidjson/document.h"
  5. #include "rapidjson/writer.h"
  6. #include "rapidjson/stringbuffer.h"
  7. #include <iostream>
  8. using namespace rapidjson;
  9. int simple_dom() {
  10. // 1. Parse a JSON string into DOM.
  11. const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
  12. Document d;
  13. d.Parse(json);
  14. // 2. Modify it by DOM.
  15. Value& s = d["stars"];
  16. s.SetInt(s.GetInt() + 1);
  17. // 3. Stringify the DOM
  18. StringBuffer buffer;
  19. Writer<StringBuffer> writer(buffer);
  20. d.Accept(writer);
  21. // Output {"project":"rapidjson","stars":11}
  22. std::cout << buffer.GetString() << std::endl;
  23. return 0;
  24. }
  25. MSH_CMD_EXPORT(simple_dom, rapid json simple dom example);