xref: /aosp_15_r20/external/jsoncpp/example/stringWrite/stringWrite.cpp (revision 4484440890e2bc6e07362b4feaf15601abfe0071)
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   */
main()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  
23    if (shouldUseOldWay) {
24      Json::FastWriter writer;
25      const std::string json_file = writer.write(root);
26      std::cout << json_file << std::endl;
27    } else {
28      Json::StreamWriterBuilder builder;
29      const std::string json_file = Json::writeString(builder, root);
30      std::cout << json_file << std::endl;
31    }
32    return EXIT_SUCCESS;
33  }
34