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