From bdb06df4b4d21846bdd89664fdf9580b75cd280f Mon Sep 17 00:00:00 2001 From: clatko Date: Wed, 19 Sep 2018 17:00:17 -0700 Subject: [PATCH] added enumByName as default with optional flag --- .../protobuf/format/JsonFormat.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/googlecode/protobuf/format/JsonFormat.java b/src/main/java/com/googlecode/protobuf/format/JsonFormat.java index c4f1602..80d7a2e 100644 --- a/src/main/java/com/googlecode/protobuf/format/JsonFormat.java +++ b/src/main/java/com/googlecode/protobuf/format/JsonFormat.java @@ -31,7 +31,6 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT import java.io.IOException; import java.math.BigInteger; -import java.nio.CharBuffer; import java.text.CharacterIterator; import java.text.StringCharacterIterator; import java.util.Iterator; @@ -72,15 +71,25 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT public class JsonFormat extends AbstractCharBasedFormatter { protected final ByteSerializer byteSerializer; - + protected boolean enumByName = true; + public JsonFormat(){ this(new DefaultByteSerializer()); } + public JsonFormat(boolean enumByName){ + this(new DefaultByteSerializer(), enumByName); + } + public JsonFormat(ByteSerializer byteSerializer) { this.byteSerializer = byteSerializer; } + public JsonFormat(ByteSerializer byteSerializer, boolean enumByName) { + this.byteSerializer = byteSerializer; + this.enumByName = enumByName; + } + /** * Outputs a textual representation of the Protocol Message supplied into the parameter output. * (This representation is the new version of the classic "ProtocolPrinter" output from the @@ -216,9 +225,13 @@ private void printFieldValue(FieldDescriptor field, Object value, JsonGenerator } case ENUM: { - generator.print("\""); - generator.print(((EnumValueDescriptor) value).getName()); - generator.print("\""); + if(enumByName) { + generator.print("\""); + generator.print(((EnumValueDescriptor) value).getName()); + generator.print("\""); + } else { + generator.print(Integer.toString(((EnumValueDescriptor) value).getNumber())); + } break; }