blob: d210bff1b18438db162e051ad6a1dd652e34550d [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
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700252 def _toChar(self, high, low=None):
253 if not low:
254 if sys.version_info[0] == 2:
255 return ("\\u%04x" % high).decode('unicode-escape').encode('utf-8')
256 else:
257 return chr(high)
258 else:
259 codepoint = (1 << 16) + ((high & 0x3ff) << 10)
260 codepoint += low & 0x3ff
261 if sys.version_info[0] == 2:
262 s = "\\U%08x" % codepoint
263 return s.decode('unicode-escape').encode('utf-8')
264 else:
265 return chr(codepoint)
266
Roger Meier85fb6de2012-11-02 00:05:42 +0000267 def readJSONString(self, skipContext):
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700268 highSurrogate = None
Roger Meier85fb6de2012-11-02 00:05:42 +0000269 string = []
270 if skipContext is False:
271 self.context.read()
272 self.readJSONSyntaxChar(QUOTE)
273 while True:
274 character = self.reader.read()
275 if character == QUOTE:
276 break
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900277 if ord(character) == ESCSEQ0:
Roger Meier85fb6de2012-11-02 00:05:42 +0000278 character = self.reader.read()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900279 if ord(character) == ESCSEQ1:
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700280 character = self.trans.read(4).decode('ascii')
281 codeunit = int(character, 16)
282 if self._isHighSurrogate(codeunit):
283 if highSurrogate:
284 raise TProtocolException(TProtocolException.INVALID_DATA,
285 "Expected low surrogate char")
286 highSurrogate = codeunit
287 continue
288 elif self._isLowSurrogate(codeunit):
289 if not highSurrogate:
290 raise TProtocolException(TProtocolException.INVALID_DATA,
291 "Expected high surrogate char")
292 character = self._toChar(highSurrogate, codeunit)
293 highSurrogate = None
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700294 else:
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700295 character = self._toChar(codeunit)
Roger Meier85fb6de2012-11-02 00:05:42 +0000296 else:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900297 if character not in ESCAPE_CHARS:
Roger Meier85fb6de2012-11-02 00:05:42 +0000298 raise TProtocolException(TProtocolException.INVALID_DATA,
299 "Expected control char")
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900300 character = ESCAPE_CHARS[character]
301 elif character in ESCAPE_CHAR_VALS:
302 raise TProtocolException(TProtocolException.INVALID_DATA,
303 "Unescaped control char")
304 elif sys.version_info[0] > 2:
305 utf8_bytes = bytearray([ord(character)])
306 while ord(self.reader.peek()) >= 0x80:
307 utf8_bytes.append(ord(self.reader.read()))
308 character = utf8_bytes.decode('utf8')
Roger Meier85fb6de2012-11-02 00:05:42 +0000309 string.append(character)
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700310
311 if highSurrogate:
312 raise TProtocolException(TProtocolException.INVALID_DATA,
313 "Expected low surrogate char")
Roger Meier85fb6de2012-11-02 00:05:42 +0000314 return ''.join(string)
315
316 def isJSONNumeric(self, character):
317 return (True if NUMERIC_CHAR.find(character) != - 1 else False)
318
319 def readJSONQuotes(self):
320 if (self.context.escapeNum()):
321 self.readJSONSyntaxChar(QUOTE)
322
323 def readJSONNumericChars(self):
324 numeric = []
325 while True:
326 character = self.reader.peek()
327 if self.isJSONNumeric(character) is False:
328 break
329 numeric.append(self.reader.read())
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900330 return b''.join(numeric).decode('ascii')
Roger Meier85fb6de2012-11-02 00:05:42 +0000331
332 def readJSONInteger(self):
333 self.context.read()
334 self.readJSONQuotes()
335 numeric = self.readJSONNumericChars()
336 self.readJSONQuotes()
337 try:
338 return int(numeric)
339 except ValueError:
340 raise TProtocolException(TProtocolException.INVALID_DATA,
341 "Bad data encounted in numeric data")
342
343 def readJSONDouble(self):
344 self.context.read()
345 if self.reader.peek() == QUOTE:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900346 string = self.readJSONString(True)
Roger Meier85fb6de2012-11-02 00:05:42 +0000347 try:
348 double = float(string)
Roger Meier0895dfe2012-12-26 22:09:55 +0100349 if (self.context.escapeNum is False and
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900350 not math.isinf(double) and
351 not math.isnan(double)):
Roger Meier85fb6de2012-11-02 00:05:42 +0000352 raise TProtocolException(TProtocolException.INVALID_DATA,
353 "Numeric data unexpectedly quoted")
354 return double
355 except ValueError:
356 raise TProtocolException(TProtocolException.INVALID_DATA,
357 "Bad data encounted in numeric data")
358 else:
359 if self.context.escapeNum() is True:
360 self.readJSONSyntaxChar(QUOTE)
361 try:
362 return float(self.readJSONNumericChars())
Roger Meierad8154a2012-12-18 21:02:16 +0100363 except ValueError:
Roger Meier85fb6de2012-11-02 00:05:42 +0000364 raise TProtocolException(TProtocolException.INVALID_DATA,
365 "Bad data encounted in numeric data")
366
367 def readJSONBase64(self):
368 string = self.readJSONString(False)
369 return base64.b64decode(string)
370
371 def readJSONObjectStart(self):
372 self.context.read()
373 self.readJSONSyntaxChar(LBRACE)
374 self.pushContext(JSONPairContext(self))
375
376 def readJSONObjectEnd(self):
377 self.readJSONSyntaxChar(RBRACE)
378 self.popContext()
379
380 def readJSONArrayStart(self):
381 self.context.read()
382 self.readJSONSyntaxChar(LBRACKET)
383 self.pushContext(JSONListContext(self))
384
385 def readJSONArrayEnd(self):
386 self.readJSONSyntaxChar(RBRACKET)
387 self.popContext()
388
389
390class TJSONProtocol(TJSONProtocolBase):
391
392 def readMessageBegin(self):
393 self.resetReadContext()
394 self.readJSONArrayStart()
395 if self.readJSONInteger() != VERSION:
396 raise TProtocolException(TProtocolException.BAD_VERSION,
397 "Message contained bad version.")
398 name = self.readJSONString(False)
399 typen = self.readJSONInteger()
400 seqid = self.readJSONInteger()
401 return (name, typen, seqid)
402
403 def readMessageEnd(self):
404 self.readJSONArrayEnd()
405
406 def readStructBegin(self):
407 self.readJSONObjectStart()
408
409 def readStructEnd(self):
410 self.readJSONObjectEnd()
411
412 def readFieldBegin(self):
413 character = self.reader.peek()
Roger Meierad8154a2012-12-18 21:02:16 +0100414 ttype = 0
Roger Meier85fb6de2012-11-02 00:05:42 +0000415 id = 0
416 if character == RBRACE:
Roger Meierad8154a2012-12-18 21:02:16 +0100417 ttype = TType.STOP
Roger Meier85fb6de2012-11-02 00:05:42 +0000418 else:
419 id = self.readJSONInteger()
420 self.readJSONObjectStart()
Roger Meierad8154a2012-12-18 21:02:16 +0100421 ttype = JTYPES[self.readJSONString(False)]
422 return (None, ttype, id)
Roger Meier85fb6de2012-11-02 00:05:42 +0000423
424 def readFieldEnd(self):
425 self.readJSONObjectEnd()
426
427 def readMapBegin(self):
428 self.readJSONArrayStart()
429 keyType = JTYPES[self.readJSONString(False)]
430 valueType = JTYPES[self.readJSONString(False)]
431 size = self.readJSONInteger()
432 self.readJSONObjectStart()
433 return (keyType, valueType, size)
434
435 def readMapEnd(self):
436 self.readJSONObjectEnd()
437 self.readJSONArrayEnd()
438
439 def readCollectionBegin(self):
440 self.readJSONArrayStart()
441 elemType = JTYPES[self.readJSONString(False)]
442 size = self.readJSONInteger()
Roger Meierad8154a2012-12-18 21:02:16 +0100443 return (elemType, size)
Roger Meier85fb6de2012-11-02 00:05:42 +0000444 readListBegin = readCollectionBegin
445 readSetBegin = readCollectionBegin
446
447 def readCollectionEnd(self):
448 self.readJSONArrayEnd()
449 readSetEnd = readCollectionEnd
450 readListEnd = readCollectionEnd
451
452 def readBool(self):
453 return (False if self.readJSONInteger() == 0 else True)
454
455 def readNumber(self):
456 return self.readJSONInteger()
457 readByte = readNumber
458 readI16 = readNumber
459 readI32 = readNumber
460 readI64 = readNumber
461
462 def readDouble(self):
463 return self.readJSONDouble()
464
465 def readString(self):
466 return self.readJSONString(False)
467
468 def readBinary(self):
469 return self.readJSONBase64()
470
471 def writeMessageBegin(self, name, request_type, seqid):
472 self.resetWriteContext()
473 self.writeJSONArrayStart()
474 self.writeJSONNumber(VERSION)
475 self.writeJSONString(name)
476 self.writeJSONNumber(request_type)
477 self.writeJSONNumber(seqid)
478
479 def writeMessageEnd(self):
480 self.writeJSONArrayEnd()
481
482 def writeStructBegin(self, name):
483 self.writeJSONObjectStart()
484
485 def writeStructEnd(self):
486 self.writeJSONObjectEnd()
487
Roger Meierad8154a2012-12-18 21:02:16 +0100488 def writeFieldBegin(self, name, ttype, id):
Roger Meier85fb6de2012-11-02 00:05:42 +0000489 self.writeJSONNumber(id)
490 self.writeJSONObjectStart()
Roger Meierad8154a2012-12-18 21:02:16 +0100491 self.writeJSONString(CTYPES[ttype])
Roger Meier85fb6de2012-11-02 00:05:42 +0000492
493 def writeFieldEnd(self):
494 self.writeJSONObjectEnd()
495
496 def writeFieldStop(self):
497 pass
498
499 def writeMapBegin(self, ktype, vtype, size):
500 self.writeJSONArrayStart()
501 self.writeJSONString(CTYPES[ktype])
502 self.writeJSONString(CTYPES[vtype])
503 self.writeJSONNumber(size)
504 self.writeJSONObjectStart()
505
506 def writeMapEnd(self):
507 self.writeJSONObjectEnd()
508 self.writeJSONArrayEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900509
Roger Meier85fb6de2012-11-02 00:05:42 +0000510 def writeListBegin(self, etype, size):
511 self.writeJSONArrayStart()
512 self.writeJSONString(CTYPES[etype])
513 self.writeJSONNumber(size)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900514
Roger Meier85fb6de2012-11-02 00:05:42 +0000515 def writeListEnd(self):
516 self.writeJSONArrayEnd()
517
518 def writeSetBegin(self, etype, size):
519 self.writeJSONArrayStart()
520 self.writeJSONString(CTYPES[etype])
521 self.writeJSONNumber(size)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900522
Roger Meier85fb6de2012-11-02 00:05:42 +0000523 def writeSetEnd(self):
524 self.writeJSONArrayEnd()
525
526 def writeBool(self, boolean):
527 self.writeJSONNumber(1 if boolean is True else 0)
528
Konrad Grochowski93fea152014-10-02 16:29:14 +0200529 def writeByte(self, byte):
530 checkIntegerLimits(byte, 8)
531 self.writeJSONNumber(byte)
532
533 def writeI16(self, i16):
534 checkIntegerLimits(i16, 16)
535 self.writeJSONNumber(i16)
536
537 def writeI32(self, i32):
538 checkIntegerLimits(i32, 32)
539 self.writeJSONNumber(i32)
540
541 def writeI64(self, i64):
542 checkIntegerLimits(i64, 64)
543 self.writeJSONNumber(i64)
Roger Meier85fb6de2012-11-02 00:05:42 +0000544
545 def writeDouble(self, dbl):
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900546 # 17 significant digits should be just enough for any double precision value.
547 self.writeJSONNumber(dbl, '{:.17g}')
Roger Meier85fb6de2012-11-02 00:05:42 +0000548
549 def writeString(self, string):
550 self.writeJSONString(string)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900551
Roger Meier85fb6de2012-11-02 00:05:42 +0000552 def writeBinary(self, binary):
553 self.writeJSONBase64(binary)
554
Roger Meier0895dfe2012-12-26 22:09:55 +0100555
Roger Meier85fb6de2012-11-02 00:05:42 +0000556class TJSONProtocolFactory:
Roger Meier85fb6de2012-11-02 00:05:42 +0000557
558 def getProtocol(self, trans):
559 return TJSONProtocol(trans)
Roger Meier0895dfe2012-12-26 22:09:55 +0100560
561
562class TSimpleJSONProtocol(TJSONProtocolBase):
563 """Simple, readable, write-only JSON protocol.
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900564
Roger Meier0895dfe2012-12-26 22:09:55 +0100565 Useful for interacting with scripting languages.
566 """
567
568 def readMessageBegin(self):
569 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900570
Roger Meier0895dfe2012-12-26 22:09:55 +0100571 def readMessageEnd(self):
572 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900573
Roger Meier0895dfe2012-12-26 22:09:55 +0100574 def readStructBegin(self):
575 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900576
Roger Meier0895dfe2012-12-26 22:09:55 +0100577 def readStructEnd(self):
578 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900579
Roger Meier0895dfe2012-12-26 22:09:55 +0100580 def writeMessageBegin(self, name, request_type, seqid):
581 self.resetWriteContext()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900582
Roger Meier0895dfe2012-12-26 22:09:55 +0100583 def writeMessageEnd(self):
584 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900585
Roger Meier0895dfe2012-12-26 22:09:55 +0100586 def writeStructBegin(self, name):
587 self.writeJSONObjectStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900588
Roger Meier0895dfe2012-12-26 22:09:55 +0100589 def writeStructEnd(self):
590 self.writeJSONObjectEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900591
Roger Meier0895dfe2012-12-26 22:09:55 +0100592 def writeFieldBegin(self, name, ttype, fid):
593 self.writeJSONString(name)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900594
Roger Meier0895dfe2012-12-26 22:09:55 +0100595 def writeFieldEnd(self):
596 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900597
Roger Meier0895dfe2012-12-26 22:09:55 +0100598 def writeMapBegin(self, ktype, vtype, size):
599 self.writeJSONObjectStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900600
Roger Meier0895dfe2012-12-26 22:09:55 +0100601 def writeMapEnd(self):
602 self.writeJSONObjectEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900603
Roger Meier0895dfe2012-12-26 22:09:55 +0100604 def _writeCollectionBegin(self, etype, size):
605 self.writeJSONArrayStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900606
Roger Meier0895dfe2012-12-26 22:09:55 +0100607 def _writeCollectionEnd(self):
608 self.writeJSONArrayEnd()
609 writeListBegin = _writeCollectionBegin
610 writeListEnd = _writeCollectionEnd
611 writeSetBegin = _writeCollectionBegin
612 writeSetEnd = _writeCollectionEnd
613
Konrad Grochowski93fea152014-10-02 16:29:14 +0200614 def writeByte(self, byte):
615 checkIntegerLimits(byte, 8)
616 self.writeJSONNumber(byte)
617
618 def writeI16(self, i16):
619 checkIntegerLimits(i16, 16)
620 self.writeJSONNumber(i16)
621
622 def writeI32(self, i32):
623 checkIntegerLimits(i32, 32)
624 self.writeJSONNumber(i32)
625
626 def writeI64(self, i64):
627 checkIntegerLimits(i64, 64)
628 self.writeJSONNumber(i64)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900629
Roger Meier0895dfe2012-12-26 22:09:55 +0100630 def writeBool(self, boolean):
631 self.writeJSONNumber(1 if boolean is True else 0)
632
633 def writeDouble(self, dbl):
634 self.writeJSONNumber(dbl)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900635
Roger Meier0895dfe2012-12-26 22:09:55 +0100636 def writeString(self, string):
637 self.writeJSONString(string)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900638
Roger Meier0895dfe2012-12-26 22:09:55 +0100639 def writeBinary(self, binary):
640 self.writeJSONBase64(binary)
641
642
643class TSimpleJSONProtocolFactory(object):
644
645 def getProtocol(self, trans):
646 return TSimpleJSONProtocol(trans)