31 package com.google.protobuf.util;
33 import static com.
google.common.truth.Truth.assertThat;
34 import static com.
google.common.truth.Truth.assertWithMessage;
36 import com.
google.common.collect.ImmutableSet;
71 import java.io.IOException;
72 import java.io.InputStream;
73 import java.io.InputStreamReader;
74 import java.io.Reader;
75 import java.io.StringReader;
76 import java.math.BigDecimal;
77 import java.math.BigInteger;
78 import java.util.Collections;
79 import java.util.HashSet;
80 import java.util.Locale;
82 import org.junit.Test;
83 import org.junit.runner.RunWith;
84 import org.junit.runners.JUnit4;
86 @RunWith(JUnit4.class)
87 public class JsonFormatTest {
90 Locale.setDefault(Locale.forLanguageTag(
"hi-IN"));
95 builder.setOptionalInt64(1234567890123456789
L);
96 builder.setOptionalUint32(5678);
97 builder.setOptionalUint64(2345678901234567890
L);
98 builder.setOptionalSint32(9012);
99 builder.setOptionalSint64(3456789012345678901
L);
100 builder.setOptionalFixed32(3456);
101 builder.setOptionalFixed64(4567890123456789012
L);
102 builder.setOptionalSfixed32(7890);
103 builder.setOptionalSfixed64(5678901234567890123
L);
104 builder.setOptionalFloat(1.5f);
105 builder.setOptionalDouble(1.25);
107 builder.setOptionalString(
"Hello world!");
109 builder.setOptionalNestedEnum(NestedEnum.BAR);
110 builder.getOptionalNestedMessageBuilder().setValue(100);
112 builder.addRepeatedInt32(1234);
113 builder.addRepeatedInt64(1234567890123456789
L);
114 builder.addRepeatedUint32(5678);
115 builder.addRepeatedUint64(2345678901234567890
L);
116 builder.addRepeatedSint32(9012);
117 builder.addRepeatedSint64(3456789012345678901
L);
118 builder.addRepeatedFixed32(3456);
119 builder.addRepeatedFixed64(4567890123456789012
L);
120 builder.addRepeatedSfixed32(7890);
121 builder.addRepeatedSfixed64(5678901234567890123
L);
122 builder.addRepeatedFloat(1.5f);
123 builder.addRepeatedDouble(1.25);
125 builder.addRepeatedString(
"Hello world!");
127 builder.addRepeatedNestedEnum(NestedEnum.BAR);
128 builder.addRepeatedNestedMessageBuilder().setValue(100);
131 builder.addRepeatedInt64(234567890123456789
L);
132 builder.addRepeatedUint32(678);
133 builder.addRepeatedUint64(345678901234567890
L);
134 builder.addRepeatedSint32(012);
135 builder.addRepeatedSint64(456789012345678901
L);
136 builder.addRepeatedFixed32(456);
137 builder.addRepeatedFixed64(567890123456789012
L);
138 builder.addRepeatedSfixed32(890);
139 builder.addRepeatedSfixed64(678901234567890123
L);
140 builder.addRepeatedFloat(11.5f);
141 builder.addRepeatedDouble(11.25);
143 builder.addRepeatedString(
"ello world!");
145 builder.addRepeatedNestedEnum(NestedEnum.BAZ);
146 builder.addRepeatedNestedMessageBuilder().setValue(200);
193 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
197 assertThat(toJsonString(
message))
200 +
" \"optionalInt32\": 1234,\n"
201 +
" \"optionalInt64\": \"1234567890123456789\",\n"
202 +
" \"optionalUint32\": 5678,\n"
203 +
" \"optionalUint64\": \"2345678901234567890\",\n"
204 +
" \"optionalSint32\": 9012,\n"
205 +
" \"optionalSint64\": \"3456789012345678901\",\n"
206 +
" \"optionalFixed32\": 3456,\n"
207 +
" \"optionalFixed64\": \"4567890123456789012\",\n"
208 +
" \"optionalSfixed32\": 7890,\n"
209 +
" \"optionalSfixed64\": \"5678901234567890123\",\n"
210 +
" \"optionalFloat\": 1.5,\n"
211 +
" \"optionalDouble\": 1.25,\n"
212 +
" \"optionalBool\": true,\n"
213 +
" \"optionalString\": \"Hello world!\",\n"
214 +
" \"optionalBytes\": \"AAEC\",\n"
215 +
" \"optionalNestedMessage\": {\n"
216 +
" \"value\": 100\n"
218 +
" \"optionalNestedEnum\": \"BAR\",\n"
219 +
" \"repeatedInt32\": [1234, 234],\n"
220 +
" \"repeatedInt64\": [\"1234567890123456789\", \"234567890123456789\"],\n"
221 +
" \"repeatedUint32\": [5678, 678],\n"
222 +
" \"repeatedUint64\": [\"2345678901234567890\", \"345678901234567890\"],\n"
223 +
" \"repeatedSint32\": [9012, 10],\n"
224 +
" \"repeatedSint64\": [\"3456789012345678901\", \"456789012345678901\"],\n"
225 +
" \"repeatedFixed32\": [3456, 456],\n"
226 +
" \"repeatedFixed64\": [\"4567890123456789012\", \"567890123456789012\"],\n"
227 +
" \"repeatedSfixed32\": [7890, 890],\n"
228 +
" \"repeatedSfixed64\": [\"5678901234567890123\", \"678901234567890123\"],\n"
229 +
" \"repeatedFloat\": [1.5, 11.5],\n"
230 +
" \"repeatedDouble\": [1.25, 11.25],\n"
231 +
" \"repeatedBool\": [true, true],\n"
232 +
" \"repeatedString\": [\"Hello world!\", \"ello world!\"],\n"
233 +
" \"repeatedBytes\": [\"AAEC\", \"AQI=\"],\n"
234 +
" \"repeatedNestedMessage\": [{\n"
235 +
" \"value\": 100\n"
237 +
" \"value\": 200\n"
239 +
" \"repeatedNestedEnum\": [\"BAR\", \"BAZ\"]\n"
242 assertRoundTripEquals(
message);
248 TestAllTypes.newBuilder()
249 .setOptionalNestedEnumValue(12345)
250 .addRepeatedNestedEnumValue(12345)
251 .addRepeatedNestedEnumValue(0)
253 assertThat(toJsonString(
message))
256 +
" \"optionalNestedEnum\": 12345,\n"
257 +
" \"repeatedNestedEnum\": [12345, \"FOO\"]\n"
259 assertRoundTripEquals(
message);
261 TestMap.Builder mapBuilder = TestMap.newBuilder();
262 mapBuilder.putInt32ToEnumMapValue(1, 0);
263 mapBuilder.putInt32ToEnumMapValue(2, 12345);
264 TestMap mapMessage = mapBuilder.build();
265 assertThat(toJsonString(mapMessage))
268 +
" \"int32ToEnumMap\": {\n"
269 +
" \"1\": \"FOO\",\n"
273 assertRoundTripEquals(mapMessage);
279 TestAllTypes.newBuilder()
280 .addRepeatedFloat(
Float.NaN)
281 .addRepeatedFloat(
Float.POSITIVE_INFINITY)
282 .addRepeatedFloat(
Float.NEGATIVE_INFINITY)
283 .addRepeatedDouble(
Double.NaN)
284 .addRepeatedDouble(
Double.POSITIVE_INFINITY)
285 .addRepeatedDouble(
Double.NEGATIVE_INFINITY)
287 assertThat(toJsonString(
message))
290 +
" \"repeatedFloat\": [\"NaN\", \"Infinity\", \"-Infinity\"],\n"
291 +
" \"repeatedDouble\": [\"NaN\", \"Infinity\", \"-Infinity\"]\n"
294 assertRoundTripEquals(
message);
299 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
302 +
" \"optionalInt32\": \"1234\",\n"
303 +
" \"optionalUint32\": \"5678\",\n"
304 +
" \"optionalSint32\": \"9012\",\n"
305 +
" \"optionalFixed32\": \"3456\",\n"
306 +
" \"optionalSfixed32\": \"7890\",\n"
307 +
" \"optionalFloat\": \"1.5\",\n"
308 +
" \"optionalDouble\": \"1.25\",\n"
309 +
" \"optionalBool\": \"true\"\n"
313 assertThat(
message.getOptionalInt32()).isEqualTo(1234);
314 assertThat(
message.getOptionalUint32()).isEqualTo(5678);
315 assertThat(
message.getOptionalSint32()).isEqualTo(9012);
316 assertThat(
message.getOptionalFixed32()).isEqualTo(3456);
317 assertThat(
message.getOptionalSfixed32()).isEqualTo(7890);
318 assertThat(
message.getOptionalFloat()).isEqualTo(1.5f);
319 assertThat(
message.getOptionalDouble()).isEqualTo(1.25);
320 assertThat(
message.getOptionalBool()).isTrue();
326 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
329 +
" \"repeatedInt32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
330 +
" \"repeatedUint32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
331 +
" \"repeatedInt64\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
332 +
" \"repeatedUint64\": [1.000, 1e5, \"1.000\", \"1e5\"]\n"
335 int[] expectedValues =
new int[] {1, 100000, 1, 100000};
336 assertThat(
builder.getRepeatedInt32Count()).isEqualTo(4);
337 assertThat(
builder.getRepeatedUint32Count()).isEqualTo(4);
338 assertThat(
builder.getRepeatedInt64Count()).isEqualTo(4);
339 assertThat(
builder.getRepeatedUint64Count()).isEqualTo(4);
340 for (
int i = 0;
i < 4; ++
i) {
341 assertThat(
builder.getRepeatedInt32(
i)).isEqualTo(expectedValues[
i]);
342 assertThat(
builder.getRepeatedUint32(
i)).isEqualTo(expectedValues[
i]);
343 assertThat(
builder.getRepeatedInt64(
i)).isEqualTo(expectedValues[
i]);
344 assertThat(
builder.getRepeatedUint64(
i)).isEqualTo(expectedValues[
i]);
348 assertRejects(
"optionalInt32",
"1.5");
349 assertRejects(
"optionalUint32",
"1.5");
350 assertRejects(
"optionalInt64",
"1.5");
351 assertRejects(
"optionalUint64",
"1.5");
355 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
359 assertWithMessage(
"Exception is expected.").fail();
360 }
catch (IOException e) {
366 assertWithMessage(
"Exception is expected.").fail();
367 }
catch (IOException e) {
373 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
382 assertAccepts(
"optionalInt32", String.valueOf(Integer.MAX_VALUE));
383 assertAccepts(
"optionalInt32", String.valueOf(Integer.MIN_VALUE));
384 assertRejects(
"optionalInt32", String.valueOf(Integer.MAX_VALUE + 1
L));
385 assertRejects(
"optionalInt32", String.valueOf(Integer.MIN_VALUE - 1
L));
387 assertAccepts(
"optionalUint32", String.valueOf(Integer.MAX_VALUE + 1
L));
388 assertRejects(
"optionalUint32",
"123456789012345");
389 assertRejects(
"optionalUint32",
"-1");
391 BigInteger one = BigInteger.ONE;
392 BigInteger maxLong =
new BigInteger(String.valueOf(Long.MAX_VALUE));
393 BigInteger minLong =
new BigInteger(String.valueOf(Long.MIN_VALUE));
394 assertAccepts(
"optionalInt64", maxLong.toString());
395 assertAccepts(
"optionalInt64", minLong.toString());
396 assertRejects(
"optionalInt64", maxLong.add(one).toString());
397 assertRejects(
"optionalInt64", minLong.subtract(one).toString());
399 assertAccepts(
"optionalUint64", maxLong.add(one).toString());
400 assertRejects(
"optionalUint64",
"1234567890123456789012345");
401 assertRejects(
"optionalUint64",
"-1");
403 assertAccepts(
"optionalBool",
"true");
404 assertRejects(
"optionalBool",
"1");
405 assertRejects(
"optionalBool",
"0");
407 assertAccepts(
"optionalFloat", String.valueOf(
Float.MAX_VALUE));
408 assertAccepts(
"optionalFloat", String.valueOf(-
Float.MAX_VALUE));
409 assertRejects(
"optionalFloat", String.valueOf(
Double.MAX_VALUE));
410 assertRejects(
"optionalFloat", String.valueOf(-
Double.MAX_VALUE));
412 BigDecimal moreThanOne =
new BigDecimal(
"1.000001");
413 BigDecimal maxDouble =
new BigDecimal(
Double.MAX_VALUE);
414 BigDecimal minDouble =
new BigDecimal(-
Double.MAX_VALUE);
415 assertAccepts(
"optionalDouble", maxDouble.toString());
416 assertAccepts(
"optionalDouble", minDouble.toString());
417 assertRejects(
"optionalDouble", maxDouble.multiply(moreThanOne).toString());
418 assertRejects(
"optionalDouble", minDouble.multiply(moreThanOne).toString());
423 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
426 +
" \"optionalInt32\": null,\n"
427 +
" \"optionalInt64\": null,\n"
428 +
" \"optionalUint32\": null,\n"
429 +
" \"optionalUint64\": null,\n"
430 +
" \"optionalSint32\": null,\n"
431 +
" \"optionalSint64\": null,\n"
432 +
" \"optionalFixed32\": null,\n"
433 +
" \"optionalFixed64\": null,\n"
434 +
" \"optionalSfixed32\": null,\n"
435 +
" \"optionalSfixed64\": null,\n"
436 +
" \"optionalFloat\": null,\n"
437 +
" \"optionalDouble\": null,\n"
438 +
" \"optionalBool\": null,\n"
439 +
" \"optionalString\": null,\n"
440 +
" \"optionalBytes\": null,\n"
441 +
" \"optionalNestedMessage\": null,\n"
442 +
" \"optionalNestedEnum\": null,\n"
443 +
" \"repeatedInt32\": null,\n"
444 +
" \"repeatedInt64\": null,\n"
445 +
" \"repeatedUint32\": null,\n"
446 +
" \"repeatedUint64\": null,\n"
447 +
" \"repeatedSint32\": null,\n"
448 +
" \"repeatedSint64\": null,\n"
449 +
" \"repeatedFixed32\": null,\n"
450 +
" \"repeatedFixed64\": null,\n"
451 +
" \"repeatedSfixed32\": null,\n"
452 +
" \"repeatedSfixed64\": null,\n"
453 +
" \"repeatedFloat\": null,\n"
454 +
" \"repeatedDouble\": null,\n"
455 +
" \"repeatedBool\": null,\n"
456 +
" \"repeatedString\": null,\n"
457 +
" \"repeatedBytes\": null,\n"
458 +
" \"repeatedNestedMessage\": null,\n"
459 +
" \"repeatedNestedEnum\": null\n"
463 assertThat(
message).isEqualTo(TestAllTypes.getDefaultInstance());
467 builder = TestAllTypes.newBuilder();
468 mergeFromJson(
"{\n" +
" \"repeatedInt32\": [null, null],\n" +
"}",
builder);
469 assertWithMessage(
"expected exception").fail();
475 builder = TestAllTypes.newBuilder();
476 mergeFromJson(
"{\n" +
" \"repeatedNestedMessage\": [null, null],\n" +
"}",
builder);
477 assertWithMessage(
"expected exception").fail();
485 TestOneof.Builder
builder = TestOneof.newBuilder();
486 mergeFromJson(
"{\n" +
" \"oneofNullValue\": null \n" +
"}",
builder);
488 assertThat(
message.getOneofFieldCase()).isEqualTo(TestOneof.OneofFieldCase.ONEOF_NULL_VALUE);
494 TestOneof.Builder
builder = TestOneof.newBuilder();
495 mergeFromJson(
"{\"oneofNestedMessage\": null, \"oneofInt32\": 1}",
builder);
497 assertThat(
message.getOneofInt32()).isEqualTo(1);
502 TestOneof.Builder
builder = TestOneof.newBuilder();
503 mergeFromJson(
"{\"oneofInt32\": 1, \"oneofNestedMessage\": null}",
builder);
505 assertThat(
message.getOneofInt32()).isEqualTo(1);
517 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
520 +
" \"optionalNestedMessage\": {},\n"
521 +
" \"optional_nested_message\": {}\n"
524 assertWithMessage(
"expected exception").fail();
531 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
534 +
" \"repeatedInt32\": [1, 2],\n"
535 +
" \"repeated_int32\": [5, 6]\n"
538 assertWithMessage(
"expected exception").fail();
545 TestOneof.Builder
builder = TestOneof.newBuilder();
546 mergeFromJson(
"{\n" +
" \"oneofInt32\": 1,\n" +
" \"oneof_int32\": 2\n" +
"}",
builder);
547 assertWithMessage(
"expected exception").fail();
554 TestOneof.Builder
builder = TestOneof.newBuilder();
556 "{\n" +
" \"oneofInt32\": 1,\n" +
" \"oneofNullValue\": null\n" +
"}",
builder);
557 assertWithMessage(
"expected exception").fail();
565 TestMap.Builder
builder = TestMap.newBuilder();
566 builder.putInt32ToInt32Map(1, 10);
567 builder.putInt64ToInt32Map(1234567890123456789
L, 10);
568 builder.putUint32ToInt32Map(2, 20);
569 builder.putUint64ToInt32Map(2234567890123456789
L, 20);
570 builder.putSint32ToInt32Map(3, 30);
571 builder.putSint64ToInt32Map(3234567890123456789
L, 30);
572 builder.putFixed32ToInt32Map(4, 40);
573 builder.putFixed64ToInt32Map(4234567890123456789
L, 40);
574 builder.putSfixed32ToInt32Map(5, 50);
575 builder.putSfixed64ToInt32Map(5234567890123456789
L, 50);
576 builder.putBoolToInt32Map(
false, 6);
577 builder.putStringToInt32Map(
"Hello", 10);
579 builder.putInt32ToInt64Map(1, 1234567890123456789
L);
580 builder.putInt32ToUint32Map(2, 20);
581 builder.putInt32ToUint64Map(2, 2234567890123456789
L);
582 builder.putInt32ToSint32Map(3, 30);
583 builder.putInt32ToSint64Map(3, 3234567890123456789
L);
584 builder.putInt32ToFixed32Map(4, 40);
585 builder.putInt32ToFixed64Map(4, 4234567890123456789
L);
586 builder.putInt32ToSfixed32Map(5, 50);
587 builder.putInt32ToSfixed64Map(5, 5234567890123456789
L);
588 builder.putInt32ToFloatMap(6, 1.5f);
589 builder.putInt32ToDoubleMap(6, 1.25);
590 builder.putInt32ToBoolMap(7,
false);
591 builder.putInt32ToStringMap(7,
"World");
593 builder.putInt32ToMessageMap(8, NestedMessage.newBuilder().setValue(1234).build());
594 builder.putInt32ToEnumMap(9, NestedEnum.BAR);
597 assertThat(toJsonString(
message))
600 +
" \"int32ToInt32Map\": {\n"
603 +
" \"int64ToInt32Map\": {\n"
604 +
" \"1234567890123456789\": 10\n"
606 +
" \"uint32ToInt32Map\": {\n"
609 +
" \"uint64ToInt32Map\": {\n"
610 +
" \"2234567890123456789\": 20\n"
612 +
" \"sint32ToInt32Map\": {\n"
615 +
" \"sint64ToInt32Map\": {\n"
616 +
" \"3234567890123456789\": 30\n"
618 +
" \"fixed32ToInt32Map\": {\n"
621 +
" \"fixed64ToInt32Map\": {\n"
622 +
" \"4234567890123456789\": 40\n"
624 +
" \"sfixed32ToInt32Map\": {\n"
627 +
" \"sfixed64ToInt32Map\": {\n"
628 +
" \"5234567890123456789\": 50\n"
630 +
" \"boolToInt32Map\": {\n"
633 +
" \"stringToInt32Map\": {\n"
636 +
" \"int32ToInt64Map\": {\n"
637 +
" \"1\": \"1234567890123456789\"\n"
639 +
" \"int32ToUint32Map\": {\n"
642 +
" \"int32ToUint64Map\": {\n"
643 +
" \"2\": \"2234567890123456789\"\n"
645 +
" \"int32ToSint32Map\": {\n"
648 +
" \"int32ToSint64Map\": {\n"
649 +
" \"3\": \"3234567890123456789\"\n"
651 +
" \"int32ToFixed32Map\": {\n"
654 +
" \"int32ToFixed64Map\": {\n"
655 +
" \"4\": \"4234567890123456789\"\n"
657 +
" \"int32ToSfixed32Map\": {\n"
660 +
" \"int32ToSfixed64Map\": {\n"
661 +
" \"5\": \"5234567890123456789\"\n"
663 +
" \"int32ToFloatMap\": {\n"
666 +
" \"int32ToDoubleMap\": {\n"
669 +
" \"int32ToBoolMap\": {\n"
672 +
" \"int32ToStringMap\": {\n"
673 +
" \"7\": \"World\"\n"
675 +
" \"int32ToBytesMap\": {\n"
676 +
" \"8\": \"AQID\"\n"
678 +
" \"int32ToMessageMap\": {\n"
680 +
" \"value\": 1234\n"
683 +
" \"int32ToEnumMap\": {\n"
684 +
" \"9\": \"BAR\"\n"
687 assertRoundTripEquals(
message);
690 builder = TestMap.newBuilder();
691 builder.putInt32ToInt32Map(1, 2);
692 builder.putInt32ToInt32Map(3, 4);
695 assertThat(toJsonString(
message))
698 +
" \"int32ToInt32Map\": {\n"
703 assertRoundTripEquals(
message);
709 TestMap.Builder
builder = TestMap.newBuilder();
712 +
" \"int32ToInt32Map\": {null: 1},\n"
713 +
" \"int32ToMessageMap\": {null: 2}\n"
716 assertWithMessage(
"expected exception").fail();
722 TestMap.Builder
builder = TestMap.newBuilder();
725 +
" \"int32ToInt32Map\": {\"1\": null},\n"
726 +
" \"int32ToMessageMap\": {\"2\": null}\n"
729 assertWithMessage(
"expected exception").fail();
738 TestMap.Builder
builder = TestMap.newBuilder();
739 mergeFromJsonIgnoringUnknownFields(
740 "{\n" +
" \"int32ToEnumMap\": {\"1\": null}\n" +
"}",
builder);
742 assertThat(
map.getInt32ToEnumMapMap()).isEmpty();
747 TestMap.Builder
builder = TestMap.newBuilder();
749 "{\n" +
" int32ToInt32Map: {1: 2},\n" +
" stringToInt32Map: {hello: 3}\n" +
"}",
builder);
751 assertThat(
message.getInt32ToInt32MapMap().get(1).intValue()).isEqualTo(2);
752 assertThat(
message.getStringToInt32MapMap().get(
"hello").intValue()).isEqualTo(3);
757 TestWrappers.Builder
builder = TestWrappers.newBuilder();
758 builder.getBoolValueBuilder().setValue(
false);
759 builder.getInt32ValueBuilder().setValue(0);
760 builder.getInt64ValueBuilder().setValue(0);
761 builder.getUint32ValueBuilder().setValue(0);
762 builder.getUint64ValueBuilder().setValue(0);
763 builder.getFloatValueBuilder().setValue(0.0f);
764 builder.getDoubleValueBuilder().setValue(0.0);
765 builder.getStringValueBuilder().setValue(
"");
769 assertThat(toJsonString(
message))
772 +
" \"int32Value\": 0,\n"
773 +
" \"uint32Value\": 0,\n"
774 +
" \"int64Value\": \"0\",\n"
775 +
" \"uint64Value\": \"0\",\n"
776 +
" \"floatValue\": 0.0,\n"
777 +
" \"doubleValue\": 0.0,\n"
778 +
" \"boolValue\": false,\n"
779 +
" \"stringValue\": \"\",\n"
780 +
" \"bytesValue\": \"\"\n"
782 assertRoundTripEquals(
message);
784 builder = TestWrappers.newBuilder();
785 builder.getBoolValueBuilder().setValue(
true);
786 builder.getInt32ValueBuilder().setValue(1);
787 builder.getInt64ValueBuilder().setValue(2);
788 builder.getUint32ValueBuilder().setValue(3);
789 builder.getUint64ValueBuilder().setValue(4);
790 builder.getFloatValueBuilder().setValue(5.0f);
791 builder.getDoubleValueBuilder().setValue(6.0);
792 builder.getStringValueBuilder().setValue(
"7");
796 assertThat(toJsonString(
message))
799 +
" \"int32Value\": 1,\n"
800 +
" \"uint32Value\": 3,\n"
801 +
" \"int64Value\": \"2\",\n"
802 +
" \"uint64Value\": \"4\",\n"
803 +
" \"floatValue\": 5.0,\n"
804 +
" \"doubleValue\": 6.0,\n"
805 +
" \"boolValue\": true,\n"
806 +
" \"stringValue\": \"7\",\n"
807 +
" \"bytesValue\": \"CA==\"\n"
809 assertRoundTripEquals(
message);
815 TestTimestamp.newBuilder()
819 assertThat(toJsonString(
message))
820 .isEqualTo(
"{\n" +
" \"timestampValue\": \"1970-01-01T00:00:00Z\"\n" +
"}");
821 assertRoundTripEquals(
message);
826 final String incorrectTimestampString =
"{\"seconds\":1800,\"nanos\":0}";
828 TestTimestamp.Builder
builder = TestTimestamp.newBuilder();
829 mergeFromJson(String.format(
"{\"timestamp_value\": %s}", incorrectTimestampString),
builder);
830 assertWithMessage(
"expected exception").fail();
835 .isEqualTo(
"Failed to parse timestamp: " + incorrectTimestampString);
842 TestDuration.newBuilder().setDurationValue(
Durations.
parse(
"12345s")).build();
844 assertThat(toJsonString(
message)).isEqualTo(
"{\n" +
" \"durationValue\": \"12345s\"\n" +
"}");
845 assertRoundTripEquals(
message);
850 final String incorrectDurationString =
"{\"seconds\":10,\"nanos\":500}";
852 TestDuration.Builder
builder = TestDuration.newBuilder();
853 mergeFromJson(String.format(
"{\"duration_value\": %s}", incorrectDurationString),
builder);
854 assertWithMessage(
"expected exception").fail();
859 .isEqualTo(
"Failed to parse duration: " + incorrectDurationString);
866 TestFieldMask.newBuilder()
870 assertThat(toJsonString(
message))
871 .isEqualTo(
"{\n" +
" \"fieldMaskValue\": \"foo.bar,baz,fooBar.baz\"\n" +
"}");
872 assertRoundTripEquals(
message);
878 TestStruct.Builder
builder = TestStruct.newBuilder();
879 Struct.Builder structBuilder =
builder.getStructValueBuilder();
880 structBuilder.putFields(
"null_value",
Value.newBuilder().setNullValueValue(0).build());
881 structBuilder.putFields(
"number_value",
Value.newBuilder().setNumberValue(1.25).
build());
882 structBuilder.putFields(
"string_value",
Value.newBuilder().setStringValue(
"hello").build());
884 subStructBuilder.putFields(
"number_value",
Value.newBuilder().setNumberValue(1234).build());
885 structBuilder.putFields(
886 "struct_value",
Value.newBuilder().setStructValue(subStructBuilder.build()).build());
888 listBuilder.addValues(
Value.newBuilder().setNumberValue(1.125).
build());
889 listBuilder.addValues(
Value.newBuilder().setNullValueValue(0).build());
890 structBuilder.putFields(
891 "list_value",
Value.newBuilder().setListValue(listBuilder.build()).build());
894 assertThat(toJsonString(
message))
897 +
" \"structValue\": {\n"
898 +
" \"null_value\": null,\n"
899 +
" \"number_value\": 1.25,\n"
900 +
" \"string_value\": \"hello\",\n"
901 +
" \"struct_value\": {\n"
902 +
" \"number_value\": 1234.0\n"
904 +
" \"list_value\": [1.125, null]\n"
907 assertRoundTripEquals(
message);
909 builder = TestStruct.newBuilder();
910 builder.setValue(
Value.newBuilder().setNullValueValue(0).build());
912 assertThat(toJsonString(
message)).isEqualTo(
"{\n" +
" \"value\": null\n" +
"}");
913 assertRoundTripEquals(
message);
915 builder = TestStruct.newBuilder();
916 listBuilder =
builder.getListValueBuilder();
917 listBuilder.addValues(
Value.newBuilder().setNumberValue(31831.125).
build());
918 listBuilder.addValues(
Value.newBuilder().setNullValueValue(0).build());
920 assertThat(toJsonString(
message))
921 .isEqualTo(
"{\n" +
" \"listValue\": [31831.125, null]\n" +
"}");
922 assertRoundTripEquals(
message);
928 TestAllTypes
content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
935 assertThat(printer.print(
message))
938 +
" \"anyValue\": {\n"
939 +
" \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
940 +
" \"optionalInt32\": 1234\n"
943 assertRoundTripEquals(
message, registry);
945 TestAny messageWithDefaultAnyValue =
946 TestAny.newBuilder().setAnyValue(
Any.getDefaultInstance()).build();
947 assertThat(printer.print(messageWithDefaultAnyValue))
948 .isEqualTo(
"{\n" +
" \"anyValue\": {}\n" +
"}");
949 assertRoundTripEquals(messageWithDefaultAnyValue, registry);
955 assertThat(printer.print(anyMessage))
958 +
" \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
960 +
" \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
961 +
" \"optionalInt32\": 1234\n"
964 assertRoundTripEquals(anyMessage, registry);
969 TestAllTypes
content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
975 assertWithMessage(
"Exception is expected.").fail();
976 }
catch (IOException e) {
984 assertThat(printer.print(
message))
987 +
" \"anyValue\": {\n"
988 +
" \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
989 +
" \"optionalInt32\": 1234\n"
992 assertRoundTripEquals(
message, registry);
994 TestAny messageWithDefaultAnyValue =
995 TestAny.newBuilder().setAnyValue(
Any.getDefaultInstance()).build();
996 assertThat(printer.print(messageWithDefaultAnyValue))
997 .isEqualTo(
"{\n" +
" \"anyValue\": {}\n" +
"}");
998 assertRoundTripEquals(messageWithDefaultAnyValue, registry);
1004 assertThat(printer.print(anyMessage))
1007 +
" \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1009 +
" \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
1010 +
" \"optionalInt32\": 1234\n"
1013 assertRoundTripEquals(anyMessage, registry);
1017 assertThat(printer.print(anyMessage))
1020 +
" \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n"
1021 +
" \"value\": 12345\n"
1023 assertRoundTripEquals(anyMessage, registry);
1025 assertThat(printer.print(anyMessage))
1028 +
" \"@type\": \"type.googleapis.com/google.protobuf.UInt32Value\",\n"
1029 +
" \"value\": 12345\n"
1031 assertRoundTripEquals(anyMessage, registry);
1033 assertThat(printer.print(anyMessage))
1036 +
" \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n"
1037 +
" \"value\": \"12345\"\n"
1039 assertRoundTripEquals(anyMessage, registry);
1040 anyMessage =
Any.pack(
UInt64Value.newBuilder().setValue(12345).build());
1041 assertThat(printer.print(anyMessage))
1044 +
" \"@type\": \"type.googleapis.com/google.protobuf.UInt64Value\",\n"
1045 +
" \"value\": \"12345\"\n"
1047 assertRoundTripEquals(anyMessage, registry);
1048 anyMessage =
Any.pack(
FloatValue.newBuilder().setValue(12345).build());
1049 assertThat(printer.print(anyMessage))
1052 +
" \"@type\": \"type.googleapis.com/google.protobuf.FloatValue\",\n"
1053 +
" \"value\": 12345.0\n"
1055 assertRoundTripEquals(anyMessage, registry);
1056 anyMessage =
Any.pack(
DoubleValue.newBuilder().setValue(12345).build());
1057 assertThat(printer.print(anyMessage))
1060 +
" \"@type\": \"type.googleapis.com/google.protobuf.DoubleValue\",\n"
1061 +
" \"value\": 12345.0\n"
1063 assertRoundTripEquals(anyMessage, registry);
1065 assertThat(printer.print(anyMessage))
1068 +
" \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
1069 +
" \"value\": true\n"
1071 assertRoundTripEquals(anyMessage, registry);
1073 assertThat(printer.print(anyMessage))
1076 +
" \"@type\": \"type.googleapis.com/google.protobuf.StringValue\",\n"
1077 +
" \"value\": \"Hello\"\n"
1079 assertRoundTripEquals(anyMessage, registry);
1081 assertThat(printer.print(anyMessage))
1084 +
" \"@type\": \"type.googleapis.com/google.protobuf.BytesValue\",\n"
1085 +
" \"value\": \"AQI=\"\n"
1087 assertRoundTripEquals(anyMessage, registry);
1091 assertThat(printer.print(anyMessage))
1094 +
" \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n"
1095 +
" \"value\": \"1969-12-31T23:59:59Z\"\n"
1097 assertRoundTripEquals(anyMessage, registry);
1101 assertThat(printer.print(anyMessage))
1104 +
" \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n"
1105 +
" \"value\": \"12345.100s\"\n"
1107 assertRoundTripEquals(anyMessage, registry);
1111 assertThat(printer.print(anyMessage))
1114 +
" \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n"
1115 +
" \"value\": \"foo.bar,baz\"\n"
1117 assertRoundTripEquals(anyMessage, registry);
1121 structBuilder.putFields(
"number",
Value.newBuilder().setNumberValue(1.125).
build());
1122 anyMessage =
Any.pack(structBuilder.build());
1123 assertThat(printer.print(anyMessage))
1126 +
" \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n"
1128 +
" \"number\": 1.125\n"
1131 assertRoundTripEquals(anyMessage, registry);
1134 Value.Builder valueBuilder =
Value.newBuilder();
1135 valueBuilder.setNumberValue(1);
1136 anyMessage =
Any.pack(valueBuilder.build());
1137 assertThat(printer.print(anyMessage))
1140 +
" \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1141 +
" \"value\": 1.0\n"
1143 assertRoundTripEquals(anyMessage, registry);
1146 anyMessage =
Any.pack(
Value.newBuilder().setNullValue(
NullValue.NULL_VALUE).build());
1147 assertThat(printer.print(anyMessage))
1150 +
" \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1151 +
" \"value\": null\n"
1153 assertRoundTripEquals(anyMessage, registry);
1162 TestAny.Builder testAny = TestAny.newBuilder();
1163 testAny.putAnyMap(
"int32_wrapper",
Any.pack(
Int32Value.of(123)));
1164 testAny.putAnyMap(
"int64_wrapper",
Any.pack(
Int64Value.of(456)));
1168 Value numberValue =
Value.newBuilder().setNumberValue(1.125).build();
1169 Struct.Builder
struct = Struct.newBuilder();
1170 struct.putFields(
"number", numberValue);
1171 testAny.putAnyMap(
"struct",
Any.pack(
struct.build()));
1176 testAny.putAnyMap(
"number_value",
Any.pack(numberValue));
1177 testAny.putAnyMap(
"any_value_number",
Any.pack(
Any.pack(numberValue)));
1178 testAny.putAnyMap(
"any_value_default",
Any.pack(
Any.getDefaultInstance()));
1179 testAny.putAnyMap(
"default",
Any.getDefaultInstance());
1181 assertThat(printer.print(testAny.build()))
1184 +
" \"anyMap\": {\n"
1185 +
" \"int32_wrapper\": {\n"
1186 +
" \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n"
1187 +
" \"value\": 123\n"
1189 +
" \"int64_wrapper\": {\n"
1190 +
" \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n"
1191 +
" \"value\": \"456\"\n"
1193 +
" \"timestamp\": {\n"
1194 +
" \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n"
1195 +
" \"value\": \"1969-12-31T23:59:59Z\"\n"
1197 +
" \"duration\": {\n"
1198 +
" \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n"
1199 +
" \"value\": \"12345.100s\"\n"
1201 +
" \"field_mask\": {\n"
1202 +
" \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n"
1203 +
" \"value\": \"foo.bar,baz\"\n"
1205 +
" \"struct\": {\n"
1206 +
" \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n"
1208 +
" \"number\": 1.125\n"
1211 +
" \"list_value\": {\n"
1212 +
" \"@type\": \"type.googleapis.com/google.protobuf.ListValue\",\n"
1213 +
" \"value\": [1.125, null]\n"
1215 +
" \"number_value\": {\n"
1216 +
" \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1217 +
" \"value\": 1.125\n"
1219 +
" \"any_value_number\": {\n"
1220 +
" \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1222 +
" \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1223 +
" \"value\": 1.125\n"
1226 +
" \"any_value_default\": {\n"
1227 +
" \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1228 +
" \"value\": {}\n"
1230 +
" \"default\": {}\n"
1233 assertRoundTripEquals(testAny.build(), registry);
1240 mergeFromJson(
"{\n" +
" \"optionalInt32\": 1234\n" +
"}",
builder);
1241 assertWithMessage(
"Exception is expected.").fail();
1242 }
catch (IOException e) {
1253 +
" \"@type\": \"type.googleapis.com/json_test.UnexpectedTypes\",\n"
1254 +
" \"optionalInt32\": 12345\n"
1257 assertWithMessage(
"Exception is expected.").fail();
1258 }
catch (IOException e) {
1266 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1267 mergeFromJson(
"{\n" +
" \"optionalInt32\": 12345,\n" +
"}",
builder);
1268 assertWithMessage(
"Exception is expected.").fail();
1269 }
catch (IOException e) {
1291 assertRejects(
"optionalBytes",
"!@#$");
1296 assertAccepts(
"optionalBytes",
"AQI");
1297 assertAccepts(
"optionalBytes",
"-_w");
1303 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1304 mergeFromJson(
"{\n" +
" \"optionalNestedEnum\": \"XXX\"\n" +
"}",
builder);
1305 assertWithMessage(
"Exception is expected.").fail();
1314 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1315 String json =
"{\n" +
" \"unknownField\": \"XXX\"\n" +
"}";
1317 assertWithMessage(
"Exception is expected.").fail();
1325 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1326 String json =
"{\n" +
" \"unknownField\": \"XXX\"\n" +
"}";
1332 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1333 String json =
"{\n" +
" \"optionalNestedEnum\": \"XXX\"\n" +
"}";
1335 assertThat(
builder.getOptionalNestedEnumValue()).isEqualTo(0);
1340 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1341 String json =
"{\n" +
" \"optionalAliasedEnum\": \"QUX\"\n" +
"}";
1343 assertThat(
builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
1345 builder = TestAllTypes.newBuilder();
1346 json =
"{\n" +
" \"optionalAliasedEnum\": \"qux\"\n" +
"}";
1348 assertThat(
builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
1350 builder = TestAllTypes.newBuilder();
1351 json =
"{\n" +
" \"optionalAliasedEnum\": \"bAz\"\n" +
"}";
1353 assertThat(
builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
1358 TestMap.Builder
builder = TestMap.newBuilder();
1361 .merge(
"{\n" +
" \"int32ToEnumMap\": {1: XXX, 2: FOO}" +
"}",
builder);
1363 assertThat(
builder.getInt32ToEnumMapMap()).containsEntry(2, NestedEnum.FOO);
1364 assertThat(
builder.getInt32ToEnumMapMap()).hasSize(1);
1369 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1372 .merge(
"{\n" +
" \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]" +
"}",
builder);
1374 assertThat(
builder.getRepeatedNestedEnum(0)).isEqualTo(NestedEnum.FOO);
1375 assertThat(
builder.getRepeatedNestedEnum(1)).isEqualTo(NestedEnum.BAR);
1376 assertThat(
builder.getRepeatedNestedEnum(2)).isEqualTo(NestedEnum.BAZ);
1377 assertThat(
builder.getRepeatedNestedEnumList()).hasSize(3);
1382 TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder();
1383 mergeFromJson(
"{\n" +
" \"optionalNestedEnum\": 2\n" +
"}", actualBuilder);
1385 TestAllTypes expected = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.BAZ).build();
1386 assertThat(actualBuilder.build()).isEqualTo(expected);
1391 TestCustomJsonName
message = TestCustomJsonName.newBuilder().setValue(12345).build();
1393 .isEqualTo(
"{\n" +
" \"@value\": 12345\n" +
"}");
1394 assertRoundTripEquals(
message);
1400 TestAllTypes
message = TestAllTypes.newBuilder().setOptionalString(
"</script>").build();
1401 assertThat(toJsonString(
message))
1402 .isEqualTo(
"{\n \"optionalString\": \"\\u003c/script\\u003e\"\n}");
1404 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1406 assertThat(
builder.getOptionalString()).isEqualTo(
message.getOptionalString());
1411 TestAllTypes
message = TestAllTypes.getDefaultInstance();
1416 +
" \"optionalInt32\": 0,\n"
1417 +
" \"optionalInt64\": \"0\",\n"
1418 +
" \"optionalUint32\": 0,\n"
1419 +
" \"optionalUint64\": \"0\",\n"
1420 +
" \"optionalSint32\": 0,\n"
1421 +
" \"optionalSint64\": \"0\",\n"
1422 +
" \"optionalFixed32\": 0,\n"
1423 +
" \"optionalFixed64\": \"0\",\n"
1424 +
" \"optionalSfixed32\": 0,\n"
1425 +
" \"optionalSfixed64\": \"0\",\n"
1426 +
" \"optionalFloat\": 0.0,\n"
1427 +
" \"optionalDouble\": 0.0,\n"
1428 +
" \"optionalBool\": false,\n"
1429 +
" \"optionalString\": \"\",\n"
1430 +
" \"optionalBytes\": \"\",\n"
1431 +
" \"optionalNestedEnum\": \"FOO\",\n"
1432 +
" \"repeatedInt32\": [],\n"
1433 +
" \"repeatedInt64\": [],\n"
1434 +
" \"repeatedUint32\": [],\n"
1435 +
" \"repeatedUint64\": [],\n"
1436 +
" \"repeatedSint32\": [],\n"
1437 +
" \"repeatedSint64\": [],\n"
1438 +
" \"repeatedFixed32\": [],\n"
1439 +
" \"repeatedFixed64\": [],\n"
1440 +
" \"repeatedSfixed32\": [],\n"
1441 +
" \"repeatedSfixed64\": [],\n"
1442 +
" \"repeatedFloat\": [],\n"
1443 +
" \"repeatedDouble\": [],\n"
1444 +
" \"repeatedBool\": [],\n"
1445 +
" \"repeatedString\": [],\n"
1446 +
" \"repeatedBytes\": [],\n"
1447 +
" \"repeatedNestedMessage\": [],\n"
1448 +
" \"repeatedNestedEnum\": [],\n"
1449 +
" \"optionalAliasedEnum\": \"ALIAS_FOO\"\n"
1452 Set<FieldDescriptor> fixedFields =
new HashSet<>();
1453 for (
FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1454 if (fieldDesc.getName().contains(
"_fixed")) {
1455 fixedFields.add(fieldDesc);
1462 +
" \"optionalFixed32\": 0,\n"
1463 +
" \"optionalFixed64\": \"0\",\n"
1464 +
" \"repeatedFixed32\": [],\n"
1465 +
" \"repeatedFixed64\": []\n"
1468 TestAllTypes messageNonDefaults =
1469 message.toBuilder().setOptionalInt64(1234).setOptionalFixed32(3232).build();
1474 +
" \"optionalInt64\": \"1234\",\n"
1475 +
" \"optionalFixed32\": 3232,\n"
1476 +
" \"optionalFixed64\": \"0\",\n"
1477 +
" \"repeatedFixed32\": [],\n"
1478 +
" \"repeatedFixed64\": []\n"
1483 assertWithMessage(
"IllegalStateException is expected.").fail();
1484 }
catch (IllegalStateException e) {
1486 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1487 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1493 assertWithMessage(
"IllegalStateException is expected.").fail();
1494 }
catch (IllegalStateException e) {
1496 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1497 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1503 assertWithMessage(
"IllegalStateException is expected.").fail();
1504 }
catch (IllegalStateException e) {
1506 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1507 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1514 .includingDefaultValueFields(fixedFields);
1515 assertWithMessage(
"IllegalStateException is expected.").fail();
1516 }
catch (IllegalStateException e) {
1518 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1519 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1523 Set<FieldDescriptor> intFields =
new HashSet<>();
1524 for (
FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1525 if (fieldDesc.getName().contains(
"_int")) {
1526 intFields.add(fieldDesc);
1533 .includingDefaultValueFields(fixedFields);
1534 assertWithMessage(
"IllegalStateException is expected.").fail();
1535 }
catch (IllegalStateException e) {
1537 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1538 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1544 assertWithMessage(
"IllegalArgumentException is expected.").fail();
1545 }
catch (IllegalArgumentException e) {
1547 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1548 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1554 assertWithMessage(
"IllegalArgumentException is expected.").fail();
1555 }
catch (IllegalArgumentException e) {
1557 assertWithMessage(
"Exception message should mention includingDefaultValueFields.")
1558 .that(e.getMessage().contains(
"includingDefaultValueFields"))
1562 TestMap mapMessage = TestMap.getDefaultInstance();
1567 +
" \"int32ToInt32Map\": {\n"
1569 +
" \"int64ToInt32Map\": {\n"
1571 +
" \"uint32ToInt32Map\": {\n"
1573 +
" \"uint64ToInt32Map\": {\n"
1575 +
" \"sint32ToInt32Map\": {\n"
1577 +
" \"sint64ToInt32Map\": {\n"
1579 +
" \"fixed32ToInt32Map\": {\n"
1581 +
" \"fixed64ToInt32Map\": {\n"
1583 +
" \"sfixed32ToInt32Map\": {\n"
1585 +
" \"sfixed64ToInt32Map\": {\n"
1587 +
" \"boolToInt32Map\": {\n"
1589 +
" \"stringToInt32Map\": {\n"
1591 +
" \"int32ToInt64Map\": {\n"
1593 +
" \"int32ToUint32Map\": {\n"
1595 +
" \"int32ToUint64Map\": {\n"
1597 +
" \"int32ToSint32Map\": {\n"
1599 +
" \"int32ToSint64Map\": {\n"
1601 +
" \"int32ToFixed32Map\": {\n"
1603 +
" \"int32ToFixed64Map\": {\n"
1605 +
" \"int32ToSfixed32Map\": {\n"
1607 +
" \"int32ToSfixed64Map\": {\n"
1609 +
" \"int32ToFloatMap\": {\n"
1611 +
" \"int32ToDoubleMap\": {\n"
1613 +
" \"int32ToBoolMap\": {\n"
1615 +
" \"int32ToStringMap\": {\n"
1617 +
" \"int32ToBytesMap\": {\n"
1619 +
" \"int32ToMessageMap\": {\n"
1621 +
" \"int32ToEnumMap\": {\n"
1625 TestOneof oneofMessage = TestOneof.getDefaultInstance();
1630 oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
1633 .isEqualTo(
"{\n \"oneofInt32\": 42\n}");
1635 TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
1636 mergeFromJson(
"{\n" +
" \"oneofNullValue\": null \n" +
"}", oneofBuilder);
1637 oneofMessage = oneofBuilder.build();
1639 .isEqualTo(
"{\n \"oneofNullValue\": null\n}");
1641 .isEqualTo(
"{\n \"oneofNullValue\": null\n}");
1646 TestAllTypes
message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
1648 .isEqualTo(
"{\n" +
" \"optionalInt32\": 12345\n" +
"}");
1650 .isEqualTo(
"{\n" +
" \"optional_int32\": 12345\n" +
"}");
1653 TestCustomJsonName messageWithCustomJsonName =
1654 TestCustomJsonName.newBuilder().setValue(12345).build();
1656 .isEqualTo(
"{\n" +
" \"value\": 12345\n" +
"}");
1659 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1661 assertThat(
builder.getOptionalInt32()).isEqualTo(12345);
1664 assertThat(
builder.getOptionalInt32()).isEqualTo(54321);
1669 TestAllTypes
message = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.BAR).build();
1671 .isEqualTo(
"{\n" +
" \"optionalNestedEnum\": 1\n" +
"}");
1676 TestAllTypes
message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
1678 .isEqualTo(
"{" +
"\"optionalInt32\":12345" +
"}");
1679 TestAllTypes message1 = TestAllTypes.getDefaultInstance();
1682 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1684 TestAllTypes message2 =
builder.build();
1685 assertThat(toCompactJsonString(message2))
1688 +
"\"optionalInt32\":1234,"
1689 +
"\"optionalInt64\":\"1234567890123456789\","
1690 +
"\"optionalUint32\":5678,"
1691 +
"\"optionalUint64\":\"2345678901234567890\","
1692 +
"\"optionalSint32\":9012,"
1693 +
"\"optionalSint64\":\"3456789012345678901\","
1694 +
"\"optionalFixed32\":3456,"
1695 +
"\"optionalFixed64\":\"4567890123456789012\","
1696 +
"\"optionalSfixed32\":7890,"
1697 +
"\"optionalSfixed64\":\"5678901234567890123\","
1698 +
"\"optionalFloat\":1.5,"
1699 +
"\"optionalDouble\":1.25,"
1700 +
"\"optionalBool\":true,"
1701 +
"\"optionalString\":\"Hello world!\","
1702 +
"\"optionalBytes\":\"AAEC\","
1703 +
"\"optionalNestedMessage\":{"
1706 +
"\"optionalNestedEnum\":\"BAR\","
1707 +
"\"repeatedInt32\":[1234,234],"
1708 +
"\"repeatedInt64\":[\"1234567890123456789\",\"234567890123456789\"],"
1709 +
"\"repeatedUint32\":[5678,678],"
1710 +
"\"repeatedUint64\":[\"2345678901234567890\",\"345678901234567890\"],"
1711 +
"\"repeatedSint32\":[9012,10],"
1712 +
"\"repeatedSint64\":[\"3456789012345678901\",\"456789012345678901\"],"
1713 +
"\"repeatedFixed32\":[3456,456],"
1714 +
"\"repeatedFixed64\":[\"4567890123456789012\",\"567890123456789012\"],"
1715 +
"\"repeatedSfixed32\":[7890,890],"
1716 +
"\"repeatedSfixed64\":[\"5678901234567890123\",\"678901234567890123\"],"
1717 +
"\"repeatedFloat\":[1.5,11.5],"
1718 +
"\"repeatedDouble\":[1.25,11.25],"
1719 +
"\"repeatedBool\":[true,true],"
1720 +
"\"repeatedString\":[\"Hello world!\",\"ello world!\"],"
1721 +
"\"repeatedBytes\":[\"AAEC\",\"AQI=\"],"
1722 +
"\"repeatedNestedMessage\":[{"
1727 +
"\"repeatedNestedEnum\":[\"BAR\",\"BAZ\"]"
1741 +
" \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
1742 +
" \"value\": false\n"
1746 assertThat(any.getValue().size()).isEqualTo(0);
1753 +
" \"nested\": {\n"
1754 +
" \"nested\": {\n"
1755 +
" \"nested\": {\n"
1756 +
" \"nested\": {\n"
1757 +
" \"value\": 1234\n"
1765 TestRecursive.Builder
builder = TestRecursive.newBuilder();
1768 assertThat(
message.getNested().getNested().getNested().getNested().getValue()).isEqualTo(1234);
1771 builder = TestRecursive.newBuilder();
1774 assertWithMessage(
"Exception is expected.").fail();
1783 InputStream throwingInputStream =
1786 public int read()
throws IOException {
1787 throw new IOException(
"12345");
1790 InputStreamReader throwingReader =
new InputStreamReader(throwingInputStream);
1794 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1796 assertWithMessage(
"Exception is expected.").fail();
1797 }
catch (IOException e) {
1798 assertThat(e).hasMessageThat().isEqualTo(
"12345");
1801 Reader invalidJsonReader =
new StringReader(
"{ xxx - yyy }");
1805 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1807 assertWithMessage(
"Exception is expected.").fail();
1816 TestAllTypes.Builder
builder = TestAllTypes.newBuilder();
1820 +
" \"optionalString\": {\n"
1821 +
" \"invalidNestedString\": \"Hello world\"\n"
1832 TestMap.Builder mapBuilder = TestMap.newBuilder();
1833 mapBuilder.putStringToInt32Map(
"\ud834\udd20", 3);
1834 mapBuilder.putStringToInt32Map(
"foo", 99);
1835 mapBuilder.putStringToInt32Map(
"xxx", 123);
1836 mapBuilder.putStringToInt32Map(
"\u20ac", 1);
1837 mapBuilder.putStringToInt32Map(
"abc", 20);
1838 mapBuilder.putStringToInt32Map(
"19", 19);
1839 mapBuilder.putStringToInt32Map(
"8", 8);
1840 mapBuilder.putStringToInt32Map(
"\ufb00", 2);
1841 mapBuilder.putInt32ToInt32Map(3, 3);
1842 mapBuilder.putInt32ToInt32Map(10, 10);
1843 mapBuilder.putInt32ToInt32Map(5, 5);
1844 mapBuilder.putInt32ToInt32Map(4, 4);
1845 mapBuilder.putInt32ToInt32Map(1, 1);
1846 mapBuilder.putInt32ToInt32Map(2, 2);
1847 mapBuilder.putInt32ToInt32Map(-3, -3);
1848 TestMap mapMessage = mapBuilder.build();
1849 assertThat(toSortedJsonString(mapMessage))
1852 +
" \"int32ToInt32Map\": {\n"
1861 +
" \"stringToInt32Map\": {\n"
1866 +
" \"xxx\": 123,\n"
1867 +
" \"\u20ac\": 1,\n"
1868 +
" \"\ufb00\": 2,\n"
1869 +
" \"\ud834\udd20\": 3\n"
1873 TestMap emptyMap = TestMap.getDefaultInstance();
1874 assertThat(toSortedJsonString(emptyMap)).isEqualTo(
"{\n}");
1879 TestAllTypes
message = TestAllTypes.newBuilder().setOptionalBool(
false).build();
1885 message.getDescriptorForType().findFieldByName(
"optional_bool")))
1886 .printingEnumsAsInts()
1888 .isEqualTo(
"{\n" +
" \"optionalBool\": false\n" +
"}");
1894 TestAllTypes.newBuilder().setOptionalFloat(-0.0
f).setOptionalDouble(-0.0).build();
1896 .isEqualTo(
"{\n \"optionalFloat\": -0.0,\n \"optionalDouble\": -0.0\n}");