-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_utils.cc
242 lines (219 loc) · 7.82 KB
/
json_utils.cc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "json_utils.h"
#include "reddit_constants.h"
#include "reddit_types.pb.h"
#include <jsoncpp/json/json.h>
#include <string>
using std::string;
#include <google/protobuf/message.h>
using google::protobuf::Message;
using google::protobuf::Reflection;
#include <google/protobuf/descriptor.h>
using google::protobuf::Descriptor;
using google::protobuf::FieldDescriptor;
namespace reddit {
Json::Value StringToJson(const std::string& str) {
Json::Reader reader;
Json::Value json;
reader.parse(str, json);
return json;
}
bool JsonHasErrors(const Json::Value& json) {
return !json["errors"].empty();
}
Comment JsonToComment(const Json::Value& json) {
Comment comment;
if (!json["replies"].isNull()) {
*comment.mutable_replies() = JsonToThing(json["replies"]);
}
SetMessageFromJson(&comment, json);
return comment;
}
Account JsonToAccount(const Json::Value& json) {
Account account;
SetMessageFromJson(&account, json);
return account;
}
Link JsonToLink(const Json::Value& json) {
Link link;
if (!json["media"].isNull()) {
link.set_media(json["media"].toStyledString());
}
if (!json["media_embed"].isNull()) {
link.set_media_embed(json["media_embed"].toStyledString());
}
SetMessageFromJson(&link, json);
return link;
}
reddit::Message JsonToMessage(const Json::Value& json) {
reddit::Message message;
if (json["new"].isBool()) {
message.set_new_(json["new"].asBool());
}
SetMessageFromJson(&message, json);
return message;
}
Subreddit JsonToSubreddit(const Json::Value& json) {
Subreddit subreddit;
SetMessageFromJson(&subreddit, json);
return subreddit;
}
Listing JsonToListing(const Json::Value& json) {
Listing list;
const Json::Value& json_children = json["children"];
for (const Json::Value& child : json_children) {
*list.add_children() = JsonToThing(child);
}
SetMessageFromJson(&list, json);
return list;
}
More JsonToMore(const Json::Value& json) {
More more;
SetMessageFromJson(&more, json);
return more;
}
Thing JsonToThing(const Json::Value& json) {
Thing thing;
if (!json.isNull() && !json.isObject()) {
return thing;
}
if (json["kind"].isString()) {
thing.set_kind(json["kind"].asString());
}
const Json::Value& data = json["data"];
if (thing.kind() == kCommentPrefix) {
*thing.mutable_comment_data() = JsonToComment(data);
} else if (thing.kind() == kAccountPrefix) {
*thing.mutable_account_data() = JsonToAccount(data);
} else if (thing.kind() == kLinkPrefix) {
*thing.mutable_link_data() = JsonToLink(data);
} else if (thing.kind() == kMessagePrefix) {
*thing.mutable_message_data() = JsonToMessage(data);
} else if (thing.kind() == kSubredditPrefix) {
*thing.mutable_subreddit_data() = JsonToSubreddit(data);
} else if (thing.kind() == kListingPrefix) {
*thing.mutable_listing_data() = JsonToListing(data);
} else if (thing.kind() == kMorePrefix) {
*thing.mutable_more_data() = JsonToMore(data);
}
return thing;
}
LoginResponse JsonToLoginResponse(const Json::Value& json) {
LoginResponse response;
for (const Json::Value& json_err : json["errors"]) {
Error* error = response.add_errors();
for (const Json::Value& json_err_info : json_err) {
if (json_err_info.isString()) {
error->add_info(json_err_info.asString());
}
}
}
SetMessageFromJson(&response, json);
return response;
}
// Iterate through unset fields in the protobuf, and set them according to the
// data in the json value
void SetMessageFromJson(google::protobuf::Message* msg, const Json::Value& json) {
const Descriptor* msg_desc = msg->GetDescriptor();
const Reflection* msg_refl = msg->GetReflection();
for (int i = 0; i < msg_desc->field_count(); ++i) {
const FieldDescriptor* field = msg_desc->field(i);
const Json::Value& json_val = json[field->name()];
if (json_val.empty()) {
continue;
}
if (field->is_repeated()) {
if (msg_refl->FieldSize(*msg, field)) {
continue;
}
for (const Json::Value& arr_item : json_val) {
MaybeAddField(msg, field, arr_item);
}
} else { // not repeated
if (msg_refl->HasField(*msg, field)) {
continue;
}
MaybeSetField(msg, field, json_val);
}
}
}
void MaybeSetField(google::protobuf::Message* msg, const FieldDescriptor* field,
const Json::Value& json_val) {
const Reflection* msg_refl = msg->GetReflection();
if (field->cpp_type() == FieldDescriptor::CPPTYPE_INT32) {
if (json_val.isNumeric()) {
msg_refl->SetInt32(msg, field, json_val.asInt());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_UINT32) {
if (json_val.isNumeric()) {
msg_refl->SetUInt32(msg, field, json_val.asUInt());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_INT64) {
if (json_val.isNumeric()) {
msg_refl->SetInt64(msg, field, json_val.asInt64());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_UINT64) {
if (json_val.isNumeric()) {
msg_refl->SetUInt64(msg, field, json_val.asUInt64());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT) {
if (json_val.isNumeric()) {
msg_refl->SetFloat(msg, field, json_val.asFloat());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE) {
if (json_val.isNumeric()) {
msg_refl->SetDouble(msg, field, json_val.asDouble());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_BOOL) {
if (json_val.isBool()) {
msg_refl->SetBool(msg, field, json_val.asBool());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
if (json_val.isString()) {
msg_refl->SetString(msg, field, json_val.asString());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
google::protobuf::Message* embedded_msg = msg_refl->MutableMessage(msg, field);
SetMessageFromJson(embedded_msg, json_val);
}
}
void MaybeAddField(google::protobuf::Message* msg, const FieldDescriptor* field,
const Json::Value& json_val) {
const Reflection* msg_refl = msg->GetReflection();
if (field->cpp_type() == FieldDescriptor::CPPTYPE_INT32) {
if (json_val.isNumeric()) {
msg_refl->AddInt32(msg, field, json_val.asInt());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_UINT32) {
if (json_val.isNumeric()) {
msg_refl->AddUInt32(msg, field, json_val.asUInt());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_INT64) {
if (json_val.isNumeric()) {
msg_refl->AddInt64(msg, field, json_val.asInt64());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_UINT64) {
if (json_val.isNumeric()) {
msg_refl->AddUInt64(msg, field, json_val.asUInt64());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT) {
if (json_val.isNumeric()) {
msg_refl->AddFloat(msg, field, json_val.asFloat());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE) {
if (json_val.isNumeric()) {
msg_refl->AddDouble(msg, field, json_val.asDouble());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_BOOL) {
if (json_val.isBool()) {
msg_refl->AddBool(msg, field, json_val.asBool());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
if (json_val.isString()) {
msg_refl->AddString(msg, field, json_val.asString());
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
google::protobuf::Message* embedded_msg = msg_refl->AddMessage(msg, field);
SetMessageFromJson(embedded_msg, json_val);
}
}
}; // namespace reddit