blob: bc9ad4e09412579f8657bb891062dbfc29a7323b [file] [log] [blame]
David Reissdb0ea152008-02-18 01:49:37 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
7#ifndef _THRIFT_PROTOCOL_TJSONPROTOCOL_H_
8#define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1
9
10#include "TProtocol.h"
David Reissdb0ea152008-02-18 01:49:37 +000011
12#include <stack>
13
T Jake Lucianib5e62212009-01-31 22:36:20 +000014namespace apache { namespace thrift { namespace protocol {
David Reissdb0ea152008-02-18 01:49:37 +000015
16// Forward declaration
17class TJSONContext;
18
19/**
20 * JSON protocol for Thrift.
21 *
David Reiss1e62ab42008-02-21 22:37:45 +000022 * Implements a protocol which uses JSON as the wire-format.
23 *
David Reissdb0ea152008-02-18 01:49:37 +000024 * Thrift types are represented as described below:
25 *
26 * 1. Every Thrift integer type is represented as a JSON number.
27 *
28 * 2. Thrift doubles are represented as JSON numbers. Some special values are
29 * represented as strings:
30 * a. "NaN" for not-a-number values
31 * b. "Infinity" for postive infinity
32 * c. "-Infinity" for negative infinity
33 *
34 * 3. Thrift string values are emitted as JSON strings, with appropriate
35 * escaping.
36 *
37 * 4. Thrift binary values are encoded into Base64 and emitted as JSON strings.
38 * The readBinary() method is written such that it will properly skip if
39 * called on a Thrift string (although it will decode garbage data).
40 *
41 * 5. Thrift structs are represented as JSON objects, with the field ID as the
42 * key, and the field value represented as a JSON object with a single
43 * key-value pair. The key is a short string identifier for that type,
44 * followed by the value. The valid type identifiers are: "tf" for bool,
45 * "i8" for byte, "i16" for 16-bit integer, "i32" for 32-bit integer, "i64"
46 * for 64-bit integer, "dbl" for double-precision loating point, "str" for
47 * string (including binary), "rec" for struct ("records"), "map" for map,
48 * "lst" for list, "set" for set.
49 *
50 * 6. Thrift lists and sets are represented as JSON arrays, with the first
51 * element of the JSON array being the string identifier for the Thrift
52 * element type and the second element of the JSON array being the count of
53 * the Thrift elements. The Thrift elements then follow.
54 *
55 * 7. Thrift maps are represented as JSON arrays, with the first two elements
56 * of the JSON array being the string identifiers for the Thrift key type
57 * and value type, followed by the count of the Thrift pairs, followed by a
58 * JSON object containing the key-value pairs. Note that JSON keys can only
59 * be strings, which means that the key type of the Thrift map should be
60 * restricted to numeric or string types -- in the case of numerics, they
61 * are serialized as strings.
62 *
63 * 8. Thrift messages are represented as JSON arrays, with the protocol
64 * version #, the message name, the message type, and the sequence ID as
65 * the first 4 elements.
66 *
67 * More discussion of the double handling is probably warranted. The aim of
68 * the current implementation is to match as closely as possible the behavior
69 * of Java's Double.toString(), which has no precision loss. Implementors in
70 * other languages should strive to achieve that where possible. I have not
71 * yet verified whether boost:lexical_cast, which is doing that work for me in
72 * C++, loses any precision, but I am leaving this as a future improvement. I
73 * may try to provide a C component for this, so that other languages could
74 * bind to the same underlying implementation for maximum consistency.
75 *
76 * Note further that JavaScript itself is not capable of representing
77 * floating point infinities -- presumably when we have a JavaScript Thrift
78 * client, this would mean that infinities get converted to not-a-number in
79 * transmission. I don't know of any work-around for this issue.
80 *
David Reissdb0ea152008-02-18 01:49:37 +000081 */
82class TJSONProtocol : public TProtocol {
83 public:
84
85 TJSONProtocol(boost::shared_ptr<TTransport> ptrans);
86
87 ~TJSONProtocol();
88
89 private:
90
91 void pushContext(boost::shared_ptr<TJSONContext> c);
92
93 void popContext();
94
95 uint32_t writeJSONEscapeChar(uint8_t ch);
96
97 uint32_t writeJSONChar(uint8_t ch);
98
99 uint32_t writeJSONString(const std::string &str);
100
101 uint32_t writeJSONBase64(const std::string &str);
102
103 template <typename NumberType>
104 uint32_t writeJSONInteger(NumberType num);
105
106 uint32_t writeJSONDouble(double num);
107
108 uint32_t writeJSONObjectStart() ;
109
110 uint32_t writeJSONObjectEnd();
111
112 uint32_t writeJSONArrayStart();
113
114 uint32_t writeJSONArrayEnd();
115
116 uint32_t readJSONSyntaxChar(uint8_t ch);
117
118 uint32_t readJSONEscapeChar(uint8_t *out);
119
120 uint32_t readJSONString(std::string &str, bool skipContext = false);
121
122 uint32_t readJSONBase64(std::string &str);
123
124 uint32_t readJSONNumericChars(std::string &str);
125
126 template <typename NumberType>
127 uint32_t readJSONInteger(NumberType &num);
128
129 uint32_t readJSONDouble(double &num);
130
131 uint32_t readJSONObjectStart();
132
133 uint32_t readJSONObjectEnd();
134
135 uint32_t readJSONArrayStart();
136
137 uint32_t readJSONArrayEnd();
138
139 public:
140
141 /**
142 * Writing functions.
143 */
144
145 uint32_t writeMessageBegin(const std::string& name,
146 const TMessageType messageType,
147 const int32_t seqid);
148
149 uint32_t writeMessageEnd();
150
David Reiss64120002008-04-29 23:12:24 +0000151 uint32_t writeStructBegin(const char* name);
David Reissdb0ea152008-02-18 01:49:37 +0000152
153 uint32_t writeStructEnd();
154
David Reiss64120002008-04-29 23:12:24 +0000155 uint32_t writeFieldBegin(const char* name,
David Reissdb0ea152008-02-18 01:49:37 +0000156 const TType fieldType,
157 const int16_t fieldId);
158
159 uint32_t writeFieldEnd();
160
161 uint32_t writeFieldStop();
162
163 uint32_t writeMapBegin(const TType keyType,
164 const TType valType,
165 const uint32_t size);
166
167 uint32_t writeMapEnd();
168
169 uint32_t writeListBegin(const TType elemType,
170 const uint32_t size);
171
172 uint32_t writeListEnd();
173
174 uint32_t writeSetBegin(const TType elemType,
175 const uint32_t size);
176
177 uint32_t writeSetEnd();
178
179 uint32_t writeBool(const bool value);
180
181 uint32_t writeByte(const int8_t byte);
182
183 uint32_t writeI16(const int16_t i16);
184
185 uint32_t writeI32(const int32_t i32);
186
187 uint32_t writeI64(const int64_t i64);
188
189 uint32_t writeDouble(const double dub);
190
191 uint32_t writeString(const std::string& str);
192
193 uint32_t writeBinary(const std::string& str);
194
195 /**
196 * Reading functions
197 */
198
199 uint32_t readMessageBegin(std::string& name,
200 TMessageType& messageType,
201 int32_t& seqid);
202
203 uint32_t readMessageEnd();
204
205 uint32_t readStructBegin(std::string& name);
206
207 uint32_t readStructEnd();
208
209 uint32_t readFieldBegin(std::string& name,
210 TType& fieldType,
211 int16_t& fieldId);
212
213 uint32_t readFieldEnd();
214
215 uint32_t readMapBegin(TType& keyType,
216 TType& valType,
217 uint32_t& size);
218
219 uint32_t readMapEnd();
220
221 uint32_t readListBegin(TType& elemType,
222 uint32_t& size);
223
224 uint32_t readListEnd();
225
226 uint32_t readSetBegin(TType& elemType,
227 uint32_t& size);
228
229 uint32_t readSetEnd();
230
231 uint32_t readBool(bool& value);
232
233 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
249 public:
250
251 LookaheadReader(TTransport &trans) :
252 trans_(&trans),
253 hasData_(false) {
254 }
255
256 uint8_t read() {
257 if (hasData_) {
258 hasData_ = false;
259 }
260 else {
261 trans_->readAll(&data_, 1);
262 }
263 return data_;
264 }
265
266 uint8_t peek() {
267 if (!hasData_) {
268 trans_->readAll(&data_, 1);
269 }
270 hasData_ = true;
271 return data_;
272 }
273
274 private:
275 TTransport *trans_;
276 bool hasData_;
277 uint8_t data_;
278 };
279
David Reissdb0ea152008-02-18 01:49:37 +0000280 private:
281
282 std::stack<boost::shared_ptr<TJSONContext> > contexts_;
283 boost::shared_ptr<TJSONContext> context_;
David Reiss1e62ab42008-02-21 22:37:45 +0000284 LookaheadReader reader_;
David Reissdb0ea152008-02-18 01:49:37 +0000285};
286
287/**
288 * Constructs input and output protocol objects given transports.
289 */
David Reiss4c266cc2009-01-15 23:56:24 +0000290class TJSONProtocolFactory : public TProtocolFactory {
David Reissdb0ea152008-02-18 01:49:37 +0000291 public:
292 TJSONProtocolFactory() {}
293
294 virtual ~TJSONProtocolFactory() {}
295
296 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
297 return boost::shared_ptr<TProtocol>(new TJSONProtocol(trans));
298 }
299};
300
T Jake Lucianib5e62212009-01-31 22:36:20 +0000301}}} // apache::thrift::protocol
David Reissdb0ea152008-02-18 01:49:37 +0000302
303
David Reiss28f298d2008-05-01 06:17:36 +0000304// TODO(dreiss): Move part of ThriftJSONString into a .cpp file and remove this.
305#include <transport/TBufferTransports.h>
306
T Jake Lucianib5e62212009-01-31 22:36:20 +0000307namespace apache { namespace thrift {
David Reissdb0ea152008-02-18 01:49:37 +0000308
309template<typename ThriftStruct>
310 std::string ThriftJSONString(const ThriftStruct& ts) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000311 using namespace apache::thrift::transport;
312 using namespace apache::thrift::protocol;
David Reissdb0ea152008-02-18 01:49:37 +0000313 TMemoryBuffer* buffer = new TMemoryBuffer;
314 boost::shared_ptr<TTransport> trans(buffer);
315 TJSONProtocol protocol(trans);
316
317 ts.write(&protocol);
318
319 uint8_t* buf;
320 uint32_t size;
321 buffer->getBuffer(&buf, &size);
322 return std::string((char*)buf, (unsigned int)size);
323}
324
T Jake Lucianib5e62212009-01-31 22:36:20 +0000325}} // apache::thrift
David Reissdb0ea152008-02-18 01:49:37 +0000326
327#endif // #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1