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