Repository: thrift Updated Branches: refs/heads/master 303eb1b4f -> 816790b18
Thrift-2535:TJSONProtocol when serialized yields TField ids rather than names Adds ability to optionally serialize TJSONProtocol with TField names Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/816790b1 Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/816790b1 Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/816790b1 Branch: refs/heads/master Commit: 816790b18d13f91efa807ce941ac0b56cd20e569 Parents: 303eb1b Author: jfarrell <[email protected]> Authored: Thu May 15 23:25:46 2014 -0400 Committer: jfarrell <[email protected]> Committed: Thu May 15 23:25:46 2014 -0400 ---------------------------------------------------------------------- .../apache/thrift/protocol/TJSONProtocol.java | 23 ++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/816790b1/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java ---------------------------------------------------------------------- diff --git a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java index 02f36e3..6ce702e 100644 --- a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java +++ b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java @@ -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)); }
