David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1 |
| 22 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 23 | #include "TVirtualProtocol.h" |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 24 | |
| 25 | #include <stack> |
| 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { namespace protocol { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 28 | |
| 29 | // Forward declaration |
| 30 | class TJSONContext; |
| 31 | |
| 32 | /** |
| 33 | * JSON protocol for Thrift. |
| 34 | * |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 35 | * Implements a protocol which uses JSON as the wire-format. |
| 36 | * |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 37 | * Thrift types are represented as described below: |
| 38 | * |
| 39 | * 1. Every Thrift integer type is represented as a JSON number. |
| 40 | * |
| 41 | * 2. Thrift doubles are represented as JSON numbers. Some special values are |
| 42 | * represented as strings: |
| 43 | * a. "NaN" for not-a-number values |
| 44 | * b. "Infinity" for postive infinity |
| 45 | * c. "-Infinity" for negative infinity |
| 46 | * |
| 47 | * 3. Thrift string values are emitted as JSON strings, with appropriate |
| 48 | * escaping. |
| 49 | * |
| 50 | * 4. Thrift binary values are encoded into Base64 and emitted as JSON strings. |
| 51 | * The readBinary() method is written such that it will properly skip if |
| 52 | * called on a Thrift string (although it will decode garbage data). |
| 53 | * |
| 54 | * 5. Thrift structs are represented as JSON objects, with the field ID as the |
| 55 | * key, and the field value represented as a JSON object with a single |
| 56 | * key-value pair. The key is a short string identifier for that type, |
| 57 | * followed by the value. The valid type identifiers are: "tf" for bool, |
| 58 | * "i8" for byte, "i16" for 16-bit integer, "i32" for 32-bit integer, "i64" |
| 59 | * for 64-bit integer, "dbl" for double-precision loating point, "str" for |
| 60 | * string (including binary), "rec" for struct ("records"), "map" for map, |
| 61 | * "lst" for list, "set" for set. |
| 62 | * |
| 63 | * 6. Thrift lists and sets are represented as JSON arrays, with the first |
| 64 | * element of the JSON array being the string identifier for the Thrift |
| 65 | * element type and the second element of the JSON array being the count of |
| 66 | * the Thrift elements. The Thrift elements then follow. |
| 67 | * |
| 68 | * 7. Thrift maps are represented as JSON arrays, with the first two elements |
| 69 | * of the JSON array being the string identifiers for the Thrift key type |
| 70 | * and value type, followed by the count of the Thrift pairs, followed by a |
| 71 | * JSON object containing the key-value pairs. Note that JSON keys can only |
| 72 | * be strings, which means that the key type of the Thrift map should be |
| 73 | * restricted to numeric or string types -- in the case of numerics, they |
| 74 | * are serialized as strings. |
| 75 | * |
| 76 | * 8. Thrift messages are represented as JSON arrays, with the protocol |
| 77 | * version #, the message name, the message type, and the sequence ID as |
| 78 | * the first 4 elements. |
| 79 | * |
| 80 | * More discussion of the double handling is probably warranted. The aim of |
| 81 | * the current implementation is to match as closely as possible the behavior |
| 82 | * of Java's Double.toString(), which has no precision loss. Implementors in |
| 83 | * other languages should strive to achieve that where possible. I have not |
| 84 | * yet verified whether boost:lexical_cast, which is doing that work for me in |
| 85 | * C++, loses any precision, but I am leaving this as a future improvement. I |
| 86 | * may try to provide a C component for this, so that other languages could |
| 87 | * bind to the same underlying implementation for maximum consistency. |
| 88 | * |
| 89 | * Note further that JavaScript itself is not capable of representing |
| 90 | * floating point infinities -- presumably when we have a JavaScript Thrift |
| 91 | * client, this would mean that infinities get converted to not-a-number in |
| 92 | * transmission. I don't know of any work-around for this issue. |
| 93 | * |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 94 | */ |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 95 | class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 96 | public: |
| 97 | |
| 98 | TJSONProtocol(boost::shared_ptr<TTransport> ptrans); |
| 99 | |
| 100 | ~TJSONProtocol(); |
| 101 | |
| 102 | private: |
| 103 | |
| 104 | void pushContext(boost::shared_ptr<TJSONContext> c); |
| 105 | |
| 106 | void popContext(); |
| 107 | |
| 108 | uint32_t writeJSONEscapeChar(uint8_t ch); |
| 109 | |
| 110 | uint32_t writeJSONChar(uint8_t ch); |
| 111 | |
| 112 | uint32_t writeJSONString(const std::string &str); |
| 113 | |
| 114 | uint32_t writeJSONBase64(const std::string &str); |
| 115 | |
| 116 | template <typename NumberType> |
| 117 | uint32_t writeJSONInteger(NumberType num); |
| 118 | |
| 119 | uint32_t writeJSONDouble(double num); |
| 120 | |
| 121 | uint32_t writeJSONObjectStart() ; |
| 122 | |
| 123 | uint32_t writeJSONObjectEnd(); |
| 124 | |
| 125 | uint32_t writeJSONArrayStart(); |
| 126 | |
| 127 | uint32_t writeJSONArrayEnd(); |
| 128 | |
| 129 | uint32_t readJSONSyntaxChar(uint8_t ch); |
| 130 | |
| 131 | uint32_t readJSONEscapeChar(uint8_t *out); |
| 132 | |
| 133 | uint32_t readJSONString(std::string &str, bool skipContext = false); |
| 134 | |
| 135 | uint32_t readJSONBase64(std::string &str); |
| 136 | |
| 137 | uint32_t readJSONNumericChars(std::string &str); |
| 138 | |
| 139 | template <typename NumberType> |
| 140 | uint32_t readJSONInteger(NumberType &num); |
| 141 | |
| 142 | uint32_t readJSONDouble(double &num); |
| 143 | |
| 144 | uint32_t readJSONObjectStart(); |
| 145 | |
| 146 | uint32_t readJSONObjectEnd(); |
| 147 | |
| 148 | uint32_t readJSONArrayStart(); |
| 149 | |
| 150 | uint32_t readJSONArrayEnd(); |
| 151 | |
| 152 | public: |
| 153 | |
| 154 | /** |
| 155 | * Writing functions. |
| 156 | */ |
| 157 | |
| 158 | uint32_t writeMessageBegin(const std::string& name, |
| 159 | const TMessageType messageType, |
| 160 | const int32_t seqid); |
| 161 | |
| 162 | uint32_t writeMessageEnd(); |
| 163 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 164 | uint32_t writeStructBegin(const char* name); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 165 | |
| 166 | uint32_t writeStructEnd(); |
| 167 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 168 | uint32_t writeFieldBegin(const char* name, |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 169 | const TType fieldType, |
| 170 | const int16_t fieldId); |
| 171 | |
| 172 | uint32_t writeFieldEnd(); |
| 173 | |
| 174 | uint32_t writeFieldStop(); |
| 175 | |
| 176 | uint32_t writeMapBegin(const TType keyType, |
| 177 | const TType valType, |
| 178 | const uint32_t size); |
| 179 | |
| 180 | uint32_t writeMapEnd(); |
| 181 | |
| 182 | uint32_t writeListBegin(const TType elemType, |
| 183 | const uint32_t size); |
| 184 | |
| 185 | uint32_t writeListEnd(); |
| 186 | |
| 187 | uint32_t writeSetBegin(const TType elemType, |
| 188 | const uint32_t size); |
| 189 | |
| 190 | uint32_t writeSetEnd(); |
| 191 | |
| 192 | uint32_t writeBool(const bool value); |
| 193 | |
| 194 | uint32_t writeByte(const int8_t byte); |
| 195 | |
| 196 | uint32_t writeI16(const int16_t i16); |
| 197 | |
| 198 | uint32_t writeI32(const int32_t i32); |
| 199 | |
| 200 | uint32_t writeI64(const int64_t i64); |
| 201 | |
| 202 | uint32_t writeDouble(const double dub); |
| 203 | |
| 204 | uint32_t writeString(const std::string& str); |
| 205 | |
| 206 | uint32_t writeBinary(const std::string& str); |
| 207 | |
| 208 | /** |
| 209 | * Reading functions |
| 210 | */ |
| 211 | |
| 212 | uint32_t readMessageBegin(std::string& name, |
| 213 | TMessageType& messageType, |
| 214 | int32_t& seqid); |
| 215 | |
| 216 | uint32_t readMessageEnd(); |
| 217 | |
| 218 | uint32_t readStructBegin(std::string& name); |
| 219 | |
| 220 | uint32_t readStructEnd(); |
| 221 | |
| 222 | uint32_t readFieldBegin(std::string& name, |
| 223 | TType& fieldType, |
| 224 | int16_t& fieldId); |
| 225 | |
| 226 | uint32_t readFieldEnd(); |
| 227 | |
| 228 | uint32_t readMapBegin(TType& keyType, |
| 229 | TType& valType, |
| 230 | uint32_t& size); |
| 231 | |
| 232 | uint32_t readMapEnd(); |
| 233 | |
| 234 | uint32_t readListBegin(TType& elemType, |
| 235 | uint32_t& size); |
| 236 | |
| 237 | uint32_t readListEnd(); |
| 238 | |
| 239 | uint32_t readSetBegin(TType& elemType, |
| 240 | uint32_t& size); |
| 241 | |
| 242 | uint32_t readSetEnd(); |
| 243 | |
| 244 | uint32_t readBool(bool& value); |
| 245 | |
| 246 | uint32_t readByte(int8_t& byte); |
| 247 | |
| 248 | uint32_t readI16(int16_t& i16); |
| 249 | |
| 250 | uint32_t readI32(int32_t& i32); |
| 251 | |
| 252 | uint32_t readI64(int64_t& i64); |
| 253 | |
| 254 | uint32_t readDouble(double& dub); |
| 255 | |
| 256 | uint32_t readString(std::string& str); |
| 257 | |
| 258 | uint32_t readBinary(std::string& str); |
| 259 | |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 260 | class LookaheadReader { |
| 261 | |
| 262 | public: |
| 263 | |
| 264 | LookaheadReader(TTransport &trans) : |
| 265 | trans_(&trans), |
| 266 | hasData_(false) { |
| 267 | } |
| 268 | |
| 269 | uint8_t read() { |
| 270 | if (hasData_) { |
| 271 | hasData_ = false; |
| 272 | } |
| 273 | else { |
| 274 | trans_->readAll(&data_, 1); |
| 275 | } |
| 276 | return data_; |
| 277 | } |
| 278 | |
| 279 | uint8_t peek() { |
| 280 | if (!hasData_) { |
| 281 | trans_->readAll(&data_, 1); |
| 282 | } |
| 283 | hasData_ = true; |
| 284 | return data_; |
| 285 | } |
| 286 | |
| 287 | private: |
| 288 | TTransport *trans_; |
| 289 | bool hasData_; |
| 290 | uint8_t data_; |
| 291 | }; |
| 292 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 293 | private: |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame^] | 294 | TTransport* trans_; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 295 | |
| 296 | std::stack<boost::shared_ptr<TJSONContext> > contexts_; |
| 297 | boost::shared_ptr<TJSONContext> context_; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 298 | LookaheadReader reader_; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * Constructs input and output protocol objects given transports. |
| 303 | */ |
David Reiss | 4c266cc | 2009-01-15 23:56:24 +0000 | [diff] [blame] | 304 | class TJSONProtocolFactory : public TProtocolFactory { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 305 | public: |
| 306 | TJSONProtocolFactory() {} |
| 307 | |
| 308 | virtual ~TJSONProtocolFactory() {} |
| 309 | |
| 310 | boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { |
| 311 | return boost::shared_ptr<TProtocol>(new TJSONProtocol(trans)); |
| 312 | } |
| 313 | }; |
| 314 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 315 | }}} // apache::thrift::protocol |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 316 | |
| 317 | |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 318 | // TODO(dreiss): Move part of ThriftJSONString into a .cpp file and remove this. |
| 319 | #include <transport/TBufferTransports.h> |
| 320 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 321 | namespace apache { namespace thrift { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 322 | |
| 323 | template<typename ThriftStruct> |
| 324 | std::string ThriftJSONString(const ThriftStruct& ts) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 325 | using namespace apache::thrift::transport; |
| 326 | using namespace apache::thrift::protocol; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 327 | TMemoryBuffer* buffer = new TMemoryBuffer; |
| 328 | boost::shared_ptr<TTransport> trans(buffer); |
| 329 | TJSONProtocol protocol(trans); |
| 330 | |
| 331 | ts.write(&protocol); |
| 332 | |
| 333 | uint8_t* buf; |
| 334 | uint32_t size; |
| 335 | buffer->getBuffer(&buf, &size); |
| 336 | return std::string((char*)buf, (unsigned int)size); |
| 337 | } |
| 338 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 339 | }} // apache::thrift |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 340 | |
| 341 | #endif // #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1 |