Thrift-2535:TJSONProtocol when serialized yields TField ids rather than names
Adds ability to optionally serialize TJSONProtocol with TField names
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 @@
* 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 @@
// 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 @@
super(trans);
}
+ public TJSONProtocol(TTransport trans, boolean fieldNamesAsString) {
+ super(trans);
+ fieldNamesAsString_ = fieldNamesAsString;
+ }
+
@Override
public void reset() {
contextStack_.clear();
@@ -513,7 +528,11 @@
@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));
}