Newer
Older
#include <google/protobuf/util/json_util.h>
#include <testpb/messages.pb.h>
TEST_CASE("simple struct", "[protobuf]") {
auto json = R"(
{
"int_value": 9223372036854775807,
"string_value": "foo bar",
"int_arr": [4, 2],
"string_arr": ["foo", "bar", "baz"],
"int_map": {
},
"string_map": {
"foo": "foo_value",
"bar": "bar_value"
},
"double_value": 42
}
)";
testproto::SimpleStruct msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(msg.string_value() == "foo bar");
CHECK(msg.int_value() == 9223372036854775807);
CHECK(msg.int_arr().size() == 2);
CHECK(msg.int_arr().Get(0) == 4);
CHECK(msg.int_arr().Get(1) == 2);
CHECK(msg.string_arr().size() == 3);
std::vector<std::string> expected{"foo", "bar", "baz"};
CHECK(
std::equal(
msg.string_arr().begin(), msg.string_arr().end(),
expected.begin(), expected.end()
)
);
CHECK(msg.int_map_size() == 2);
CHECK(msg.int_map().at(42) == 4);
CHECK(msg.int_map().at(72) == 2);
CHECK(msg.string_map_size() == 2);
CHECK(msg.string_map().at("foo") == "foo_value");
CHECK(msg.string_map().at("bar") == "bar_value");
CHECK(msg.double_value() == 42);
}
TEST_CASE("unmarshal bytes", "[protobuf]") {
auto json = R"(
{
"bytes_value": "AEJmb29iYXI=",
}
)";
testproto::BytesType msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
using namespace std::string_literals;
CHECK(msg.bytes_value() == "\x00\x42"s "foobar");
}
TEST_CASE("two dimensional array", "[protobuf]") {
auto json = R"(
{
arr: [
{
str_arr: ["foo", "bar"]
}
]
}
)";
testproto::TwoDimensionalArray msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
REQUIRE(msg.arr().size() == 1);
REQUIRE(msg.arr().at(0).str_arr().size() == 2);
REQUIRE(msg.arr().at(0).str_arr().at(0) == "foo");
REQUIRE(msg.arr().at(0).str_arr().at(1) == "bar");
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
}
TEST_CASE("invalid json", "[protobuf]") {
auto json = R"(
{
"string_value": invalid,
}
)";
testproto::SimpleStruct msg;
google::protobuf::util::JsonParseOptions options;
auto status = JsonStringToMessage(json, &msg, options);
REQUIRE_FALSE(status.ok());
}
TEST_CASE("null/missing integer value", "[protobuf]") {
auto json = R"(
{
"int_value": null,
"string_value": "foo bar",
}
)";
google::protobuf::util::JsonParseOptions options;
testproto::SimpleStruct msg;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
REQUIRE(status.ok());
REQUIRE(msg.string_value() == "foo bar");
REQUIRE(msg.int_value() == 0);
json = R"(
{
"string_value": "foo bar",
}
)";
msg = testproto::SimpleStruct{};
status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
REQUIRE(status.ok());
REQUIRE(msg.string_value() == "foo bar");
REQUIRE(msg.int_value() == 0);
}
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
TEST_CASE("json marshal double", "[protobuf]") {
testproto::SimpleStruct msg;
msg.set_double_value(42);
std::string json;
google::protobuf::util::JsonOptions options;
options.add_whitespace = true;
options.preserve_proto_field_names = true;
options.always_print_enums_as_ints = true;
auto status = google::protobuf::util::MessageToJsonString(msg, &json, options);
auto expected = R"({
"double_value": 42
}
)";
REQUIRE(json == expected);
}
TEST_CASE("json marshal uint64", "[protobuf]") {
testproto::SimpleStruct msg;
msg.set_uint64_value(42);
std::string json;
google::protobuf::util::JsonOptions options;
options.add_whitespace = true;
options.preserve_proto_field_names = true;
options.always_print_enums_as_ints = true;
auto status = google::protobuf::util::MessageToJsonString(msg, &json, options);
auto expected = R"({
"uint64_value": "42"
}
)";
REQUIRE(json == expected);
}
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
TEST_CASE("json marshal naming", "[protobuf]") {
testproto::JsonNaming msg;
msg.set_foo_value(1);
msg.set_barvalue(2);
msg.set_bazvalue(3);
msg.set_qux_value(4);
std::string json;
google::protobuf::util::JsonOptions options;
options.add_whitespace = true;
options.preserve_proto_field_names = true;
options.always_print_primitive_fields = true;
options.always_print_enums_as_ints = true;
auto status = google::protobuf::util::MessageToJsonString(msg, &json, options);
auto expected = R"({
"foo_value": 1,
"barValue": 2,
"BazValue": 3,
"quX_Value": 4
}
)";
REQUIRE(json == expected);
}
TEST_CASE("json unmarshal naming", "[protobuf]") {
SECTION("accept snake case") {
auto json = R"(
{
"quX_Value": 42,
}
)";
testproto::JsonNaming msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(msg.qux_value() == 42);
}
SECTION("accept camelCase case") {
auto json = R"(
{
"barValue": 42,
}
)";
testproto::JsonNaming msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(msg.barvalue() == 42);
}
SECTION("accept CamelCase case") {
auto json = R"(
{
"BazValue": 42,
}
)";
testproto::JsonNaming msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(msg.bazvalue() == 42);
}
}
TEST_CASE("oneof unmarshal", "[protobuf]") {
SECTION("accept string value") {
auto json = R"(
{
"string_value": "foo bar",
}
)";
testproto::OneofValue msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(msg.data_case() == testproto::OneofValue::kStringValue);
CHECK_FALSE(msg.has_struct_value());
CHECK(msg.string_value() == "foo bar");
}
SECTION("accept struct value") {
auto json = R"(
{
"struct_value": {
"int_value": 42
}
}
)";
testproto::OneofValue msg;
google::protobuf::util::JsonParseOptions options;
auto status = google::protobuf::util::JsonStringToMessage(json, &msg, options);
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(msg.data_case() == testproto::OneofValue::kStructValue);
CHECK(msg.has_struct_value());
CHECK(msg.struct_value().int_value() == 42);
}
}
TEST_CASE("oneof marshal", "[protobuf]") {
SECTION("marshal string value") {
testproto::OneofValue msg;
msg.set_string_value("foo bar");
google::protobuf::util::JsonPrintOptions options;
options.preserve_proto_field_names = true;
std::string json;
auto status = google::protobuf::util::MessageToJsonString(msg, &json, options);
auto expected = R"({"string_value":"foo bar"})";
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(json == expected);
}
SECTION("marshal struct value") {
testproto::OneofValue msg;
msg.mutable_struct_value()->set_int_value(42);
google::protobuf::util::JsonPrintOptions options;
options.preserve_proto_field_names = true;
std::string json;
auto status = google::protobuf::util::MessageToJsonString(msg, &json, options);
auto expected = R"({"struct_value":{"int_value":"42"}})";
CHECK(status.error_message() == "");
REQUIRE(status.ok());
CHECK(json == expected);
}
}
TEST_CASE("empty messages with OmitEmpty", "[protobuf]") {
testproto::MessageType msg;
msg.add_foo_arr();
google::protobuf::util::JsonPrintOptions options;
options.preserve_proto_field_names = true;
options.always_print_primitive_fields = false;
std::string json;
auto status = google::protobuf::util::MessageToJsonString(msg, &json, options);
auto expected = R"({"foo_arr":[{}]})";
CHECK(json == expected);
}