Newer
Older
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "catch.hpp"
#include <google/protobuf/util/json_util.h>
#include <testpb/messages.pb.h>
TEST_CASE("simple struct", "[protobuf]") {
auto json = R"(
{
"int_value": 42,
"string_value": "foo bar",
"int_arr": [4, 2],
"string_arr": ["foo", "bar", "baz"],
"int_map": {
"foo": 4,
"bar": 2,
},
"string_map": {
"foo": "foo_value",
"bar": "bar_value"
}
}
)";
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() == 42);
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("foo") == 4);
CHECK(msg.int_map().at("bar") == 2);
CHECK(msg.string_map_size() == 2);
CHECK(msg.string_map().at("foo") == "foo_value");
CHECK(msg.string_map().at("bar") == "bar_value");
}
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);
}
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);
}
SECTION("marshal test") {
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);
}
}