-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_write.cpp
163 lines (135 loc) · 4.97 KB
/
read_write.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "read_write.h"
#include <json/json.h>
#include <numeric>
#include <sstream>
#include <iostream>
#include <fstream>
void ReadWrite::ReadJson() {
std::string json = "{\"name\":\"Tom\",\"age\":29,\"weight\":65.2,\"height\":175,\"children\":[\"Bob\",\"Alice\"]}";
Json::Value root;
std::stringstream ss{json};
Json::CharReaderBuilder builder;
std::string errs;
bool ret = Json::parseFromStream(builder, ss, &root, &errs);
if (!ret || !root.isObject()) {
std::cout << "cannot convert string to json, err: " << errs << std::endl;
return;
}
std::string name = root["name"].asString();
int32_t age = root["age"].asInt();
double weight = root["weight"].asDouble();
uint32_t height = root["height"].asUInt();
std::vector<std::string> children_names;
Json::Value children = root["children"];
if (!children.isNull() && children.isArray()) {
for (uint32_t i = 0; i < children.size(); ++i) {
children_names.push_back(children[i].asString());
}
}
std::cout << "ConvertStringToJson, Name: " << name << ", age: " << age << ", weight: " << weight
<< ", height: " << height << ", children: "
<< std::accumulate(std::next(children_names.begin()), children_names.end(),
std::string{*children_names.begin()},
[](std::string out, const std::string &in){return out + ", " + in;})
<< std::endl;
}
void ReadWrite::ReadJson2() {
std::string json = "{\"name\":\"Tom\",\r\n\"age\":29,\r\n\"weight\":65.2,\r\n\"height\":175,\r\n\"children\":[\"Bob\",\r\n\"Alice\"]}";
Json::Value root;
std::stringstream ss{json};
Json::CharReaderBuilder builder;
std::string errs;
bool ret = Json::parseFromStream(builder, ss, &root, &errs);
if (!ret || !root.isObject()) {
std::cout << "cannot convert string to json, err: " << errs << std::endl;
return;
}
std::cout << "convert string to json" << std::endl;
}
void ReadWrite::WriteJson() {
Json::Value root(Json::objectValue);
root["name"] = "Tom";
root["age"] = 29;
root["weight"] = 65.2;
root["height"] = 175;
Json::Value children(Json::arrayValue);
children.append("Bob");
children.append("Alice");
root["children"] = children;
// FastWriter, StyledWriter, StyledStreamWriter, and Writer are deprecated.
// Use StreamWriterBuilder, which creates a StreamWriter with a slightly different API
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::stringstream ss;
writer->write(root, &ss);
std::cout << "ConvertJsonToString, json: " << ss.str() << std::endl;
}
void ReadWrite::WriteJson2() {
Json::Value root(Json::objectValue);
root["name"] = "Tom";
root["age"] = 29;
root["weight"] = 65.2;
root["height"] = 175;
Json::Value children(Json::arrayValue);
children.append("Bob");
children.append("Alice");
root["children"] = children;
// FastWriter, StyledWriter, StyledStreamWriter, and Writer are deprecated.
// Use StreamWriterBuilder, which creates a StreamWriter with a slightly different API
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "\r";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::stringstream ss;
writer->write(root, &ss);
std::ofstream ofs;
ofs.open("/tmp/test.json");
writer->write(root, &ofs);
std::cout << "ConvertJsonToString, json: " << ss.str() << std::endl;
}
void ReadWrite::JsonTraversal() {
Json::Value root(Json::objectValue);
root["name"] = "Tom";
root["age"] = 29;
root["weight"] = 65.2;
root["height"] = 175;
Json::Value children(Json::arrayValue);
Json::Value child_bob(Json::objectValue);
child_bob["name"] = "Bob";
child_bob["age"] = 3;
children.append(child_bob);
Json::Value child_alice(Json::objectValue);
child_alice["name"] = "Alice";
child_alice["age"] = 1;
children.append(child_alice);
root["children"] = children;
Json::Value children_name(Json::arrayValue);
children_name.append("Bob");
children_name.append("Alice");
root["children_name"] = children_name;
OutputJson(root, "");
}
void ReadWrite::OutputJson(const Json::Value &root, const std::string &pre) {
if (!root.isObject()) {
std::cout << pre << root.asString() << ",\n";
return;
}
Json::Value::Members members = root.getMemberNames();
std::cout << pre << "{\n";
for (auto iter = members.begin(); iter != members.end(); ++iter) {
uint32_t value_type = root[*iter].type();
std::cout << pre << "\t\"" << *iter << "(" << value_type << ")\": ";
if (value_type == Json::objectValue) {
OutputJson(root[*iter], "\t\t");
} else if (value_type == Json::arrayValue) {
std::cout << pre << "[\n";
for (int i = 0; i < root[*iter].size(); ++i) {
OutputJson(root[*iter][i], "\t\t");
}
std::cout << pre << "\t],\n";
} else {
std::cout << root[*iter].asString() << ",\n";
}
}
std::cout << pre << "},\n";
}