Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/protobuf/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java

Issue 2600753002: Reverts third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 23 matching lines...) Expand all
34 import com.google.protobuf.BoolValue; 34 import com.google.protobuf.BoolValue;
35 import com.google.protobuf.ByteString; 35 import com.google.protobuf.ByteString;
36 import com.google.protobuf.BytesValue; 36 import com.google.protobuf.BytesValue;
37 import com.google.protobuf.DoubleValue; 37 import com.google.protobuf.DoubleValue;
38 import com.google.protobuf.FloatValue; 38 import com.google.protobuf.FloatValue;
39 import com.google.protobuf.Int32Value; 39 import com.google.protobuf.Int32Value;
40 import com.google.protobuf.Int64Value; 40 import com.google.protobuf.Int64Value;
41 import com.google.protobuf.InvalidProtocolBufferException; 41 import com.google.protobuf.InvalidProtocolBufferException;
42 import com.google.protobuf.ListValue; 42 import com.google.protobuf.ListValue;
43 import com.google.protobuf.Message; 43 import com.google.protobuf.Message;
44 import com.google.protobuf.NullValue;
45 import com.google.protobuf.StringValue; 44 import com.google.protobuf.StringValue;
46 import com.google.protobuf.Struct; 45 import com.google.protobuf.Struct;
47 import com.google.protobuf.UInt32Value; 46 import com.google.protobuf.UInt32Value;
48 import com.google.protobuf.UInt64Value; 47 import com.google.protobuf.UInt64Value;
49 import com.google.protobuf.Value; 48 import com.google.protobuf.Value;
50 import com.google.protobuf.util.JsonFormat.TypeRegistry; 49 import com.google.protobuf.util.JsonFormat.TypeRegistry;
51 import com.google.protobuf.util.JsonTestProto.TestAllTypes; 50 import com.google.protobuf.util.JsonTestProto.TestAllTypes;
52 import com.google.protobuf.util.JsonTestProto.TestAllTypes.NestedEnum; 51 import com.google.protobuf.util.JsonTestProto.TestAllTypes.NestedEnum;
53 import com.google.protobuf.util.JsonTestProto.TestAllTypes.NestedMessage; 52 import com.google.protobuf.util.JsonTestProto.TestAllTypes.NestedMessage;
54 import com.google.protobuf.util.JsonTestProto.TestAny; 53 import com.google.protobuf.util.JsonTestProto.TestAny;
55 import com.google.protobuf.util.JsonTestProto.TestCustomJsonName; 54 import com.google.protobuf.util.JsonTestProto.TestCustomJsonName;
56 import com.google.protobuf.util.JsonTestProto.TestDuration; 55 import com.google.protobuf.util.JsonTestProto.TestDuration;
57 import com.google.protobuf.util.JsonTestProto.TestFieldMask; 56 import com.google.protobuf.util.JsonTestProto.TestFieldMask;
58 import com.google.protobuf.util.JsonTestProto.TestMap; 57 import com.google.protobuf.util.JsonTestProto.TestMap;
59 import com.google.protobuf.util.JsonTestProto.TestOneof; 58 import com.google.protobuf.util.JsonTestProto.TestOneof;
60 import com.google.protobuf.util.JsonTestProto.TestRecursive;
61 import com.google.protobuf.util.JsonTestProto.TestStruct; 59 import com.google.protobuf.util.JsonTestProto.TestStruct;
62 import com.google.protobuf.util.JsonTestProto.TestTimestamp; 60 import com.google.protobuf.util.JsonTestProto.TestTimestamp;
63 import com.google.protobuf.util.JsonTestProto.TestWrappers; 61 import com.google.protobuf.util.JsonTestProto.TestWrappers;
62
63 import junit.framework.TestCase;
64
64 import java.io.IOException; 65 import java.io.IOException;
65 import java.math.BigDecimal; 66 import java.math.BigDecimal;
66 import java.math.BigInteger; 67 import java.math.BigInteger;
67 import java.util.HashMap;
68 import java.util.Map;
69 import junit.framework.TestCase;
70 68
71 public class JsonFormatTest extends TestCase { 69 public class JsonFormatTest extends TestCase {
72 private void setAllFields(TestAllTypes.Builder builder) { 70 private void setAllFields(TestAllTypes.Builder builder) {
73 builder.setOptionalInt32(1234); 71 builder.setOptionalInt32(1234);
74 builder.setOptionalInt64(1234567890123456789L); 72 builder.setOptionalInt64(1234567890123456789L);
75 builder.setOptionalUint32(5678); 73 builder.setOptionalUint32(5678);
76 builder.setOptionalUint64(2345678901234567890L); 74 builder.setOptionalUint64(2345678901234567890L);
77 builder.setOptionalSint32(9012); 75 builder.setOptionalSint32(9012);
78 builder.setOptionalSint64(3456789012345678901L); 76 builder.setOptionalSint64(3456789012345678901L);
79 builder.setOptionalFixed32(3456); 77 builder.setOptionalFixed32(3456);
80 builder.setOptionalFixed64(4567890123456789012L); 78 builder.setOptionalFixed64(4567890123456789012L);
81 builder.setOptionalSfixed32(7890); 79 builder.setOptionalSfixed32(7890);
82 builder.setOptionalSfixed64(5678901234567890123L); 80 builder.setOptionalSfixed64(5678901234567890123L);
83 builder.setOptionalFloat(1.5f); 81 builder.setOptionalFloat(1.5f);
84 builder.setOptionalDouble(1.25); 82 builder.setOptionalDouble(1.25);
85 builder.setOptionalBool(true); 83 builder.setOptionalBool(true);
86 builder.setOptionalString("Hello world!"); 84 builder.setOptionalString("Hello world!");
87 builder.setOptionalBytes(ByteString.copyFrom(new byte[] {0, 1, 2})); 85 builder.setOptionalBytes(ByteString.copyFrom(new byte[]{0, 1, 2}));
88 builder.setOptionalNestedEnum(NestedEnum.BAR); 86 builder.setOptionalNestedEnum(NestedEnum.BAR);
89 builder.getOptionalNestedMessageBuilder().setValue(100); 87 builder.getOptionalNestedMessageBuilder().setValue(100);
90 88
91 builder.addRepeatedInt32(1234); 89 builder.addRepeatedInt32(1234);
92 builder.addRepeatedInt64(1234567890123456789L); 90 builder.addRepeatedInt64(1234567890123456789L);
93 builder.addRepeatedUint32(5678); 91 builder.addRepeatedUint32(5678);
94 builder.addRepeatedUint64(2345678901234567890L); 92 builder.addRepeatedUint64(2345678901234567890L);
95 builder.addRepeatedSint32(9012); 93 builder.addRepeatedSint32(9012);
96 builder.addRepeatedSint64(3456789012345678901L); 94 builder.addRepeatedSint64(3456789012345678901L);
97 builder.addRepeatedFixed32(3456); 95 builder.addRepeatedFixed32(3456);
98 builder.addRepeatedFixed64(4567890123456789012L); 96 builder.addRepeatedFixed64(4567890123456789012L);
99 builder.addRepeatedSfixed32(7890); 97 builder.addRepeatedSfixed32(7890);
100 builder.addRepeatedSfixed64(5678901234567890123L); 98 builder.addRepeatedSfixed64(5678901234567890123L);
101 builder.addRepeatedFloat(1.5f); 99 builder.addRepeatedFloat(1.5f);
102 builder.addRepeatedDouble(1.25); 100 builder.addRepeatedDouble(1.25);
103 builder.addRepeatedBool(true); 101 builder.addRepeatedBool(true);
104 builder.addRepeatedString("Hello world!"); 102 builder.addRepeatedString("Hello world!");
105 builder.addRepeatedBytes(ByteString.copyFrom(new byte[] {0, 1, 2})); 103 builder.addRepeatedBytes(ByteString.copyFrom(new byte[]{0, 1, 2}));
106 builder.addRepeatedNestedEnum(NestedEnum.BAR); 104 builder.addRepeatedNestedEnum(NestedEnum.BAR);
107 builder.addRepeatedNestedMessageBuilder().setValue(100); 105 builder.addRepeatedNestedMessageBuilder().setValue(100);
108 106
109 builder.addRepeatedInt32(234); 107 builder.addRepeatedInt32(234);
110 builder.addRepeatedInt64(234567890123456789L); 108 builder.addRepeatedInt64(234567890123456789L);
111 builder.addRepeatedUint32(678); 109 builder.addRepeatedUint32(678);
112 builder.addRepeatedUint64(345678901234567890L); 110 builder.addRepeatedUint64(345678901234567890L);
113 builder.addRepeatedSint32(012); 111 builder.addRepeatedSint32(012);
114 builder.addRepeatedSint64(456789012345678901L); 112 builder.addRepeatedSint64(456789012345678901L);
115 builder.addRepeatedFixed32(456); 113 builder.addRepeatedFixed32(456);
116 builder.addRepeatedFixed64(567890123456789012L); 114 builder.addRepeatedFixed64(567890123456789012L);
117 builder.addRepeatedSfixed32(890); 115 builder.addRepeatedSfixed32(890);
118 builder.addRepeatedSfixed64(678901234567890123L); 116 builder.addRepeatedSfixed64(678901234567890123L);
119 builder.addRepeatedFloat(11.5f); 117 builder.addRepeatedFloat(11.5f);
120 builder.addRepeatedDouble(11.25); 118 builder.addRepeatedDouble(11.25);
121 builder.addRepeatedBool(true); 119 builder.addRepeatedBool(true);
122 builder.addRepeatedString("ello world!"); 120 builder.addRepeatedString("ello world!");
123 builder.addRepeatedBytes(ByteString.copyFrom(new byte[] {1, 2})); 121 builder.addRepeatedBytes(ByteString.copyFrom(new byte[]{1, 2}));
124 builder.addRepeatedNestedEnum(NestedEnum.BAZ); 122 builder.addRepeatedNestedEnum(NestedEnum.BAZ);
125 builder.addRepeatedNestedMessageBuilder().setValue(200); 123 builder.addRepeatedNestedMessageBuilder().setValue(200);
126 } 124 }
127 125
128 private void assertRoundTripEquals(Message message) throws Exception { 126 private void assertRoundTripEquals(Message message) throws Exception {
129 assertRoundTripEquals(message, TypeRegistry.getEmptyTypeRegistry()); 127 assertRoundTripEquals(message, TypeRegistry.getEmptyTypeRegistry());
130 } 128 }
131 129
132 private void assertRoundTripEquals(Message message, TypeRegistry registry) thr ows Exception { 130 private void assertRoundTripEquals(Message message, TypeRegistry registry) thr ows Exception {
133 JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry ); 131 JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry );
134 JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(registry); 132 JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(registry);
135 Message.Builder builder = message.newBuilderForType(); 133 Message.Builder builder = message.newBuilderForType();
136 parser.merge(printer.print(message), builder); 134 parser.merge(printer.print(message), builder);
137 Message parsedMessage = builder.build(); 135 Message parsedMessage = builder.build();
138 assertEquals(message.toString(), parsedMessage.toString()); 136 assertEquals(message.toString(), parsedMessage.toString());
139 } 137 }
140 138
141 private String toJsonString(Message message) throws IOException { 139 private String toJsonString(Message message) throws IOException {
142 return JsonFormat.printer().print(message); 140 return JsonFormat.printer().print(message);
143 } 141 }
144 private String toCompactJsonString(Message message) throws IOException {
145 return JsonFormat.printer().omittingInsignificantWhitespace().print(message) ;
146 }
147 142
148 private void mergeFromJson(String json, Message.Builder builder) throws IOExce ption { 143 private void mergeFromJson(String json, Message.Builder builder) throws IOExce ption {
149 JsonFormat.parser().merge(json, builder); 144 JsonFormat.parser().merge(json, builder);
150 } 145 }
151 146
152 public void testAllFields() throws Exception { 147 public void testAllFields() throws Exception {
153 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 148 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
154 setAllFields(builder); 149 setAllFields(builder);
155 TestAllTypes message = builder.build(); 150 TestAllTypes message = builder.build();
156 151
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 + " \"value\": 200\n" 191 + " \"value\": 200\n"
197 + " }],\n" 192 + " }],\n"
198 + " \"repeatedNestedEnum\": [\"BAR\", \"BAZ\"]\n" 193 + " \"repeatedNestedEnum\": [\"BAR\", \"BAZ\"]\n"
199 + "}", 194 + "}",
200 toJsonString(message)); 195 toJsonString(message));
201 196
202 assertRoundTripEquals(message); 197 assertRoundTripEquals(message);
203 } 198 }
204 199
205 public void testUnknownEnumValues() throws Exception { 200 public void testUnknownEnumValues() throws Exception {
206 TestAllTypes message = 201 TestAllTypes message = TestAllTypes.newBuilder()
207 TestAllTypes.newBuilder() 202 .setOptionalNestedEnumValue(12345)
208 .setOptionalNestedEnumValue(12345) 203 .addRepeatedNestedEnumValue(12345)
209 .addRepeatedNestedEnumValue(12345) 204 .addRepeatedNestedEnumValue(0)
210 .addRepeatedNestedEnumValue(0) 205 .build();
211 .build();
212 assertEquals( 206 assertEquals(
213 "{\n" 207 "{\n"
214 + " \"optionalNestedEnum\": 12345,\n" 208 + " \"optionalNestedEnum\": 12345,\n"
215 + " \"repeatedNestedEnum\": [12345, \"FOO\"]\n" 209 + " \"repeatedNestedEnum\": [12345, \"FOO\"]\n"
216 + "}", 210 + "}", toJsonString(message));
217 toJsonString(message));
218 assertRoundTripEquals(message); 211 assertRoundTripEquals(message);
219 212
220 TestMap.Builder mapBuilder = TestMap.newBuilder(); 213 TestMap.Builder mapBuilder = TestMap.newBuilder();
221 mapBuilder.putInt32ToEnumMapValue(1, 0); 214 mapBuilder.getMutableInt32ToEnumMapValue().put(1, 0);
222 Map<Integer, Integer> mapWithInvalidValues = new HashMap<Integer, Integer>() ; 215 mapBuilder.getMutableInt32ToEnumMapValue().put(2, 12345);
223 mapWithInvalidValues.put(2, 12345);
224 mapBuilder.putAllInt32ToEnumMapValue(mapWithInvalidValues);
225 TestMap mapMessage = mapBuilder.build(); 216 TestMap mapMessage = mapBuilder.build();
226 assertEquals( 217 assertEquals(
227 "{\n" 218 "{\n"
228 + " \"int32ToEnumMap\": {\n" 219 + " \"int32ToEnumMap\": {\n"
229 + " \"1\": \"FOO\",\n" 220 + " \"1\": \"FOO\",\n"
230 + " \"2\": 12345\n" 221 + " \"2\": 12345\n"
231 + " }\n" 222 + " }\n"
232 + "}", 223 + "}",
233 toJsonString(mapMessage)); 224 toJsonString(mapMessage));
234 assertRoundTripEquals(mapMessage); 225 assertRoundTripEquals(mapMessage);
235 } 226 }
236 227
237 public void testSpecialFloatValues() throws Exception { 228 public void testSpecialFloatValues() throws Exception {
238 TestAllTypes message = 229 TestAllTypes message = TestAllTypes.newBuilder()
239 TestAllTypes.newBuilder() 230 .addRepeatedFloat(Float.NaN)
240 .addRepeatedFloat(Float.NaN) 231 .addRepeatedFloat(Float.POSITIVE_INFINITY)
241 .addRepeatedFloat(Float.POSITIVE_INFINITY) 232 .addRepeatedFloat(Float.NEGATIVE_INFINITY)
242 .addRepeatedFloat(Float.NEGATIVE_INFINITY) 233 .addRepeatedDouble(Double.NaN)
243 .addRepeatedDouble(Double.NaN) 234 .addRepeatedDouble(Double.POSITIVE_INFINITY)
244 .addRepeatedDouble(Double.POSITIVE_INFINITY) 235 .addRepeatedDouble(Double.NEGATIVE_INFINITY)
245 .addRepeatedDouble(Double.NEGATIVE_INFINITY) 236 .build();
246 .build();
247 assertEquals( 237 assertEquals(
248 "{\n" 238 "{\n"
249 + " \"repeatedFloat\": [\"NaN\", \"Infinity\", \"-Infinity\"],\n" 239 + " \"repeatedFloat\": [\"NaN\", \"Infinity\", \"-Infinity\"],\n"
250 + " \"repeatedDouble\": [\"NaN\", \"Infinity\", \"-Infinity\"]\n" 240 + " \"repeatedDouble\": [\"NaN\", \"Infinity\", \"-Infinity\"]\n"
251 + "}", 241 + "}", toJsonString(message));
252 toJsonString(message));
253 242
254 assertRoundTripEquals(message); 243 assertRoundTripEquals(message);
255 } 244 }
256 245
257 public void testParserAcceptStringForNumbericField() throws Exception { 246 public void testParserAcceptStringForNumbericField() throws Exception {
258 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 247 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
259 mergeFromJson( 248 mergeFromJson(
260 "{\n" 249 "{\n"
261 + " \"optionalInt32\": \"1234\",\n" 250 + " \"optionalInt32\": \"1234\",\n"
262 + " \"optionalUint32\": \"5678\",\n" 251 + " \"optionalUint32\": \"5678\",\n"
263 + " \"optionalSint32\": \"9012\",\n" 252 + " \"optionalSint32\": \"9012\",\n"
264 + " \"optionalFixed32\": \"3456\",\n" 253 + " \"optionalFixed32\": \"3456\",\n"
265 + " \"optionalSfixed32\": \"7890\",\n" 254 + " \"optionalSfixed32\": \"7890\",\n"
266 + " \"optionalFloat\": \"1.5\",\n" 255 + " \"optionalFloat\": \"1.5\",\n"
267 + " \"optionalDouble\": \"1.25\",\n" 256 + " \"optionalDouble\": \"1.25\",\n"
268 + " \"optionalBool\": \"true\"\n" 257 + " \"optionalBool\": \"true\"\n"
269 + "}", 258 + "}", builder);
270 builder);
271 TestAllTypes message = builder.build(); 259 TestAllTypes message = builder.build();
272 assertEquals(1234, message.getOptionalInt32()); 260 assertEquals(1234, message.getOptionalInt32());
273 assertEquals(5678, message.getOptionalUint32()); 261 assertEquals(5678, message.getOptionalUint32());
274 assertEquals(9012, message.getOptionalSint32()); 262 assertEquals(9012, message.getOptionalSint32());
275 assertEquals(3456, message.getOptionalFixed32()); 263 assertEquals(3456, message.getOptionalFixed32());
276 assertEquals(7890, message.getOptionalSfixed32()); 264 assertEquals(7890, message.getOptionalSfixed32());
277 assertEquals(1.5f, message.getOptionalFloat()); 265 assertEquals(1.5f, message.getOptionalFloat());
278 assertEquals(1.25, message.getOptionalDouble()); 266 assertEquals(1.25, message.getOptionalDouble());
279 assertEquals(true, message.getOptionalBool()); 267 assertEquals(true, message.getOptionalBool());
280 } 268 }
281 269
282 public void testParserAcceptFloatingPointValueForIntegerField() throws Excepti on { 270 public void testParserAcceptFloatingPointValueForIntegerField() throws Excepti on {
283 // Test that numeric values like "1.000", "1e5" will also be accepted. 271 // Test that numeric values like "1.000", "1e5" will also be accepted.
284 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 272 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
285 mergeFromJson( 273 mergeFromJson(
286 "{\n" 274 "{\n"
287 + " \"repeatedInt32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n" 275 + " \"repeatedInt32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
288 + " \"repeatedUint32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n" 276 + " \"repeatedUint32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
289 + " \"repeatedInt64\": [1.000, 1e5, \"1.000\", \"1e5\"],\n" 277 + " \"repeatedInt64\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
290 + " \"repeatedUint64\": [1.000, 1e5, \"1.000\", \"1e5\"]\n" 278 + " \"repeatedUint64\": [1.000, 1e5, \"1.000\", \"1e5\"]\n"
291 + "}", 279 + "}", builder);
292 builder); 280 int[] expectedValues = new int[]{1, 100000, 1, 100000};
293 int[] expectedValues = new int[] {1, 100000, 1, 100000};
294 assertEquals(4, builder.getRepeatedInt32Count()); 281 assertEquals(4, builder.getRepeatedInt32Count());
295 assertEquals(4, builder.getRepeatedUint32Count()); 282 assertEquals(4, builder.getRepeatedUint32Count());
296 assertEquals(4, builder.getRepeatedInt64Count()); 283 assertEquals(4, builder.getRepeatedInt64Count());
297 assertEquals(4, builder.getRepeatedUint64Count()); 284 assertEquals(4, builder.getRepeatedUint64Count());
298 for (int i = 0; i < 4; ++i) { 285 for (int i = 0; i < 4; ++i) {
299 assertEquals(expectedValues[i], builder.getRepeatedInt32(i)); 286 assertEquals(expectedValues[i], builder.getRepeatedInt32(i));
300 assertEquals(expectedValues[i], builder.getRepeatedUint32(i)); 287 assertEquals(expectedValues[i], builder.getRepeatedUint32(i));
301 assertEquals(expectedValues[i], builder.getRepeatedInt64(i)); 288 assertEquals(expectedValues[i], builder.getRepeatedInt64(i));
302 assertEquals(expectedValues[i], builder.getRepeatedUint64(i)); 289 assertEquals(expectedValues[i], builder.getRepeatedUint64(i));
303 } 290 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 assertAccepts("optionalDouble", maxDouble.toString()); 359 assertAccepts("optionalDouble", maxDouble.toString());
373 assertAccepts("optionalDouble", minDouble.toString()); 360 assertAccepts("optionalDouble", minDouble.toString());
374 assertRejects("optionalDouble", maxDouble.multiply(moreThanOne).toString()); 361 assertRejects("optionalDouble", maxDouble.multiply(moreThanOne).toString());
375 assertRejects("optionalDouble", minDouble.multiply(moreThanOne).toString()); 362 assertRejects("optionalDouble", minDouble.multiply(moreThanOne).toString());
376 } 363 }
377 364
378 public void testParserAcceptNull() throws Exception { 365 public void testParserAcceptNull() throws Exception {
379 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 366 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
380 mergeFromJson( 367 mergeFromJson(
381 "{\n" 368 "{\n"
382 + " \"optionalInt32\": null,\n" 369 + " \"optionalInt32\": null,\n"
383 + " \"optionalInt64\": null,\n" 370 + " \"optionalInt64\": null,\n"
384 + " \"optionalUint32\": null,\n" 371 + " \"optionalUint32\": null,\n"
385 + " \"optionalUint64\": null,\n" 372 + " \"optionalUint64\": null,\n"
386 + " \"optionalSint32\": null,\n" 373 + " \"optionalSint32\": null,\n"
387 + " \"optionalSint64\": null,\n" 374 + " \"optionalSint64\": null,\n"
388 + " \"optionalFixed32\": null,\n" 375 + " \"optionalFixed32\": null,\n"
389 + " \"optionalFixed64\": null,\n" 376 + " \"optionalFixed64\": null,\n"
390 + " \"optionalSfixed32\": null,\n" 377 + " \"optionalSfixed32\": null,\n"
391 + " \"optionalSfixed64\": null,\n" 378 + " \"optionalSfixed64\": null,\n"
392 + " \"optionalFloat\": null,\n" 379 + " \"optionalFloat\": null,\n"
393 + " \"optionalDouble\": null,\n" 380 + " \"optionalDouble\": null,\n"
394 + " \"optionalBool\": null,\n" 381 + " \"optionalBool\": null,\n"
395 + " \"optionalString\": null,\n" 382 + " \"optionalString\": null,\n"
396 + " \"optionalBytes\": null,\n" 383 + " \"optionalBytes\": null,\n"
397 + " \"optionalNestedMessage\": null,\n" 384 + " \"optionalNestedMessage\": null,\n"
398 + " \"optionalNestedEnum\": null,\n" 385 + " \"optionalNestedEnum\": null,\n"
399 + " \"repeatedInt32\": null,\n" 386 + " \"repeatedInt32\": null,\n"
400 + " \"repeatedInt64\": null,\n" 387 + " \"repeatedInt64\": null,\n"
401 + " \"repeatedUint32\": null,\n" 388 + " \"repeatedUint32\": null,\n"
402 + " \"repeatedUint64\": null,\n" 389 + " \"repeatedUint64\": null,\n"
403 + " \"repeatedSint32\": null,\n" 390 + " \"repeatedSint32\": null,\n"
404 + " \"repeatedSint64\": null,\n" 391 + " \"repeatedSint64\": null,\n"
405 + " \"repeatedFixed32\": null,\n" 392 + " \"repeatedFixed32\": null,\n"
406 + " \"repeatedFixed64\": null,\n" 393 + " \"repeatedFixed64\": null,\n"
407 + " \"repeatedSfixed32\": null,\n" 394 + " \"repeatedSfixed32\": null,\n"
408 + " \"repeatedSfixed64\": null,\n" 395 + " \"repeatedSfixed64\": null,\n"
409 + " \"repeatedFloat\": null,\n" 396 + " \"repeatedFloat\": null,\n"
410 + " \"repeatedDouble\": null,\n" 397 + " \"repeatedDouble\": null,\n"
411 + " \"repeatedBool\": null,\n" 398 + " \"repeatedBool\": null,\n"
412 + " \"repeatedString\": null,\n" 399 + " \"repeatedString\": null,\n"
413 + " \"repeatedBytes\": null,\n" 400 + " \"repeatedBytes\": null,\n"
414 + " \"repeatedNestedMessage\": null,\n" 401 + " \"repeatedNestedMessage\": null,\n"
415 + " \"repeatedNestedEnum\": null\n" 402 + " \"repeatedNestedEnum\": null\n"
416 + "}", 403 + "}", builder);
417 builder);
418 TestAllTypes message = builder.build(); 404 TestAllTypes message = builder.build();
419 assertEquals(TestAllTypes.getDefaultInstance(), message); 405 assertEquals(TestAllTypes.getDefaultInstance(), message);
420 406
421 // Repeated field elements cannot be null. 407 // Repeated field elements cannot be null.
422 try { 408 try {
423 builder = TestAllTypes.newBuilder(); 409 builder = TestAllTypes.newBuilder();
424 mergeFromJson("{\n" + " \"repeatedInt32\": [null, null],\n" + "}", builde r); 410 mergeFromJson(
411 "{\n"
412 + " \"repeatedInt32\": [null, null],\n"
413 + "}", builder);
425 fail(); 414 fail();
426 } catch (InvalidProtocolBufferException e) { 415 } catch (InvalidProtocolBufferException e) {
427 // Exception expected. 416 // Exception expected.
428 } 417 }
429 418
430 try { 419 try {
431 builder = TestAllTypes.newBuilder(); 420 builder = TestAllTypes.newBuilder();
432 mergeFromJson("{\n" + " \"repeatedNestedMessage\": [null, null],\n" + "}" , builder); 421 mergeFromJson(
422 "{\n"
423 + " \"repeatedNestedMessage\": [null, null],\n"
424 + "}", builder);
433 fail(); 425 fail();
434 } catch (InvalidProtocolBufferException e) { 426 } catch (InvalidProtocolBufferException e) {
435 // Exception expected. 427 // Exception expected.
436 } 428 }
437 } 429 }
438 430
439 public void testNullInOneof() throws Exception {
440 TestOneof.Builder builder = TestOneof.newBuilder();
441 mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", builder);
442 TestOneof message = builder.build();
443 assertEquals(TestOneof.OneofFieldCase.ONEOF_NULL_VALUE, message.getOneofFiel dCase());
444 assertEquals(NullValue.NULL_VALUE, message.getOneofNullValue());
445 }
446
447 public void testParserRejectDuplicatedFields() throws Exception { 431 public void testParserRejectDuplicatedFields() throws Exception {
448 // TODO(xiaofeng): The parser we are currently using (GSON) will accept and keep the last 432 // TODO(xiaofeng): The parser we are currently using (GSON) will accept and keep the last
449 // one if multiple entries have the same name. This is not the desired behav ior but it can 433 // one if multiple entries have the same name. This is not the desired behav ior but it can
450 // only be fixed by using our own parser. Here we only test the cases where the names are 434 // only be fixed by using our own parser. Here we only test the cases where the names are
451 // different but still referring to the same field. 435 // different but still referring to the same field.
452 436
453 // Duplicated optional fields. 437 // Duplicated optional fields.
454 try { 438 try {
455 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 439 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
456 mergeFromJson( 440 mergeFromJson(
457 "{\n" 441 "{\n"
458 + " \"optionalNestedMessage\": {},\n" 442 + " \"optionalNestedMessage\": {},\n"
459 + " \"optional_nested_message\": {}\n" 443 + " \"optional_nested_message\": {}\n"
460 + "}", 444 + "}", builder);
461 builder);
462 fail(); 445 fail();
463 } catch (InvalidProtocolBufferException e) { 446 } catch (InvalidProtocolBufferException e) {
464 // Exception expected. 447 // Exception expected.
465 } 448 }
466 449
467 // Duplicated repeated fields. 450 // Duplicated repeated fields.
468 try { 451 try {
469 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 452 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
470 mergeFromJson( 453 mergeFromJson(
471 "{\n" 454 "{\n"
472 + " \"repeatedNestedMessage\": [null, null],\n" 455 + " \"repeatedNestedMessage\": [null, null],\n"
473 + " \"repeated_nested_message\": [null, null]\n" 456 + " \"repeated_nested_message\": [null, null]\n"
474 + "}", 457 + "}", builder);
475 builder);
476 fail(); 458 fail();
477 } catch (InvalidProtocolBufferException e) { 459 } catch (InvalidProtocolBufferException e) {
478 // Exception expected. 460 // Exception expected.
479 } 461 }
480 462
481 // Duplicated oneof fields. 463 // Duplicated oneof fields.
482 try { 464 try {
483 TestOneof.Builder builder = TestOneof.newBuilder(); 465 TestOneof.Builder builder = TestOneof.newBuilder();
484 mergeFromJson("{\n" + " \"oneofInt32\": 1,\n" + " \"oneof_int32\": 2\n" + "}", builder); 466 mergeFromJson(
467 "{\n"
468 + " \"oneofInt32\": 1,\n"
469 + " \"oneof_int32\": 2\n"
470 + "}", builder);
485 fail(); 471 fail();
486 } catch (InvalidProtocolBufferException e) { 472 } catch (InvalidProtocolBufferException e) {
487 // Exception expected. 473 // Exception expected.
488 } 474 }
489 } 475 }
490 476
491 public void testMapFields() throws Exception { 477 public void testMapFields() throws Exception {
492 TestMap.Builder builder = TestMap.newBuilder(); 478 TestMap.Builder builder = TestMap.newBuilder();
493 builder.putInt32ToInt32Map(1, 10); 479 builder.getMutableInt32ToInt32Map().put(1, 10);
494 builder.putInt64ToInt32Map(1234567890123456789L, 10); 480 builder.getMutableInt64ToInt32Map().put(1234567890123456789L, 10);
495 builder.putUint32ToInt32Map(2, 20); 481 builder.getMutableUint32ToInt32Map().put(2, 20);
496 builder.putUint64ToInt32Map(2234567890123456789L, 20); 482 builder.getMutableUint64ToInt32Map().put(2234567890123456789L, 20);
497 builder.putSint32ToInt32Map(3, 30); 483 builder.getMutableSint32ToInt32Map().put(3, 30);
498 builder.putSint64ToInt32Map(3234567890123456789L, 30); 484 builder.getMutableSint64ToInt32Map().put(3234567890123456789L, 30);
499 builder.putFixed32ToInt32Map(4, 40); 485 builder.getMutableFixed32ToInt32Map().put(4, 40);
500 builder.putFixed64ToInt32Map(4234567890123456789L, 40); 486 builder.getMutableFixed64ToInt32Map().put(4234567890123456789L, 40);
501 builder.putSfixed32ToInt32Map(5, 50); 487 builder.getMutableSfixed32ToInt32Map().put(5, 50);
502 builder.putSfixed64ToInt32Map(5234567890123456789L, 50); 488 builder.getMutableSfixed64ToInt32Map().put(5234567890123456789L, 50);
503 builder.putBoolToInt32Map(false, 6); 489 builder.getMutableBoolToInt32Map().put(false, 6);
504 builder.putStringToInt32Map("Hello", 10); 490 builder.getMutableStringToInt32Map().put("Hello", 10);
505 491
506 builder.putInt32ToInt64Map(1, 1234567890123456789L); 492 builder.getMutableInt32ToInt64Map().put(1, 1234567890123456789L);
507 builder.putInt32ToUint32Map(2, 20); 493 builder.getMutableInt32ToUint32Map().put(2, 20);
508 builder.putInt32ToUint64Map(2, 2234567890123456789L); 494 builder.getMutableInt32ToUint64Map().put(2, 2234567890123456789L);
509 builder.putInt32ToSint32Map(3, 30); 495 builder.getMutableInt32ToSint32Map().put(3, 30);
510 builder.putInt32ToSint64Map(3, 3234567890123456789L); 496 builder.getMutableInt32ToSint64Map().put(3, 3234567890123456789L);
511 builder.putInt32ToFixed32Map(4, 40); 497 builder.getMutableInt32ToFixed32Map().put(4, 40);
512 builder.putInt32ToFixed64Map(4, 4234567890123456789L); 498 builder.getMutableInt32ToFixed64Map().put(4, 4234567890123456789L);
513 builder.putInt32ToSfixed32Map(5, 50); 499 builder.getMutableInt32ToSfixed32Map().put(5, 50);
514 builder.putInt32ToSfixed64Map(5, 5234567890123456789L); 500 builder.getMutableInt32ToSfixed64Map().put(5, 5234567890123456789L);
515 builder.putInt32ToFloatMap(6, 1.5f); 501 builder.getMutableInt32ToFloatMap().put(6, 1.5f);
516 builder.putInt32ToDoubleMap(6, 1.25); 502 builder.getMutableInt32ToDoubleMap().put(6, 1.25);
517 builder.putInt32ToBoolMap(7, false); 503 builder.getMutableInt32ToBoolMap().put(7, false);
518 builder.putInt32ToStringMap(7, "World"); 504 builder.getMutableInt32ToStringMap().put(7, "World");
519 builder.putInt32ToBytesMap(8, ByteString.copyFrom(new byte[] {1, 2, 3})); 505 builder.getMutableInt32ToBytesMap().put(
520 builder.putInt32ToMessageMap(8, NestedMessage.newBuilder().setValue(1234).bu ild()); 506 8, ByteString.copyFrom(new byte[]{1, 2, 3}));
521 builder.putInt32ToEnumMap(9, NestedEnum.BAR); 507 builder.getMutableInt32ToMessageMap().put(
508 8, NestedMessage.newBuilder().setValue(1234).build());
509 builder.getMutableInt32ToEnumMap().put(9, NestedEnum.BAR);
522 TestMap message = builder.build(); 510 TestMap message = builder.build();
523 511
524 assertEquals( 512 assertEquals(
525 "{\n" 513 "{\n"
526 + " \"int32ToInt32Map\": {\n" 514 + " \"int32ToInt32Map\": {\n"
527 + " \"1\": 10\n" 515 + " \"1\": 10\n"
528 + " },\n" 516 + " },\n"
529 + " \"int64ToInt32Map\": {\n" 517 + " \"int64ToInt32Map\": {\n"
530 + " \"1234567890123456789\": 10\n" 518 + " \"1234567890123456789\": 10\n"
531 + " },\n" 519 + " },\n"
532 + " \"uint32ToInt32Map\": {\n" 520 + " \"uint32ToInt32Map\": {\n"
533 + " \"2\": 20\n" 521 + " \"2\": 20\n"
534 + " },\n" 522 + " },\n"
535 + " \"uint64ToInt32Map\": {\n" 523 + " \"uint64ToInt32Map\": {\n"
536 + " \"2234567890123456789\": 20\n" 524 + " \"2234567890123456789\": 20\n"
537 + " },\n" 525 + " },\n"
538 + " \"sint32ToInt32Map\": {\n" 526 + " \"sint32ToInt32Map\": {\n"
539 + " \"3\": 30\n" 527 + " \"3\": 30\n"
540 + " },\n" 528 + " },\n"
541 + " \"sint64ToInt32Map\": {\n" 529 + " \"sint64ToInt32Map\": {\n"
542 + " \"3234567890123456789\": 30\n" 530 + " \"3234567890123456789\": 30\n"
543 + " },\n" 531 + " },\n"
544 + " \"fixed32ToInt32Map\": {\n" 532 + " \"fixed32ToInt32Map\": {\n"
545 + " \"4\": 40\n" 533 + " \"4\": 40\n"
546 + " },\n" 534 + " },\n"
547 + " \"fixed64ToInt32Map\": {\n" 535 + " \"fixed64ToInt32Map\": {\n"
548 + " \"4234567890123456789\": 40\n" 536 + " \"4234567890123456789\": 40\n"
549 + " },\n" 537 + " },\n"
550 + " \"sfixed32ToInt32Map\": {\n" 538 + " \"sfixed32ToInt32Map\": {\n"
551 + " \"5\": 50\n" 539 + " \"5\": 50\n"
552 + " },\n" 540 + " },\n"
553 + " \"sfixed64ToInt32Map\": {\n" 541 + " \"sfixed64ToInt32Map\": {\n"
554 + " \"5234567890123456789\": 50\n" 542 + " \"5234567890123456789\": 50\n"
555 + " },\n" 543 + " },\n"
556 + " \"boolToInt32Map\": {\n" 544 + " \"boolToInt32Map\": {\n"
557 + " \"false\": 6\n" 545 + " \"false\": 6\n"
558 + " },\n" 546 + " },\n"
559 + " \"stringToInt32Map\": {\n" 547 + " \"stringToInt32Map\": {\n"
560 + " \"Hello\": 10\n" 548 + " \"Hello\": 10\n"
561 + " },\n" 549 + " },\n"
562 + " \"int32ToInt64Map\": {\n" 550 + " \"int32ToInt64Map\": {\n"
563 + " \"1\": \"1234567890123456789\"\n" 551 + " \"1\": \"1234567890123456789\"\n"
564 + " },\n" 552 + " },\n"
565 + " \"int32ToUint32Map\": {\n" 553 + " \"int32ToUint32Map\": {\n"
566 + " \"2\": 20\n" 554 + " \"2\": 20\n"
567 + " },\n" 555 + " },\n"
568 + " \"int32ToUint64Map\": {\n" 556 + " \"int32ToUint64Map\": {\n"
569 + " \"2\": \"2234567890123456789\"\n" 557 + " \"2\": \"2234567890123456789\"\n"
570 + " },\n" 558 + " },\n"
571 + " \"int32ToSint32Map\": {\n" 559 + " \"int32ToSint32Map\": {\n"
572 + " \"3\": 30\n" 560 + " \"3\": 30\n"
573 + " },\n" 561 + " },\n"
574 + " \"int32ToSint64Map\": {\n" 562 + " \"int32ToSint64Map\": {\n"
575 + " \"3\": \"3234567890123456789\"\n" 563 + " \"3\": \"3234567890123456789\"\n"
576 + " },\n" 564 + " },\n"
577 + " \"int32ToFixed32Map\": {\n" 565 + " \"int32ToFixed32Map\": {\n"
578 + " \"4\": 40\n" 566 + " \"4\": 40\n"
579 + " },\n" 567 + " },\n"
580 + " \"int32ToFixed64Map\": {\n" 568 + " \"int32ToFixed64Map\": {\n"
581 + " \"4\": \"4234567890123456789\"\n" 569 + " \"4\": \"4234567890123456789\"\n"
582 + " },\n" 570 + " },\n"
583 + " \"int32ToSfixed32Map\": {\n" 571 + " \"int32ToSfixed32Map\": {\n"
584 + " \"5\": 50\n" 572 + " \"5\": 50\n"
585 + " },\n" 573 + " },\n"
586 + " \"int32ToSfixed64Map\": {\n" 574 + " \"int32ToSfixed64Map\": {\n"
587 + " \"5\": \"5234567890123456789\"\n" 575 + " \"5\": \"5234567890123456789\"\n"
588 + " },\n" 576 + " },\n"
589 + " \"int32ToFloatMap\": {\n" 577 + " \"int32ToFloatMap\": {\n"
590 + " \"6\": 1.5\n" 578 + " \"6\": 1.5\n"
591 + " },\n" 579 + " },\n"
592 + " \"int32ToDoubleMap\": {\n" 580 + " \"int32ToDoubleMap\": {\n"
593 + " \"6\": 1.25\n" 581 + " \"6\": 1.25\n"
594 + " },\n" 582 + " },\n"
595 + " \"int32ToBoolMap\": {\n" 583 + " \"int32ToBoolMap\": {\n"
596 + " \"7\": false\n" 584 + " \"7\": false\n"
597 + " },\n" 585 + " },\n"
598 + " \"int32ToStringMap\": {\n" 586 + " \"int32ToStringMap\": {\n"
599 + " \"7\": \"World\"\n" 587 + " \"7\": \"World\"\n"
600 + " },\n" 588 + " },\n"
601 + " \"int32ToBytesMap\": {\n" 589 + " \"int32ToBytesMap\": {\n"
602 + " \"8\": \"AQID\"\n" 590 + " \"8\": \"AQID\"\n"
603 + " },\n" 591 + " },\n"
604 + " \"int32ToMessageMap\": {\n" 592 + " \"int32ToMessageMap\": {\n"
605 + " \"8\": {\n" 593 + " \"8\": {\n"
606 + " \"value\": 1234\n" 594 + " \"value\": 1234\n"
607 + " }\n" 595 + " }\n"
608 + " },\n" 596 + " },\n"
609 + " \"int32ToEnumMap\": {\n" 597 + " \"int32ToEnumMap\": {\n"
610 + " \"9\": \"BAR\"\n" 598 + " \"9\": \"BAR\"\n"
611 + " }\n" 599 + " }\n"
612 + "}", 600 + "}", toJsonString(message));
613 toJsonString(message));
614 assertRoundTripEquals(message); 601 assertRoundTripEquals(message);
615 602
616 // Test multiple entries. 603 // Test multiple entries.
617 builder = TestMap.newBuilder(); 604 builder = TestMap.newBuilder();
618 builder.putInt32ToInt32Map(1, 2); 605 builder.getMutableInt32ToInt32Map().put(1, 2);
619 builder.putInt32ToInt32Map(3, 4); 606 builder.getMutableInt32ToInt32Map().put(3, 4);
620 message = builder.build(); 607 message = builder.build();
621 608
622 assertEquals( 609 assertEquals(
623 "{\n" + " \"int32ToInt32Map\": {\n" + " \"1\": 2,\n" + " \"3\": 4 \n" + " }\n" + "}", 610 "{\n"
624 toJsonString(message)); 611 + " \"int32ToInt32Map\": {\n"
612 + " \"1\": 2,\n"
613 + " \"3\": 4\n"
614 + " }\n"
615 + "}", toJsonString(message));
625 assertRoundTripEquals(message); 616 assertRoundTripEquals(message);
626 } 617 }
627 618
628 public void testMapNullValueIsRejected() throws Exception { 619 public void testMapNullValueIsRejected() throws Exception {
629 try { 620 try {
630 TestMap.Builder builder = TestMap.newBuilder(); 621 TestMap.Builder builder = TestMap.newBuilder();
631 mergeFromJson( 622 mergeFromJson(
632 "{\n" 623 "{\n"
633 + " \"int32ToInt32Map\": {null: 1},\n" 624 + " \"int32ToInt32Map\": {null: 1},\n"
634 + " \"int32ToMessageMap\": {null: 2}\n" 625 + " \"int32ToMessageMap\": {null: 2}\n"
635 + "}", 626 + "}", builder);
636 builder);
637 fail(); 627 fail();
638 } catch (InvalidProtocolBufferException e) { 628 } catch (InvalidProtocolBufferException e) {
639 // Exception expected. 629 // Exception expected.
640 } 630 }
641 631
642 try { 632 try {
643 TestMap.Builder builder = TestMap.newBuilder(); 633 TestMap.Builder builder = TestMap.newBuilder();
644 mergeFromJson( 634 mergeFromJson(
645 "{\n" 635 "{\n"
646 + " \"int32ToInt32Map\": {\"1\": null},\n" 636 + " \"int32ToInt32Map\": {\"1\": null},\n"
647 + " \"int32ToMessageMap\": {\"2\": null}\n" 637 + " \"int32ToMessageMap\": {\"2\": null}\n"
648 + "}", 638 + "}", builder);
649 builder);
650 fail(); 639 fail();
651 } catch (InvalidProtocolBufferException e) { 640 } catch (InvalidProtocolBufferException e) {
652 // Exception expected. 641 // Exception expected.
653 } 642 }
654 } 643 }
655 644
656 public void testParserAcceptNonQuotedObjectKey() throws Exception { 645 public void testParserAcceptNonQuotedObjectKey() throws Exception {
657 TestMap.Builder builder = TestMap.newBuilder(); 646 TestMap.Builder builder = TestMap.newBuilder();
658 mergeFromJson( 647 mergeFromJson(
659 "{\n" + " int32ToInt32Map: {1: 2},\n" + " stringToInt32Map: {hello: 3} \n" + "}", builder); 648 "{\n"
649 + " int32ToInt32Map: {1: 2},\n"
650 + " stringToInt32Map: {hello: 3}\n"
651 + "}", builder);
660 TestMap message = builder.build(); 652 TestMap message = builder.build();
661 assertEquals(2, message.getInt32ToInt32Map().get(1).intValue()); 653 assertEquals(2, message.getInt32ToInt32Map().get(1).intValue());
662 assertEquals(3, message.getStringToInt32Map().get("hello").intValue()); 654 assertEquals(3, message.getStringToInt32Map().get("hello").intValue());
663 } 655 }
664 656
665 public void testWrappers() throws Exception { 657 public void testWrappers() throws Exception {
666 TestWrappers.Builder builder = TestWrappers.newBuilder(); 658 TestWrappers.Builder builder = TestWrappers.newBuilder();
667 builder.getBoolValueBuilder().setValue(false); 659 builder.getBoolValueBuilder().setValue(false);
668 builder.getInt32ValueBuilder().setValue(0); 660 builder.getInt32ValueBuilder().setValue(0);
669 builder.getInt64ValueBuilder().setValue(0); 661 builder.getInt64ValueBuilder().setValue(0);
670 builder.getUint32ValueBuilder().setValue(0); 662 builder.getUint32ValueBuilder().setValue(0);
671 builder.getUint64ValueBuilder().setValue(0); 663 builder.getUint64ValueBuilder().setValue(0);
672 builder.getFloatValueBuilder().setValue(0.0f); 664 builder.getFloatValueBuilder().setValue(0.0f);
673 builder.getDoubleValueBuilder().setValue(0.0); 665 builder.getDoubleValueBuilder().setValue(0.0);
674 builder.getStringValueBuilder().setValue(""); 666 builder.getStringValueBuilder().setValue("");
675 builder.getBytesValueBuilder().setValue(ByteString.EMPTY); 667 builder.getBytesValueBuilder().setValue(ByteString.EMPTY);
676 TestWrappers message = builder.build(); 668 TestWrappers message = builder.build();
677 669
678 assertEquals( 670 assertEquals(
679 "{\n" 671 "{\n"
680 + " \"int32Value\": 0,\n" 672 + " \"int32Value\": 0,\n"
681 + " \"uint32Value\": 0,\n" 673 + " \"uint32Value\": 0,\n"
682 + " \"int64Value\": \"0\",\n" 674 + " \"int64Value\": \"0\",\n"
683 + " \"uint64Value\": \"0\",\n" 675 + " \"uint64Value\": \"0\",\n"
684 + " \"floatValue\": 0.0,\n" 676 + " \"floatValue\": 0.0,\n"
685 + " \"doubleValue\": 0.0,\n" 677 + " \"doubleValue\": 0.0,\n"
686 + " \"boolValue\": false,\n" 678 + " \"boolValue\": false,\n"
687 + " \"stringValue\": \"\",\n" 679 + " \"stringValue\": \"\",\n"
688 + " \"bytesValue\": \"\"\n" 680 + " \"bytesValue\": \"\"\n"
689 + "}", 681 + "}", toJsonString(message));
690 toJsonString(message));
691 assertRoundTripEquals(message); 682 assertRoundTripEquals(message);
692 683
693 builder = TestWrappers.newBuilder(); 684 builder = TestWrappers.newBuilder();
694 builder.getBoolValueBuilder().setValue(true); 685 builder.getBoolValueBuilder().setValue(true);
695 builder.getInt32ValueBuilder().setValue(1); 686 builder.getInt32ValueBuilder().setValue(1);
696 builder.getInt64ValueBuilder().setValue(2); 687 builder.getInt64ValueBuilder().setValue(2);
697 builder.getUint32ValueBuilder().setValue(3); 688 builder.getUint32ValueBuilder().setValue(3);
698 builder.getUint64ValueBuilder().setValue(4); 689 builder.getUint64ValueBuilder().setValue(4);
699 builder.getFloatValueBuilder().setValue(5.0f); 690 builder.getFloatValueBuilder().setValue(5.0f);
700 builder.getDoubleValueBuilder().setValue(6.0); 691 builder.getDoubleValueBuilder().setValue(6.0);
701 builder.getStringValueBuilder().setValue("7"); 692 builder.getStringValueBuilder().setValue("7");
702 builder.getBytesValueBuilder().setValue(ByteString.copyFrom(new byte[] {8})) ; 693 builder.getBytesValueBuilder().setValue(ByteString.copyFrom(new byte[]{8}));
703 message = builder.build(); 694 message = builder.build();
704 695
705 assertEquals( 696 assertEquals(
706 "{\n" 697 "{\n"
707 + " \"int32Value\": 1,\n" 698 + " \"int32Value\": 1,\n"
708 + " \"uint32Value\": 3,\n" 699 + " \"uint32Value\": 3,\n"
709 + " \"int64Value\": \"2\",\n" 700 + " \"int64Value\": \"2\",\n"
710 + " \"uint64Value\": \"4\",\n" 701 + " \"uint64Value\": \"4\",\n"
711 + " \"floatValue\": 5.0,\n" 702 + " \"floatValue\": 5.0,\n"
712 + " \"doubleValue\": 6.0,\n" 703 + " \"doubleValue\": 6.0,\n"
713 + " \"boolValue\": true,\n" 704 + " \"boolValue\": true,\n"
714 + " \"stringValue\": \"7\",\n" 705 + " \"stringValue\": \"7\",\n"
715 + " \"bytesValue\": \"CA==\"\n" 706 + " \"bytesValue\": \"CA==\"\n"
716 + "}", 707 + "}", toJsonString(message));
717 toJsonString(message));
718 assertRoundTripEquals(message); 708 assertRoundTripEquals(message);
719 } 709 }
720 710
721 public void testTimestamp() throws Exception { 711 public void testTimestamp() throws Exception {
722 TestTimestamp message = 712 TestTimestamp message = TestTimestamp.newBuilder()
723 TestTimestamp.newBuilder() 713 .setTimestampValue(TimeUtil.parseTimestamp("1970-01-01T00:00:00Z"))
724 .setTimestampValue(Timestamps.parse("1970-01-01T00:00:00Z")) 714 .build();
725 .build();
726 715
727 assertEquals( 716 assertEquals(
728 "{\n" + " \"timestampValue\": \"1970-01-01T00:00:00Z\"\n" + "}", toJson String(message)); 717 "{\n"
718 + " \"timestampValue\": \"1970-01-01T00:00:00Z\"\n"
719 + "}", toJsonString(message));
729 assertRoundTripEquals(message); 720 assertRoundTripEquals(message);
730 } 721 }
731 722
732 public void testDuration() throws Exception { 723 public void testDuration() throws Exception {
733 TestDuration message = 724 TestDuration message = TestDuration.newBuilder()
734 TestDuration.newBuilder().setDurationValue(Durations.parse("12345s")).bu ild(); 725 .setDurationValue(TimeUtil.parseDuration("12345s"))
726 .build();
735 727
736 assertEquals("{\n" + " \"durationValue\": \"12345s\"\n" + "}", toJsonString (message)); 728 assertEquals(
729 "{\n"
730 + " \"durationValue\": \"12345s\"\n"
731 + "}", toJsonString(message));
737 assertRoundTripEquals(message); 732 assertRoundTripEquals(message);
738 } 733 }
739 734
740 public void testFieldMask() throws Exception { 735 public void testFieldMask() throws Exception {
741 TestFieldMask message = 736 TestFieldMask message = TestFieldMask.newBuilder()
742 TestFieldMask.newBuilder() 737 .setFieldMaskValue(FieldMaskUtil.fromString("foo.bar,baz"))
743 .setFieldMaskValue(FieldMaskUtil.fromString("foo.bar,baz,foo_bar.baz ")) 738 .build();
744 .build();
745 739
746 assertEquals( 740 assertEquals(
747 "{\n" + " \"fieldMaskValue\": \"foo.bar,baz,fooBar.baz\"\n" + "}", toJs onString(message)); 741 "{\n"
742 + " \"fieldMaskValue\": \"foo.bar,baz\"\n"
743 + "}", toJsonString(message));
748 assertRoundTripEquals(message); 744 assertRoundTripEquals(message);
749 } 745 }
750 746
751 public void testStruct() throws Exception { 747 public void testStruct() throws Exception {
752 // Build a struct with all possible values. 748 // Build a struct with all possible values.
753 TestStruct.Builder builder = TestStruct.newBuilder(); 749 TestStruct.Builder builder = TestStruct.newBuilder();
754 Struct.Builder structBuilder = builder.getStructValueBuilder(); 750 Struct.Builder structBuilder = builder.getStructValueBuilder();
755 structBuilder.putFields("null_value", Value.newBuilder().setNullValueValue(0 ).build()); 751 structBuilder.getMutableFields().put(
756 structBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1. 25).build()); 752 "null_value", Value.newBuilder().setNullValueValue(0).build());
757 structBuilder.putFields("string_value", Value.newBuilder().setStringValue("h ello").build()); 753 structBuilder.getMutableFields().put(
754 "number_value", Value.newBuilder().setNumberValue(1.25).build());
755 structBuilder.getMutableFields().put(
756 "string_value", Value.newBuilder().setStringValue("hello").build());
758 Struct.Builder subStructBuilder = Struct.newBuilder(); 757 Struct.Builder subStructBuilder = Struct.newBuilder();
759 subStructBuilder.putFields("number_value", Value.newBuilder().setNumberValue (1234).build()); 758 subStructBuilder.getMutableFields().put(
760 structBuilder.putFields( 759 "number_value", Value.newBuilder().setNumberValue(1234).build());
760 structBuilder.getMutableFields().put(
761 "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build ()).build()); 761 "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build ()).build());
762 ListValue.Builder listBuilder = ListValue.newBuilder(); 762 ListValue.Builder listBuilder = ListValue.newBuilder();
763 listBuilder.addValues(Value.newBuilder().setNumberValue(1.125).build()); 763 listBuilder.addValues(Value.newBuilder().setNumberValue(1.125).build());
764 listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build()); 764 listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
765 structBuilder.putFields( 765 structBuilder.getMutableFields().put(
766 "list_value", Value.newBuilder().setListValue(listBuilder.build()).build ()); 766 "list_value", Value.newBuilder().setListValue(listBuilder.build()).build ());
767 TestStruct message = builder.build(); 767 TestStruct message = builder.build();
768 768
769 assertEquals( 769 assertEquals(
770 "{\n" 770 "{\n"
771 + " \"structValue\": {\n" 771 + " \"structValue\": {\n"
772 + " \"null_value\": null,\n" 772 + " \"null_value\": null,\n"
773 + " \"number_value\": 1.25,\n" 773 + " \"number_value\": 1.25,\n"
774 + " \"string_value\": \"hello\",\n" 774 + " \"string_value\": \"hello\",\n"
775 + " \"struct_value\": {\n" 775 + " \"struct_value\": {\n"
776 + " \"number_value\": 1234.0\n" 776 + " \"number_value\": 1234.0\n"
777 + " },\n" 777 + " },\n"
778 + " \"list_value\": [1.125, null]\n" 778 + " \"list_value\": [1.125, null]\n"
779 + " }\n" 779 + " }\n"
780 + "}", 780 + "}", toJsonString(message));
781 toJsonString(message));
782 assertRoundTripEquals(message); 781 assertRoundTripEquals(message);
783 782
784 builder = TestStruct.newBuilder(); 783 builder = TestStruct.newBuilder();
785 builder.setValue(Value.newBuilder().setNullValueValue(0).build()); 784 builder.setValue(Value.newBuilder().setNullValueValue(0).build());
786 message = builder.build(); 785 message = builder.build();
787 assertEquals("{\n" + " \"value\": null\n" + "}", toJsonString(message)); 786 assertEquals(
787 "{\n"
788 + " \"value\": null\n"
789 + "}", toJsonString(message));
788 assertRoundTripEquals(message); 790 assertRoundTripEquals(message);
789 791
790 builder = TestStruct.newBuilder(); 792 builder = TestStruct.newBuilder();
791 listBuilder = builder.getListValueBuilder(); 793 listBuilder = builder.getListValueBuilder();
792 listBuilder.addValues(Value.newBuilder().setNumberValue(31831.125).build()); 794 listBuilder.addValues(Value.newBuilder().setNumberValue(31831.125).build());
793 listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build()); 795 listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
794 message = builder.build(); 796 message = builder.build();
795 assertEquals("{\n" + " \"listValue\": [31831.125, null]\n" + "}", toJsonStr ing(message)); 797 assertEquals(
798 "{\n"
799 + " \"listValue\": [31831.125, null]\n"
800 + "}", toJsonString(message));
796 assertRoundTripEquals(message); 801 assertRoundTripEquals(message);
797 } 802 }
798 803
799 public void testAnyFields() throws Exception { 804 public void testAnyFields() throws Exception {
800 TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).buil d(); 805 TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).buil d();
801 TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build( ); 806 TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build( );
802 807
803 // A TypeRegistry must be provided in order to convert Any types. 808 // A TypeRegistry must be provided in order to convert Any types.
804 try { 809 try {
805 toJsonString(message); 810 toJsonString(message);
806 fail("Exception is expected."); 811 fail("Exception is expected.");
807 } catch (IOException e) { 812 } catch (IOException e) {
808 // Expected. 813 // Expected.
809 } 814 }
810 815
811 JsonFormat.TypeRegistry registry = 816 JsonFormat.TypeRegistry registry = JsonFormat.TypeRegistry.newBuilder()
812 JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).b uild(); 817 .add(TestAllTypes.getDescriptor()).build();
813 JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry ); 818 JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry );
814 819
815 assertEquals( 820 assertEquals(
816 "{\n" 821 "{\n"
817 + " \"anyValue\": {\n" 822 + " \"anyValue\": {\n"
818 + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" 823 + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
819 + " \"optionalInt32\": 1234\n" 824 + " \"optionalInt32\": 1234\n"
820 + " }\n" 825 + " }\n"
821 + "}", 826 + "}" , printer.print(message));
822 printer.print(message));
823 assertRoundTripEquals(message, registry); 827 assertRoundTripEquals(message, registry);
824 828
825 TestAny messageWithDefaultAnyValue =
826 TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
827 assertEquals(
828 "{\n"
829 + " \"anyValue\": {}\n"
830 + "}",
831 printer.print(messageWithDefaultAnyValue));
832 assertRoundTripEquals(messageWithDefaultAnyValue, registry);
833 829
834 // Well-known types have a special formatting when embedded in Any. 830 // Well-known types have a special formatting when embedded in Any.
835 // 831 //
836 // 1. Any in Any. 832 // 1. Any in Any.
837 Any anyMessage = Any.pack(Any.pack(content)); 833 Any anyMessage = Any.pack(Any.pack(content));
838 assertEquals( 834 assertEquals(
839 "{\n" 835 "{\n"
840 + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n" 836 + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
841 + " \"value\": {\n" 837 + " \"value\": {\n"
842 + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" 838 + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
843 + " \"optionalInt32\": 1234\n" 839 + " \"optionalInt32\": 1234\n"
844 + " }\n" 840 + " }\n"
845 + "}", 841 + "}", printer.print(anyMessage));
846 printer.print(anyMessage));
847 assertRoundTripEquals(anyMessage, registry); 842 assertRoundTripEquals(anyMessage, registry);
848 843
849 // 2. Wrappers in Any. 844 // 2. Wrappers in Any.
850 anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build()); 845 anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build());
851 assertEquals( 846 assertEquals(
852 "{\n" 847 "{\n"
853 + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\ n" 848 + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n"
854 + " \"value\": 12345\n" 849 + " \"value\": 12345\n"
855 + "}", 850 + "}", printer.print(anyMessage));
856 printer.print(anyMessage));
857 assertRoundTripEquals(anyMessage, registry); 851 assertRoundTripEquals(anyMessage, registry);
858 anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build()); 852 anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build());
859 assertEquals( 853 assertEquals(
860 "{\n" 854 "{\n"
861 + " \"@type\": \"type.googleapis.com/google.protobuf.UInt32Value\", \n" 855 + " \"@type\": \"type.googleapis.com/google.protobuf.UInt32Value\",\n"
862 + " \"value\": 12345\n" 856 + " \"value\": 12345\n"
863 + "}", 857 + "}", printer.print(anyMessage));
864 printer.print(anyMessage));
865 assertRoundTripEquals(anyMessage, registry); 858 assertRoundTripEquals(anyMessage, registry);
866 anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build()); 859 anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build());
867 assertEquals( 860 assertEquals(
868 "{\n" 861 "{\n"
869 + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\ n" 862 + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n"
870 + " \"value\": \"12345\"\n" 863 + " \"value\": \"12345\"\n"
871 + "}", 864 + "}", printer.print(anyMessage));
872 printer.print(anyMessage));
873 assertRoundTripEquals(anyMessage, registry); 865 assertRoundTripEquals(anyMessage, registry);
874 anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build()); 866 anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
875 assertEquals( 867 assertEquals(
876 "{\n" 868 "{\n"
877 + " \"@type\": \"type.googleapis.com/google.protobuf.UInt64Value\", \n" 869 + " \"@type\": \"type.googleapis.com/google.protobuf.UInt64Value\",\n"
878 + " \"value\": \"12345\"\n" 870 + " \"value\": \"12345\"\n"
879 + "}", 871 + "}", printer.print(anyMessage));
880 printer.print(anyMessage));
881 assertRoundTripEquals(anyMessage, registry); 872 assertRoundTripEquals(anyMessage, registry);
882 anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build()); 873 anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
883 assertEquals( 874 assertEquals(
884 "{\n" 875 "{\n"
885 + " \"@type\": \"type.googleapis.com/google.protobuf.FloatValue\",\ n" 876 + " \"@type\": \"type.googleapis.com/google.protobuf.FloatValue\",\n"
886 + " \"value\": 12345.0\n" 877 + " \"value\": 12345.0\n"
887 + "}", 878 + "}", printer.print(anyMessage));
888 printer.print(anyMessage));
889 assertRoundTripEquals(anyMessage, registry); 879 assertRoundTripEquals(anyMessage, registry);
890 anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build()); 880 anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
891 assertEquals( 881 assertEquals(
892 "{\n" 882 "{\n"
893 + " \"@type\": \"type.googleapis.com/google.protobuf.DoubleValue\", \n" 883 + " \"@type\": \"type.googleapis.com/google.protobuf.DoubleValue\",\n"
894 + " \"value\": 12345.0\n" 884 + " \"value\": 12345.0\n"
895 + "}", 885 + "}", printer.print(anyMessage));
896 printer.print(anyMessage));
897 assertRoundTripEquals(anyMessage, registry); 886 assertRoundTripEquals(anyMessage, registry);
898 anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build()); 887 anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build());
899 assertEquals( 888 assertEquals(
900 "{\n" 889 "{\n"
901 + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n " 890 + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
902 + " \"value\": true\n" 891 + " \"value\": true\n"
903 + "}", 892 + "}", printer.print(anyMessage));
904 printer.print(anyMessage));
905 assertRoundTripEquals(anyMessage, registry); 893 assertRoundTripEquals(anyMessage, registry);
906 anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build()); 894 anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build());
907 assertEquals( 895 assertEquals(
908 "{\n" 896 "{\n"
909 + " \"@type\": \"type.googleapis.com/google.protobuf.StringValue\", \n" 897 + " \"@type\": \"type.googleapis.com/google.protobuf.StringValue\",\n"
910 + " \"value\": \"Hello\"\n" 898 + " \"value\": \"Hello\"\n"
911 + "}", 899 + "}", printer.print(anyMessage));
912 printer.print(anyMessage));
913 assertRoundTripEquals(anyMessage, registry); 900 assertRoundTripEquals(anyMessage, registry);
914 anyMessage = 901 anyMessage = Any.pack(BytesValue.newBuilder().setValue(
915 Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] {1, 2})).build()); 902 ByteString.copyFrom(new byte[]{1, 2})).build());
916 assertEquals( 903 assertEquals(
917 "{\n" 904 "{\n"
918 + " \"@type\": \"type.googleapis.com/google.protobuf.BytesValue\",\ n" 905 + " \"@type\": \"type.googleapis.com/google.protobuf.BytesValue\",\n"
919 + " \"value\": \"AQI=\"\n" 906 + " \"value\": \"AQI=\"\n"
920 + "}", 907 + "}", printer.print(anyMessage));
921 printer.print(anyMessage));
922 assertRoundTripEquals(anyMessage, registry); 908 assertRoundTripEquals(anyMessage, registry);
923 909
924 // 3. Timestamp in Any. 910 // 3. Timestamp in Any.
925 anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")); 911 anyMessage = Any.pack(TimeUtil.parseTimestamp("1969-12-31T23:59:59Z"));
926 assertEquals( 912 assertEquals(
927 "{\n" 913 "{\n"
928 + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n " 914 + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n"
929 + " \"value\": \"1969-12-31T23:59:59Z\"\n" 915 + " \"value\": \"1969-12-31T23:59:59Z\"\n"
930 + "}", 916 + "}", printer.print(anyMessage));
931 printer.print(anyMessage));
932 assertRoundTripEquals(anyMessage, registry); 917 assertRoundTripEquals(anyMessage, registry);
933 918
934 // 4. Duration in Any 919 // 4. Duration in Any
935 anyMessage = Any.pack(Durations.parse("12345.10s")); 920 anyMessage = Any.pack(TimeUtil.parseDuration("12345.10s"));
936 assertEquals( 921 assertEquals(
937 "{\n" 922 "{\n"
938 + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n" 923 + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n"
939 + " \"value\": \"12345.100s\"\n" 924 + " \"value\": \"12345.100s\"\n"
940 + "}", 925 + "}", printer.print(anyMessage));
941 printer.print(anyMessage));
942 assertRoundTripEquals(anyMessage, registry); 926 assertRoundTripEquals(anyMessage, registry);
943 927
944 // 5. FieldMask in Any 928 // 5. FieldMask in Any
945 anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz")); 929 anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
946 assertEquals( 930 assertEquals(
947 "{\n" 931 "{\n"
948 + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n " 932 + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n"
949 + " \"value\": \"foo.bar,baz\"\n" 933 + " \"value\": \"foo.bar,baz\"\n"
950 + "}", 934 + "}", printer.print(anyMessage));
951 printer.print(anyMessage));
952 assertRoundTripEquals(anyMessage, registry); 935 assertRoundTripEquals(anyMessage, registry);
953 936
954 // 6. Struct in Any 937 // 6. Struct in Any
955 Struct.Builder structBuilder = Struct.newBuilder(); 938 Struct.Builder structBuilder = Struct.newBuilder();
956 structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).b uild()); 939 structBuilder.getMutableFields().put(
940 "number", Value.newBuilder().setNumberValue(1.125).build());
957 anyMessage = Any.pack(structBuilder.build()); 941 anyMessage = Any.pack(structBuilder.build());
958 assertEquals( 942 assertEquals(
959 "{\n" 943 "{\n"
960 + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n" 944 + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n"
961 + " \"value\": {\n" 945 + " \"value\": {\n"
962 + " \"number\": 1.125\n" 946 + " \"number\": 1.125\n"
963 + " }\n" 947 + " }\n"
964 + "}", 948 + "}", printer.print(anyMessage));
965 printer.print(anyMessage));
966 assertRoundTripEquals(anyMessage, registry); 949 assertRoundTripEquals(anyMessage, registry);
967
968 // 7. Value (number type) in Any
969 Value.Builder valueBuilder = Value.newBuilder(); 950 Value.Builder valueBuilder = Value.newBuilder();
970 valueBuilder.setNumberValue(1); 951 valueBuilder.setNumberValue(1);
971 anyMessage = Any.pack(valueBuilder.build()); 952 anyMessage = Any.pack(valueBuilder.build());
972 assertEquals( 953 assertEquals(
973 "{\n" 954 "{\n"
974 + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n" 955 + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
975 + " \"value\": 1.0\n" 956 + " \"value\": 1.0\n"
976 + "}", 957 + "}", printer.print(anyMessage));
977 printer.print(anyMessage));
978 assertRoundTripEquals(anyMessage, registry); 958 assertRoundTripEquals(anyMessage, registry);
979
980 // 8. Value (null type) in Any
981 anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE). build());
982 assertEquals(
983 "{\n"
984 + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
985 + " \"value\": null\n"
986 + "}",
987 printer.print(anyMessage));
988 assertRoundTripEquals(anyMessage, registry);
989 }
990
991 public void testAnyInMaps() throws Exception {
992 JsonFormat.TypeRegistry registry =
993 JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).b uild();
994 JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry );
995
996 TestAny.Builder testAny = TestAny.newBuilder();
997 testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue (123).build()));
998 testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue (456).build()));
999 testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:5 9Z")));
1000 testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
1001 testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,b az")));
1002 Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
1003 Struct.Builder struct = Struct.newBuilder();
1004 struct.putFields("number", numberValue);
1005 testAny.putAnyMap("struct", Any.pack(struct.build()));
1006 Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).buil d();
1007 testAny.putAnyMap(
1008 "list_value",
1009 Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullVal ue).build()));
1010 testAny.putAnyMap("number_value", Any.pack(numberValue));
1011 testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
1012 testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
1013 testAny.putAnyMap("default", Any.getDefaultInstance());
1014
1015 assertEquals(
1016 "{\n"
1017 + " \"anyMap\": {\n"
1018 + " \"int32_wrapper\": {\n"
1019 + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value \",\n"
1020 + " \"value\": 123\n"
1021 + " },\n"
1022 + " \"int64_wrapper\": {\n"
1023 + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value \",\n"
1024 + " \"value\": \"456\"\n"
1025 + " },\n"
1026 + " \"timestamp\": {\n"
1027 + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\ ",\n"
1028 + " \"value\": \"1969-12-31T23:59:59Z\"\n"
1029 + " },\n"
1030 + " \"duration\": {\n"
1031 + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\" ,\n"
1032 + " \"value\": \"12345.100s\"\n"
1033 + " },\n"
1034 + " \"field_mask\": {\n"
1035 + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\ ",\n"
1036 + " \"value\": \"foo.bar,baz\"\n"
1037 + " },\n"
1038 + " \"struct\": {\n"
1039 + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\ n"
1040 + " \"value\": {\n"
1041 + " \"number\": 1.125\n"
1042 + " }\n"
1043 + " },\n"
1044 + " \"list_value\": {\n"
1045 + " \"@type\": \"type.googleapis.com/google.protobuf.ListValue\ ",\n"
1046 + " \"value\": [1.125, null]\n"
1047 + " },\n"
1048 + " \"number_value\": {\n"
1049 + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n "
1050 + " \"value\": 1.125\n"
1051 + " },\n"
1052 + " \"any_value_number\": {\n"
1053 + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1054 + " \"value\": {\n"
1055 + " \"@type\": \"type.googleapis.com/google.protobuf.Value\", \n"
1056 + " \"value\": 1.125\n"
1057 + " }\n"
1058 + " },\n"
1059 + " \"any_value_default\": {\n"
1060 + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
1061 + " \"value\": {}\n"
1062 + " },\n"
1063 + " \"default\": {}\n"
1064 + " }\n"
1065 + "}",
1066 printer.print(testAny.build()));
1067 assertRoundTripEquals(testAny.build(), registry);
1068 } 959 }
1069 960
1070 public void testParserMissingTypeUrl() throws Exception { 961 public void testParserMissingTypeUrl() throws Exception {
1071 try { 962 try {
1072 Any.Builder builder = Any.newBuilder(); 963 Any.Builder builder = Any.newBuilder();
1073 mergeFromJson("{\n" + " \"optionalInt32\": 1234\n" + "}", builder); 964 mergeFromJson(
965 "{\n"
966 + " \"optionalInt32\": 1234\n"
967 + "}", builder);
1074 fail("Exception is expected."); 968 fail("Exception is expected.");
1075 } catch (IOException e) { 969 } catch (IOException e) {
1076 // Expected. 970 // Expected.
1077 } 971 }
1078 } 972 }
1079 973
1080 public void testParserUnexpectedTypeUrl() throws Exception { 974 public void testParserUnexpectedTypeUrl() throws Exception {
1081 try { 975 try {
1082 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 976 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1083 mergeFromJson( 977 mergeFromJson(
1084 "{\n" 978 "{\n"
1085 + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" 979 + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
1086 + " \"optionalInt32\": 12345\n" 980 + " \"optionalInt32\": 12345\n"
1087 + "}", 981 + "}", builder);
1088 builder);
1089 fail("Exception is expected."); 982 fail("Exception is expected.");
1090 } catch (IOException e) { 983 } catch (IOException e) {
1091 // Expected. 984 // Expected.
1092 } 985 }
1093 } 986 }
1094 987
1095 public void testParserRejectTrailingComma() throws Exception { 988 public void testParserRejectTrailingComma() throws Exception {
1096 try { 989 try {
1097 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 990 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1098 mergeFromJson("{\n" + " \"optionalInt32\": 12345,\n" + "}", builder); 991 mergeFromJson(
992 "{\n"
993 + " \"optionalInt32\": 12345,\n"
994 + "}", builder);
1099 fail("Exception is expected."); 995 fail("Exception is expected.");
1100 } catch (IOException e) { 996 } catch (IOException e) {
1101 // Expected. 997 // Expected.
1102 } 998 }
1103 999
1104 // TODO(xiaofeng): GSON allows trailing comma in arrays even after I set 1000 // TODO(xiaofeng): GSON allows trailing comma in arrays even after I set
1105 // the JsonReader to non-lenient mode. If we want to enforce strict JSON 1001 // the JsonReader to non-lenient mode. If we want to enforce strict JSON
1106 // compliance, we might want to switch to a different JSON parser or 1002 // compliance, we might want to switch to a different JSON parser or
1107 // implement one by ourselves. 1003 // implement one by ourselves.
1108 // try { 1004 // try {
1109 // TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 1005 // TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1110 // JsonFormat.merge( 1006 // JsonFormat.merge(
1111 // "{\n" 1007 // "{\n"
1112 // + " \"repeatedInt32\": [12345,]\n" 1008 // + " \"repeatedInt32\": [12345,]\n"
1113 // + "}", builder); 1009 // + "}", builder);
1114 // fail("Exception is expected."); 1010 // fail("Exception is expected.");
1115 // } catch (IOException e) { 1011 // } catch (IOException e) {
1116 // // Expected. 1012 // // Expected.
1117 // } 1013 // }
1118 } 1014 }
1119 1015
1120 public void testParserRejectInvalidBase64() throws Exception { 1016 public void testParserRejectInvalidBase64() throws Exception {
1121 assertRejects("optionalBytes", "!@#$"); 1017 assertRejects("optionalBytes", "!@#$");
1122 } 1018 // We use standard BASE64 with paddings.
1123 1019 assertRejects("optionalBytes", "AQI");
1124 public void testParserAcceptBase64Variants() throws Exception {
1125 assertAccepts("optionalBytes", "AQI");
1126 } 1020 }
1127 1021
1128 public void testParserRejectInvalidEnumValue() throws Exception { 1022 public void testParserRejectInvalidEnumValue() throws Exception {
1129 try { 1023 try {
1130 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 1024 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1131 mergeFromJson("{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}", builder ); 1025 mergeFromJson(
1026 "{\n"
1027 + " \"optionalNestedEnum\": \"XXX\"\n"
1028 + "}", builder);
1132 fail("Exception is expected."); 1029 fail("Exception is expected.");
1133 } catch (InvalidProtocolBufferException e) { 1030 } catch (InvalidProtocolBufferException e) {
1134 // Expected. 1031 // Expected.
1135 } 1032 }
1136 } 1033 }
1137 1034
1138 public void testParserUnknownFields() throws Exception {
1139 try {
1140 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1141 String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}";
1142 JsonFormat.parser().merge(json, builder);
1143 fail("Exception is expected.");
1144 } catch (InvalidProtocolBufferException e) {
1145 // Expected.
1146 }
1147 }
1148
1149 public void testParserIgnoringUnknownFields() throws Exception {
1150 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1151 String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}";
1152 JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
1153 }
1154
1155 public void testCustomJsonName() throws Exception { 1035 public void testCustomJsonName() throws Exception {
1156 TestCustomJsonName message = TestCustomJsonName.newBuilder().setValue(12345) .build(); 1036 TestCustomJsonName message = TestCustomJsonName.newBuilder().setValue(12345) .build();
1157 assertEquals("{\n" + " \"@value\": 12345\n" + "}", JsonFormat.printer().pri nt(message)); 1037 assertEquals("{\n" + " \"@value\": 12345\n" + "}", JsonFormat.printer().pri nt(message));
1158 assertRoundTripEquals(message); 1038 assertRoundTripEquals(message);
1159 } 1039 }
1160 1040
1161 public void testDefaultGsonDoesNotHtmlEscape() throws Exception { 1041 public void testDefaultGsonDoesNotHtmlEscape() throws Exception {
1162 TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("=").buil d(); 1042 TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("=").buil d();
1163 assertEquals( 1043 assertEquals(
1164 "{\n" + " \"optionalString\": \"=\"" + "\n}", JsonFormat.printer().prin t(message)); 1044 "{\n" + " \"optionalString\": \"=\"" + "\n}", JsonFormat.printer().prin t(message));
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 + " \"int32ToStringMap\": {\n" 1140 + " \"int32ToStringMap\": {\n"
1261 + " },\n" 1141 + " },\n"
1262 + " \"int32ToBytesMap\": {\n" 1142 + " \"int32ToBytesMap\": {\n"
1263 + " },\n" 1143 + " },\n"
1264 + " \"int32ToMessageMap\": {\n" 1144 + " \"int32ToMessageMap\": {\n"
1265 + " },\n" 1145 + " },\n"
1266 + " \"int32ToEnumMap\": {\n" 1146 + " \"int32ToEnumMap\": {\n"
1267 + " }\n" 1147 + " }\n"
1268 + "}", 1148 + "}",
1269 JsonFormat.printer().includingDefaultValueFields().print(mapMessage)); 1149 JsonFormat.printer().includingDefaultValueFields().print(mapMessage));
1270
1271 TestOneof oneofMessage = TestOneof.getDefaultInstance();
1272 assertEquals("{\n}", JsonFormat.printer().print(oneofMessage));
1273 assertEquals("{\n}", JsonFormat.printer().includingDefaultValueFields().prin t(oneofMessage));
1274
1275 oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
1276 assertEquals("{\n \"oneofInt32\": 42\n}",
1277 JsonFormat.printer().print(oneofMessage));
1278 assertEquals("{\n \"oneofInt32\": 42\n}",
1279 JsonFormat.printer().includingDefaultValueFields().print(oneofMessage));
1280
1281 TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
1282 mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", oneofBuilder);
1283 oneofMessage = oneofBuilder.build();
1284 assertEquals("{\n \"oneofNullValue\": null\n}", JsonFormat.printer().print( oneofMessage));
1285 assertEquals("{\n \"oneofNullValue\": null\n}",
1286 JsonFormat.printer().includingDefaultValueFields().print(oneofMessage));
1287 } 1150 }
1288 1151
1289 public void testPreservingProtoFieldNames() throws Exception { 1152 public void testPreservingProtoFieldNames() throws Exception {
1290 TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).bui ld(); 1153 TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).bui ld();
1291 assertEquals("{\n" + " \"optionalInt32\": 12345\n" + "}", JsonFormat.printe r().print(message)); 1154 assertEquals("{\n" + " \"optionalInt32\": 12345\n" + "}", JsonFormat.printe r().print(message));
1292 assertEquals( 1155 assertEquals(
1293 "{\n" + " \"optional_int32\": 12345\n" + "}", 1156 "{\n" + " \"optional_int32\": 12345\n" + "}",
1294 JsonFormat.printer().preservingProtoFieldNames().print(message)); 1157 JsonFormat.printer().preservingProtoFieldNames().print(message));
1295 1158
1296 // The json_name field option is ignored when configured to use original pro to field names. 1159 // The json_name field option is ignored when configured to use original pro to field names.
1297 TestCustomJsonName messageWithCustomJsonName = 1160 TestCustomJsonName messageWithCustomJsonName =
1298 TestCustomJsonName.newBuilder().setValue(12345).build(); 1161 TestCustomJsonName.newBuilder().setValue(12345).build();
1299 assertEquals( 1162 assertEquals(
1300 "{\n" + " \"value\": 12345\n" + "}", 1163 "{\n" + " \"value\": 12345\n" + "}",
1301 JsonFormat.printer().preservingProtoFieldNames().print(messageWithCustom JsonName)); 1164 JsonFormat.printer().preservingProtoFieldNames().print(messageWithCustom JsonName));
1302 1165
1303 // Parsers accept both original proto field names and lowerCamelCase names. 1166 // Parsers accept both original proto field names and lowerCamelCase names.
1304 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 1167 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1305 JsonFormat.parser().merge("{\"optionalInt32\": 12345}", builder); 1168 JsonFormat.parser().merge("{\"optionalInt32\": 12345}", builder);
1306 assertEquals(12345, builder.getOptionalInt32()); 1169 assertEquals(12345, builder.getOptionalInt32());
1307 builder.clear(); 1170 builder.clear();
1308 JsonFormat.parser().merge("{\"optional_int32\": 54321}", builder); 1171 JsonFormat.parser().merge("{\"optional_int32\": 54321}", builder);
1309 assertEquals(54321, builder.getOptionalInt32()); 1172 assertEquals(54321, builder.getOptionalInt32());
1310 } 1173 }
1311
1312 public void testOmittingInsignificantWhiteSpace() throws Exception {
1313 TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).bui ld();
1314 assertEquals(
1315 "{" + "\"optionalInt32\":12345" + "}",
1316 JsonFormat.printer().omittingInsignificantWhitespace().print(message));
1317 TestAllTypes message1 = TestAllTypes.getDefaultInstance();
1318 assertEquals("{}", JsonFormat.printer().omittingInsignificantWhitespace().pr int(message1));
1319 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1320 setAllFields(builder);
1321 TestAllTypes message2 = builder.build();
1322 assertEquals(
1323 "{"
1324 + "\"optionalInt32\":1234,"
1325 + "\"optionalInt64\":\"1234567890123456789\","
1326 + "\"optionalUint32\":5678,"
1327 + "\"optionalUint64\":\"2345678901234567890\","
1328 + "\"optionalSint32\":9012,"
1329 + "\"optionalSint64\":\"3456789012345678901\","
1330 + "\"optionalFixed32\":3456,"
1331 + "\"optionalFixed64\":\"4567890123456789012\","
1332 + "\"optionalSfixed32\":7890,"
1333 + "\"optionalSfixed64\":\"5678901234567890123\","
1334 + "\"optionalFloat\":1.5,"
1335 + "\"optionalDouble\":1.25,"
1336 + "\"optionalBool\":true,"
1337 + "\"optionalString\":\"Hello world!\","
1338 + "\"optionalBytes\":\"AAEC\","
1339 + "\"optionalNestedMessage\":{"
1340 + "\"value\":100"
1341 + "},"
1342 + "\"optionalNestedEnum\":\"BAR\","
1343 + "\"repeatedInt32\":[1234,234],"
1344 + "\"repeatedInt64\":[\"1234567890123456789\",\"234567890123456789\" ],"
1345 + "\"repeatedUint32\":[5678,678],"
1346 + "\"repeatedUint64\":[\"2345678901234567890\",\"345678901234567890\ "],"
1347 + "\"repeatedSint32\":[9012,10],"
1348 + "\"repeatedSint64\":[\"3456789012345678901\",\"456789012345678901\ "],"
1349 + "\"repeatedFixed32\":[3456,456],"
1350 + "\"repeatedFixed64\":[\"4567890123456789012\",\"567890123456789012 \"],"
1351 + "\"repeatedSfixed32\":[7890,890],"
1352 + "\"repeatedSfixed64\":[\"5678901234567890123\",\"67890123456789012 3\"],"
1353 + "\"repeatedFloat\":[1.5,11.5],"
1354 + "\"repeatedDouble\":[1.25,11.25],"
1355 + "\"repeatedBool\":[true,true],"
1356 + "\"repeatedString\":[\"Hello world!\",\"ello world!\"],"
1357 + "\"repeatedBytes\":[\"AAEC\",\"AQI=\"],"
1358 + "\"repeatedNestedMessage\":[{"
1359 + "\"value\":100"
1360 + "},{"
1361 + "\"value\":200"
1362 + "}],"
1363 + "\"repeatedNestedEnum\":[\"BAR\",\"BAZ\"]"
1364 + "}",
1365 toCompactJsonString(message2));
1366 }
1367
1368 // Regression test for b/29892357
1369 public void testEmptyWrapperTypesInAny() throws Exception {
1370 JsonFormat.TypeRegistry registry =
1371 JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).b uild();
1372 JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(registry);
1373
1374 Any.Builder builder = Any.newBuilder();
1375 parser.merge(
1376 "{\n"
1377 + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n "
1378 + " \"value\": false\n"
1379 + "}\n",
1380 builder);
1381 Any any = builder.build();
1382 assertEquals(0, any.getValue().size());
1383 }
1384
1385 public void testRecursionLimit() throws Exception {
1386 String input =
1387 "{\n"
1388 + " \"nested\": {\n"
1389 + " \"nested\": {\n"
1390 + " \"nested\": {\n"
1391 + " \"nested\": {\n"
1392 + " \"value\": 1234\n"
1393 + " }\n"
1394 + " }\n"
1395 + " }\n"
1396 + " }\n"
1397 + "}\n";
1398
1399 JsonFormat.Parser parser = JsonFormat.parser();
1400 TestRecursive.Builder builder = TestRecursive.newBuilder();
1401 parser.merge(input, builder);
1402 TestRecursive message = builder.build();
1403 assertEquals(1234, message.getNested().getNested().getNested().getNested().g etValue());
1404
1405 parser = JsonFormat.parser().usingRecursionLimit(3);
1406 builder = TestRecursive.newBuilder();
1407 try {
1408 parser.merge(input, builder);
1409 fail("Exception is expected.");
1410 } catch (InvalidProtocolBufferException e) {
1411 // Expected.
1412 }
1413 }
1414 } 1174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698