blob: 799c3616b0dd5a4a5b0542f95dd2b859921c4ee4 [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
David Reiss6806fb82010-10-06 17:09:52 +000023#include "TVirtualProtocol.h"
David Reissdb0ea152008-02-18 01:49:37 +000024
25#include <stack>
26
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace protocol {
David Reissdb0ea152008-02-18 01:49:37 +000028
29// Forward declaration
30class TJSONContext;
31
32/**
33 * JSON protocol for Thrift.
34 *
David Reiss1e62ab42008-02-21 22:37:45 +000035 * Implements a protocol which uses JSON as the wire-format.
36 *
David Reissdb0ea152008-02-18 01:49:37 +000037 * 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 *
David Reissdb0ea152008-02-18 01:49:37 +000089 */
David Reiss6806fb82010-10-06 17:09:52 +000090class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> {
David Reissdb0ea152008-02-18 01:49:37 +000091 public:
92
93 TJSONProtocol(boost::shared_ptr<TTransport> ptrans);
94
95 ~TJSONProtocol();
96
97 private:
98
99 void pushContext(boost::shared_ptr<TJSONContext> c);
100
101 void popContext();
102
103 uint32_t writeJSONEscapeChar(uint8_t ch);
104
105 uint32_t writeJSONChar(uint8_t ch);
106
107 uint32_t writeJSONString(const std::string &str);
108
109 uint32_t writeJSONBase64(const std::string &str);
110
111 template <typename NumberType>
112 uint32_t writeJSONInteger(NumberType num);
113
114 uint32_t writeJSONDouble(double num);
115
116 uint32_t writeJSONObjectStart() ;
117
118 uint32_t writeJSONObjectEnd();
119
120 uint32_t writeJSONArrayStart();
121
122 uint32_t writeJSONArrayEnd();
123
124 uint32_t readJSONSyntaxChar(uint8_t ch);
125
126 uint32_t readJSONEscapeChar(uint8_t *out);
127
128 uint32_t readJSONString(std::string &str, bool skipContext = false);
129
130 uint32_t readJSONBase64(std::string &str);
131
132 uint32_t readJSONNumericChars(std::string &str);
133
134 template <typename NumberType>
135 uint32_t readJSONInteger(NumberType &num);
136
137 uint32_t readJSONDouble(double &num);
138
139 uint32_t readJSONObjectStart();
140
141 uint32_t readJSONObjectEnd();
142
143 uint32_t readJSONArrayStart();
144
145 uint32_t readJSONArrayEnd();
146
147 public:
148
149 /**
150 * Writing functions.
151 */
152
153 uint32_t writeMessageBegin(const std::string& name,
154 const TMessageType messageType,
155 const int32_t seqid);
156
157 uint32_t writeMessageEnd();
158
David Reiss64120002008-04-29 23:12:24 +0000159 uint32_t writeStructBegin(const char* name);
David Reissdb0ea152008-02-18 01:49:37 +0000160
161 uint32_t writeStructEnd();
162
David Reiss64120002008-04-29 23:12:24 +0000163 uint32_t writeFieldBegin(const char* name,
David Reissdb0ea152008-02-18 01:49:37 +0000164 const TType fieldType,
165 const int16_t fieldId);
166
167 uint32_t writeFieldEnd();
168
169 uint32_t writeFieldStop();
170
171 uint32_t writeMapBegin(const TType keyType,
172 const TType valType,
173 const uint32_t size);
174
175 uint32_t writeMapEnd();
176
177 uint32_t writeListBegin(const TType elemType,
178 const uint32_t size);
179
180 uint32_t writeListEnd();
181
182 uint32_t writeSetBegin(const TType elemType,
183 const uint32_t size);
184
185 uint32_t writeSetEnd();
186
187 uint32_t writeBool(const bool value);
188
189 uint32_t writeByte(const int8_t byte);
190
191 uint32_t writeI16(const int16_t i16);
192
193 uint32_t writeI32(const int32_t i32);
194
195 uint32_t writeI64(const int64_t i64);
196
197 uint32_t writeDouble(const double dub);
198
199 uint32_t writeString(const std::string& str);
200
201 uint32_t writeBinary(const std::string& str);
202
203 /**
204 * Reading functions
205 */
206
207 uint32_t readMessageBegin(std::string& name,
208 TMessageType& messageType,
209 int32_t& seqid);
210
211 uint32_t readMessageEnd();
212
213 uint32_t readStructBegin(std::string& name);
214
215 uint32_t readStructEnd();
216
217 uint32_t readFieldBegin(std::string& name,
218 TType& fieldType,
219 int16_t& fieldId);
220
221 uint32_t readFieldEnd();
222
223 uint32_t readMapBegin(TType& keyType,
224 TType& valType,
225 uint32_t& size);
226
227 uint32_t readMapEnd();
228
229 uint32_t readListBegin(TType& elemType,
230 uint32_t& size);
231
232 uint32_t readListEnd();
233
234 uint32_t readSetBegin(TType& elemType,
235 uint32_t& size);
236
237 uint32_t readSetEnd();
238
239 uint32_t readBool(bool& value);
240
David Reiss8dfc7322010-10-06 17:09:58 +0000241 // Provide the default readBool() implementation for std::vector<bool>
242 using TVirtualProtocol<TJSONProtocol>::readBool;
243
David Reissdb0ea152008-02-18 01:49:37 +0000244 uint32_t readByte(int8_t& byte);
245
246 uint32_t readI16(int16_t& i16);
247
248 uint32_t readI32(int32_t& i32);
249
250 uint32_t readI64(int64_t& i64);
251
252 uint32_t readDouble(double& dub);
253
254 uint32_t readString(std::string& str);
255
256 uint32_t readBinary(std::string& str);
257
David Reiss1e62ab42008-02-21 22:37:45 +0000258 class LookaheadReader {
259
260 public:
261
262 LookaheadReader(TTransport &trans) :
263 trans_(&trans),
264 hasData_(false) {
265 }
266
267 uint8_t read() {
268 if (hasData_) {
269 hasData_ = false;
270 }
271 else {
272 trans_->readAll(&data_, 1);
273 }
274 return data_;
275 }
276
277 uint8_t peek() {
278 if (!hasData_) {
279 trans_->readAll(&data_, 1);
280 }
281 hasData_ = true;
282 return data_;
283 }
284
285 private:
286 TTransport *trans_;
287 bool hasData_;
288 uint8_t data_;
289 };
290
David Reissdb0ea152008-02-18 01:49:37 +0000291 private:
David Reisse71115b2010-10-06 17:09:56 +0000292 TTransport* trans_;
David Reissdb0ea152008-02-18 01:49:37 +0000293
294 std::stack<boost::shared_ptr<TJSONContext> > contexts_;
295 boost::shared_ptr<TJSONContext> context_;
David Reiss1e62ab42008-02-21 22:37:45 +0000296 LookaheadReader reader_;
David Reissdb0ea152008-02-18 01:49:37 +0000297};
298
299/**
300 * Constructs input and output protocol objects given transports.
301 */
David Reiss4c266cc2009-01-15 23:56:24 +0000302class TJSONProtocolFactory : public TProtocolFactory {
David Reissdb0ea152008-02-18 01:49:37 +0000303 public:
304 TJSONProtocolFactory() {}
305
306 virtual ~TJSONProtocolFactory() {}
307
308 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
309 return boost::shared_ptr<TProtocol>(new TJSONProtocol(trans));
310 }
311};
312
T Jake Lucianib5e62212009-01-31 22:36:20 +0000313}}} // apache::thrift::protocol
David Reissdb0ea152008-02-18 01:49:37 +0000314
315
David Reiss28f298d2008-05-01 06:17:36 +0000316// TODO(dreiss): Move part of ThriftJSONString into a .cpp file and remove this.
317#include <transport/TBufferTransports.h>
318
T Jake Lucianib5e62212009-01-31 22:36:20 +0000319namespace apache { namespace thrift {
David Reissdb0ea152008-02-18 01:49:37 +0000320
321template<typename ThriftStruct>
322 std::string ThriftJSONString(const ThriftStruct& ts) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000323 using namespace apache::thrift::transport;
324 using namespace apache::thrift::protocol;
David Reissdb0ea152008-02-18 01:49:37 +0000325 TMemoryBuffer* buffer = new TMemoryBuffer;
326 boost::shared_ptr<TTransport> trans(buffer);
327 TJSONProtocol protocol(trans);
328
329 ts.write(&protocol);
330
331 uint8_t* buf;
332 uint32_t size;
333 buffer->getBuffer(&buf, &size);
334 return std::string((char*)buf, (unsigned int)size);
335}
336
T Jake Lucianib5e62212009-01-31 22:36:20 +0000337}} // apache::thrift
David Reissdb0ea152008-02-18 01:49:37 +0000338
339#endif // #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1