blob: aada3fca72e0a9205d8af39504996fe016840eed [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:
Nobuaki Sukegawa7f404fd2015-11-14 17:05:42 +0900254 codepoint = high
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700255 else:
256 codepoint = (1 << 16) + ((high & 0x3ff) << 10)
257 codepoint += low & 0x3ff
Nobuaki Sukegawa7f404fd2015-11-14 17:05:42 +0900258 if sys.version_info[0] == 2:
259 return unichr(codepoint).encode('utf-8')
260 else:
261 return chr(codepoint)
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700262
Roger Meier85fb6de2012-11-02 00:05:42 +0000263 def readJSONString(self, skipContext):
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700264 highSurrogate = None
Roger Meier85fb6de2012-11-02 00:05:42 +0000265 string = []
266 if skipContext is False:
267 self.context.read()
268 self.readJSONSyntaxChar(QUOTE)
269 while True:
270 character = self.reader.read()
271 if character == QUOTE:
272 break
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900273 if ord(character) == ESCSEQ0:
Roger Meier85fb6de2012-11-02 00:05:42 +0000274 character = self.reader.read()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900275 if ord(character) == ESCSEQ1:
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700276 character = self.trans.read(4).decode('ascii')
277 codeunit = int(character, 16)
278 if self._isHighSurrogate(codeunit):
279 if highSurrogate:
280 raise TProtocolException(TProtocolException.INVALID_DATA,
281 "Expected low surrogate char")
282 highSurrogate = codeunit
283 continue
284 elif self._isLowSurrogate(codeunit):
285 if not highSurrogate:
286 raise TProtocolException(TProtocolException.INVALID_DATA,
287 "Expected high surrogate char")
288 character = self._toChar(highSurrogate, codeunit)
289 highSurrogate = None
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700290 else:
Phongphan Phuttha369d62e2015-11-09 02:05:09 +0700291 character = self._toChar(codeunit)
Roger Meier85fb6de2012-11-02 00:05:42 +0000292 else:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900293 if character not in ESCAPE_CHARS:
Roger Meier85fb6de2012-11-02 00:05:42 +0000294 raise TProtocolException(TProtocolException.INVALID_DATA,
295 "Expected control char")
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900296 character = ESCAPE_CHARS[character]
297 elif character in ESCAPE_CHAR_VALS:
298 raise TProtocolException(TProtocolException.INVALID_DATA,
299 "Unescaped control char")
300 elif sys.version_info[0] > 2:
301 utf8_bytes = bytearray([ord(character)])
302 while ord(self.reader.peek()) >= 0x80:
303 utf8_bytes.append(ord(self.reader.read()))
304 character = utf8_bytes.decode('utf8')
Roger Meier85fb6de2012-11-02 00:05:42 +0000305 string.append(character)
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700306
307 if highSurrogate:
308 raise TProtocolException(TProtocolException.INVALID_DATA,
309 "Expected low surrogate char")
Roger Meier85fb6de2012-11-02 00:05:42 +0000310 return ''.join(string)
311
312 def isJSONNumeric(self, character):
313 return (True if NUMERIC_CHAR.find(character) != - 1 else False)
314
315 def readJSONQuotes(self):
316 if (self.context.escapeNum()):
317 self.readJSONSyntaxChar(QUOTE)
318
319 def readJSONNumericChars(self):
320 numeric = []
321 while True:
322 character = self.reader.peek()
323 if self.isJSONNumeric(character) is False:
324 break
325 numeric.append(self.reader.read())
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900326 return b''.join(numeric).decode('ascii')
Roger Meier85fb6de2012-11-02 00:05:42 +0000327
328 def readJSONInteger(self):
329 self.context.read()
330 self.readJSONQuotes()
331 numeric = self.readJSONNumericChars()
332 self.readJSONQuotes()
333 try:
334 return int(numeric)
335 except ValueError:
336 raise TProtocolException(TProtocolException.INVALID_DATA,
337 "Bad data encounted in numeric data")
338
339 def readJSONDouble(self):
340 self.context.read()
341 if self.reader.peek() == QUOTE:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900342 string = self.readJSONString(True)
Roger Meier85fb6de2012-11-02 00:05:42 +0000343 try:
344 double = float(string)
Roger Meier0895dfe2012-12-26 22:09:55 +0100345 if (self.context.escapeNum is False and
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900346 not math.isinf(double) and
347 not math.isnan(double)):
Roger Meier85fb6de2012-11-02 00:05:42 +0000348 raise TProtocolException(TProtocolException.INVALID_DATA,
349 "Numeric data unexpectedly quoted")
350 return double
351 except ValueError:
352 raise TProtocolException(TProtocolException.INVALID_DATA,
353 "Bad data encounted in numeric data")
354 else:
355 if self.context.escapeNum() is True:
356 self.readJSONSyntaxChar(QUOTE)
357 try:
358 return float(self.readJSONNumericChars())
Roger Meierad8154a2012-12-18 21:02:16 +0100359 except ValueError:
Roger Meier85fb6de2012-11-02 00:05:42 +0000360 raise TProtocolException(TProtocolException.INVALID_DATA,
361 "Bad data encounted in numeric data")
362
363 def readJSONBase64(self):
364 string = self.readJSONString(False)
365 return base64.b64decode(string)
366
367 def readJSONObjectStart(self):
368 self.context.read()
369 self.readJSONSyntaxChar(LBRACE)
370 self.pushContext(JSONPairContext(self))
371
372 def readJSONObjectEnd(self):
373 self.readJSONSyntaxChar(RBRACE)
374 self.popContext()
375
376 def readJSONArrayStart(self):
377 self.context.read()
378 self.readJSONSyntaxChar(LBRACKET)
379 self.pushContext(JSONListContext(self))
380
381 def readJSONArrayEnd(self):
382 self.readJSONSyntaxChar(RBRACKET)
383 self.popContext()
384
385
386class TJSONProtocol(TJSONProtocolBase):
387
388 def readMessageBegin(self):
389 self.resetReadContext()
390 self.readJSONArrayStart()
391 if self.readJSONInteger() != VERSION:
392 raise TProtocolException(TProtocolException.BAD_VERSION,
393 "Message contained bad version.")
394 name = self.readJSONString(False)
395 typen = self.readJSONInteger()
396 seqid = self.readJSONInteger()
397 return (name, typen, seqid)
398
399 def readMessageEnd(self):
400 self.readJSONArrayEnd()
401
402 def readStructBegin(self):
403 self.readJSONObjectStart()
404
405 def readStructEnd(self):
406 self.readJSONObjectEnd()
407
408 def readFieldBegin(self):
409 character = self.reader.peek()
Roger Meierad8154a2012-12-18 21:02:16 +0100410 ttype = 0
Roger Meier85fb6de2012-11-02 00:05:42 +0000411 id = 0
412 if character == RBRACE:
Roger Meierad8154a2012-12-18 21:02:16 +0100413 ttype = TType.STOP
Roger Meier85fb6de2012-11-02 00:05:42 +0000414 else:
415 id = self.readJSONInteger()
416 self.readJSONObjectStart()
Roger Meierad8154a2012-12-18 21:02:16 +0100417 ttype = JTYPES[self.readJSONString(False)]
418 return (None, ttype, id)
Roger Meier85fb6de2012-11-02 00:05:42 +0000419
420 def readFieldEnd(self):
421 self.readJSONObjectEnd()
422
423 def readMapBegin(self):
424 self.readJSONArrayStart()
425 keyType = JTYPES[self.readJSONString(False)]
426 valueType = JTYPES[self.readJSONString(False)]
427 size = self.readJSONInteger()
428 self.readJSONObjectStart()
429 return (keyType, valueType, size)
430
431 def readMapEnd(self):
432 self.readJSONObjectEnd()
433 self.readJSONArrayEnd()
434
435 def readCollectionBegin(self):
436 self.readJSONArrayStart()
437 elemType = JTYPES[self.readJSONString(False)]
438 size = self.readJSONInteger()
Roger Meierad8154a2012-12-18 21:02:16 +0100439 return (elemType, size)
Roger Meier85fb6de2012-11-02 00:05:42 +0000440 readListBegin = readCollectionBegin
441 readSetBegin = readCollectionBegin
442
443 def readCollectionEnd(self):
444 self.readJSONArrayEnd()
445 readSetEnd = readCollectionEnd
446 readListEnd = readCollectionEnd
447
448 def readBool(self):
449 return (False if self.readJSONInteger() == 0 else True)
450
451 def readNumber(self):
452 return self.readJSONInteger()
453 readByte = readNumber
454 readI16 = readNumber
455 readI32 = readNumber
456 readI64 = readNumber
457
458 def readDouble(self):
459 return self.readJSONDouble()
460
461 def readString(self):
462 return self.readJSONString(False)
463
464 def readBinary(self):
465 return self.readJSONBase64()
466
467 def writeMessageBegin(self, name, request_type, seqid):
468 self.resetWriteContext()
469 self.writeJSONArrayStart()
470 self.writeJSONNumber(VERSION)
471 self.writeJSONString(name)
472 self.writeJSONNumber(request_type)
473 self.writeJSONNumber(seqid)
474
475 def writeMessageEnd(self):
476 self.writeJSONArrayEnd()
477
478 def writeStructBegin(self, name):
479 self.writeJSONObjectStart()
480
481 def writeStructEnd(self):
482 self.writeJSONObjectEnd()
483
Roger Meierad8154a2012-12-18 21:02:16 +0100484 def writeFieldBegin(self, name, ttype, id):
Roger Meier85fb6de2012-11-02 00:05:42 +0000485 self.writeJSONNumber(id)
486 self.writeJSONObjectStart()
Roger Meierad8154a2012-12-18 21:02:16 +0100487 self.writeJSONString(CTYPES[ttype])
Roger Meier85fb6de2012-11-02 00:05:42 +0000488
489 def writeFieldEnd(self):
490 self.writeJSONObjectEnd()
491
492 def writeFieldStop(self):
493 pass
494
495 def writeMapBegin(self, ktype, vtype, size):
496 self.writeJSONArrayStart()
497 self.writeJSONString(CTYPES[ktype])
498 self.writeJSONString(CTYPES[vtype])
499 self.writeJSONNumber(size)
500 self.writeJSONObjectStart()
501
502 def writeMapEnd(self):
503 self.writeJSONObjectEnd()
504 self.writeJSONArrayEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900505
Roger Meier85fb6de2012-11-02 00:05:42 +0000506 def writeListBegin(self, etype, size):
507 self.writeJSONArrayStart()
508 self.writeJSONString(CTYPES[etype])
509 self.writeJSONNumber(size)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900510
Roger Meier85fb6de2012-11-02 00:05:42 +0000511 def writeListEnd(self):
512 self.writeJSONArrayEnd()
513
514 def writeSetBegin(self, etype, size):
515 self.writeJSONArrayStart()
516 self.writeJSONString(CTYPES[etype])
517 self.writeJSONNumber(size)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900518
Roger Meier85fb6de2012-11-02 00:05:42 +0000519 def writeSetEnd(self):
520 self.writeJSONArrayEnd()
521
522 def writeBool(self, boolean):
523 self.writeJSONNumber(1 if boolean is True else 0)
524
Konrad Grochowski93fea152014-10-02 16:29:14 +0200525 def writeByte(self, byte):
526 checkIntegerLimits(byte, 8)
527 self.writeJSONNumber(byte)
528
529 def writeI16(self, i16):
530 checkIntegerLimits(i16, 16)
531 self.writeJSONNumber(i16)
532
533 def writeI32(self, i32):
534 checkIntegerLimits(i32, 32)
535 self.writeJSONNumber(i32)
536
537 def writeI64(self, i64):
538 checkIntegerLimits(i64, 64)
539 self.writeJSONNumber(i64)
Roger Meier85fb6de2012-11-02 00:05:42 +0000540
541 def writeDouble(self, dbl):
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900542 # 17 significant digits should be just enough for any double precision value.
543 self.writeJSONNumber(dbl, '{:.17g}')
Roger Meier85fb6de2012-11-02 00:05:42 +0000544
545 def writeString(self, string):
546 self.writeJSONString(string)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900547
Roger Meier85fb6de2012-11-02 00:05:42 +0000548 def writeBinary(self, binary):
549 self.writeJSONBase64(binary)
550
Roger Meier0895dfe2012-12-26 22:09:55 +0100551
Roger Meier85fb6de2012-11-02 00:05:42 +0000552class TJSONProtocolFactory:
Roger Meier85fb6de2012-11-02 00:05:42 +0000553
554 def getProtocol(self, trans):
555 return TJSONProtocol(trans)
Roger Meier0895dfe2012-12-26 22:09:55 +0100556
557
558class TSimpleJSONProtocol(TJSONProtocolBase):
559 """Simple, readable, write-only JSON protocol.
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900560
Roger Meier0895dfe2012-12-26 22:09:55 +0100561 Useful for interacting with scripting languages.
562 """
563
564 def readMessageBegin(self):
565 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900566
Roger Meier0895dfe2012-12-26 22:09:55 +0100567 def readMessageEnd(self):
568 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900569
Roger Meier0895dfe2012-12-26 22:09:55 +0100570 def readStructBegin(self):
571 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900572
Roger Meier0895dfe2012-12-26 22:09:55 +0100573 def readStructEnd(self):
574 raise NotImplementedError()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900575
Roger Meier0895dfe2012-12-26 22:09:55 +0100576 def writeMessageBegin(self, name, request_type, seqid):
577 self.resetWriteContext()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900578
Roger Meier0895dfe2012-12-26 22:09:55 +0100579 def writeMessageEnd(self):
580 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900581
Roger Meier0895dfe2012-12-26 22:09:55 +0100582 def writeStructBegin(self, name):
583 self.writeJSONObjectStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900584
Roger Meier0895dfe2012-12-26 22:09:55 +0100585 def writeStructEnd(self):
586 self.writeJSONObjectEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900587
Roger Meier0895dfe2012-12-26 22:09:55 +0100588 def writeFieldBegin(self, name, ttype, fid):
589 self.writeJSONString(name)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900590
Roger Meier0895dfe2012-12-26 22:09:55 +0100591 def writeFieldEnd(self):
592 pass
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900593
Roger Meier0895dfe2012-12-26 22:09:55 +0100594 def writeMapBegin(self, ktype, vtype, size):
595 self.writeJSONObjectStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900596
Roger Meier0895dfe2012-12-26 22:09:55 +0100597 def writeMapEnd(self):
598 self.writeJSONObjectEnd()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900599
Roger Meier0895dfe2012-12-26 22:09:55 +0100600 def _writeCollectionBegin(self, etype, size):
601 self.writeJSONArrayStart()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900602
Roger Meier0895dfe2012-12-26 22:09:55 +0100603 def _writeCollectionEnd(self):
604 self.writeJSONArrayEnd()
605 writeListBegin = _writeCollectionBegin
606 writeListEnd = _writeCollectionEnd
607 writeSetBegin = _writeCollectionBegin
608 writeSetEnd = _writeCollectionEnd
609
Konrad Grochowski93fea152014-10-02 16:29:14 +0200610 def writeByte(self, byte):
611 checkIntegerLimits(byte, 8)
612 self.writeJSONNumber(byte)
613
614 def writeI16(self, i16):
615 checkIntegerLimits(i16, 16)
616 self.writeJSONNumber(i16)
617
618 def writeI32(self, i32):
619 checkIntegerLimits(i32, 32)
620 self.writeJSONNumber(i32)
621
622 def writeI64(self, i64):
623 checkIntegerLimits(i64, 64)
624 self.writeJSONNumber(i64)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900625
Roger Meier0895dfe2012-12-26 22:09:55 +0100626 def writeBool(self, boolean):
627 self.writeJSONNumber(1 if boolean is True else 0)
628
629 def writeDouble(self, dbl):
630 self.writeJSONNumber(dbl)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900631
Roger Meier0895dfe2012-12-26 22:09:55 +0100632 def writeString(self, string):
633 self.writeJSONString(string)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900634
Roger Meier0895dfe2012-12-26 22:09:55 +0100635 def writeBinary(self, binary):
636 self.writeJSONBase64(binary)
637
638
639class TSimpleJSONProtocolFactory(object):
640
641 def getProtocol(self, trans):
642 return TSimpleJSONProtocol(trans)