protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf.util;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 import static com.google.common.truth.Truth.assertWithMessage;
35 
36 import com.google.common.collect.ImmutableSet;
37 import com.google.protobuf.Any;
44 import com.google.protobuf.FloatValue;
46 import com.google.protobuf.Int64Value;
49 import com.google.protobuf.Message;
50 import com.google.protobuf.NullValue;
52 import com.google.protobuf.Struct;
55 import com.google.protobuf.Value;
57 import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes;
58 import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes.AliasedEnum;
59 import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes.NestedEnum;
60 import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes.NestedMessage;
61 import com.google.protobuf.util.proto.JsonTestProto.TestAny;
62 import com.google.protobuf.util.proto.JsonTestProto.TestCustomJsonName;
63 import com.google.protobuf.util.proto.JsonTestProto.TestDuration;
64 import com.google.protobuf.util.proto.JsonTestProto.TestFieldMask;
65 import com.google.protobuf.util.proto.JsonTestProto.TestMap;
66 import com.google.protobuf.util.proto.JsonTestProto.TestOneof;
67 import com.google.protobuf.util.proto.JsonTestProto.TestRecursive;
68 import com.google.protobuf.util.proto.JsonTestProto.TestStruct;
69 import com.google.protobuf.util.proto.JsonTestProto.TestTimestamp;
70 import com.google.protobuf.util.proto.JsonTestProto.TestWrappers;
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;
81 import java.util.Set;
82 import org.junit.Test;
83 import org.junit.runner.RunWith;
84 import org.junit.runners.JUnit4;
85 
86 @RunWith(JUnit4.class)
87 public class JsonFormatTest {
88  public JsonFormatTest() {
89  // Test that locale does not affect JsonFormat.
90  Locale.setDefault(Locale.forLanguageTag("hi-IN"));
91  }
92 
93  private void setAllFields(TestAllTypes.Builder builder) {
94  builder.setOptionalInt32(1234);
95  builder.setOptionalInt64(1234567890123456789L);
96  builder.setOptionalUint32(5678);
97  builder.setOptionalUint64(2345678901234567890L);
98  builder.setOptionalSint32(9012);
99  builder.setOptionalSint64(3456789012345678901L);
100  builder.setOptionalFixed32(3456);
101  builder.setOptionalFixed64(4567890123456789012L);
102  builder.setOptionalSfixed32(7890);
103  builder.setOptionalSfixed64(5678901234567890123L);
104  builder.setOptionalFloat(1.5f);
105  builder.setOptionalDouble(1.25);
106  builder.setOptionalBool(true);
107  builder.setOptionalString("Hello world!");
108  builder.setOptionalBytes(ByteString.copyFrom(new byte[] {0, 1, 2}));
109  builder.setOptionalNestedEnum(NestedEnum.BAR);
110  builder.getOptionalNestedMessageBuilder().setValue(100);
111 
112  builder.addRepeatedInt32(1234);
113  builder.addRepeatedInt64(1234567890123456789L);
114  builder.addRepeatedUint32(5678);
115  builder.addRepeatedUint64(2345678901234567890L);
116  builder.addRepeatedSint32(9012);
117  builder.addRepeatedSint64(3456789012345678901L);
118  builder.addRepeatedFixed32(3456);
119  builder.addRepeatedFixed64(4567890123456789012L);
120  builder.addRepeatedSfixed32(7890);
121  builder.addRepeatedSfixed64(5678901234567890123L);
122  builder.addRepeatedFloat(1.5f);
123  builder.addRepeatedDouble(1.25);
124  builder.addRepeatedBool(true);
125  builder.addRepeatedString("Hello world!");
126  builder.addRepeatedBytes(ByteString.copyFrom(new byte[] {0, 1, 2}));
127  builder.addRepeatedNestedEnum(NestedEnum.BAR);
128  builder.addRepeatedNestedMessageBuilder().setValue(100);
129 
130  builder.addRepeatedInt32(234);
131  builder.addRepeatedInt64(234567890123456789L);
132  builder.addRepeatedUint32(678);
133  builder.addRepeatedUint64(345678901234567890L);
134  builder.addRepeatedSint32(012);
135  builder.addRepeatedSint64(456789012345678901L);
136  builder.addRepeatedFixed32(456);
137  builder.addRepeatedFixed64(567890123456789012L);
138  builder.addRepeatedSfixed32(890);
139  builder.addRepeatedSfixed64(678901234567890123L);
140  builder.addRepeatedFloat(11.5f);
141  builder.addRepeatedDouble(11.25);
142  builder.addRepeatedBool(true);
143  builder.addRepeatedString("ello world!");
144  builder.addRepeatedBytes(ByteString.copyFrom(new byte[] {1, 2}));
145  builder.addRepeatedNestedEnum(NestedEnum.BAZ);
146  builder.addRepeatedNestedMessageBuilder().setValue(200);
147  }
148 
149  private void assertRoundTripEquals(Message message) throws Exception {
150  assertRoundTripEquals(message, TypeRegistry.getEmptyTypeRegistry());
151  }
152 
153  private void assertRoundTripEquals(Message message, TypeRegistry registry) throws Exception {
154  JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);
156  Message.Builder builder = message.newBuilderForType();
157  parser.merge(printer.print(message), builder);
158  Message parsedMessage = builder.build();
159  assertThat(parsedMessage.toString()).isEqualTo(message.toString());
160  }
161 
163  throws Exception {
164  JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);
166  Message.Builder builder = message.newBuilderForType();
167  parser.merge(printer.print(message), builder);
168  Message parsedMessage = builder.build();
169  assertThat(parsedMessage.toString()).isEqualTo(message.toString());
170  }
171 
172  private String toJsonString(Message message) throws IOException {
173  return JsonFormat.printer().print(message);
174  }
175  private String toCompactJsonString(Message message) throws IOException {
177  }
178  private String toSortedJsonString(Message message) throws IOException {
179  return JsonFormat.printer().sortingMapKeys().print(message);
180  }
181 
182  private void mergeFromJson(String json, Message.Builder builder) throws IOException {
183  JsonFormat.parser().merge(json, builder);
184  }
185 
187  throws IOException {
189  }
190 
191  @Test
192  public void testAllFields() throws Exception {
193  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
194  setAllFields(builder);
195  TestAllTypes message = builder.build();
196 
197  assertThat(toJsonString(message))
198  .isEqualTo(
199  "{\n"
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"
217  + " },\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"
236  + " }, {\n"
237  + " \"value\": 200\n"
238  + " }],\n"
239  + " \"repeatedNestedEnum\": [\"BAR\", \"BAZ\"]\n"
240  + "}");
241 
242  assertRoundTripEquals(message);
243  }
244 
245  @Test
246  public void testUnknownEnumValues() throws Exception {
247  TestAllTypes message =
248  TestAllTypes.newBuilder()
249  .setOptionalNestedEnumValue(12345)
250  .addRepeatedNestedEnumValue(12345)
251  .addRepeatedNestedEnumValue(0)
252  .build();
253  assertThat(toJsonString(message))
254  .isEqualTo(
255  "{\n"
256  + " \"optionalNestedEnum\": 12345,\n"
257  + " \"repeatedNestedEnum\": [12345, \"FOO\"]\n"
258  + "}");
259  assertRoundTripEquals(message);
260 
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))
266  .isEqualTo(
267  "{\n"
268  + " \"int32ToEnumMap\": {\n"
269  + " \"1\": \"FOO\",\n"
270  + " \"2\": 12345\n"
271  + " }\n"
272  + "}");
273  assertRoundTripEquals(mapMessage);
274  }
275 
276  @Test
277  public void testSpecialFloatValues() throws Exception {
278  TestAllTypes message =
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)
286  .build();
287  assertThat(toJsonString(message))
288  .isEqualTo(
289  "{\n"
290  + " \"repeatedFloat\": [\"NaN\", \"Infinity\", \"-Infinity\"],\n"
291  + " \"repeatedDouble\": [\"NaN\", \"Infinity\", \"-Infinity\"]\n"
292  + "}");
293 
294  assertRoundTripEquals(message);
295  }
296 
297  @Test
298  public void testParserAcceptStringForNumericField() throws Exception {
299  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
300  mergeFromJson(
301  "{\n"
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"
310  + "}",
311  builder);
312  TestAllTypes message = builder.build();
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();
321  }
322 
323  @Test
324  public void testParserAcceptFloatingPointValueForIntegerField() throws Exception {
325  // Test that numeric values like "1.000", "1e5" will also be accepted.
326  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
327  mergeFromJson(
328  "{\n"
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"
333  + "}",
334  builder);
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]);
345  }
346 
347  // Non-integers will still be rejected.
348  assertRejects("optionalInt32", "1.5");
349  assertRejects("optionalUint32", "1.5");
350  assertRejects("optionalInt64", "1.5");
351  assertRejects("optionalUint64", "1.5");
352  }
353 
354  private void assertRejects(String name, String value) {
355  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
356  try {
357  // Numeric form is rejected.
358  mergeFromJson("{\"" + name + "\":" + value + "}", builder);
359  assertWithMessage("Exception is expected.").fail();
360  } catch (IOException e) {
361  // Expected.
362  }
363  try {
364  // String form is also rejected.
365  mergeFromJson("{\"" + name + "\":\"" + value + "\"}", builder);
366  assertWithMessage("Exception is expected.").fail();
367  } catch (IOException e) {
368  // Expected.
369  }
370  }
371 
372  private void assertAccepts(String name, String value) throws IOException {
373  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
374  // Both numeric form and string form are accepted.
375  mergeFromJson("{\"" + name + "\":" + value + "}", builder);
376  builder.clear();
377  mergeFromJson("{\"" + name + "\":\"" + value + "\"}", builder);
378  }
379 
380  @Test
381  public void testParserRejectOutOfRangeNumericValues() throws Exception {
382  assertAccepts("optionalInt32", String.valueOf(Integer.MAX_VALUE));
383  assertAccepts("optionalInt32", String.valueOf(Integer.MIN_VALUE));
384  assertRejects("optionalInt32", String.valueOf(Integer.MAX_VALUE + 1L));
385  assertRejects("optionalInt32", String.valueOf(Integer.MIN_VALUE - 1L));
386 
387  assertAccepts("optionalUint32", String.valueOf(Integer.MAX_VALUE + 1L));
388  assertRejects("optionalUint32", "123456789012345");
389  assertRejects("optionalUint32", "-1");
390 
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());
398 
399  assertAccepts("optionalUint64", maxLong.add(one).toString());
400  assertRejects("optionalUint64", "1234567890123456789012345");
401  assertRejects("optionalUint64", "-1");
402 
403  assertAccepts("optionalBool", "true");
404  assertRejects("optionalBool", "1");
405  assertRejects("optionalBool", "0");
406 
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));
411 
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());
419  }
420 
421  @Test
422  public void testParserAcceptNull() throws Exception {
423  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
424  mergeFromJson(
425  "{\n"
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"
460  + "}",
461  builder);
462  TestAllTypes message = builder.build();
463  assertThat(message).isEqualTo(TestAllTypes.getDefaultInstance());
464 
465  // Repeated field elements cannot be null.
466  try {
467  builder = TestAllTypes.newBuilder();
468  mergeFromJson("{\n" + " \"repeatedInt32\": [null, null],\n" + "}", builder);
469  assertWithMessage("expected exception").fail();
470  } catch (InvalidProtocolBufferException e) {
471  // Exception expected.
472  }
473 
474  try {
475  builder = TestAllTypes.newBuilder();
476  mergeFromJson("{\n" + " \"repeatedNestedMessage\": [null, null],\n" + "}", builder);
477  assertWithMessage("expected exception").fail();
478  } catch (InvalidProtocolBufferException e) {
479  // Exception expected.
480  }
481  }
482 
483  @Test
484  public void testNullInOneof() throws Exception {
485  TestOneof.Builder builder = TestOneof.newBuilder();
486  mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", builder);
487  TestOneof message = builder.build();
488  assertThat(message.getOneofFieldCase()).isEqualTo(TestOneof.OneofFieldCase.ONEOF_NULL_VALUE);
489  assertThat(message.getOneofNullValue()).isEqualTo(NullValue.NULL_VALUE);
490  }
491 
492  @Test
493  public void testNullFirstInDuplicateOneof() throws Exception {
494  TestOneof.Builder builder = TestOneof.newBuilder();
495  mergeFromJson("{\"oneofNestedMessage\": null, \"oneofInt32\": 1}", builder);
496  TestOneof message = builder.build();
497  assertThat(message.getOneofInt32()).isEqualTo(1);
498  }
499 
500  @Test
501  public void testNullLastInDuplicateOneof() throws Exception {
502  TestOneof.Builder builder = TestOneof.newBuilder();
503  mergeFromJson("{\"oneofInt32\": 1, \"oneofNestedMessage\": null}", builder);
504  TestOneof message = builder.build();
505  assertThat(message.getOneofInt32()).isEqualTo(1);
506  }
507 
508  @Test
509  public void testParserRejectDuplicatedFields() throws Exception {
510  // TODO(xiaofeng): The parser we are currently using (GSON) will accept and keep the last
511  // one if multiple entries have the same name. This is not the desired behavior but it can
512  // only be fixed by using our own parser. Here we only test the cases where the names are
513  // different but still referring to the same field.
514 
515  // Duplicated optional fields.
516  try {
517  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
518  mergeFromJson(
519  "{\n"
520  + " \"optionalNestedMessage\": {},\n"
521  + " \"optional_nested_message\": {}\n"
522  + "}",
523  builder);
524  assertWithMessage("expected exception").fail();
525  } catch (InvalidProtocolBufferException e) {
526  // Exception expected.
527  }
528 
529  // Duplicated repeated fields.
530  try {
531  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
532  mergeFromJson(
533  "{\n"
534  + " \"repeatedInt32\": [1, 2],\n"
535  + " \"repeated_int32\": [5, 6]\n"
536  + "}",
537  builder);
538  assertWithMessage("expected exception").fail();
539  } catch (InvalidProtocolBufferException e) {
540  // Exception expected.
541  }
542 
543  // Duplicated oneof fields, same name.
544  try {
545  TestOneof.Builder builder = TestOneof.newBuilder();
546  mergeFromJson("{\n" + " \"oneofInt32\": 1,\n" + " \"oneof_int32\": 2\n" + "}", builder);
547  assertWithMessage("expected exception").fail();
548  } catch (InvalidProtocolBufferException e) {
549  // Exception expected.
550  }
551 
552  // Duplicated oneof fields, different name.
553  try {
554  TestOneof.Builder builder = TestOneof.newBuilder();
555  mergeFromJson(
556  "{\n" + " \"oneofInt32\": 1,\n" + " \"oneofNullValue\": null\n" + "}", builder);
557  assertWithMessage("expected exception").fail();
558  } catch (InvalidProtocolBufferException e) {
559  // Exception expected.
560  }
561  }
562 
563  @Test
564  public void testMapFields() throws Exception {
565  TestMap.Builder builder = TestMap.newBuilder();
566  builder.putInt32ToInt32Map(1, 10);
567  builder.putInt64ToInt32Map(1234567890123456789L, 10);
568  builder.putUint32ToInt32Map(2, 20);
569  builder.putUint64ToInt32Map(2234567890123456789L, 20);
570  builder.putSint32ToInt32Map(3, 30);
571  builder.putSint64ToInt32Map(3234567890123456789L, 30);
572  builder.putFixed32ToInt32Map(4, 40);
573  builder.putFixed64ToInt32Map(4234567890123456789L, 40);
574  builder.putSfixed32ToInt32Map(5, 50);
575  builder.putSfixed64ToInt32Map(5234567890123456789L, 50);
576  builder.putBoolToInt32Map(false, 6);
577  builder.putStringToInt32Map("Hello", 10);
578 
579  builder.putInt32ToInt64Map(1, 1234567890123456789L);
580  builder.putInt32ToUint32Map(2, 20);
581  builder.putInt32ToUint64Map(2, 2234567890123456789L);
582  builder.putInt32ToSint32Map(3, 30);
583  builder.putInt32ToSint64Map(3, 3234567890123456789L);
584  builder.putInt32ToFixed32Map(4, 40);
585  builder.putInt32ToFixed64Map(4, 4234567890123456789L);
586  builder.putInt32ToSfixed32Map(5, 50);
587  builder.putInt32ToSfixed64Map(5, 5234567890123456789L);
588  builder.putInt32ToFloatMap(6, 1.5f);
589  builder.putInt32ToDoubleMap(6, 1.25);
590  builder.putInt32ToBoolMap(7, false);
591  builder.putInt32ToStringMap(7, "World");
592  builder.putInt32ToBytesMap(8, ByteString.copyFrom(new byte[] {1, 2, 3}));
593  builder.putInt32ToMessageMap(8, NestedMessage.newBuilder().setValue(1234).build());
594  builder.putInt32ToEnumMap(9, NestedEnum.BAR);
595  TestMap message = builder.build();
596 
597  assertThat(toJsonString(message))
598  .isEqualTo(
599  "{\n"
600  + " \"int32ToInt32Map\": {\n"
601  + " \"1\": 10\n"
602  + " },\n"
603  + " \"int64ToInt32Map\": {\n"
604  + " \"1234567890123456789\": 10\n"
605  + " },\n"
606  + " \"uint32ToInt32Map\": {\n"
607  + " \"2\": 20\n"
608  + " },\n"
609  + " \"uint64ToInt32Map\": {\n"
610  + " \"2234567890123456789\": 20\n"
611  + " },\n"
612  + " \"sint32ToInt32Map\": {\n"
613  + " \"3\": 30\n"
614  + " },\n"
615  + " \"sint64ToInt32Map\": {\n"
616  + " \"3234567890123456789\": 30\n"
617  + " },\n"
618  + " \"fixed32ToInt32Map\": {\n"
619  + " \"4\": 40\n"
620  + " },\n"
621  + " \"fixed64ToInt32Map\": {\n"
622  + " \"4234567890123456789\": 40\n"
623  + " },\n"
624  + " \"sfixed32ToInt32Map\": {\n"
625  + " \"5\": 50\n"
626  + " },\n"
627  + " \"sfixed64ToInt32Map\": {\n"
628  + " \"5234567890123456789\": 50\n"
629  + " },\n"
630  + " \"boolToInt32Map\": {\n"
631  + " \"false\": 6\n"
632  + " },\n"
633  + " \"stringToInt32Map\": {\n"
634  + " \"Hello\": 10\n"
635  + " },\n"
636  + " \"int32ToInt64Map\": {\n"
637  + " \"1\": \"1234567890123456789\"\n"
638  + " },\n"
639  + " \"int32ToUint32Map\": {\n"
640  + " \"2\": 20\n"
641  + " },\n"
642  + " \"int32ToUint64Map\": {\n"
643  + " \"2\": \"2234567890123456789\"\n"
644  + " },\n"
645  + " \"int32ToSint32Map\": {\n"
646  + " \"3\": 30\n"
647  + " },\n"
648  + " \"int32ToSint64Map\": {\n"
649  + " \"3\": \"3234567890123456789\"\n"
650  + " },\n"
651  + " \"int32ToFixed32Map\": {\n"
652  + " \"4\": 40\n"
653  + " },\n"
654  + " \"int32ToFixed64Map\": {\n"
655  + " \"4\": \"4234567890123456789\"\n"
656  + " },\n"
657  + " \"int32ToSfixed32Map\": {\n"
658  + " \"5\": 50\n"
659  + " },\n"
660  + " \"int32ToSfixed64Map\": {\n"
661  + " \"5\": \"5234567890123456789\"\n"
662  + " },\n"
663  + " \"int32ToFloatMap\": {\n"
664  + " \"6\": 1.5\n"
665  + " },\n"
666  + " \"int32ToDoubleMap\": {\n"
667  + " \"6\": 1.25\n"
668  + " },\n"
669  + " \"int32ToBoolMap\": {\n"
670  + " \"7\": false\n"
671  + " },\n"
672  + " \"int32ToStringMap\": {\n"
673  + " \"7\": \"World\"\n"
674  + " },\n"
675  + " \"int32ToBytesMap\": {\n"
676  + " \"8\": \"AQID\"\n"
677  + " },\n"
678  + " \"int32ToMessageMap\": {\n"
679  + " \"8\": {\n"
680  + " \"value\": 1234\n"
681  + " }\n"
682  + " },\n"
683  + " \"int32ToEnumMap\": {\n"
684  + " \"9\": \"BAR\"\n"
685  + " }\n"
686  + "}");
687  assertRoundTripEquals(message);
688 
689  // Test multiple entries.
690  builder = TestMap.newBuilder();
691  builder.putInt32ToInt32Map(1, 2);
692  builder.putInt32ToInt32Map(3, 4);
693  message = builder.build();
694 
695  assertThat(toJsonString(message))
696  .isEqualTo(
697  "{\n"
698  + " \"int32ToInt32Map\": {\n"
699  + " \"1\": 2,\n"
700  + " \"3\": 4\n"
701  + " }\n"
702  + "}");
703  assertRoundTripEquals(message);
704  }
705 
706  @Test
707  public void testMapNullValueIsRejected() throws Exception {
708  try {
709  TestMap.Builder builder = TestMap.newBuilder();
710  mergeFromJson(
711  "{\n"
712  + " \"int32ToInt32Map\": {null: 1},\n"
713  + " \"int32ToMessageMap\": {null: 2}\n"
714  + "}",
715  builder);
716  assertWithMessage("expected exception").fail();
717  } catch (InvalidProtocolBufferException e) {
718  // Exception expected.
719  }
720 
721  try {
722  TestMap.Builder builder = TestMap.newBuilder();
723  mergeFromJson(
724  "{\n"
725  + " \"int32ToInt32Map\": {\"1\": null},\n"
726  + " \"int32ToMessageMap\": {\"2\": null}\n"
727  + "}",
728  builder);
729  assertWithMessage("expected exception").fail();
730 
731  } catch (InvalidProtocolBufferException e) {
732  // Exception expected.
733  }
734  }
735 
736  @Test
737  public void testMapEnumNullValueIsIgnored() throws Exception {
738  TestMap.Builder builder = TestMap.newBuilder();
739  mergeFromJsonIgnoringUnknownFields(
740  "{\n" + " \"int32ToEnumMap\": {\"1\": null}\n" + "}", builder);
741  TestMap map = builder.build();
742  assertThat(map.getInt32ToEnumMapMap()).isEmpty();
743  }
744 
745  @Test
746  public void testParserAcceptNonQuotedObjectKey() throws Exception {
747  TestMap.Builder builder = TestMap.newBuilder();
748  mergeFromJson(
749  "{\n" + " int32ToInt32Map: {1: 2},\n" + " stringToInt32Map: {hello: 3}\n" + "}", builder);
750  TestMap message = builder.build();
751  assertThat(message.getInt32ToInt32MapMap().get(1).intValue()).isEqualTo(2);
752  assertThat(message.getStringToInt32MapMap().get("hello").intValue()).isEqualTo(3);
753  }
754 
755  @Test
756  public void testWrappers() throws Exception {
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("");
766  builder.getBytesValueBuilder().setValue(ByteString.EMPTY);
767  TestWrappers message = builder.build();
768 
769  assertThat(toJsonString(message))
770  .isEqualTo(
771  "{\n"
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"
781  + "}");
782  assertRoundTripEquals(message);
783 
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");
793  builder.getBytesValueBuilder().setValue(ByteString.copyFrom(new byte[] {8}));
794  message = builder.build();
795 
796  assertThat(toJsonString(message))
797  .isEqualTo(
798  "{\n"
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"
808  + "}");
809  assertRoundTripEquals(message);
810  }
811 
812  @Test
813  public void testTimestamp() throws Exception {
814  TestTimestamp message =
815  TestTimestamp.newBuilder()
816  .setTimestampValue(Timestamps.parse("1970-01-01T00:00:00Z"))
817  .build();
818 
819  assertThat(toJsonString(message))
820  .isEqualTo("{\n" + " \"timestampValue\": \"1970-01-01T00:00:00Z\"\n" + "}");
821  assertRoundTripEquals(message);
822  }
823 
824  @Test
825  public void testTimestampMergeError() throws Exception {
826  final String incorrectTimestampString = "{\"seconds\":1800,\"nanos\":0}";
827  try {
828  TestTimestamp.Builder builder = TestTimestamp.newBuilder();
829  mergeFromJson(String.format("{\"timestamp_value\": %s}", incorrectTimestampString), builder);
830  assertWithMessage("expected exception").fail();
831  } catch (InvalidProtocolBufferException e) {
832  // Exception expected.
833  assertThat(e)
834  .hasMessageThat()
835  .isEqualTo("Failed to parse timestamp: " + incorrectTimestampString);
836  }
837  }
838 
839  @Test
840  public void testDuration() throws Exception {
841  TestDuration message =
842  TestDuration.newBuilder().setDurationValue(Durations.parse("12345s")).build();
843 
844  assertThat(toJsonString(message)).isEqualTo("{\n" + " \"durationValue\": \"12345s\"\n" + "}");
845  assertRoundTripEquals(message);
846  }
847 
848  @Test
849  public void testDurationMergeError() throws Exception {
850  final String incorrectDurationString = "{\"seconds\":10,\"nanos\":500}";
851  try {
852  TestDuration.Builder builder = TestDuration.newBuilder();
853  mergeFromJson(String.format("{\"duration_value\": %s}", incorrectDurationString), builder);
854  assertWithMessage("expected exception").fail();
855  } catch (InvalidProtocolBufferException e) {
856  // Exception expected.
857  assertThat(e)
858  .hasMessageThat()
859  .isEqualTo("Failed to parse duration: " + incorrectDurationString);
860  }
861  }
862 
863  @Test
864  public void testFieldMask() throws Exception {
865  TestFieldMask message =
866  TestFieldMask.newBuilder()
867  .setFieldMaskValue(FieldMaskUtil.fromString("foo.bar,baz,foo_bar.baz"))
868  .build();
869 
870  assertThat(toJsonString(message))
871  .isEqualTo("{\n" + " \"fieldMaskValue\": \"foo.bar,baz,fooBar.baz\"\n" + "}");
872  assertRoundTripEquals(message);
873  }
874 
875  @Test
876  public void testStruct() throws Exception {
877  // Build a struct with all possible values.
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());
883  Struct.Builder subStructBuilder = Struct.newBuilder();
884  subStructBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1234).build());
885  structBuilder.putFields(
886  "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build()).build());
887  ListValue.Builder listBuilder = ListValue.newBuilder();
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());
892  TestStruct message = builder.build();
893 
894  assertThat(toJsonString(message))
895  .isEqualTo(
896  "{\n"
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"
903  + " },\n"
904  + " \"list_value\": [1.125, null]\n"
905  + " }\n"
906  + "}");
907  assertRoundTripEquals(message);
908 
909  builder = TestStruct.newBuilder();
910  builder.setValue(Value.newBuilder().setNullValueValue(0).build());
911  message = builder.build();
912  assertThat(toJsonString(message)).isEqualTo("{\n" + " \"value\": null\n" + "}");
913  assertRoundTripEquals(message);
914 
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());
919  message = builder.build();
920  assertThat(toJsonString(message))
921  .isEqualTo("{\n" + " \"listValue\": [31831.125, null]\n" + "}");
922  assertRoundTripEquals(message);
923  }
924 
925 
926  @Test
927  public void testAnyFieldsWithCustomAddedTypeRegistry() throws Exception {
928  TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
929  TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
930 
931  com.google.protobuf.TypeRegistry registry =
932  com.google.protobuf.TypeRegistry.newBuilder().add(content.getDescriptorForType()).build();
933  JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);
934 
935  assertThat(printer.print(message))
936  .isEqualTo(
937  "{\n"
938  + " \"anyValue\": {\n"
939  + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
940  + " \"optionalInt32\": 1234\n"
941  + " }\n"
942  + "}");
943  assertRoundTripEquals(message, registry);
944 
945  TestAny messageWithDefaultAnyValue =
946  TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
947  assertThat(printer.print(messageWithDefaultAnyValue))
948  .isEqualTo("{\n" + " \"anyValue\": {}\n" + "}");
949  assertRoundTripEquals(messageWithDefaultAnyValue, registry);
950 
951  // Well-known types have a special formatting when embedded in Any.
952  //
953  // 1. Any in Any.
954  Any anyMessage = Any.pack(Any.pack(content));
955  assertThat(printer.print(anyMessage))
956  .isEqualTo(
957  "{\n"
958  + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
959  + " \"value\": {\n"
960  + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
961  + " \"optionalInt32\": 1234\n"
962  + " }\n"
963  + "}");
964  assertRoundTripEquals(anyMessage, registry);
965  }
966 
967  @Test
968  public void testAnyFields() throws Exception {
969  TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
970  TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
971 
972  // A TypeRegistry must be provided in order to convert Any types.
973  try {
974  toJsonString(message);
975  assertWithMessage("Exception is expected.").fail();
976  } catch (IOException e) {
977  // Expected.
978  }
979 
980  JsonFormat.TypeRegistry registry =
981  JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build();
982  JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);
983 
984  assertThat(printer.print(message))
985  .isEqualTo(
986  "{\n"
987  + " \"anyValue\": {\n"
988  + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
989  + " \"optionalInt32\": 1234\n"
990  + " }\n"
991  + "}");
992  assertRoundTripEquals(message, registry);
993 
994  TestAny messageWithDefaultAnyValue =
995  TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
996  assertThat(printer.print(messageWithDefaultAnyValue))
997  .isEqualTo("{\n" + " \"anyValue\": {}\n" + "}");
998  assertRoundTripEquals(messageWithDefaultAnyValue, registry);
999 
1000  // Well-known types have a special formatting when embedded in Any.
1001  //
1002  // 1. Any in Any.
1003  Any anyMessage = Any.pack(Any.pack(content));
1004  assertThat(printer.print(anyMessage))
1005  .isEqualTo(
1006  "{\n"
1007  + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1008  + " \"value\": {\n"
1009  + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
1010  + " \"optionalInt32\": 1234\n"
1011  + " }\n"
1012  + "}");
1013  assertRoundTripEquals(anyMessage, registry);
1014 
1015  // 2. Wrappers in Any.
1016  anyMessage = Any.pack(Int32Value.of(12345));
1017  assertThat(printer.print(anyMessage))
1018  .isEqualTo(
1019  "{\n"
1020  + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n"
1021  + " \"value\": 12345\n"
1022  + "}");
1023  assertRoundTripEquals(anyMessage, registry);
1024  anyMessage = Any.pack(UInt32Value.of(12345));
1025  assertThat(printer.print(anyMessage))
1026  .isEqualTo(
1027  "{\n"
1028  + " \"@type\": \"type.googleapis.com/google.protobuf.UInt32Value\",\n"
1029  + " \"value\": 12345\n"
1030  + "}");
1031  assertRoundTripEquals(anyMessage, registry);
1032  anyMessage = Any.pack(Int64Value.of(12345));
1033  assertThat(printer.print(anyMessage))
1034  .isEqualTo(
1035  "{\n"
1036  + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n"
1037  + " \"value\": \"12345\"\n"
1038  + "}");
1039  assertRoundTripEquals(anyMessage, registry);
1040  anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
1041  assertThat(printer.print(anyMessage))
1042  .isEqualTo(
1043  "{\n"
1044  + " \"@type\": \"type.googleapis.com/google.protobuf.UInt64Value\",\n"
1045  + " \"value\": \"12345\"\n"
1046  + "}");
1047  assertRoundTripEquals(anyMessage, registry);
1048  anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
1049  assertThat(printer.print(anyMessage))
1050  .isEqualTo(
1051  "{\n"
1052  + " \"@type\": \"type.googleapis.com/google.protobuf.FloatValue\",\n"
1053  + " \"value\": 12345.0\n"
1054  + "}");
1055  assertRoundTripEquals(anyMessage, registry);
1056  anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
1057  assertThat(printer.print(anyMessage))
1058  .isEqualTo(
1059  "{\n"
1060  + " \"@type\": \"type.googleapis.com/google.protobuf.DoubleValue\",\n"
1061  + " \"value\": 12345.0\n"
1062  + "}");
1063  assertRoundTripEquals(anyMessage, registry);
1064  anyMessage = Any.pack(BoolValue.of(true));
1065  assertThat(printer.print(anyMessage))
1066  .isEqualTo(
1067  "{\n"
1068  + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
1069  + " \"value\": true\n"
1070  + "}");
1071  assertRoundTripEquals(anyMessage, registry);
1072  anyMessage = Any.pack(StringValue.of("Hello"));
1073  assertThat(printer.print(anyMessage))
1074  .isEqualTo(
1075  "{\n"
1076  + " \"@type\": \"type.googleapis.com/google.protobuf.StringValue\",\n"
1077  + " \"value\": \"Hello\"\n"
1078  + "}");
1079  assertRoundTripEquals(anyMessage, registry);
1080  anyMessage = Any.pack(BytesValue.of(ByteString.copyFrom(new byte[] {1, 2})));
1081  assertThat(printer.print(anyMessage))
1082  .isEqualTo(
1083  "{\n"
1084  + " \"@type\": \"type.googleapis.com/google.protobuf.BytesValue\",\n"
1085  + " \"value\": \"AQI=\"\n"
1086  + "}");
1087  assertRoundTripEquals(anyMessage, registry);
1088 
1089  // 3. Timestamp in Any.
1090  anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"));
1091  assertThat(printer.print(anyMessage))
1092  .isEqualTo(
1093  "{\n"
1094  + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n"
1095  + " \"value\": \"1969-12-31T23:59:59Z\"\n"
1096  + "}");
1097  assertRoundTripEquals(anyMessage, registry);
1098 
1099  // 4. Duration in Any
1100  anyMessage = Any.pack(Durations.parse("12345.10s"));
1101  assertThat(printer.print(anyMessage))
1102  .isEqualTo(
1103  "{\n"
1104  + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n"
1105  + " \"value\": \"12345.100s\"\n"
1106  + "}");
1107  assertRoundTripEquals(anyMessage, registry);
1108 
1109  // 5. FieldMask in Any
1110  anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
1111  assertThat(printer.print(anyMessage))
1112  .isEqualTo(
1113  "{\n"
1114  + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n"
1115  + " \"value\": \"foo.bar,baz\"\n"
1116  + "}");
1117  assertRoundTripEquals(anyMessage, registry);
1118 
1119  // 6. Struct in Any
1120  Struct.Builder structBuilder = Struct.newBuilder();
1121  structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build());
1122  anyMessage = Any.pack(structBuilder.build());
1123  assertThat(printer.print(anyMessage))
1124  .isEqualTo(
1125  "{\n"
1126  + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n"
1127  + " \"value\": {\n"
1128  + " \"number\": 1.125\n"
1129  + " }\n"
1130  + "}");
1131  assertRoundTripEquals(anyMessage, registry);
1132 
1133  // 7. Value (number type) in Any
1134  Value.Builder valueBuilder = Value.newBuilder();
1135  valueBuilder.setNumberValue(1);
1136  anyMessage = Any.pack(valueBuilder.build());
1137  assertThat(printer.print(anyMessage))
1138  .isEqualTo(
1139  "{\n"
1140  + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1141  + " \"value\": 1.0\n"
1142  + "}");
1143  assertRoundTripEquals(anyMessage, registry);
1144 
1145  // 8. Value (null type) in Any
1146  anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
1147  assertThat(printer.print(anyMessage))
1148  .isEqualTo(
1149  "{\n"
1150  + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1151  + " \"value\": null\n"
1152  + "}");
1153  assertRoundTripEquals(anyMessage, registry);
1154  }
1155 
1156  @Test
1157  public void testAnyInMaps() throws Exception {
1158  JsonFormat.TypeRegistry registry =
1159  JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build();
1160  JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);
1161 
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)));
1165  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
1166  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
1167  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
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()));
1172  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
1173  testAny.putAnyMap(
1174  "list_value",
1175  Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).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());
1180 
1181  assertThat(printer.print(testAny.build()))
1182  .isEqualTo(
1183  "{\n"
1184  + " \"anyMap\": {\n"
1185  + " \"int32_wrapper\": {\n"
1186  + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n"
1187  + " \"value\": 123\n"
1188  + " },\n"
1189  + " \"int64_wrapper\": {\n"
1190  + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n"
1191  + " \"value\": \"456\"\n"
1192  + " },\n"
1193  + " \"timestamp\": {\n"
1194  + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n"
1195  + " \"value\": \"1969-12-31T23:59:59Z\"\n"
1196  + " },\n"
1197  + " \"duration\": {\n"
1198  + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n"
1199  + " \"value\": \"12345.100s\"\n"
1200  + " },\n"
1201  + " \"field_mask\": {\n"
1202  + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n"
1203  + " \"value\": \"foo.bar,baz\"\n"
1204  + " },\n"
1205  + " \"struct\": {\n"
1206  + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n"
1207  + " \"value\": {\n"
1208  + " \"number\": 1.125\n"
1209  + " }\n"
1210  + " },\n"
1211  + " \"list_value\": {\n"
1212  + " \"@type\": \"type.googleapis.com/google.protobuf.ListValue\",\n"
1213  + " \"value\": [1.125, null]\n"
1214  + " },\n"
1215  + " \"number_value\": {\n"
1216  + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1217  + " \"value\": 1.125\n"
1218  + " },\n"
1219  + " \"any_value_number\": {\n"
1220  + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1221  + " \"value\": {\n"
1222  + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
1223  + " \"value\": 1.125\n"
1224  + " }\n"
1225  + " },\n"
1226  + " \"any_value_default\": {\n"
1227  + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1228  + " \"value\": {}\n"
1229  + " },\n"
1230  + " \"default\": {}\n"
1231  + " }\n"
1232  + "}");
1233  assertRoundTripEquals(testAny.build(), registry);
1234  }
1235 
1236  @Test
1237  public void testParserMissingTypeUrl() throws Exception {
1238  try {
1239  Any.Builder builder = Any.newBuilder();
1240  mergeFromJson("{\n" + " \"optionalInt32\": 1234\n" + "}", builder);
1241  assertWithMessage("Exception is expected.").fail();
1242  } catch (IOException e) {
1243  // Expected.
1244  }
1245  }
1246 
1247  @Test
1248  public void testParserUnexpectedTypeUrl() throws Exception {
1249  try {
1250  Any.Builder builder = Any.newBuilder();
1251  mergeFromJson(
1252  "{\n"
1253  + " \"@type\": \"type.googleapis.com/json_test.UnexpectedTypes\",\n"
1254  + " \"optionalInt32\": 12345\n"
1255  + "}",
1256  builder);
1257  assertWithMessage("Exception is expected.").fail();
1258  } catch (IOException e) {
1259  // Expected.
1260  }
1261  }
1262 
1263  @Test
1264  public void testParserRejectTrailingComma() throws Exception {
1265  try {
1266  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1267  mergeFromJson("{\n" + " \"optionalInt32\": 12345,\n" + "}", builder);
1268  assertWithMessage("Exception is expected.").fail();
1269  } catch (IOException e) {
1270  // Expected.
1271  }
1272 
1273  // TODO(xiaofeng): GSON allows trailing comma in arrays even after I set
1274  // the JsonReader to non-lenient mode. If we want to enforce strict JSON
1275  // compliance, we might want to switch to a different JSON parser or
1276  // implement one by ourselves.
1277  // try {
1278  // TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1279  // JsonFormat.merge(
1280  // "{\n"
1281  // + " \"repeatedInt32\": [12345,]\n"
1282  // + "}", builder);
1283  // fail("Exception is expected.");
1284  // } catch (IOException e) {
1285  // // Expected.
1286  // }
1287  }
1288 
1289  @Test
1290  public void testParserRejectInvalidBase64() throws Exception {
1291  assertRejects("optionalBytes", "!@#$");
1292  }
1293 
1294  @Test
1295  public void testParserAcceptBase64Variants() throws Exception {
1296  assertAccepts("optionalBytes", "AQI"); // No padding
1297  assertAccepts("optionalBytes", "-_w"); // base64Url, no padding
1298  }
1299 
1300  @Test
1301  public void testParserRejectInvalidEnumValue() throws Exception {
1302  try {
1303  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1304  mergeFromJson("{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}", builder);
1305  assertWithMessage("Exception is expected.").fail();
1306  } catch (InvalidProtocolBufferException e) {
1307  // Expected.
1308  }
1309  }
1310 
1311  @Test
1312  public void testParserUnknownFields() throws Exception {
1313  try {
1314  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1315  String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}";
1316  JsonFormat.parser().merge(json, builder);
1317  assertWithMessage("Exception is expected.").fail();
1318  } catch (InvalidProtocolBufferException e) {
1319  // Expected.
1320  }
1321  }
1322 
1323  @Test
1324  public void testParserIgnoringUnknownFields() throws Exception {
1325  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1326  String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}";
1328  }
1329 
1330  @Test
1331  public void testParserIgnoringUnknownEnums() throws Exception {
1332  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1333  String json = "{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}";
1335  assertThat(builder.getOptionalNestedEnumValue()).isEqualTo(0);
1336  }
1337 
1338  @Test
1339  public void testParserSupportAliasEnums() throws Exception {
1340  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1341  String json = "{\n" + " \"optionalAliasedEnum\": \"QUX\"\n" + "}";
1342  JsonFormat.parser().merge(json, builder);
1343  assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
1344 
1345  builder = TestAllTypes.newBuilder();
1346  json = "{\n" + " \"optionalAliasedEnum\": \"qux\"\n" + "}";
1347  JsonFormat.parser().merge(json, builder);
1348  assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
1349 
1350  builder = TestAllTypes.newBuilder();
1351  json = "{\n" + " \"optionalAliasedEnum\": \"bAz\"\n" + "}";
1352  JsonFormat.parser().merge(json, builder);
1353  assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
1354  }
1355 
1356  @Test
1357  public void testUnknownEnumMap() throws Exception {
1358  TestMap.Builder builder = TestMap.newBuilder();
1359  JsonFormat.parser()
1361  .merge("{\n" + " \"int32ToEnumMap\": {1: XXX, 2: FOO}" + "}", builder);
1362 
1363  assertThat(builder.getInt32ToEnumMapMap()).containsEntry(2, NestedEnum.FOO);
1364  assertThat(builder.getInt32ToEnumMapMap()).hasSize(1);
1365  }
1366 
1367  @Test
1368  public void testRepeatedUnknownEnum() throws Exception {
1369  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1370  JsonFormat.parser()
1372  .merge("{\n" + " \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]" + "}", builder);
1373 
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);
1378  }
1379 
1380  @Test
1381  public void testParserIntegerEnumValue() throws Exception {
1382  TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder();
1383  mergeFromJson("{\n" + " \"optionalNestedEnum\": 2\n" + "}", actualBuilder);
1384 
1385  TestAllTypes expected = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.BAZ).build();
1386  assertThat(actualBuilder.build()).isEqualTo(expected);
1387  }
1388 
1389  @Test
1390  public void testCustomJsonName() throws Exception {
1391  TestCustomJsonName message = TestCustomJsonName.newBuilder().setValue(12345).build();
1392  assertThat(JsonFormat.printer().print(message))
1393  .isEqualTo("{\n" + " \"@value\": 12345\n" + "}");
1394  assertRoundTripEquals(message);
1395  }
1396 
1397  // Regression test for b/73832901. Make sure html tags are escaped.
1398  @Test
1399  public void testHtmlEscape() throws Exception {
1400  TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("</script>").build();
1401  assertThat(toJsonString(message))
1402  .isEqualTo("{\n \"optionalString\": \"\\u003c/script\\u003e\"\n}");
1403 
1404  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1405  JsonFormat.parser().merge(toJsonString(message), builder);
1406  assertThat(builder.getOptionalString()).isEqualTo(message.getOptionalString());
1407  }
1408 
1409  @Test
1410  public void testIncludingDefaultValueFields() throws Exception {
1411  TestAllTypes message = TestAllTypes.getDefaultInstance();
1412  assertThat(JsonFormat.printer().print(message)).isEqualTo("{\n}");
1413  assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
1414  .isEqualTo(
1415  "{\n"
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"
1450  + "}");
1451 
1452  Set<FieldDescriptor> fixedFields = new HashSet<>();
1453  for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1454  if (fieldDesc.getName().contains("_fixed")) {
1455  fixedFields.add(fieldDesc);
1456  }
1457  }
1458 
1459  assertThat(JsonFormat.printer().includingDefaultValueFields(fixedFields).print(message))
1460  .isEqualTo(
1461  "{\n"
1462  + " \"optionalFixed32\": 0,\n"
1463  + " \"optionalFixed64\": \"0\",\n"
1464  + " \"repeatedFixed32\": [],\n"
1465  + " \"repeatedFixed64\": []\n"
1466  + "}");
1467 
1468  TestAllTypes messageNonDefaults =
1469  message.toBuilder().setOptionalInt64(1234).setOptionalFixed32(3232).build();
1470  assertThat(
1471  JsonFormat.printer().includingDefaultValueFields(fixedFields).print(messageNonDefaults))
1472  .isEqualTo(
1473  "{\n"
1474  + " \"optionalInt64\": \"1234\",\n"
1475  + " \"optionalFixed32\": 3232,\n"
1476  + " \"optionalFixed64\": \"0\",\n"
1477  + " \"repeatedFixed32\": [],\n"
1478  + " \"repeatedFixed64\": []\n"
1479  + "}");
1480 
1481  try {
1482  JsonFormat.printer().includingDefaultValueFields().includingDefaultValueFields();
1483  assertWithMessage("IllegalStateException is expected.").fail();
1484  } catch (IllegalStateException e) {
1485  // Expected.
1486  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1487  .that(e.getMessage().contains("includingDefaultValueFields"))
1488  .isTrue();
1489  }
1490 
1491  try {
1492  JsonFormat.printer().includingDefaultValueFields().includingDefaultValueFields(fixedFields);
1493  assertWithMessage("IllegalStateException is expected.").fail();
1494  } catch (IllegalStateException e) {
1495  // Expected.
1496  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1497  .that(e.getMessage().contains("includingDefaultValueFields"))
1498  .isTrue();
1499  }
1500 
1501  try {
1502  JsonFormat.printer().includingDefaultValueFields(fixedFields).includingDefaultValueFields();
1503  assertWithMessage("IllegalStateException is expected.").fail();
1504  } catch (IllegalStateException e) {
1505  // Expected.
1506  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1507  .that(e.getMessage().contains("includingDefaultValueFields"))
1508  .isTrue();
1509  }
1510 
1511  try {
1513  .includingDefaultValueFields(fixedFields)
1514  .includingDefaultValueFields(fixedFields);
1515  assertWithMessage("IllegalStateException is expected.").fail();
1516  } catch (IllegalStateException e) {
1517  // Expected.
1518  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1519  .that(e.getMessage().contains("includingDefaultValueFields"))
1520  .isTrue();
1521  }
1522 
1523  Set<FieldDescriptor> intFields = new HashSet<>();
1524  for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1525  if (fieldDesc.getName().contains("_int")) {
1526  intFields.add(fieldDesc);
1527  }
1528  }
1529 
1530  try {
1532  .includingDefaultValueFields(intFields)
1533  .includingDefaultValueFields(fixedFields);
1534  assertWithMessage("IllegalStateException is expected.").fail();
1535  } catch (IllegalStateException e) {
1536  // Expected.
1537  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1538  .that(e.getMessage().contains("includingDefaultValueFields"))
1539  .isTrue();
1540  }
1541 
1542  try {
1544  assertWithMessage("IllegalArgumentException is expected.").fail();
1545  } catch (IllegalArgumentException e) {
1546  // Expected.
1547  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1548  .that(e.getMessage().contains("includingDefaultValueFields"))
1549  .isTrue();
1550  }
1551 
1552  try {
1554  assertWithMessage("IllegalArgumentException is expected.").fail();
1555  } catch (IllegalArgumentException e) {
1556  // Expected.
1557  assertWithMessage("Exception message should mention includingDefaultValueFields.")
1558  .that(e.getMessage().contains("includingDefaultValueFields"))
1559  .isTrue();
1560  }
1561 
1562  TestMap mapMessage = TestMap.getDefaultInstance();
1563  assertThat(JsonFormat.printer().print(mapMessage)).isEqualTo("{\n}");
1564  assertThat(JsonFormat.printer().includingDefaultValueFields().print(mapMessage))
1565  .isEqualTo(
1566  "{\n"
1567  + " \"int32ToInt32Map\": {\n"
1568  + " },\n"
1569  + " \"int64ToInt32Map\": {\n"
1570  + " },\n"
1571  + " \"uint32ToInt32Map\": {\n"
1572  + " },\n"
1573  + " \"uint64ToInt32Map\": {\n"
1574  + " },\n"
1575  + " \"sint32ToInt32Map\": {\n"
1576  + " },\n"
1577  + " \"sint64ToInt32Map\": {\n"
1578  + " },\n"
1579  + " \"fixed32ToInt32Map\": {\n"
1580  + " },\n"
1581  + " \"fixed64ToInt32Map\": {\n"
1582  + " },\n"
1583  + " \"sfixed32ToInt32Map\": {\n"
1584  + " },\n"
1585  + " \"sfixed64ToInt32Map\": {\n"
1586  + " },\n"
1587  + " \"boolToInt32Map\": {\n"
1588  + " },\n"
1589  + " \"stringToInt32Map\": {\n"
1590  + " },\n"
1591  + " \"int32ToInt64Map\": {\n"
1592  + " },\n"
1593  + " \"int32ToUint32Map\": {\n"
1594  + " },\n"
1595  + " \"int32ToUint64Map\": {\n"
1596  + " },\n"
1597  + " \"int32ToSint32Map\": {\n"
1598  + " },\n"
1599  + " \"int32ToSint64Map\": {\n"
1600  + " },\n"
1601  + " \"int32ToFixed32Map\": {\n"
1602  + " },\n"
1603  + " \"int32ToFixed64Map\": {\n"
1604  + " },\n"
1605  + " \"int32ToSfixed32Map\": {\n"
1606  + " },\n"
1607  + " \"int32ToSfixed64Map\": {\n"
1608  + " },\n"
1609  + " \"int32ToFloatMap\": {\n"
1610  + " },\n"
1611  + " \"int32ToDoubleMap\": {\n"
1612  + " },\n"
1613  + " \"int32ToBoolMap\": {\n"
1614  + " },\n"
1615  + " \"int32ToStringMap\": {\n"
1616  + " },\n"
1617  + " \"int32ToBytesMap\": {\n"
1618  + " },\n"
1619  + " \"int32ToMessageMap\": {\n"
1620  + " },\n"
1621  + " \"int32ToEnumMap\": {\n"
1622  + " }\n"
1623  + "}");
1624 
1625  TestOneof oneofMessage = TestOneof.getDefaultInstance();
1626  assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n}");
1627  assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
1628  .isEqualTo("{\n}");
1629 
1630  oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
1631  assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n \"oneofInt32\": 42\n}");
1632  assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
1633  .isEqualTo("{\n \"oneofInt32\": 42\n}");
1634 
1635  TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
1636  mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", oneofBuilder);
1637  oneofMessage = oneofBuilder.build();
1638  assertThat(JsonFormat.printer().print(oneofMessage))
1639  .isEqualTo("{\n \"oneofNullValue\": null\n}");
1640  assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
1641  .isEqualTo("{\n \"oneofNullValue\": null\n}");
1642  }
1643 
1644  @Test
1645  public void testPreservingProtoFieldNames() throws Exception {
1646  TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
1647  assertThat(JsonFormat.printer().print(message))
1648  .isEqualTo("{\n" + " \"optionalInt32\": 12345\n" + "}");
1649  assertThat(JsonFormat.printer().preservingProtoFieldNames().print(message))
1650  .isEqualTo("{\n" + " \"optional_int32\": 12345\n" + "}");
1651 
1652  // The json_name field option is ignored when configured to use original proto field names.
1653  TestCustomJsonName messageWithCustomJsonName =
1654  TestCustomJsonName.newBuilder().setValue(12345).build();
1655  assertThat(JsonFormat.printer().preservingProtoFieldNames().print(messageWithCustomJsonName))
1656  .isEqualTo("{\n" + " \"value\": 12345\n" + "}");
1657 
1658  // Parsers accept both original proto field names and lowerCamelCase names.
1659  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1660  JsonFormat.parser().merge("{\"optionalInt32\": 12345}", builder);
1661  assertThat(builder.getOptionalInt32()).isEqualTo(12345);
1662  builder.clear();
1663  JsonFormat.parser().merge("{\"optional_int32\": 54321}", builder);
1664  assertThat(builder.getOptionalInt32()).isEqualTo(54321);
1665  }
1666 
1667  @Test
1668  public void testPrintingEnumsAsInts() throws Exception {
1669  TestAllTypes message = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.BAR).build();
1670  assertThat(JsonFormat.printer().printingEnumsAsInts().print(message))
1671  .isEqualTo("{\n" + " \"optionalNestedEnum\": 1\n" + "}");
1672  }
1673 
1674  @Test
1675  public void testOmittingInsignificantWhiteSpace() throws Exception {
1676  TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
1678  .isEqualTo("{" + "\"optionalInt32\":12345" + "}");
1679  TestAllTypes message1 = TestAllTypes.getDefaultInstance();
1680  assertThat(JsonFormat.printer().omittingInsignificantWhitespace().print(message1))
1681  .isEqualTo("{}");
1682  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1683  setAllFields(builder);
1684  TestAllTypes message2 = builder.build();
1685  assertThat(toCompactJsonString(message2))
1686  .isEqualTo(
1687  "{"
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\":{"
1704  + "\"value\":100"
1705  + "},"
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\":[{"
1723  + "\"value\":100"
1724  + "},{"
1725  + "\"value\":200"
1726  + "}],"
1727  + "\"repeatedNestedEnum\":[\"BAR\",\"BAZ\"]"
1728  + "}");
1729  }
1730 
1731  // Regression test for b/29892357
1732  @Test
1733  public void testEmptyWrapperTypesInAny() throws Exception {
1734  JsonFormat.TypeRegistry registry =
1735  JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build();
1737 
1738  Any.Builder builder = Any.newBuilder();
1739  parser.merge(
1740  "{\n"
1741  + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
1742  + " \"value\": false\n"
1743  + "}\n",
1744  builder);
1745  Any any = builder.build();
1746  assertThat(any.getValue().size()).isEqualTo(0);
1747  }
1748 
1749  @Test
1750  public void testRecursionLimit() throws Exception {
1751  String input =
1752  "{\n"
1753  + " \"nested\": {\n"
1754  + " \"nested\": {\n"
1755  + " \"nested\": {\n"
1756  + " \"nested\": {\n"
1757  + " \"value\": 1234\n"
1758  + " }\n"
1759  + " }\n"
1760  + " }\n"
1761  + " }\n"
1762  + "}\n";
1763 
1765  TestRecursive.Builder builder = TestRecursive.newBuilder();
1766  parser.merge(input, builder);
1767  TestRecursive message = builder.build();
1768  assertThat(message.getNested().getNested().getNested().getNested().getValue()).isEqualTo(1234);
1769 
1770  parser = JsonFormat.parser().usingRecursionLimit(3);
1771  builder = TestRecursive.newBuilder();
1772  try {
1773  parser.merge(input, builder);
1774  assertWithMessage("Exception is expected.").fail();
1775  } catch (InvalidProtocolBufferException e) {
1776  // Expected.
1777  }
1778  }
1779 
1780  // Test that we are not leaking out JSON exceptions.
1781  @Test
1782  public void testJsonException() throws Exception {
1783  InputStream throwingInputStream =
1784  new InputStream() {
1785  @Override
1786  public int read() throws IOException {
1787  throw new IOException("12345");
1788  }
1789  };
1790  InputStreamReader throwingReader = new InputStreamReader(throwingInputStream);
1791  // When the underlying reader throws IOException, JsonFormat should forward
1792  // through this IOException.
1793  try {
1794  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1795  JsonFormat.parser().merge(throwingReader, builder);
1796  assertWithMessage("Exception is expected.").fail();
1797  } catch (IOException e) {
1798  assertThat(e).hasMessageThat().isEqualTo("12345");
1799  }
1800 
1801  Reader invalidJsonReader = new StringReader("{ xxx - yyy }");
1802  // When the JSON parser throws parser exceptions, JsonFormat should turn
1803  // that into InvalidProtocolBufferException.
1804  try {
1805  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1806  JsonFormat.parser().merge(invalidJsonReader, builder);
1807  assertWithMessage("Exception is expected.").fail();
1808  } catch (InvalidProtocolBufferException e) {
1809  // Expected.
1810  }
1811  }
1812 
1813  // Test that an error is thrown if a nested JsonObject is parsed as a primitive field.
1814  @Test
1815  public void testJsonObjectForPrimitiveField() throws Exception {
1816  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1817  try {
1818  mergeFromJson(
1819  "{\n"
1820  + " \"optionalString\": {\n"
1821  + " \"invalidNestedString\": \"Hello world\"\n"
1822  + " }\n"
1823  + "}\n",
1824  builder);
1825  } catch (InvalidProtocolBufferException e) {
1826  // Expected.
1827  }
1828  }
1829 
1830  @Test
1831  public void testSortedMapKeys() throws Exception {
1832  TestMap.Builder mapBuilder = TestMap.newBuilder();
1833  mapBuilder.putStringToInt32Map("\ud834\udd20", 3); // utf-8 F0 9D 84 A0
1834  mapBuilder.putStringToInt32Map("foo", 99);
1835  mapBuilder.putStringToInt32Map("xxx", 123);
1836  mapBuilder.putStringToInt32Map("\u20ac", 1); // utf-8 E2 82 AC
1837  mapBuilder.putStringToInt32Map("abc", 20);
1838  mapBuilder.putStringToInt32Map("19", 19);
1839  mapBuilder.putStringToInt32Map("8", 8);
1840  mapBuilder.putStringToInt32Map("\ufb00", 2); // utf-8 EF AC 80
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))
1850  .isEqualTo(
1851  "{\n"
1852  + " \"int32ToInt32Map\": {\n"
1853  + " \"-3\": -3,\n"
1854  + " \"1\": 1,\n"
1855  + " \"2\": 2,\n"
1856  + " \"3\": 3,\n"
1857  + " \"4\": 4,\n"
1858  + " \"5\": 5,\n"
1859  + " \"10\": 10\n"
1860  + " },\n"
1861  + " \"stringToInt32Map\": {\n"
1862  + " \"19\": 19,\n"
1863  + " \"8\": 8,\n"
1864  + " \"abc\": 20,\n"
1865  + " \"foo\": 99,\n"
1866  + " \"xxx\": 123,\n"
1867  + " \"\u20ac\": 1,\n"
1868  + " \"\ufb00\": 2,\n"
1869  + " \"\ud834\udd20\": 3\n"
1870  + " }\n"
1871  + "}");
1872 
1873  TestMap emptyMap = TestMap.getDefaultInstance();
1874  assertThat(toSortedJsonString(emptyMap)).isEqualTo("{\n}");
1875  }
1876 
1877  @Test
1879  TestAllTypes message = TestAllTypes.newBuilder().setOptionalBool(false).build();
1880 
1881  assertThat(
1884  ImmutableSet.of(
1885  message.getDescriptorForType().findFieldByName("optional_bool")))
1886  .printingEnumsAsInts()
1887  .print(message))
1888  .isEqualTo("{\n" + " \"optionalBool\": false\n" + "}");
1889  }
1890 
1891  @Test
1892  public void testPreservesFloatingPointNegative0() throws Exception {
1893  TestAllTypes message =
1894  TestAllTypes.newBuilder().setOptionalFloat(-0.0f).setOptionalDouble(-0.0).build();
1895  assertThat(JsonFormat.printer().print(message))
1896  .isEqualTo("{\n \"optionalFloat\": -0.0,\n \"optionalDouble\": -0.0\n}");
1897  }
1898 }
com.google.protobuf.util.JsonFormatTest.testParserRejectOutOfRangeNumericValues
void testParserRejectOutOfRangeNumericValues()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:381
com.google.protobuf.util.JsonFormatTest.testAnyInMaps
void testAnyInMaps()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1157
com.google.protobuf.Descriptors
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/Descriptors.java:80
check_grpcio_tools.content
content
Definition: check_grpcio_tools.py:26
com.google.protobuf.util.JsonFormatTest.testIncludingDefaultValueFields
void testIncludingDefaultValueFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1410
Any
struct Any Any
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:633
com.google.protobuf.util.JsonFormatTest.testParserRejectInvalidBase64
void testParserRejectInvalidBase64()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1290
com.google.protobuf.util.JsonFormat.Parser.merge
void merge(String json, Message.Builder builder)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:473
com.google.protobuf.util.JsonFormatTest.testStruct
void testStruct()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:876
com.google.protobuf.util.JsonFormatTest.testMapNullValueIsRejected
void testMapNullValueIsRejected()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:707
com.google.protobuf.util.JsonFormat.Printer.printingEnumsAsInts
final boolean printingEnumsAsInts
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:139
TestStruct
Test
void Test(StringPiece pattern, const RE2::Options &options, StringPiece text)
Definition: bloaty/third_party/re2/re2/fuzzing/re2_fuzzer.cc:20
com.google.protobuf.util.JsonFormat.TypeRegistry.newBuilder
static Builder newBuilder()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:517
com.google.protobuf.util.JsonFormatTest.testParserUnexpectedTypeUrl
void testParserUnexpectedTypeUrl()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1248
com.google.protobuf::ListValue
com.google.protobuf.util.JsonFormat.parser
static Parser parser()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:396
com.google.protobuf.util.JsonFormatTest.testPreservesFloatingPointNegative0
void testPreservesFloatingPointNegative0()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1892
DoubleValue
struct DoubleValue DoubleValue
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:639
com.google.protobuf.util.JsonFormat.Printer.omittingInsignificantWhitespace
final boolean omittingInsignificantWhitespace
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:138
com.google.protobuf.util.JsonFormatTest.testParserRejectInvalidEnumValue
void testParserRejectInvalidEnumValue()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1301
com.google.protobuf.util.JsonFormatTest.testRepeatedUnknownEnum
void testRepeatedUnknownEnum()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1368
com.google.protobuf.util.JsonFormatTest.testMapEnumNullValueIsIgnored
void testMapEnumNullValueIsIgnored()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:737
com.google.protobuf.util.JsonFormatTest.testNullLastInDuplicateOneof
void testNullLastInDuplicateOneof()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:501
com.google.protobuf.util.JsonFormat.printer
static Printer printer()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:109
com.google.protobuf.Message.toString
String toString()
com.google.protobuf.util.JsonFormatTest.testHtmlEscape
void testHtmlEscape()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1399
com.google.protobuf.util.Durations.parse
static Duration parse(String value)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:241
com.google.protobuf.util.JsonFormat.Printer.print
String print(MessageOrBuilder message)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:379
com.google.protobuf.util.JsonFormatTest.testParserAcceptNull
void testParserAcceptNull()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:422
com.google.protobuf.util.JsonFormatTest.setAllFields
void setAllFields(TestAllTypes.Builder builder)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:93
com.google.protobuf.util.JsonFormatTest.testNullInOneof
void testNullInOneof()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:484
com.google.protobuf.util.JsonFormatTest.testNullFirstInDuplicateOneof
void testNullFirstInDuplicateOneof()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:493
build
Definition: build.py:1
com.google.protobuf.util.JsonFormatTest.testTimestampMergeError
void testTimestampMergeError()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:825
setup.name
name
Definition: setup.py:542
com.google.protobuf::BoolValue
Json::nullValue
@ nullValue
'null' value
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:464
TestTimestamp
com.google.protobuf.util.JsonFormatTest.testJsonException
void testJsonException()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1782
com.google.protobuf
Definition: bloaty/third_party/protobuf/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java:2
com.google.protobuf.util.JsonFormatTest.testSpecialFloatValues
void testSpecialFloatValues()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:277
com.google.protobuf.util.JsonFormatTest.testUnknownEnumValues
void testUnknownEnumValues()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:246
com.google.protobuf::Int32Value
com.google.protobuf.util.JsonFormatTest.toSortedJsonString
String toSortedJsonString(Message message)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:178
UInt64Value
struct UInt64Value UInt64Value
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:675
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
com.google.protobuf.util.JsonFormatTest.testRecursionLimit
void testRecursionLimit()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1750
com.google.protobuf.util.JsonFormatTest.assertAccepts
void assertAccepts(String name, String value)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:372
TestMap
NullValue
NullValue
Definition: bloaty/third_party/protobuf/src/google/protobuf/struct.pb.h:83
com.google.protobuf.util.JsonFormatTest.testTimestamp
void testTimestamp()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:813
com.google.protobuf.util.Durations
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:54
TestOneof
com.google.protobuf.util.JsonFormatTest.testParserAcceptBase64Variants
void testParserAcceptBase64Variants()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1295
TestAllTypes
com.google.protobuf.util.JsonFormatTest.testPrintingEnumsAsInts
void testPrintingEnumsAsInts()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1668
com.google.protobuf.util.JsonFormatTest.testMapFields
void testMapFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:564
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
asyncio_get_stats.parser
parser
Definition: asyncio_get_stats.py:34
com.google.protobuf.util
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:31
com.google.protobuf.util.JsonFormatTest.testParserUnknownFields
void testParserUnknownFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1312
com.google.protobuf.TypeRegistry
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TypeRegistry.java:46
TestDuration
com.google.protobuf.util.JsonFormat
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:101
com.google.protobuf.util.JsonFormatTest.assertRoundTripEquals
void assertRoundTripEquals(Message message)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:149
com.google.protobuf.util.JsonFormatTest.mergeFromJsonIgnoringUnknownFields
void mergeFromJsonIgnoringUnknownFields(String json, Message.Builder builder)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:186
com.google.protobuf.util.FieldMaskUtil.fromString
static FieldMask fromString(String value)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java:84
com.google.protobuf.util.JsonFormatTest.testFieldMask
void testFieldMask()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:864
com.google.protobuf.ByteString.EMPTY
static final ByteString EMPTY
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ByteString.java:85
com.google.protobuf.Message.Builder
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/Message.java:104
com.google.protobuf.util.JsonFormatTest.testParserIgnoringUnknownFields
void testParserIgnoringUnknownFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1324
Int64Value
struct Int64Value Int64Value
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:653
testing::internal::Float
FloatingPoint< float > Float
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:396
com.google.protobuf.util.JsonFormatTest.assertRejects
void assertRejects(String name, String value)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:354
com.google.protobuf.util.Timestamps.parse
static Timestamp parse(String value)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:232
com.google.protobuf.util.JsonFormat.Printer.usingTypeRegistry
Printer usingTypeRegistry(TypeRegistry oldRegistry)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:167
com.google.protobuf.util.JsonFormat.TypeRegistry.Builder.build
TypeRegistry build()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:577
com.google.protobuf.util.JsonFormatTest.testParserAcceptStringForNumericField
void testParserAcceptStringForNumericField()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:298
com.google.protobuf.util.JsonFormatTest.testParserAcceptNonQuotedObjectKey
void testParserAcceptNonQuotedObjectKey()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:746
com.google.protobuf.util.JsonFormatTest.mergeFromJson
void mergeFromJson(String json, Message.Builder builder)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:182
com.google.protobuf.util.JsonFormatTest.testParserRejectTrailingComma
void testParserRejectTrailingComma()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1264
com.google.protobuf.util.JsonFormat.Parser.usingTypeRegistry
Parser usingTypeRegistry(TypeRegistry oldRegistry)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:433
com.google.protobuf.util.JsonFormatTest.testParserIntegerEnumValue
void testParserIntegerEnumValue()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1381
com.google.protobuf.util.JsonFormatTest.testWrappers
void testWrappers()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:756
com.google.protobuf.util.JsonFormatTest.testPrintingEnumsAsIntsChainedAfterIncludingDefaultValueFields
void testPrintingEnumsAsIntsChainedAfterIncludingDefaultValueFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1878
com.google.protobuf.util.JsonFormatTest.testJsonObjectForPrimitiveField
void testJsonObjectForPrimitiveField()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1815
com.google.protobuf::StringValue
com.google.protobuf.util.JsonFormat.Parser.ignoringUnknownFields
final boolean ignoringUnknownFields
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:410
TestFieldMask
com.google.protobuf.util.JsonFormatTest.testParserMissingTypeUrl
void testParserMissingTypeUrl()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1237
com.google.protobuf.util.FieldMaskUtil
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java:52
com.google.protobuf::DoubleValue
value
const char * value
Definition: hpack_parser_table.cc:165
com.google.protobuf.util.JsonFormatTest.testParserSupportAliasEnums
void testParserSupportAliasEnums()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1339
java
com.google.protobuf.Descriptors.Descriptor
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/Descriptors.java:629
com.google.protobuf.util.JsonFormatTest.toJsonString
String toJsonString(Message message)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:172
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
TestAny
BytesValue
struct BytesValue BytesValue
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:636
Struct
struct Struct Struct
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:670
com.google.protobuf.util.JsonFormat.Printer
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:124
com.google.protobuf.util.JsonFormatTest.assertRoundTripEquals
void assertRoundTripEquals(Message message, com.google.protobuf.TypeRegistry registry)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:162
com.google.protobuf::UInt64Value
com.google.protobuf.util.JsonFormatTest.testParserAcceptFloatingPointValueForIntegerField
void testParserAcceptFloatingPointValueForIntegerField()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:324
com.google.protobuf.util.JsonFormatTest.testParserIgnoringUnknownEnums
void testParserIgnoringUnknownEnums()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1331
com.google.protobuf.util.JsonFormatTest.testOmittingInsignificantWhiteSpace
void testOmittingInsignificantWhiteSpace()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1675
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
com.google.protobuf.util.Timestamps
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:54
com.google.protobuf.util.JsonFormatTest.testEmptyWrapperTypesInAny
void testEmptyWrapperTypesInAny()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1733
com.google.protobuf.util.JsonFormatTest.toCompactJsonString
String toCompactJsonString(Message message)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:175
TestCustomJsonName
testing::internal::Double
FloatingPoint< double > Double
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:397
com.google.protobuf.util.JsonFormatTest.testAnyFieldsWithCustomAddedTypeRegistry
void testAnyFieldsWithCustomAddedTypeRegistry()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:927
com.google.protobuf.util.JsonFormat.TypeRegistry
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:507
BoolValue
struct BoolValue BoolValue
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:635
com.google.protobuf::BytesValue
com.google
com
com.google.protobuf.util.JsonFormatTest.assertRoundTripEquals
void assertRoundTripEquals(Message message, TypeRegistry registry)
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:153
com.google.protobuf.util.JsonFormat.Parser
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:407
StringValue
struct StringValue StringValue
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:669
com.google.protobuf.util.JsonFormatTest.testCustomJsonName
void testCustomJsonName()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1390
com.google.protobuf.util.JsonFormatTest.JsonFormatTest
JsonFormatTest()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:88
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
com.google.protobuf.util.JsonFormatTest.testAllFields
void testAllFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:192
TestWrappers
com.google.protobuf::UInt32Value
com.google.protobuf.InvalidProtocolBufferException
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java:41
com.google.protobuf.util.JsonFormat.Printer.sortingMapKeys
final boolean sortingMapKeys
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:140
com.google.protobuf.util.JsonFormat.TypeRegistry.Builder.add
Builder add(Descriptor messageType)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:550
com.google.protobuf.util.JsonFormatTest.testAnyFields
void testAnyFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:968
com.google.protobuf.util.JsonFormatTest.testDurationMergeError
void testDurationMergeError()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:849
Int32Value
struct Int32Value Int32Value
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:652
com.google.protobuf.util.JsonFormat.Printer.preservingProtoFieldNames
final boolean preservingProtoFieldNames
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:137
com.google.protobuf.TypeRegistry.Builder.build
TypeRegistry build()
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TypeRegistry.java:124
Value
struct Value Value
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:676
com.google.protobuf.util.JsonFormatTest.testSortedMapKeys
void testSortedMapKeys()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1831
ListValue
struct ListValue ListValue
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:655
com.google.protobuf.util.JsonFormatTest.testPreservingProtoFieldNames
void testPreservingProtoFieldNames()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1645
FloatValue
struct FloatValue FloatValue
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:650
com.google.protobuf.TypeRegistry.newBuilder
static Builder newBuilder()
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TypeRegistry.java:59
com.google.protobuf.util.JsonFormatTest.testParserRejectDuplicatedFields
void testParserRejectDuplicatedFields()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:509
TestRecursive
com.google.protobuf.util.JsonFormatTest.testUnknownEnumMap
void testUnknownEnumMap()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:1357
UInt32Value
struct UInt32Value UInt32Value
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:674
com.google.protobuf.Message
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/Message.java:50
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
com.google.protobuf.util.JsonFormatTest.testDuration
void testDuration()
Definition: protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java:840
com.google.protobuf.util.JsonFormat.Printer.includingDefaultValueFields
Set< FieldDescriptor > includingDefaultValueFields
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:136
com.google.protobuf.util.JsonFormat.TypeRegistry.getEmptyTypeRegistry
static TypeRegistry getEmptyTypeRegistry()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:513
com.google.protobuf.TypeRegistry.Builder.add
Builder add(Descriptor messageType)
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TypeRegistry.java:101
com.google.protobuf.Descriptors.FieldDescriptor
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/Descriptors.java:949
com.google.protobuf.ByteString
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ByteString.java:67


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:14