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