@@ -179,61 +179,60 @@ TEST_P(JsonTest, TestDefaultValues) {
179
179
180
180
PrintOptions options;
181
181
options.always_print_primitive_fields = true ;
182
- EXPECT_THAT (ToJson (m, options), IsOkAndHolds (" { \ " boolValue\ " :false,"
183
- " \ " int32Value\ " :0,"
184
- " \ " int64Value\" : \" 0 \" , "
185
- " \ " uint32Value\ " :0,"
186
- " \ " uint64Value\" : \" 0 \" , "
187
- " \ " floatValue\ " :0,"
188
- " \ " doubleValue\ " :0,"
189
- " \ " stringValue\" : \"\" , "
190
- " \ " bytesValue\" : \"\" , "
191
- " \ " enumValue\" : \ " FOO\" , "
192
- " \ " repeatedBoolValue\ " :[],"
193
- " \ " repeatedInt32Value\ " :[],"
194
- " \ " repeatedInt64Value\ " :[],"
195
- " \ " repeatedUint32Value\ " :[],"
196
- " \ " repeatedUint64Value\ " :[],"
197
- " \ " repeatedFloatValue\ " :[],"
198
- " \ " repeatedDoubleValue\ " :[],"
199
- " \ " repeatedStringValue\ " :[],"
200
- " \ " repeatedBytesValue\ " :[],"
201
- " \ " repeatedEnumValue\ " :[],"
202
- " \ " repeatedMessageValue\ " :[]"
182
+ EXPECT_THAT (ToJson (m, options), IsOkAndHolds (R"( { "boolValue":false,) "
183
+ R"( "int32Value":0,) "
184
+ R"( "int64Value":"0", ) "
185
+ R"( "uint32Value":0,) "
186
+ R"( "uint64Value":"0", ) "
187
+ R"( "floatValue":0,) "
188
+ R"( "doubleValue":0,) "
189
+ R"( "stringValue":"", ) "
190
+ R"( "bytesValue":"", ) "
191
+ R"( "enumValue": "FOO", ) "
192
+ R"( "repeatedBoolValue":[],) "
193
+ R"( "repeatedInt32Value":[],) "
194
+ R"( "repeatedInt64Value":[],) "
195
+ R"( "repeatedUint32Value":[],) "
196
+ R"( "repeatedUint64Value":[],) "
197
+ R"( "repeatedFloatValue":[],) "
198
+ R"( "repeatedDoubleValue":[],) "
199
+ R"( "repeatedStringValue":[],) "
200
+ R"( "repeatedBytesValue":[],) "
201
+ R"( "repeatedEnumValue":[],) "
202
+ R"( "repeatedMessageValue":[]) "
203
203
" }" ));
204
204
205
205
m.set_string_value (" i am a test string value" );
206
206
m.set_bytes_value (" i am a test bytes value" );
207
207
m.set_optional_bool_value (false );
208
208
m.set_optional_string_value (" " );
209
209
m.set_optional_bytes_value (" " );
210
- EXPECT_THAT (
211
- ToJson (m, options),
212
- IsOkAndHolds (" {\" boolValue\" :false,"
213
- " \" int32Value\" :0,"
214
- " \" int64Value\" :\" 0\" ,"
215
- " \" uint32Value\" :0,"
216
- " \" uint64Value\" :\" 0\" ,"
217
- " \" floatValue\" :0,"
218
- " \" doubleValue\" :0,"
219
- " \" stringValue\" :\" i am a test string value\" ,"
220
- " \" bytesValue\" :\" aSBhbSBhIHRlc3QgYnl0ZXMgdmFsdWU=\" ,"
221
- " \" enumValue\" :\" FOO\" ,"
222
- " \" repeatedBoolValue\" :[],"
223
- " \" repeatedInt32Value\" :[],"
224
- " \" repeatedInt64Value\" :[],"
225
- " \" repeatedUint32Value\" :[],"
226
- " \" repeatedUint64Value\" :[],"
227
- " \" repeatedFloatValue\" :[],"
228
- " \" repeatedDoubleValue\" :[],"
229
- " \" repeatedStringValue\" :[],"
230
- " \" repeatedBytesValue\" :[],"
231
- " \" repeatedEnumValue\" :[],"
232
- " \" repeatedMessageValue\" :[],"
233
- " \" optionalBoolValue\" :false,"
234
- " \" optionalStringValue\" :\"\" ,"
235
- " \" optionalBytesValue\" :\"\" "
236
- " }" ));
210
+ EXPECT_THAT (ToJson (m, options),
211
+ IsOkAndHolds (R"( {"boolValue":false,)"
212
+ R"( "int32Value":0,)"
213
+ R"( "int64Value":"0",)"
214
+ R"( "uint32Value":0,)"
215
+ R"( "uint64Value":"0",)"
216
+ R"( "floatValue":0,)"
217
+ R"( "doubleValue":0,)"
218
+ R"( "stringValue":"i am a test string value",)"
219
+ R"( "bytesValue":"aSBhbSBhIHRlc3QgYnl0ZXMgdmFsdWU=",)"
220
+ R"( "enumValue":"FOO",)"
221
+ R"( "repeatedBoolValue":[],)"
222
+ R"( "repeatedInt32Value":[],)"
223
+ R"( "repeatedInt64Value":[],)"
224
+ R"( "repeatedUint32Value":[],)"
225
+ R"( "repeatedUint64Value":[],)"
226
+ R"( "repeatedFloatValue":[],)"
227
+ R"( "repeatedDoubleValue":[],)"
228
+ R"( "repeatedStringValue":[],)"
229
+ R"( "repeatedBytesValue":[],)"
230
+ R"( "repeatedEnumValue":[],)"
231
+ R"( "repeatedMessageValue":[],)"
232
+ R"( "optionalBoolValue":false,)"
233
+ R"( "optionalStringValue":"",)"
234
+ R"( "optionalBytesValue":"")"
235
+ " }" ));
237
236
238
237
EXPECT_THAT (
239
238
ToJson (protobuf_unittest::TestAllTypes (), options),
@@ -274,6 +273,79 @@ TEST_P(JsonTest, TestDefaultValues) {
274
273
R"( ,"cordWithZero":"12\u00003","replacementString":"${unknown}"})" ));
275
274
}
276
275
276
+ TEST_P (JsonTest, TestAlwaysPrintWithoutPresenceFields) {
277
+ TestMessage m;
278
+ EXPECT_THAT (ToJson (m), IsOkAndHolds (" {}" ));
279
+
280
+ PrintOptions options;
281
+ options.always_print_without_presence_fields = true ;
282
+ EXPECT_THAT (ToJson (m, options), IsOkAndHolds (R"( {"boolValue":false,)"
283
+ R"( "int32Value":0,)"
284
+ R"( "int64Value":"0",)"
285
+ R"( "uint32Value":0,)"
286
+ R"( "uint64Value":"0",)"
287
+ R"( "floatValue":0,)"
288
+ R"( "doubleValue":0,)"
289
+ R"( "stringValue":"",)"
290
+ R"( "bytesValue":"",)"
291
+ R"( "enumValue":"FOO",)"
292
+ R"( "repeatedBoolValue":[],)"
293
+ R"( "repeatedInt32Value":[],)"
294
+ R"( "repeatedInt64Value":[],)"
295
+ R"( "repeatedUint32Value":[],)"
296
+ R"( "repeatedUint64Value":[],)"
297
+ R"( "repeatedFloatValue":[],)"
298
+ R"( "repeatedDoubleValue":[],)"
299
+ R"( "repeatedStringValue":[],)"
300
+ R"( "repeatedBytesValue":[],)"
301
+ R"( "repeatedEnumValue":[],)"
302
+ R"( "repeatedMessageValue":[])"
303
+ " }" ));
304
+
305
+ m.set_string_value (" i am a test string value" );
306
+ m.set_bytes_value (" i am a test bytes value" );
307
+ m.set_optional_bool_value (false );
308
+ m.set_optional_string_value (" " );
309
+ m.set_optional_bytes_value (" " );
310
+ EXPECT_THAT (ToJson (m, options),
311
+ IsOkAndHolds (R"( {"boolValue":false,)"
312
+ R"( "int32Value":0,)"
313
+ R"( "int64Value":"0",)"
314
+ R"( "uint32Value":0,)"
315
+ R"( "uint64Value":"0",)"
316
+ R"( "floatValue":0,)"
317
+ R"( "doubleValue":0,)"
318
+ R"( "stringValue":"i am a test string value",)"
319
+ R"( "bytesValue":"aSBhbSBhIHRlc3QgYnl0ZXMgdmFsdWU=",)"
320
+ R"( "enumValue":"FOO",)"
321
+ R"( "repeatedBoolValue":[],)"
322
+ R"( "repeatedInt32Value":[],)"
323
+ R"( "repeatedInt64Value":[],)"
324
+ R"( "repeatedUint32Value":[],)"
325
+ R"( "repeatedUint64Value":[],)"
326
+ R"( "repeatedFloatValue":[],)"
327
+ R"( "repeatedDoubleValue":[],)"
328
+ R"( "repeatedStringValue":[],)"
329
+ R"( "repeatedBytesValue":[],)"
330
+ R"( "repeatedEnumValue":[],)"
331
+ R"( "repeatedMessageValue":[],)"
332
+ R"( "optionalBoolValue":false,)"
333
+ R"( "optionalStringValue":"",)"
334
+ R"( "optionalBytesValue":"")"
335
+ " }" ));
336
+
337
+ EXPECT_THAT (
338
+ ToJson (protobuf_unittest::TestAllTypes (), options),
339
+ IsOkAndHolds (
340
+ R"( {"repeatedInt32":[],"repeatedInt64":[],"repeatedUint32":[],"repeatedUint64":[],)"
341
+ R"( "repeatedSint32":[],"repeatedSint64":[],"repeatedFixed32":[],"repeatedFixed64":[],)"
342
+ R"( "repeatedSfixed32":[],"repeatedSfixed64":[],"repeatedFloat":[],"repeatedDouble":[],)"
343
+ R"( "repeatedBool":[],"repeatedString":[],"repeatedBytes":[],"repeatedgroup":[],)"
344
+ R"( "repeatedNestedMessage":[],"repeatedForeignMessage":[],"repeatedImportMessage":[],)"
345
+ R"( "repeatedNestedEnum":[],"repeatedForeignEnum":[],"repeatedImportEnum":[],)"
346
+ R"( "repeatedStringPiece":[],"repeatedCord":[],"repeatedLazyMessage":[]})" ));
347
+ }
348
+
277
349
TEST_P (JsonTest, TestPreserveProtoFieldNames) {
278
350
TestMessage m;
279
351
m.mutable_message_value ();
0 commit comments