diff options
author | Dave Protasowski <dprotaso@gmail.com> | 2016-09-08 13:31:46 -0400 |
---|---|---|
committer | Dave Protasowski <dprotaso@gmail.com> | 2016-09-08 15:08:30 -0400 |
commit | 1fc416be21983a024c9e90f43e13152395643b04 (patch) | |
tree | 58c17925e0f494fb3b66a3c5b2852a1533fee27b /java/util/src/main | |
parent | 4bc16578537495b5ee010d89f2909858312210ac (diff) | |
download | protobuf-1fc416be21983a024c9e90f43e13152395643b04.tar.gz protobuf-1fc416be21983a024c9e90f43e13152395643b04.tar.bz2 protobuf-1fc416be21983a024c9e90f43e13152395643b04.zip |
Allow the JsonFormat.Parser to ignore unknown fields
The default behaviour of throwing an exception remains
Diffstat (limited to 'java/util/src/main')
-rw-r--r-- | java/util/src/main/java/com/google/protobuf/util/JsonFormat.java | 28 |
1 files changed, 22 insertions, 6 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 ad50cc0e..d4db9c80 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 @@ -224,7 +224,7 @@ public class JsonFormat { * Creates a {@link Parser} with default configuration. */ public static Parser parser() { - return new Parser(TypeRegistry.getEmptyTypeRegistry()); + return new Parser(TypeRegistry.getEmptyTypeRegistry(), false); } /** @@ -232,9 +232,11 @@ public class JsonFormat { */ public static class Parser { private final TypeRegistry registry; + private final boolean ignoringUnknownFields; - private Parser(TypeRegistry registry) { + private Parser(TypeRegistry registry, boolean ignoreUnknownFields) { this.registry = registry; + this.ignoringUnknownFields = ignoreUnknownFields; } /** @@ -247,7 +249,16 @@ public class JsonFormat { if (this.registry != TypeRegistry.getEmptyTypeRegistry()) { throw new IllegalArgumentException("Only one registry is allowed."); } - return new Parser(registry); + return new Parser(registry, this.ignoringUnknownFields); + } + + /** + * Creates a new {@link Parser} configured to not throw an exception + * when an unknown field is encountered. The new Parser clones all other + * configurations from this Parser. + */ + public Parser ignoringUnknownFields() { + return new Parser(this.registry, true); } /** @@ -259,7 +270,7 @@ public class JsonFormat { public void merge(String json, Message.Builder builder) throws InvalidProtocolBufferException { // TODO(xiaofeng): Investigate the allocation overhead and optimize for // mobile. - new ParserImpl(registry).merge(json, builder); + new ParserImpl(registry, ignoringUnknownFields).merge(json, builder); } /** @@ -272,7 +283,7 @@ public class JsonFormat { public void merge(Reader json, Message.Builder builder) throws IOException { // TODO(xiaofeng): Investigate the allocation overhead and optimize for // mobile. - new ParserImpl(registry).merge(json, builder); + new ParserImpl(registry, ignoringUnknownFields).merge(json, builder); } } @@ -1024,9 +1035,11 @@ public class JsonFormat { private static class ParserImpl { private final TypeRegistry registry; private final JsonParser jsonParser; + private final boolean ignoringUnknownFields; - ParserImpl(TypeRegistry registry) { + ParserImpl(TypeRegistry registry, boolean ignoreUnknownFields) { this.registry = registry; + this.ignoringUnknownFields = ignoreUnknownFields; this.jsonParser = new JsonParser(); } @@ -1191,6 +1204,9 @@ public class JsonFormat { } FieldDescriptor field = fieldNameMap.get(entry.getKey()); if (field == null) { + if (ignoringUnknownFields) { + continue; + } throw new InvalidProtocolBufferException( "Cannot find field: " + entry.getKey() |