| 30 |
ashish |
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 |
*/
|
|
|
19 |
|
|
|
20 |
#ifndef _THRIFT_PROTOCOL_TJSONPROTOCOL_H_
|
|
|
21 |
#define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1
|
|
|
22 |
|
|
|
23 |
#include "TProtocol.h"
|
|
|
24 |
|
|
|
25 |
#include <stack>
|
|
|
26 |
|
|
|
27 |
namespace apache { namespace thrift { namespace protocol {
|
|
|
28 |
|
|
|
29 |
// Forward declaration
|
|
|
30 |
class TJSONContext;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* JSON protocol for Thrift.
|
|
|
34 |
*
|
|
|
35 |
* Implements a protocol which uses JSON as the wire-format.
|
|
|
36 |
*
|
|
|
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 |
*
|
|
|
94 |
*/
|
|
|
95 |
class TJSONProtocol : public TProtocol {
|
|
|
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 |
|
|
|
164 |
uint32_t writeStructBegin(const char* name);
|
|
|
165 |
|
|
|
166 |
uint32_t writeStructEnd();
|
|
|
167 |
|
|
|
168 |
uint32_t writeFieldBegin(const char* name,
|
|
|
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 |
|
|
|
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 |
|
|
|
293 |
private:
|
|
|
294 |
|
|
|
295 |
std::stack<boost::shared_ptr<TJSONContext> > contexts_;
|
|
|
296 |
boost::shared_ptr<TJSONContext> context_;
|
|
|
297 |
LookaheadReader reader_;
|
|
|
298 |
};
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Constructs input and output protocol objects given transports.
|
|
|
302 |
*/
|
|
|
303 |
class TJSONProtocolFactory : public TProtocolFactory {
|
|
|
304 |
public:
|
|
|
305 |
TJSONProtocolFactory() {}
|
|
|
306 |
|
|
|
307 |
virtual ~TJSONProtocolFactory() {}
|
|
|
308 |
|
|
|
309 |
boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
|
|
|
310 |
return boost::shared_ptr<TProtocol>(new TJSONProtocol(trans));
|
|
|
311 |
}
|
|
|
312 |
};
|
|
|
313 |
|
|
|
314 |
}}} // apache::thrift::protocol
|
|
|
315 |
|
|
|
316 |
|
|
|
317 |
// TODO(dreiss): Move part of ThriftJSONString into a .cpp file and remove this.
|
|
|
318 |
#include <transport/TBufferTransports.h>
|
|
|
319 |
|
|
|
320 |
namespace apache { namespace thrift {
|
|
|
321 |
|
|
|
322 |
template<typename ThriftStruct>
|
|
|
323 |
std::string ThriftJSONString(const ThriftStruct& ts) {
|
|
|
324 |
using namespace apache::thrift::transport;
|
|
|
325 |
using namespace apache::thrift::protocol;
|
|
|
326 |
TMemoryBuffer* buffer = new TMemoryBuffer;
|
|
|
327 |
boost::shared_ptr<TTransport> trans(buffer);
|
|
|
328 |
TJSONProtocol protocol(trans);
|
|
|
329 |
|
|
|
330 |
ts.write(&protocol);
|
|
|
331 |
|
|
|
332 |
uint8_t* buf;
|
|
|
333 |
uint32_t size;
|
|
|
334 |
buffer->getBuffer(&buf, &size);
|
|
|
335 |
return std::string((char*)buf, (unsigned int)size);
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
}} // apache::thrift
|
|
|
339 |
|
|
|
340 |
#endif // #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1
|