blob: 9c2f8726978093752a013a7d1a6b2238a7e9d745 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Reissdb0ea152008-02-18 01:49:37 +000019
20#ifndef _THRIFT_PROTOCOL_TJSONPROTOCOL_H_
21#define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1
22
Roger Meier4285ba22013-06-10 21:17:23 +020023#include <thrift/protocol/TVirtualProtocol.h>
David Reissdb0ea152008-02-18 01:49:37 +000024
25#include <stack>
26
Konrad Grochowski16a23a62014-11-13 15:33:38 +010027namespace apache {
28namespace thrift {
29namespace protocol {
David Reissdb0ea152008-02-18 01:49:37 +000030
31// Forward declaration
32class TJSONContext;
33
34/**
35 * JSON protocol for Thrift.
36 *
David Reiss1e62ab42008-02-21 22:37:45 +000037 * Implements a protocol which uses JSON as the wire-format.
38 *
David Reissdb0ea152008-02-18 01:49:37 +000039 * 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 Grochowski3b5dacb2014-11-24 10:55:31 +010046 * b. "Infinity" for positive infinity
David Reissdb0ea152008-02-18 01:49:37 +000047 * 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 Sukegawaa1754372015-10-10 10:44:07 +090056 * 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 Reissdb0ea152008-02-18 01:49:37 +000060 * 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 Apple117a5cc2017-03-29 20:39:36 -070090 * 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 Reissdb0ea152008-02-18 01:49:37 +000095 *
David Reissdb0ea152008-02-18 01:49:37 +000096 */
David Reiss6806fb82010-10-06 17:09:52 +000097class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098public:
cyy316723a2019-01-05 16:35:14 +080099 TJSONProtocol(std::shared_ptr<TTransport> ptrans);
David Reissdb0ea152008-02-18 01:49:37 +0000100
101 ~TJSONProtocol();
102
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100103private:
cyy316723a2019-01-05 16:35:14 +0800104 void pushContext(std::shared_ptr<TJSONContext> c);
David Reissdb0ea152008-02-18 01:49:37 +0000105
106 void popContext();
107
108 uint32_t writeJSONEscapeChar(uint8_t ch);
109
110 uint32_t writeJSONChar(uint8_t ch);
111
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100112 uint32_t writeJSONString(const std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000113
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100114 uint32_t writeJSONBase64(const std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000115
116 template <typename NumberType>
117 uint32_t writeJSONInteger(NumberType num);
118
119 uint32_t writeJSONDouble(double num);
120
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100121 uint32_t writeJSONObjectStart();
David Reissdb0ea152008-02-18 01:49:37 +0000122
123 uint32_t writeJSONObjectEnd();
124
125 uint32_t writeJSONArrayStart();
126
127 uint32_t writeJSONArrayEnd();
128
129 uint32_t readJSONSyntaxChar(uint8_t ch);
130
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200131 uint32_t readJSONEscapeChar(uint16_t* out);
David Reissdb0ea152008-02-18 01:49:37 +0000132
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100133 uint32_t readJSONString(std::string& str, bool skipContext = false);
David Reissdb0ea152008-02-18 01:49:37 +0000134
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100135 uint32_t readJSONBase64(std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000136
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100137 uint32_t readJSONNumericChars(std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000138
139 template <typename NumberType>
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100140 uint32_t readJSONInteger(NumberType& num);
David Reissdb0ea152008-02-18 01:49:37 +0000141
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100142 uint32_t readJSONDouble(double& num);
David Reissdb0ea152008-02-18 01:49:37 +0000143
144 uint32_t readJSONObjectStart();
145
146 uint32_t readJSONObjectEnd();
147
148 uint32_t readJSONArrayStart();
149
150 uint32_t readJSONArrayEnd();
151
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100152public:
David Reissdb0ea152008-02-18 01:49:37 +0000153 /**
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 Reiss64120002008-04-29 23:12:24 +0000163 uint32_t writeStructBegin(const char* name);
David Reissdb0ea152008-02-18 01:49:37 +0000164
165 uint32_t writeStructEnd();
166
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100167 uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
David Reissdb0ea152008-02-18 01:49:37 +0000168
169 uint32_t writeFieldEnd();
170
171 uint32_t writeFieldStop();
172
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100173 uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
David Reissdb0ea152008-02-18 01:49:37 +0000174
175 uint32_t writeMapEnd();
176
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100177 uint32_t writeListBegin(const TType elemType, const uint32_t size);
David Reissdb0ea152008-02-18 01:49:37 +0000178
179 uint32_t writeListEnd();
180
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100181 uint32_t writeSetBegin(const TType elemType, const uint32_t size);
David Reissdb0ea152008-02-18 01:49:37 +0000182
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 Grochowski16a23a62014-11-13 15:33:38 +0100205 uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
David Reissdb0ea152008-02-18 01:49:37 +0000206
207 uint32_t readMessageEnd();
208
209 uint32_t readStructBegin(std::string& name);
210
211 uint32_t readStructEnd();
212
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100213 uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
David Reissdb0ea152008-02-18 01:49:37 +0000214
215 uint32_t readFieldEnd();
216
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100217 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
David Reissdb0ea152008-02-18 01:49:37 +0000218
219 uint32_t readMapEnd();
220
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100221 uint32_t readListBegin(TType& elemType, uint32_t& size);
David Reissdb0ea152008-02-18 01:49:37 +0000222
223 uint32_t readListEnd();
224
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100225 uint32_t readSetBegin(TType& elemType, uint32_t& size);
David Reissdb0ea152008-02-18 01:49:37 +0000226
227 uint32_t readSetEnd();
228
229 uint32_t readBool(bool& value);
230
David Reiss8dfc7322010-10-06 17:09:58 +0000231 // Provide the default readBool() implementation for std::vector<bool>
232 using TVirtualProtocol<TJSONProtocol>::readBool;
233
David Reissdb0ea152008-02-18 01:49:37 +0000234 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 Reiss1e62ab42008-02-21 22:37:45 +0000248 class LookaheadReader {
249
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100250 public:
251 LookaheadReader(TTransport& trans) : trans_(&trans), hasData_(false) {}
David Reiss1e62ab42008-02-21 22:37:45 +0000252
253 uint8_t read() {
254 if (hasData_) {
255 hasData_ = false;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100256 } else {
David Reiss1e62ab42008-02-21 22:37:45 +0000257 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 Grochowski16a23a62014-11-13 15:33:38 +0100270 private:
271 TTransport* trans_;
David Reiss1e62ab42008-02-21 22:37:45 +0000272 bool hasData_;
273 uint8_t data_;
274 };
275
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100276private:
David Reisse71115b2010-10-06 17:09:56 +0000277 TTransport* trans_;
David Reissdb0ea152008-02-18 01:49:37 +0000278
cyy316723a2019-01-05 16:35:14 +0800279 std::stack<std::shared_ptr<TJSONContext> > contexts_;
280 std::shared_ptr<TJSONContext> context_;
David Reiss1e62ab42008-02-21 22:37:45 +0000281 LookaheadReader reader_;
David Reissdb0ea152008-02-18 01:49:37 +0000282};
283
284/**
285 * Constructs input and output protocol objects given transports.
286 */
David Reiss4c266cc2009-01-15 23:56:24 +0000287class TJSONProtocolFactory : public TProtocolFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100288public:
David Reissdb0ea152008-02-18 01:49:37 +0000289 TJSONProtocolFactory() {}
290
291 virtual ~TJSONProtocolFactory() {}
292
cyy316723a2019-01-05 16:35:14 +0800293 std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> trans) {
294 return std::shared_ptr<TProtocol>(new TJSONProtocol(trans));
David Reissdb0ea152008-02-18 01:49:37 +0000295 }
296};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100297}
298}
299} // apache::thrift::protocol
David Reissdb0ea152008-02-18 01:49:37 +0000300
David Reiss28f298d2008-05-01 06:17:36 +0000301// TODO(dreiss): Move part of ThriftJSONString into a .cpp file and remove this.
Roger Meier49ff8b12012-04-13 09:12:31 +0000302#include <thrift/transport/TBufferTransports.h>
David Reiss28f298d2008-05-01 06:17:36 +0000303
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100304namespace apache {
305namespace thrift {
David Reissdb0ea152008-02-18 01:49:37 +0000306
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100307template <typename ThriftStruct>
308std::string ThriftJSONString(const ThriftStruct& ts) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000309 using namespace apache::thrift::transport;
310 using namespace apache::thrift::protocol;
David Reissdb0ea152008-02-18 01:49:37 +0000311 TMemoryBuffer* buffer = new TMemoryBuffer;
cyy316723a2019-01-05 16:35:14 +0800312 std::shared_ptr<TTransport> trans(buffer);
David Reissdb0ea152008-02-18 01:49:37 +0000313 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 Grochowski16a23a62014-11-13 15:33:38 +0100322}
323} // apache::thrift
David Reissdb0ea152008-02-18 01:49:37 +0000324
325#endif // #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1