From 816790b18d13f91efa807ce941ac0b56cd20e569 Mon Sep 17 00:00:00 2001 From: jfarrell Date: Thu, 15 May 2014 23:25:46 -0400 Subject: [PATCH] Thrift-2535:TJSONProtocol when serialized yields TField ids rather than names Adds ability to optionally serialize TJSONProtocol with TField names --- .../apache/thrift/protocol/TJSONProtocol.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java index 02f36e3cc..6ce702e29 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)); }