Dear Wiki user, You have subscribed to a wiki page or wiki category on "Thrift Wiki" for change notification.
The following page has been changed by StuartSierra: http://wiki.apache.org/thrift/ThriftUsageJava The comment on the change is: Added binary deserialization and ThriftJavaBeans style. ------------------------------------------------------------------------------ import com.facebook.thrift.TBase; import com.facebook.thrift.TException; import com.facebook.thrift.TSerializer; + import com.facebook.thrift.TDeserializer; import com.facebook.thrift.protocol.TBinaryProtocol; import com.facebook.thrift.protocol.TProtocol; import com.facebook.thrift.protocol.TProtocolFactory; @@ -31, +32 @@ work.op = Operation.ADD; }}} + Or, if you used the "beans" option to generate ThriftJavaBeans code: + + {{{ + Work work = new Work(); + work.setNum1(7); + work.setNum2(9); + work.setOp(Operation.ADD); + }}} + - == Serializing to a Byte Array == + == Serializing to/from a Byte Array == {{{ TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory()); byte[] bytes = serializer.serialize(work); + + TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory()); + Work moreWork = new Work(); + deserializer.deserialize(moreWork, bytes); }}} == Serializing to "Simple" JSON == @@ -45, +59 @@ String json = serializer.toString(work); }}} - The "Simple" JSON protocol produces output suitable for AJAX or scripting languages. It does not preserve Thrift's type information and cannot be read back in by Thrift. Using the sample object created above, the output is: + The "Simple" JSON protocol produces output suitable for AJAX or scripting languages. It does not preserve Thrift's field tags and cannot be read back in by Thrift. Using the object created above, the "Simple" JSON string is: {{{ {"num1":7,"num2":9,"op":1}
