Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/main/java/com/googlecode/protobuf/format/JsonFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down