1 #include "json/json.h"
2 #include <fstream>
3 #include <iostream>
4 #if AOS_COMP_CLI
5 #include "aos/cli.h"
6 #endif
7 /** \brief Parse from stream, collect comments and capture error info.
8  * Example Usage:
9  * $g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream
10  * $./readFromStream
11  * // comment head
12  * {
13  *    // comment before
14  *    "key" : "value"
15  * }
16  * // comment after
17  * // comment tail
18  */
19 // int main(int argc, char* argv[]) {
20 extern "C" {
21 
jsoncpp_comp_read_stream_example(int argc,char ** argv)22 int jsoncpp_comp_read_stream_example(int argc, char **argv)
23 {
24   Json::Value root;
25   std::ifstream ifs;
26   ifs.open(argv[1]);
27 
28   Json::CharReaderBuilder builder;
29   builder["collectComments"] = true;
30   JSONCPP_STRING errs;
31   if (!parseFromStream(builder, ifs, &root, &errs)) {
32     // std::cout << errs << std::endl;
33     printf("Read json error!\r\n");
34     return EXIT_FAILURE;
35   }
36   // std::cout << root << std::endl;
37   printf("josn read root = %s !\r\n", root);
38   return EXIT_SUCCESS;
39 }
40 
41 /** \brief Write the Value object to a stream.
42  * Example Usage:
43  * $g++ streamWrite.cpp -ljsoncpp -std=c++11 -o streamWrite
44  * $./streamWrite
45  * {
46  *     "Age" : 20,
47  *     "Name" : "robin"
48  * }
49  */
jsoncpp_comp_write_stream_example(int argc,char ** argv)50 int jsoncpp_comp_write_stream_example(int argc, char **argv)
51 {
52   Json::Value root;
53   Json::StreamWriterBuilder builder;
54   const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
55 
56   root["Name"] = "robin";
57   root["Age"] = 20;
58   writer->write(root, &std::cout);
59 
60   return EXIT_SUCCESS;
61 }
62 
63 /**
64  * \brief Parse a raw string into Value object using the CharReaderBuilder
65  * class, or the legacy Reader class.
66  * Example Usage:
67  * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
68  * $./readFromString
69  * colin
70  * 20
71  */
jsoncpp_comp_read_string_example(int argc,char ** argv)72 int jsoncpp_comp_read_string_example(int argc, char **argv)
73 {
74   const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
75   const auto rawJsonLength = static_cast<int>(rawJson.length());
76   constexpr bool shouldUseOldWay = false;
77   JSONCPP_STRING err;
78   Json::Value root;
79 
80   if (shouldUseOldWay) {
81     Json::Reader reader;
82     reader.parse(rawJson, root);
83   } else {
84     Json::CharReaderBuilder builder;
85     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
86     if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
87                        &err)) {
88       printf("Read json error!\r\n");
89       // std::cout << "error" << std::endl;
90       return EXIT_FAILURE;
91     }
92   }
93   const std::string name = root["Name"].asString();
94   const int age = root["Age"].asInt();
95 
96   // std::cout << name << std::endl;
97   // std::cout << age << std::endl;
98   printf("json read name = %s !\r\n", name.c_str());
99   printf("josn read age = %d !\r\n", age);
100   return EXIT_SUCCESS;
101 }
102 
103 /** \brief Write a Value object to a string.
104  * Example Usage:
105  * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
106  * $./stringWrite
107  * {
108  *     "action" : "run",
109  *     "data" :
110  *     {
111  *         "number" : 1
112  *     }
113  * }
114  */
jsoncpp_comp_write_string_example(int argc,char ** argv)115 int jsoncpp_comp_write_string_example(int argc, char **argv)
116 {
117   Json::Value root;
118   Json::Value data;
119   constexpr bool shouldUseOldWay = false;
120   root["action"] = "run";
121   data["number"] = 1;
122   root["data"] = data;
123 
124   if (shouldUseOldWay) {
125     Json::FastWriter writer;
126     const std::string json_file = writer.write(root);
127     // std::cout << json_file << std::endl;
128     printf("json write json_file = %s !\r\n", json_file.c_str());
129   } else {
130     Json::StreamWriterBuilder builder;
131     const std::string json_file = Json::writeString(builder, root);
132     // std::cout << json_file << std::endl;
133     printf("json write json_file = %s !\r\n", json_file.c_str());
134   }
135   return EXIT_SUCCESS;
136 }
137 
138 }
139 
140 #if AOS_COMP_CLI
141 /* reg args: fun, cmd, description*/
142 // ALIOS_CLI_CMD_REGISTER(jsoncpp_comp_read_stream_example, jsoncpp_read_stream_example, jsoncpp component base example)
143 // ALIOS_CLI_CMD_REGISTER(jsoncpp_comp_write_stream_example, jsoncpp_write_stream_example, jsoncpp component base example)
144 ALIOS_CLI_CMD_REGISTER(jsoncpp_comp_read_string_example, jsoncpp_read_string_example, jsoncpp component base example)
145 ALIOS_CLI_CMD_REGISTER(jsoncpp_comp_write_string_example, jsoncpp_write_string_example, jsoncpp component base example)
146 #endif
147