aboutsummaryrefslogtreecommitdiff
path: root/java/util
diff options
context:
space:
mode:
authorJosh Haberman <jhaberman@gmail.com>2018-09-04 10:58:54 -0700
committerJosh Haberman <jhaberman@gmail.com>2018-09-04 10:58:54 -0700
commitd61aede89cf188367766b971f59cf57d7835d8e8 (patch)
treea19842c62c3c8f51389912ecafdf932d0a572bea /java/util
parent45d03a977193d1dcce5251e4bffe17bf0ba738ec (diff)
downloadprotobuf-d61aede89cf188367766b971f59cf57d7835d8e8.tar.gz
protobuf-d61aede89cf188367766b971f59cf57d7835d8e8.tar.bz2
protobuf-d61aede89cf188367766b971f59cf57d7835d8e8.zip
Down-integrate from google3.
Diffstat (limited to 'java/util')
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/JsonFormat.java6
-rw-r--r--java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java48
2 files changed, 24 insertions, 30 deletions
diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
index 955dfd86..46deb5d1 100644
--- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
+++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
@@ -1540,7 +1540,7 @@ public class JsonFormat {
Object key = parseFieldValue(keyField, new JsonPrimitive(entry.getKey()), entryBuilder);
Object value = parseFieldValue(valueField, entry.getValue(), entryBuilder);
if (value == null) {
- if(ignoringUnknownFields && valueField.getType() == Type.ENUM) {
+ if (ignoringUnknownFields && valueField.getType() == Type.ENUM) {
continue;
} else {
throw new InvalidProtocolBufferException("Map value cannot be null.");
@@ -1562,11 +1562,11 @@ public class JsonFormat {
for (int i = 0; i < array.size(); ++i) {
Object value = parseFieldValue(field, array.get(i), builder);
if (value == null) {
- if(ignoringUnknownFields && field.getType() == Type.ENUM) {
+ if (ignoringUnknownFields && field.getType() == Type.ENUM) {
continue;
} else {
throw new InvalidProtocolBufferException(
- "Repeated field elements cannot be null in field: " + field.getFullName());
+ "Repeated field elements cannot be null in field: " + field.getFullName());
}
}
builder.addRepeatedField(field, value);
diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
index 7637c267..64413bc1 100644
--- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
+++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
@@ -161,7 +161,8 @@ public class JsonFormatTest extends TestCase {
JsonFormat.parser().merge(json, builder);
}
- private void mergeFromJsonIgnoringUnknownFields(String json, Message.Builder builder) throws IOException {
+ private void mergeFromJsonIgnoringUnknownFields(String json, Message.Builder builder)
+ throws IOException {
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
}
@@ -677,17 +678,14 @@ public class JsonFormatTest extends TestCase {
// Exception expected.
}
}
-
+
public void testMapEnumNullValueIsIgnored() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
mergeFromJsonIgnoringUnknownFields(
- "{\n"
- + " \"int32ToEnumMap\": {\"1\": null}\n"
- + "}",
- builder);
- TestMap map = builder.build();
- assertEquals(0, map.getInt32ToEnumMapMap().entrySet().size());
- }
+ "{\n" + " \"int32ToEnumMap\": {\"1\": null}\n" + "}", builder);
+ TestMap map = builder.build();
+ assertEquals(0, map.getInt32ToEnumMapMap().size());
+ }
public void testParserAcceptNonQuotedObjectKey() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
@@ -1188,40 +1186,36 @@ public class JsonFormatTest extends TestCase {
String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}";
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
}
-
+
public void testParserIgnoringUnknownEnums() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
String json = "{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}";
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
assertEquals(0, builder.getOptionalNestedEnumValue());
}
-
+
public void testUnknownEnumMap() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
- JsonFormat.parser().ignoringUnknownFields().merge(
- "{\n"
- + " \"int32ToEnumMap\": {1: XXX, 2: FOO}"
- + "}",
- builder);
-
- assertEquals(NestedEnum.FOO, builder.getInt32ToEnumMapMap().get(2));
- assertEquals(1, builder.getInt32ToEnumMapMap().size());
+ JsonFormat.parser()
+ .ignoringUnknownFields()
+ .merge("{\n" + " \"int32ToEnumMap\": {1: XXX, 2: FOO}" + "}", builder);
+
+ assertEquals(NestedEnum.FOO, builder.getInt32ToEnumMapMap().get(2));
+ assertEquals(1, builder.getInt32ToEnumMapMap().size());
}
-
+
public void testRepeatedUnknownEnum() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
- JsonFormat.parser().ignoringUnknownFields().merge(
- "{\n"
- + " \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]"
- + "}",
- builder);
-
+ JsonFormat.parser()
+ .ignoringUnknownFields()
+ .merge("{\n" + " \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]" + "}", builder);
+
assertEquals(NestedEnum.FOO, builder.getRepeatedNestedEnum(0));
assertEquals(NestedEnum.BAR, builder.getRepeatedNestedEnum(1));
assertEquals(NestedEnum.BAZ, builder.getRepeatedNestedEnum(2));
assertEquals(3, builder.getRepeatedNestedEnumList().size());
}
-
+
public void testParserIntegerEnumValue() throws Exception {
TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder();
mergeFromJson("{\n" + " \"optionalNestedEnum\": 2\n" + "}", actualBuilder);