Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
1fc416be
Commit
1fc416be
authored
Sep 08, 2016
by
Dave Protasowski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow the JsonFormat.Parser to ignore unknown fields
The default behaviour of throwing an exception remains
parent
4bc16578
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
6 deletions
+38
-6
JsonFormat.java
...il/src/main/java/com/google/protobuf/util/JsonFormat.java
+22
-6
JsonFormatTest.java
...rc/test/java/com/google/protobuf/util/JsonFormatTest.java
+16
-0
No files found.
java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
View file @
1fc416be
...
...
@@ -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
()
...
...
java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
View file @
1fc416be
...
...
@@ -1030,6 +1030,22 @@ public class JsonFormatTest extends TestCase {
}
}
public
void
testParserUnknownFields
()
throws
Exception
{
try
{
TestAllTypes
.
Builder
builder
=
TestAllTypes
.
newBuilder
();
String
json
=
"{\n"
+
" \"unknownField\": \"XXX\"\n"
+
"}"
;
JsonFormat
.
parser
().
merge
(
json
,
builder
);
fail
(
"Exception is expected."
);
}
catch
(
InvalidProtocolBufferException
e
)
{
// Expected.
}
}
public
void
testParserIgnoringUnknownFields
()
throws
Exception
{
TestAllTypes
.
Builder
builder
=
TestAllTypes
.
newBuilder
();
String
json
=
"{\n"
+
" \"unknownField\": \"XXX\"\n"
+
"}"
;
JsonFormat
.
parser
().
ignoringUnknownFields
().
merge
(
json
,
builder
);
}
public
void
testCustomJsonName
()
throws
Exception
{
TestCustomJsonName
message
=
TestCustomJsonName
.
newBuilder
().
setValue
(
12345
).
build
();
assertEquals
(
"{\n"
+
" \"@value\": 12345\n"
+
"}"
,
JsonFormat
.
printer
().
print
(
message
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment