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
34a1b6e6
Commit
34a1b6e6
authored
Dec 08, 2016
by
Feng Xiao
Committed by
GitHub
Dec 08, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2394 from cwelton/formatting
oneOf fix for JsonFormat includingDefaultValueFields
parents
46ae90dc
1a56251a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
6 deletions
+30
-6
JsonFormat.java
...il/src/main/java/com/google/protobuf/util/JsonFormat.java
+13
-6
JsonFormatTest.java
...rc/test/java/com/google/protobuf/util/JsonFormatTest.java
+17
-0
No files found.
java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
View file @
34a1b6e6
...
...
@@ -49,6 +49,7 @@ import com.google.protobuf.Descriptors.EnumDescriptor;
import
com.google.protobuf.Descriptors.EnumValueDescriptor
;
import
com.google.protobuf.Descriptors.FieldDescriptor
;
import
com.google.protobuf.Descriptors.FileDescriptor
;
import
com.google.protobuf.Descriptors.OneofDescriptor
;
import
com.google.protobuf.DoubleValue
;
import
com.google.protobuf.Duration
;
import
com.google.protobuf.DynamicMessage
;
...
...
@@ -782,12 +783,18 @@ public class JsonFormat {
if
(
includingDefaultValueFields
)
{
fieldsToPrint
=
new
TreeMap
<
FieldDescriptor
,
Object
>();
for
(
FieldDescriptor
field
:
message
.
getDescriptorForType
().
getFields
())
{
if
(
field
.
isOptional
()
&&
field
.
getJavaType
()
==
FieldDescriptor
.
JavaType
.
MESSAGE
&&
!
message
.
hasField
(
field
))
{
// Always skip empty optional message fields. If not we will recurse indefinitely if
// a message has itself as a sub-field.
continue
;
if
(
field
.
isOptional
())
{
if
(
field
.
getJavaType
()
==
FieldDescriptor
.
JavaType
.
MESSAGE
&&
!
message
.
hasField
(
field
)){
// Always skip empty optional message fields. If not we will recurse indefinitely if
// a message has itself as a sub-field.
continue
;
}
OneofDescriptor
oneof
=
field
.
getContainingOneof
();
if
(
oneof
!=
null
&&
!
message
.
hasField
(
field
))
{
// Skip all oneof fields except the one that is actually set
continue
;
}
}
fieldsToPrint
.
put
(
field
,
message
.
getField
(
field
));
}
...
...
java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
View file @
34a1b6e6
...
...
@@ -1267,6 +1267,23 @@ public class JsonFormatTest extends TestCase {
+
" }\n"
+
"}"
,
JsonFormat
.
printer
().
includingDefaultValueFields
().
print
(
mapMessage
));
TestOneof
oneofMessage
=
TestOneof
.
getDefaultInstance
();
assertEquals
(
"{\n}"
,
JsonFormat
.
printer
().
print
(
oneofMessage
));
assertEquals
(
"{\n}"
,
JsonFormat
.
printer
().
includingDefaultValueFields
().
print
(
oneofMessage
));
oneofMessage
=
TestOneof
.
newBuilder
().
setOneofInt32
(
42
).
build
();
assertEquals
(
"{\n \"oneofInt32\": 42\n}"
,
JsonFormat
.
printer
().
print
(
oneofMessage
));
assertEquals
(
"{\n \"oneofInt32\": 42\n}"
,
JsonFormat
.
printer
().
includingDefaultValueFields
().
print
(
oneofMessage
));
TestOneof
.
Builder
oneofBuilder
=
TestOneof
.
newBuilder
();
mergeFromJson
(
"{\n"
+
" \"oneofNullValue\": null \n"
+
"}"
,
oneofBuilder
);
oneofMessage
=
oneofBuilder
.
build
();
assertEquals
(
"{\n \"oneofNullValue\": null\n}"
,
JsonFormat
.
printer
().
print
(
oneofMessage
));
assertEquals
(
"{\n \"oneofNullValue\": null\n}"
,
JsonFormat
.
printer
().
includingDefaultValueFields
().
print
(
oneofMessage
));
}
public
void
testPreservingProtoFieldNames
()
throws
Exception
{
...
...
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