Thrift: Distinguish between string and binary types in C++ and Java.
authorDavid Reiss <dreiss@apache.org>
Fri, 15 Feb 2008 01:38:18 +0000 (01:38 +0000)
committerDavid Reiss <dreiss@apache.org>
Fri, 15 Feb 2008 01:38:18 +0000 (01:38 +0000)
Summary:
The upcoming TJSONProtocol handles string and binary types quite differently.
This change makes that distinction in all parts of the C++ binding.

Java already distinguished between string and binary, but this change
also updates the Java skip method to skip over strings as binary
so we don't get encoding errors when skipping binary data.

Reviewed By: mcslee

Test Plan: make check

Revert Plan: ok

Other Notes:
I just pulled this out of Chad Walters' JSON patch.
The only other change was adding readBinary (or was it writeBinary)
to TDenseProtocol.  Maybe inheriting from TBinaryProtocol wasn't a good idea.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665481 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_cpp_generator.cc
lib/cpp/src/protocol/TBinaryProtocol.cpp
lib/cpp/src/protocol/TBinaryProtocol.h
lib/cpp/src/protocol/TDebugProtocol.cpp
lib/cpp/src/protocol/TDebugProtocol.h
lib/cpp/src/protocol/TDenseProtocol.cpp
lib/cpp/src/protocol/TDenseProtocol.h
lib/cpp/src/protocol/TOneWayProtocol.h
lib/cpp/src/protocol/TProtocol.h
lib/java/src/protocol/TProtocolUtil.java

index eb4b1dc..1fb63b9 100644 (file)
@@ -2185,7 +2185,12 @@ void t_cpp_generator::generate_deserialize_field(ofstream& out,
       throw "compiler error: cannot serialize void field in a struct: " + name;
       break;
     case t_base_type::TYPE_STRING:
-      out << "readString(" << name << ");";
+      if (((t_base_type*)type)->is_binary()) {
+        out << "readBinary(" << name << ");";
+      }
+      else {
+        out << "readString(" << name << ");";
+      }
       break;
     case t_base_type::TYPE_BOOL:
       out << "readBool(" << name << ");";
@@ -2401,7 +2406,12 @@ void t_cpp_generator::generate_serialize_field(ofstream& out,
           "compiler error: cannot serialize void field in a struct: " + name;
         break;
       case t_base_type::TYPE_STRING:
-        out << "writeString(" << name << ");";
+        if (((t_base_type*)type)->is_binary()) {
+          out << "writeBinary(" << name << ");";
+        }
+        else {
+          out << "writeString(" << name << ");";
+        }
         break;
       case t_base_type::TYPE_BOOL:
         out << "writeBool(" << name << ");";
index 289d614..929499b 100644 (file)
@@ -188,6 +188,10 @@ uint32_t TBinaryProtocol::writeString(const string& str) {
   return result + size;
 }
 
+uint32_t TBinaryProtocol::writeBinary(const string& str) {
+  return TBinaryProtocol::writeString(str);
+}
+
 /**
  * Reading functions
  */
@@ -379,6 +383,10 @@ uint32_t TBinaryProtocol::readString(string& str) {
   return result + readStringBody(str, size);
 }
 
+uint32_t TBinaryProtocol::readBinary(string& str) {
+  return TBinaryProtocol::readString(str);
+}
+
 uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) {
   uint32_t result = 0;
 
index c7e2791..9203865 100644 (file)
@@ -119,9 +119,10 @@ class TBinaryProtocol : public TProtocol {
 
   uint32_t writeDouble(const double dub);
 
-
   uint32_t writeString(const std::string& str);
 
+  uint32_t writeBinary(const std::string& str);
+
   /**
    * Reading functions
    */
@@ -173,6 +174,8 @@ class TBinaryProtocol : public TProtocol {
 
   uint32_t readString(std::string& str);
 
+  uint32_t readBinary(std::string& str);
+
  protected:
   uint32_t readStringBody(std::string& str, int32_t sz);
 
index 2f23b11..a24bb5b 100644 (file)
@@ -312,4 +312,8 @@ uint32_t TDebugProtocol::writeString(const string& str) {
   return writeItem(output);
 }
 
+uint32_t TDebugProtocol::writeBinary(const string& str) {
+  return TDebugProtocol::writeString(str);
+}
+
 }}} // facebook::thrift::protocol
index 90b7688..196cff2 100644 (file)
@@ -104,6 +104,8 @@ class TDebugProtocol : public TWriteOnlyProtocol {
 
   uint32_t writeString(const std::string& str);
 
+  uint32_t writeBinary(const std::string& str);
+
 
  private:
   void indentUp();
index 55f3902..dbb2d4a 100644 (file)
@@ -439,6 +439,10 @@ uint32_t TDenseProtocol::writeString(const std::string& str) {
   return subWriteString(str);
 }
 
+uint32_t TDenseProtocol::writeBinary(const std::string& str) {
+  return TDenseProtocol::writeString(str);
+}
+
 inline uint32_t TDenseProtocol::subWriteI32(const int32_t i32) {
   return vlqWrite(i32);
 }
@@ -718,6 +722,10 @@ uint32_t TDenseProtocol::readString(std::string& str) {
   return subReadString(str);
 }
 
+uint32_t TDenseProtocol::readBinary(std::string& str) {
+  return TDenseProtocol::readString(str);
+}
+
 uint32_t TDenseProtocol::subReadI32(int32_t& i32) {
   uint64_t u64;
   uint32_t rv = vlqRead(u64);
index dae2c18..713b57a 100644 (file)
@@ -126,6 +126,8 @@ class TDenseProtocol : public TBinaryProtocol {
 
   virtual uint32_t writeString(const std::string& str);
 
+  virtual uint32_t writeBinary(const std::string& str);
+
 
   /*
    * Helper writing functions (don't do state transitions).
@@ -189,6 +191,7 @@ class TDenseProtocol : public TBinaryProtocol {
 
   uint32_t readString(std::string& str);
 
+  uint32_t readBinary(std::string& str);
 
   /*
    * Helper reading functions (don't do state transitions).
index 245f830..1765413 100644 (file)
@@ -137,6 +137,10 @@ class TWriteOnlyProtocol : public TProtocol {
         subclass_ + " does not support reading (yet).");
   }
 
+  uint32_t readBinary(std::string& str) {
+    throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
+        subclass_ + " does not support reading (yet).");
+  }
 
  private:
   std::string subclass_;
index 85cc48a..aecc4e7 100644 (file)
@@ -159,6 +159,8 @@ class TProtocol {
 
   virtual uint32_t writeString(const std::string& str) = 0;
 
+  virtual uint32_t writeBinary(const std::string& str) = 0;
+
   /**
    * Reading functions
    */
@@ -209,6 +211,8 @@ class TProtocol {
 
   virtual uint32_t readString(std::string& str) = 0;
 
+  virtual uint32_t readBinary(std::string& str) = 0;
+
   /**
    * Method to arbitrarily skip over data.
    */
@@ -247,7 +251,7 @@ class TProtocol {
     case T_STRING:
       {
         std::string str;
-        return readString(str);
+        return readBinary(str);
       }
     case T_STRUCT:
       {
index 0b85d38..5c665ab 100644 (file)
@@ -52,7 +52,7 @@ public class TProtocolUtil {
       }
     case TType.STRING:
       {
-        prot.readString();
+        prot.readBinary();
         break;
       }
     case TType.STRUCT: