Thrift-2535:TJSONProtocol when serialized yields TField ids rather than names

Adds ability to optionally serialize TJSONProtocol with TField names
This commit is contained in:
jfarrell 2014-05-15 23:25:46 -04:00
parent 303eb1b4f0
commit 816790b18d

View File

@ -42,9 +42,16 @@ public class TJSONProtocol extends TProtocol {
* Factory for JSON protocol objects
*/
public static class Factory implements TProtocolFactory {
protected boolean fieldNamesAsString_ = false;
public Factory() {}
public Factory(boolean fieldNamesAsString) {
fieldNamesAsString_ = fieldNamesAsString;
}
public TProtocol getProtocol(TTransport trans) {
return new TJSONProtocol(trans);
return new TJSONProtocol(trans, fieldNamesAsString_);
}
}
@ -285,6 +292,9 @@ public class TJSONProtocol extends TProtocol {
// Reader that manages a 1-byte buffer
private LookaheadReader reader_ = new LookaheadReader();
// Write out the TField names as a string instead of the default integer value
private boolean fieldNamesAsString_ = false;
// Push a new JSON context onto the stack.
private void pushContext(JSONBaseContext c) {
contextStack_.push(context_);
@ -303,6 +313,11 @@ public class TJSONProtocol extends TProtocol {
super(trans);
}
public TJSONProtocol(TTransport trans, boolean fieldNamesAsString) {
super(trans);
fieldNamesAsString_ = fieldNamesAsString;
}
@Override
public void reset() {
contextStack_.clear();
@ -513,7 +528,11 @@ public class TJSONProtocol extends TProtocol {
@Override
public void writeFieldBegin(TField field) throws TException {
writeJSONInteger(field.id);
if (fieldNamesAsString_) {
writeString(field.name);
} else {
writeJSONInteger(field.id);
}
writeJSONObjectStart();
writeJSONString(getTypeNameForTypeID(field.type));
}