blob: 5834eff00542a8870d187a54d5355d1da53a576a [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
90 * yet verified whether boost:lexical_cast, which is doing that work for me in
91 * C++, loses any precision, but I am leaving this as a future improvement. I
92 * may try to provide a C component for this, so that other languages could
93 * bind to the same underlying implementation for maximum consistency.
94 *
David Reissdb0ea152008-02-18 01:49:37 +000095 */
David Reiss6806fb82010-10-06 17:09:52 +000096class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097public:
David Reissdb0ea152008-02-18 01:49:37 +000098 TJSONProtocol(boost::shared_ptr<TTransport> ptrans);
99
100 ~TJSONProtocol();
101
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100102private:
David Reissdb0ea152008-02-18 01:49:37 +0000103 void pushContext(boost::shared_ptr<TJSONContext> c);
104
105 void popContext();
106
107 uint32_t writeJSONEscapeChar(uint8_t ch);
108
109 uint32_t writeJSONChar(uint8_t ch);
110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111 uint32_t writeJSONString(const std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000112
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 uint32_t writeJSONBase64(const std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000114
115 template <typename NumberType>
116 uint32_t writeJSONInteger(NumberType num);
117
118 uint32_t writeJSONDouble(double num);
119
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100120 uint32_t writeJSONObjectStart();
David Reissdb0ea152008-02-18 01:49:37 +0000121
122 uint32_t writeJSONObjectEnd();
123
124 uint32_t writeJSONArrayStart();
125
126 uint32_t writeJSONArrayEnd();
127
128 uint32_t readJSONSyntaxChar(uint8_t ch);
129
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200130 uint32_t readJSONEscapeChar(uint16_t* out);
David Reissdb0ea152008-02-18 01:49:37 +0000131
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100132 uint32_t readJSONString(std::string& str, bool skipContext = false);
David Reissdb0ea152008-02-18 01:49:37 +0000133
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134 uint32_t readJSONBase64(std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000135
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100136 uint32_t readJSONNumericChars(std::string& str);
David Reissdb0ea152008-02-18 01:49:37 +0000137
138 template <typename NumberType>
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100139 uint32_t readJSONInteger(NumberType& num);
David Reissdb0ea152008-02-18 01:49:37 +0000140
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100141 uint32_t readJSONDouble(double& num);
David Reissdb0ea152008-02-18 01:49:37 +0000142
143 uint32_t readJSONObjectStart();
144
145 uint32_t readJSONObjectEnd();
146
147 uint32_t readJSONArrayStart();
148
149 uint32_t readJSONArrayEnd();
150
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100151public:
David Reissdb0ea152008-02-18 01:49:37 +0000152 /**
153 * Writing functions.
154 */
155
156 uint32_t writeMessageBegin(const std::string& name,
157 const TMessageType messageType,
158 const int32_t seqid);
159
160 uint32_t writeMessageEnd();
161
David Reiss64120002008-04-29 23:12:24 +0000162 uint32_t writeStructBegin(const char* name);
David Reissdb0ea152008-02-18 01:49:37 +0000163
164 uint32_t writeStructEnd();
165
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100166 uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
David Reissdb0ea152008-02-18 01:49:37 +0000167
168 uint32_t writeFieldEnd();
169
170 uint32_t writeFieldStop();
171
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100172 uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
David Reissdb0ea152008-02-18 01:49:37 +0000173
174 uint32_t writeMapEnd();
175
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100176 uint32_t writeListBegin(const TType elemType, const uint32_t size);
David Reissdb0ea152008-02-18 01:49:37 +0000177
178 uint32_t writeListEnd();
179
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100180 uint32_t writeSetBegin(const TType elemType, const uint32_t size);
David Reissdb0ea152008-02-18 01:49:37 +0000181
182 uint32_t writeSetEnd();
183
184 uint32_t writeBool(const bool value);
185
186 uint32_t writeByte(const int8_t byte);
187
188 uint32_t writeI16(const int16_t i16);
189
190 uint32_t writeI32(const int32_t i32);
191
192 uint32_t writeI64(const int64_t i64);
193
194 uint32_t writeDouble(const double dub);
195
196 uint32_t writeString(const std::string& str);
197
198 uint32_t writeBinary(const std::string& str);
199
200 /**
201 * Reading functions
202 */
203
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100204 uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
David Reissdb0ea152008-02-18 01:49:37 +0000205
206 uint32_t readMessageEnd();
207
208 uint32_t readStructBegin(std::string& name);
209
210 uint32_t readStructEnd();
211
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100212 uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
David Reissdb0ea152008-02-18 01:49:37 +0000213
214 uint32_t readFieldEnd();
215
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100216 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
David Reissdb0ea152008-02-18 01:49:37 +0000217
218 uint32_t readMapEnd();
219
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100220 uint32_t readListBegin(TType& elemType, uint32_t& size);
David Reissdb0ea152008-02-18 01:49:37 +0000221
222 uint32_t readListEnd();
223
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100224 uint32_t readSetBegin(TType& elemType, uint32_t& size);
David Reissdb0ea152008-02-18 01:49:37 +0000225
226 uint32_t readSetEnd();
227
228 uint32_t readBool(bool& value);
229
David Reiss8dfc7322010-10-06 17:09:58 +0000230 // Provide the default readBool() implementation for std::vector<bool>
231 using TVirtualProtocol<TJSONProtocol>::readBool;
232
David Reissdb0ea152008-02-18 01:49:37 +0000233 uint32_t readByte(int8_t& byte);
234
235 uint32_t readI16(int16_t& i16);
236
237 uint32_t readI32(int32_t& i32);
238
239 uint32_t readI64(int64_t& i64);
240
241 uint32_t readDouble(double& dub);
242
243 uint32_t readString(std::string& str);
244
245 uint32_t readBinary(std::string& str);
246
David Reiss1e62ab42008-02-21 22:37:45 +0000247 class LookaheadReader {
248
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100249 public:
250 LookaheadReader(TTransport& trans) : trans_(&trans), hasData_(false) {}
David Reiss1e62ab42008-02-21 22:37:45 +0000251
252 uint8_t read() {
253 if (hasData_) {
254 hasData_ = false;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100255 } else {
David Reiss1e62ab42008-02-21 22:37:45 +0000256 trans_->readAll(&data_, 1);
257 }
258 return data_;
259 }
260
261 uint8_t peek() {
262 if (!hasData_) {
263 trans_->readAll(&data_, 1);
264 }
265 hasData_ = true;
266 return data_;
267 }
268
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100269 private:
270 TTransport* trans_;
David Reiss1e62ab42008-02-21 22:37:45 +0000271 bool hasData_;
272 uint8_t data_;
273 };
274
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100275private:
David Reisse71115b2010-10-06 17:09:56 +0000276 TTransport* trans_;
David Reissdb0ea152008-02-18 01:49:37 +0000277
278 std::stack<boost::shared_ptr<TJSONContext> > contexts_;
279 boost::shared_ptr<TJSONContext> context_;
David Reiss1e62ab42008-02-21 22:37:45 +0000280 LookaheadReader reader_;
David Reissdb0ea152008-02-18 01:49:37 +0000281};
282
283/**
284 * Constructs input and output protocol objects given transports.
285 */
David Reiss4c266cc2009-01-15 23:56:24 +0000286class TJSONProtocolFactory : public TProtocolFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100287public:
David Reissdb0ea152008-02-18 01:49:37 +0000288 TJSONProtocolFactory() {}
289
290 virtual ~TJSONProtocolFactory() {}
291
292 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
293 return boost::shared_ptr<TProtocol>(new TJSONProtocol(trans));
294 }
295};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100296}
297}
298} // apache::thrift::protocol
David Reissdb0ea152008-02-18 01:49:37 +0000299
David Reiss28f298d2008-05-01 06:17:36 +0000300// TODO(dreiss): Move part of ThriftJSONString into a .cpp file and remove this.
Roger Meier49ff8b12012-04-13 09:12:31 +0000301#include <thrift/transport/TBufferTransports.h>
David Reiss28f298d2008-05-01 06:17:36 +0000302
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100303namespace apache {
304namespace thrift {
David Reissdb0ea152008-02-18 01:49:37 +0000305
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100306template <typename ThriftStruct>
307std::string ThriftJSONString(const ThriftStruct& ts) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000308 using namespace apache::thrift::transport;
309 using namespace apache::thrift::protocol;
David Reissdb0ea152008-02-18 01:49:37 +0000310 TMemoryBuffer* buffer = new TMemoryBuffer;
311 boost::shared_ptr<TTransport> trans(buffer);
312 TJSONProtocol protocol(trans);
313
314 ts.write(&protocol);
315
316 uint8_t* buf;
317 uint32_t size;
318 buffer->getBuffer(&buf, &size);
319 return std::string((char*)buf, (unsigned int)size);
320}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100321}
322} // apache::thrift
David Reissdb0ea152008-02-18 01:49:37 +0000323
324#endif // #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1