diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2016-12-08 11:17:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-08 11:17:10 -0800 |
commit | 34a1b6e6b8c0d477504d09df4df4b86770e47872 (patch) | |
tree | 10f401d3230026b535f1dccbe12c6475bb961a9d /java/util/src/main | |
parent | 46ae90dc5e145b12fffa7e053a908a9f3e066286 (diff) | |
parent | 1a56251a3582d8b060ca65db6ef1d178c744f81f (diff) | |
download | protobuf-34a1b6e6b8c0d477504d09df4df4b86770e47872.tar.gz protobuf-34a1b6e6b8c0d477504d09df4df4b86770e47872.tar.bz2 protobuf-34a1b6e6b8c0d477504d09df4df4b86770e47872.zip |
Merge pull request #2394 from cwelton/formatting
oneOf fix for JsonFormat includingDefaultValueFields
Diffstat (limited to 'java/util/src/main')
-rw-r--r-- | java/util/src/main/java/com/google/protobuf/util/JsonFormat.java | 19 |
1 files changed, 13 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 7f6c8aea..ac712c94 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 @@ -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)); } |