stringWrite.cpp 785 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "json/json.h"
  2. #include <iostream>
  3. /** \brief Write a Value object to a string.
  4. * Example Usage:
  5. * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
  6. * $./stringWrite
  7. * {
  8. * "action" : "run",
  9. * "data" :
  10. * {
  11. * "number" : 1
  12. * }
  13. * }
  14. */
  15. int main() {
  16. Json::Value root;
  17. Json::Value data;
  18. constexpr bool shouldUseOldWay = false;
  19. root["action"] = "run";
  20. data["number"] = 1;
  21. root["data"] = data;
  22. if (shouldUseOldWay) {
  23. Json::FastWriter writer;
  24. const std::string json_file = writer.write(root);
  25. std::cout << json_file << std::endl;
  26. } else {
  27. Json::StreamWriterBuilder builder;
  28. const std::string json_file = Json::writeString(builder, root);
  29. std::cout << json_file << std::endl;
  30. }
  31. return EXIT_SUCCESS;
  32. }