TJSONProtocol writing support in Java

Summary: TJSONProtocol for Java with write support and a TSerializer utility for easier conversion of Thrift objects into byte[] or strings.

Reviewed By: dreiss

Test Plan: Included a basic piece of this in test/ client for Java.

Revert: OK

DiffCamp Revision: 3890


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665367 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/TSerializer.java b/lib/java/src/TSerializer.java
new file mode 100644
index 0000000..2fb3794
--- /dev/null
+++ b/lib/java/src/TSerializer.java
@@ -0,0 +1,73 @@
+// Copyright (c) 2006- Facebook
+// Distributed under the Thrift Software License
+//
+// See accompanying file LICENSE or visit the Thrift site at:
+// http://developers.facebook.com/thrift/
+
+package com.facebook.thrift;
+
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+
+import com.facebook.thrift.protocol.TBinaryProtocol;
+import com.facebook.thrift.protocol.TProtocol;
+import com.facebook.thrift.protocol.TProtocolFactory;
+import com.facebook.thrift.transport.TIOStreamTransport;
+import com.facebook.thrift.transport.TTransport;
+
+/**
+ * Generic utility for easily serializing objects into a byte array.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+public class TSerializer {
+
+  private static class TByteArrayTransport extends TIOStreamTransport {
+
+    private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();
+
+    public TByteArrayTransport() {
+      outputStream_ = baos_;
+    }
+
+    public byte[] get() {
+      return baos_.toByteArray();
+    }
+
+    public void reset() {
+      baos_.reset();
+    }
+  }
+
+  private TProtocol protocol_;
+
+  private final TByteArrayTransport transport_ = new TByteArrayTransport();
+
+  public TSerializer() {
+    this(new TBinaryProtocol.Factory());
+  }
+
+  public TSerializer(TProtocolFactory protocolFactory) {
+    protocol_ = protocolFactory.getProtocol(transport_);
+  }
+
+  public byte[] serialize(TBase base) throws TException {
+    transport_.reset();
+    base.write(protocol_);
+    byte[] data = transport_.get();
+    return data;
+  }
+
+  public String toString(TBase base, String charset) throws TException {
+    try {
+      return new String(serialize(base), charset);
+    } catch (UnsupportedEncodingException uex) {
+      throw new TException("JVM DOES NOT SUPPORT ENCODING");
+    }
+  }
+
+  public String toString(TBase base) throws TException {
+    return new String(serialize(base));
+  }
+}
+