blob: e98f4cf0d07b02194a941818c8a428c6ff98ece9 [file] [log] [blame]
Roger Meier85fb6de2012-11-02 00:05:42 +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#
19
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090020from .TProtocol import TType, TProtocolBase, TProtocolException, checkIntegerLimits
Roger Meier0895dfe2012-12-26 22:09:55 +010021import base64
Roger Meier0895dfe2012-12-26 22:09:55 +010022import math
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090023import sys
24
25from ..compat import str_to_binary
26
Roger Meier85fb6de2012-11-02 00:05:42 +000027
Roger Meier0895dfe2012-12-26 22:09:55 +010028__all__ = ['TJSONProtocol',
29 'TJSONProtocolFactory',
30 'TSimpleJSONProtocol',
31 'TSimpleJSONProtocolFactory']
Roger Meier85fb6de2012-11-02 00:05:42 +000032
33VERSION = 1
34
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090035COMMA = b','
36COLON = b':'
37LBRACE = b'{'
38RBRACE = b'}'
39LBRACKET = b'['
40RBRACKET = b']'
41QUOTE = b'"'
42BACKSLASH = b'\\'
43ZERO = b'0'
Roger Meier85fb6de2012-11-02 00:05:42 +000044
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090045ESCSEQ0 = ord('\\')
46ESCSEQ1 = ord('u')
47ESCAPE_CHAR_VALS = {
48 '"': '\\"',
49 '\\': '\\\\',
50 '\b': '\\b',
51 '\f': '\\f',
52 '\n': '\\n',
53 '\r': '\\r',
54 '\t': '\\t',
55 # '/': '\\/',
56}
57ESCAPE_CHARS = {
58 b'"': '"',
59 b'\\': '\\',
60 b'b': '\b',
61 b'f': '\f',
62 b'n': '\n',
63 b'r': '\r',
64 b't': '\t',
65 b'/': '/',
66}
67NUMERIC_CHAR = b'+-.0123456789Ee'
Roger Meier85fb6de2012-11-02 00:05:42 +000068
69CTYPES = {TType.BOOL: 'tf',
70 TType.BYTE: 'i8',
71 TType.I16: 'i16',
72 TType.I32: 'i32',
73 TType.I64: 'i64',
74 TType.DOUBLE: 'dbl',
75 TType.STRING: 'str',
Roger Meier05ab89a2012-11-02 10:36:59 +000076 TType.STRUCT: 'rec',
Roger Meier85fb6de2012-11-02 00:05:42 +000077 TType.LIST: 'lst',
78 TType.SET: 'set',
79 TType.MAP: 'map'}
80
81JTYPES = {}
82for key in CTYPES.keys():
83 JTYPES[CTYPES[key]] = key
84
85
Roger Meierad8154a2012-12-18 21:02:16 +010086class JSONBaseContext(object):
Roger Meier85fb6de2012-11-02 00:05:42 +000087
88 def __init__(self, protocol):
89 self.protocol = protocol
90 self.first = True
91
92 def doIO(self, function):
93 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090094
Roger Meier85fb6de2012-11-02 00:05:42 +000095 def write(self):
96 pass
97
98 def read(self):
99 pass
100
101 def escapeNum(self):
102 return False
103
Roger Meier0895dfe2012-12-26 22:09:55 +0100104 def __str__(self):
105 return self.__class__.__name__
106
Roger Meier85fb6de2012-11-02 00:05:42 +0000107
108class JSONListContext(JSONBaseContext):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900109
Roger Meier85fb6de2012-11-02 00:05:42 +0000110 def doIO(self, function):
111 if self.first is True:
112 self.first = False
113 else:
114 function(COMMA)
115
116 def write(self):
117 self.doIO(self.protocol.trans.write)
118
119 def read(self):
120 self.doIO(self.protocol.readJSONSyntaxChar)
121
122
123class JSONPairContext(JSONBaseContext):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900124
Roger Meier0895dfe2012-12-26 22:09:55 +0100125 def __init__(self, protocol):
126 super(JSONPairContext, self).__init__(protocol)
127 self.colon = True
Roger Meier85fb6de2012-11-02 00:05:42 +0000128
129 def doIO(self, function):
Roger Meier0895dfe2012-12-26 22:09:55 +0100130 if self.first:
Roger Meier85fb6de2012-11-02 00:05:42 +0000131 self.first = False
132 self.colon = True
133 else:
Roger Meier0895dfe2012-12-26 22:09:55 +0100134 function(COLON if self.colon else COMMA)
Roger Meier85fb6de2012-11-02 00:05:42 +0000135 self.colon = not self.colon
136
137 def write(self):
138 self.doIO(self.protocol.trans.write)
139
140 def read(self):
141 self.doIO(self.protocol.readJSONSyntaxChar)
142
143 def escapeNum(self):
144 return self.colon
145
Roger Meier0895dfe2012-12-26 22:09:55 +0100146 def __str__(self):
147 return '%s, colon=%s' % (self.__class__.__name__, self.colon)
148
Roger Meier85fb6de2012-11-02 00:05:42 +0000149
150class LookaheadReader():
151 hasData = False
152 data = ''
153
154 def __init__(self, protocol):
155 self.protocol = protocol
156
157 def read(self):
158 if self.hasData is True:
159 self.hasData = False
160 else:
161 self.data = self.protocol.trans.read(1)
162 return self.data
163
164 def peek(self):
165 if self.hasData is False:
166 self.data = self.protocol.trans.read(1)
167 self.hasData = True
168 return self.data
169
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900170
Roger Meier85fb6de2012-11-02 00:05:42 +0000171class TJSONProtocolBase(TProtocolBase):
172
173 def __init__(self, trans):
174 TProtocolBase.__init__(self, trans)
Roger Meierad8154a2012-12-18 21:02:16 +0100175 self.resetWriteContext()
176 self.resetReadContext()
Roger Meier85fb6de2012-11-02 00:05:42 +0000177
178 def resetWriteContext(self):
Roger Meier0895dfe2012-12-26 22:09:55 +0100179 self.context = JSONBaseContext(self)
180 self.contextStack = [self.context]
Roger Meier85fb6de2012-11-02 00:05:42 +0000181
182 def resetReadContext(self):
183 self.resetWriteContext()
184 self.reader = LookaheadReader(self)
185
186 def pushContext(self, ctx):
187 self.contextStack.append(ctx)
188 self.context = ctx
189
190 def popContext(self):
191 self.contextStack.pop()
Roger Meier0895dfe2012-12-26 22:09:55 +0100192 if self.contextStack:
193 self.context = self.contextStack[-1]
194 else:
195 self.context = JSONBaseContext(self)
Roger Meier85fb6de2012-11-02 00:05:42 +0000196
197 def writeJSONString(self, string):
198 self.context.write()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900199 json_str = ['"']
200 for s in string:
201 escaped = ESCAPE_CHAR_VALS.get(s, s)
202 json_str.append(escaped)
203 json_str.append('"')
204 self.trans.write(str_to_binary(''.join(json_str)))
Roger Meier85fb6de2012-11-02 00:05:42 +0000205
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900206 def writeJSONNumber(self, number, formatter='{}'):
Roger Meier85fb6de2012-11-02 00:05:42 +0000207 self.context.write()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900208 jsNumber = str(formatter.format(number)).encode('ascii')
Roger Meier85fb6de2012-11-02 00:05:42 +0000209 if self.context.escapeNum():
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900210 self.trans.write(QUOTE)
211 self.trans.write(jsNumber)
212 self.trans.write(QUOTE)
213 else:
214 self.trans.write(jsNumber)
Roger Meier85fb6de2012-11-02 00:05:42 +0000215
216 def writeJSONBase64(self, binary):
217 self.context.write()
218 self.trans.write(QUOTE)
219 self.trans.write(base64.b64encode(binary))
220 self.trans.write(QUOTE)
221
222 def writeJSONObjectStart(self):
223 self.context.write()
224 self.trans.write(LBRACE)
225 self.pushContext(JSONPairContext(self))
226
227 def writeJSONObjectEnd(self):
228 self.popContext()
229 self.trans.write(RBRACE)
230
231 def writeJSONArrayStart(self):
232 self.context.write()
233 self.trans.write(LBRACKET)
234 self.pushContext(JSONListContext(self))
235
236 def writeJSONArrayEnd(self):
237 self.popContext()
238 self.trans.write(RBRACKET)
239
240 def readJSONSyntaxChar(self, character):
241 current = self.reader.read()
242 if character != current:
243 raise TProtocolException(TProtocolException.INVALID_DATA,
244 "Unexpected character: %s" % current)
245
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700246 def _isHighSurrogate(self, codeunit):
247 return codeunit >= 0xd800 and codeunit <= 0xdbff
248
249 def _isLowSurrogate(self, codeunit):
250 return codeunit >= 0xdc00 and codeunit <= 0xdfff
251
Roger Meier85fb6de2012-11-02 00:05:42 +0000252 def readJSONString(self, skipContext):
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700253 highSurrogate = None
Roger Meier85fb6de2012-11-02 00:05:42 +0000254 string = []
255 if skipContext is False:
256 self.context.read()
257 self.readJSONSyntaxChar(QUOTE)
258 while True:
259 character = self.reader.read()
260 if character == QUOTE:
261 break
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900262 if ord(character) == ESCSEQ0:
Roger Meier85fb6de2012-11-02 00:05:42 +0000263 character = self.reader.read()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900264 if ord(character) == ESCSEQ1:
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700265 if sys.version_info[0] == 2:
266 import json
267 character = self.trans.read(4)
268 codeunit = int(character, 16)
269 if self._isHighSurrogate(codeunit):
270 if highSurrogate:
271 raise TProtocolException(TProtocolException.INVALID_DATA,
272 "Expected low surrogate char")
273 highSurrogate = character
274 continue
275 elif self._isLowSurrogate(codeunit):
276 if not highSurrogate:
277 raise TProtocolException(TProtocolException.INVALID_DATA,
278 "Expected high surrogate char")
279 character = json.JSONDecoder().decode('"\\u%s\\u%s"' % (highSurrogate, character)).encode('utf-8')
280 highSurrogate = None
281 else:
282 character = json.JSONDecoder().decode('"\\u%s"' % character).encode('utf-8')
283 else:
284 character = chr(int(self.trans.read(4)))
Roger Meier85fb6de2012-11-02 00:05:42 +0000285 else:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900286 if character not in ESCAPE_CHARS:
Roger Meier85fb6de2012-11-02 00:05:42 +0000287 raise TProtocolException(TProtocolException.INVALID_DATA,
288 "Expected control char")
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900289 character = ESCAPE_CHARS[character]
290 elif character in ESCAPE_CHAR_VALS:
291 raise TProtocolException(TProtocolException.INVALID_DATA,
292 "Unescaped control char")
293 elif sys.version_info[0] > 2:
294 utf8_bytes = bytearray([ord(character)])
295 while ord(self.reader.peek()) >= 0x80:
296 utf8_bytes.append(ord(self.reader.read()))
297 character = utf8_bytes.decode('utf8')
Roger Meier85fb6de2012-11-02 00:05:42 +0000298 string.append(character)
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700299
300 if highSurrogate:
301 raise TProtocolException(TProtocolException.INVALID_DATA,
302 "Expected low surrogate char")
Roger Meier85fb6de2012-11-02 00:05:42 +0000303 return ''.join(string)
304
305 def isJSONNumeric(self, character):
306 return (True if NUMERIC_CHAR.find(character) != - 1 else False)
307
308 def readJSONQuotes(self):
309 if (self.context.escapeNum()):
310 self.readJSONSyntaxChar(QUOTE)
311
312 def readJSONNumericChars(self):
313 numeric = []
314 while True:
315 character = self.reader.peek()
316 if self.isJSONNumeric(character) is False:
317 break
318 numeric.append(self.reader.read())
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900319 return b''.join(numeric).decode('ascii')
Roger Meier85fb6de2012-11-02 00:05:42 +0000320
321 def readJSONInteger(self):
322 self.context.read()
323 self.readJSONQuotes()
324 numeric = self.readJSONNumericChars()
325 self.readJSONQuotes()
326 try:
327 return int(numeric)
328 except ValueError:
329 raise TProtocolException(TProtocolException.INVALID_DATA,
330 "Bad data encounted in numeric data")
331
332 def readJSONDouble(self):
333 self.context.read()
334 if self.reader.peek() == QUOTE:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900335 string = self.readJSONString(True)
Roger Meier85fb6de2012-11-02 00:05:42 +0000336 try:
337 double = float(string)
Roger Meier0895dfe2012-12-26 22:09:55 +0100338 if (self.context.escapeNum is False and
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900339 not math.isinf(double) and
340 not math.isnan(double)):
Roger Meier85fb6de2012-11-02 00:05:42 +0000341 raise TProtocolException(TProtocolException.INVALID_DATA,
342 "Numeric data unexpectedly quoted")
343 return double
344 except ValueError:
345 raise TProtocolException(TProtocolException.INVALID_DATA,
346 "Bad data encounted in numeric data")
347 else:
348 if self.context.escapeNum() is True:
349 self.readJSONSyntaxChar(QUOTE)
350 try:
351 return float(self.readJSONNumericChars())
Roger Meierad8154a2012-12-18 21:02:16 +0100352 except ValueError:
Roger Meier85fb6de2012-11-02 00:05:42 +0000353 raise TProtocolException(TProtocolException.INVALID_DATA,
354 "Bad data encounted in numeric data")
355
356 def readJSONBase64(self):
357 string = self.readJSONString(False)
358 return base64.b64decode(string)
359
360 def readJSONObjectStart(self):
361 self.context.read()
362 self.readJSONSyntaxChar(LBRACE)
363 self.pushContext(JSONPairContext(self))
364
365 def readJSONObjectEnd(self):
366 self.readJSONSyntaxChar(RBRACE)
367 self.popContext()
368
369 def readJSONArrayStart(self):
370 self.context.read()
371 self.readJSONSyntaxChar(LBRACKET)
372 self.pushContext(JSONListContext(self))
373
374 def readJSONArrayEnd(self):
375 self.readJSONSyntaxChar(RBRACKET)
376 self.popContext()
377
378
379class TJSONProtocol(TJSONProtocolBase):
380
381 def readMessageBegin(self):
382 self.resetReadContext()
383 self.readJSONArrayStart()
384 if self.readJSONInteger() != VERSION:
385 raise TProtocolException(TProtocolException.BAD_VERSION,
386 "Message contained bad version.")
387 name = self.readJSONString(False)
388 typen = self.readJSONInteger()
389 seqid = self.readJSONInteger()
390 return (name, typen, seqid)
391
392 def readMessageEnd(self):
393 self.readJSONArrayEnd()
394
395 def readStructBegin(self):
396 self.readJSONObjectStart()
397
398 def readStructEnd(self):
399 self.readJSONObjectEnd()
400
401 def readFieldBegin(self):
402 character = self.reader.peek()
Roger Meierad8154a2012-12-18 21:02:16 +0100403 ttype = 0
Roger Meier85fb6de2012-11-02 00:05:42 +0000404 id = 0
405 if character == RBRACE:
Roger Meierad8154a2012-12-18 21:02:16 +0100406 ttype = TType.STOP
Roger Meier85fb6de2012-11-02 00:05:42 +0000407 else:
408 id = self.readJSONInteger()
409 self.readJSONObjectStart()
Roger Meierad8154a2012-12-18 21:02:16 +0100410 ttype = JTYPES[self.readJSONString(False)]
411 return (None, ttype, id)
Roger Meier85fb6de2012-11-02 00:05:42 +0000412
413 def readFieldEnd(self):
414 self.readJSONObjectEnd()
415
416 def readMapBegin(self):
417 self.readJSONArrayStart()
418 keyType = JTYPES[self.readJSONString(False)]
419 valueType = JTYPES[self.readJSONString(False)]
420 size = self.readJSONInteger()
421 self.readJSONObjectStart()
422 return (keyType, valueType, size)
423
424 def readMapEnd(self):
425 self.readJSONObjectEnd()
426 self.readJSONArrayEnd()
427
428 def readCollectionBegin(self):
429 self.readJSONArrayStart()
430 elemType = JTYPES[self.readJSONString(False)]
431 size = self.readJSONInteger()
Roger Meierad8154a2012-12-18 21:02:16 +0100432 return (elemType, size)
Roger Meier85fb6de2012-11-02 00:05:42 +0000433 readListBegin = readCollectionBegin
434 readSetBegin = readCollectionBegin
435
436 def readCollectionEnd(self):
437 self.readJSONArrayEnd()
438 readSetEnd = readCollectionEnd
439 readListEnd = readCollectionEnd
440
441 def readBool(self):
442 return (False if self.readJSONInteger() == 0 else True)
443
444 def readNumber(self):
445 return self.readJSONInteger()
446 readByte = readNumber
447 readI16 = readNumber
448 readI32 = readNumber
449 readI64 = readNumber
450
451 def readDouble(self):
452 return self.readJSONDouble()
453
454 def readString(self):
455 return self.readJSONString(False)
456
457 def readBinary(self):
458 return self.readJSONBase64()
459
460 def writeMessageBegin(self, name, request_type, seqid):
461 self.resetWriteContext()
462 self.writeJSONArrayStart()
463 self.writeJSONNumber(VERSION)
464 self.writeJSONString(name)
465 self.writeJSONNumber(request_type)
466 self.writeJSONNumber(seqid)
467
468 def writeMessageEnd(self):
469 self.writeJSONArrayEnd()
470
471 def writeStructBegin(self, name):
472 self.writeJSONObjectStart()
473
474 def writeStructEnd(self):
475 self.writeJSONObjectEnd()
476
Roger Meierad8154a2012-12-18 21:02:16 +0100477 def writeFieldBegin(self, name, ttype, id):
Roger Meier85fb6de2012-11-02 00:05:42 +0000478 self.writeJSONNumber(id)
479 self.writeJSONObjectStart()
Roger Meierad8154a2012-12-18 21:02:16 +0100480 self.writeJSONString(CTYPES[ttype])
Roger Meier85fb6de2012-11-02 00:05:42 +0000481
482 def writeFieldEnd(self):
483 self.writeJSONObjectEnd()
484
485 def writeFieldStop(self):
486 pass
487
488 def writeMapBegin(self, ktype, vtype, size):
489 self.writeJSONArrayStart()
490 self.writeJSONString(CTYPES[ktype])
491 self.writeJSONString(CTYPES[vtype])
492 self.writeJSONNumber(size)
493 self.writeJSONObjectStart()
494
495 def writeMapEnd(self):
496 self.writeJSONObjectEnd()
497 self.writeJSONArrayEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900498
Roger Meier85fb6de2012-11-02 00:05:42 +0000499 def writeListBegin(self, etype, size):
500 self.writeJSONArrayStart()
501 self.writeJSONString(CTYPES[etype])
502 self.writeJSONNumber(size)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900503
Roger Meier85fb6de2012-11-02 00:05:42 +0000504 def writeListEnd(self):
505 self.writeJSONArrayEnd()
506
507 def writeSetBegin(self, etype, size):
508 self.writeJSONArrayStart()
509 self.writeJSONString(CTYPES[etype])
510 self.writeJSONNumber(size)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900511
Roger Meier85fb6de2012-11-02 00:05:42 +0000512 def writeSetEnd(self):
513 self.writeJSONArrayEnd()
514
515 def writeBool(self, boolean):
516 self.writeJSONNumber(1 if boolean is True else 0)
517
Konrad Grochowski93fea152014-10-02 16:29:14 +0200518 def writeByte(self, byte):
519 checkIntegerLimits(byte, 8)
520 self.writeJSONNumber(byte)
521
522 def writeI16(self, i16):
523 checkIntegerLimits(i16, 16)
524 self.writeJSONNumber(i16)
525
526 def writeI32(self, i32):
527 checkIntegerLimits(i32, 32)
528 self.writeJSONNumber(i32)
529
530 def writeI64(self, i64):
531 checkIntegerLimits(i64, 64)
532 self.writeJSONNumber(i64)
Roger Meier85fb6de2012-11-02 00:05:42 +0000533
534 def writeDouble(self, dbl):
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900535 # 17 significant digits should be just enough for any double precision value.
536 self.writeJSONNumber(dbl, '{:.17g}')
Roger Meier85fb6de2012-11-02 00:05:42 +0000537
538 def writeString(self, string):
539 self.writeJSONString(string)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900540
Roger Meier85fb6de2012-11-02 00:05:42 +0000541 def writeBinary(self, binary):
542 self.writeJSONBase64(binary)
543
Roger Meier0895dfe2012-12-26 22:09:55 +0100544
Roger Meier85fb6de2012-11-02 00:05:42 +0000545class TJSONProtocolFactory:
Roger Meier85fb6de2012-11-02 00:05:42 +0000546
547 def getProtocol(self, trans):
548 return TJSONProtocol(trans)
Roger Meier0895dfe2012-12-26 22:09:55 +0100549
550
551class TSimpleJSONProtocol(TJSONProtocolBase):
552 """Simple, readable, write-only JSON protocol.
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900553
Roger Meier0895dfe2012-12-26 22:09:55 +0100554 Useful for interacting with scripting languages.
555 """
556
557 def readMessageBegin(self):
558 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900559
Roger Meier0895dfe2012-12-26 22:09:55 +0100560 def readMessageEnd(self):
561 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900562
Roger Meier0895dfe2012-12-26 22:09:55 +0100563 def readStructBegin(self):
564 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900565
Roger Meier0895dfe2012-12-26 22:09:55 +0100566 def readStructEnd(self):
567 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900568
Roger Meier0895dfe2012-12-26 22:09:55 +0100569 def writeMessageBegin(self, name, request_type, seqid):
570 self.resetWriteContext()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900571
Roger Meier0895dfe2012-12-26 22:09:55 +0100572 def writeMessageEnd(self):
573 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900574
Roger Meier0895dfe2012-12-26 22:09:55 +0100575 def writeStructBegin(self, name):
576 self.writeJSONObjectStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900577
Roger Meier0895dfe2012-12-26 22:09:55 +0100578 def writeStructEnd(self):
579 self.writeJSONObjectEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900580
Roger Meier0895dfe2012-12-26 22:09:55 +0100581 def writeFieldBegin(self, name, ttype, fid):
582 self.writeJSONString(name)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900583
Roger Meier0895dfe2012-12-26 22:09:55 +0100584 def writeFieldEnd(self):
585 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900586
Roger Meier0895dfe2012-12-26 22:09:55 +0100587 def writeMapBegin(self, ktype, vtype, size):
588 self.writeJSONObjectStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900589
Roger Meier0895dfe2012-12-26 22:09:55 +0100590 def writeMapEnd(self):
591 self.writeJSONObjectEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900592
Roger Meier0895dfe2012-12-26 22:09:55 +0100593 def _writeCollectionBegin(self, etype, size):
594 self.writeJSONArrayStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900595
Roger Meier0895dfe2012-12-26 22:09:55 +0100596 def _writeCollectionEnd(self):
597 self.writeJSONArrayEnd()
598 writeListBegin = _writeCollectionBegin
599 writeListEnd = _writeCollectionEnd
600 writeSetBegin = _writeCollectionBegin
601 writeSetEnd = _writeCollectionEnd
602
Konrad Grochowski93fea152014-10-02 16:29:14 +0200603 def writeByte(self, byte):
604 checkIntegerLimits(byte, 8)
605 self.writeJSONNumber(byte)
606
607 def writeI16(self, i16):
608 checkIntegerLimits(i16, 16)
609 self.writeJSONNumber(i16)
610
611 def writeI32(self, i32):
612 checkIntegerLimits(i32, 32)
613 self.writeJSONNumber(i32)
614
615 def writeI64(self, i64):
616 checkIntegerLimits(i64, 64)
617 self.writeJSONNumber(i64)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900618
Roger Meier0895dfe2012-12-26 22:09:55 +0100619 def writeBool(self, boolean):
620 self.writeJSONNumber(1 if boolean is True else 0)
621
622 def writeDouble(self, dbl):
623 self.writeJSONNumber(dbl)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900624
Roger Meier0895dfe2012-12-26 22:09:55 +0100625 def writeString(self, string):
626 self.writeJSONString(string)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900627
Roger Meier0895dfe2012-12-26 22:09:55 +0100628 def writeBinary(self, binary):
629 self.writeJSONBase64(binary)
630
631
632class TSimpleJSONProtocolFactory(object):
633
634 def getProtocol(self, trans):
635 return TSimpleJSONProtocol(trans)