Skip to content

added enumByName as default with optional flag #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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