Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 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 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 20 | from .TProtocol import TType, TProtocolBase, TProtocolException, checkIntegerLimits |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 21 | import base64 |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 22 | import math |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 23 | import sys |
| 24 | |
| 25 | from ..compat import str_to_binary |
| 26 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 27 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 28 | __all__ = ['TJSONProtocol', |
| 29 | 'TJSONProtocolFactory', |
| 30 | 'TSimpleJSONProtocol', |
| 31 | 'TSimpleJSONProtocolFactory'] |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 32 | |
| 33 | VERSION = 1 |
| 34 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 35 | COMMA = b',' |
| 36 | COLON = b':' |
| 37 | LBRACE = b'{' |
| 38 | RBRACE = b'}' |
| 39 | LBRACKET = b'[' |
| 40 | RBRACKET = b']' |
| 41 | QUOTE = b'"' |
| 42 | BACKSLASH = b'\\' |
| 43 | ZERO = b'0' |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 44 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 45 | ESCSEQ0 = ord('\\') |
| 46 | ESCSEQ1 = ord('u') |
| 47 | ESCAPE_CHAR_VALS = { |
| 48 | '"': '\\"', |
| 49 | '\\': '\\\\', |
| 50 | '\b': '\\b', |
| 51 | '\f': '\\f', |
| 52 | '\n': '\\n', |
| 53 | '\r': '\\r', |
| 54 | '\t': '\\t', |
| 55 | # '/': '\\/', |
| 56 | } |
| 57 | ESCAPE_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 | } |
| 67 | NUMERIC_CHAR = b'+-.0123456789Ee' |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 68 | |
| 69 | CTYPES = {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 Meier | 05ab89a | 2012-11-02 10:36:59 +0000 | [diff] [blame] | 76 | TType.STRUCT: 'rec', |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 77 | TType.LIST: 'lst', |
| 78 | TType.SET: 'set', |
| 79 | TType.MAP: 'map'} |
| 80 | |
| 81 | JTYPES = {} |
| 82 | for key in CTYPES.keys(): |
| 83 | JTYPES[CTYPES[key]] = key |
| 84 | |
| 85 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 86 | class JSONBaseContext(object): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 87 | |
| 88 | def __init__(self, protocol): |
| 89 | self.protocol = protocol |
| 90 | self.first = True |
| 91 | |
| 92 | def doIO(self, function): |
| 93 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 94 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 95 | def write(self): |
| 96 | pass |
| 97 | |
| 98 | def read(self): |
| 99 | pass |
| 100 | |
| 101 | def escapeNum(self): |
| 102 | return False |
| 103 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 104 | def __str__(self): |
| 105 | return self.__class__.__name__ |
| 106 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 107 | |
| 108 | class JSONListContext(JSONBaseContext): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 109 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 110 | 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 | |
| 123 | class JSONPairContext(JSONBaseContext): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 124 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 125 | def __init__(self, protocol): |
| 126 | super(JSONPairContext, self).__init__(protocol) |
| 127 | self.colon = True |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 128 | |
| 129 | def doIO(self, function): |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 130 | if self.first: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 131 | self.first = False |
| 132 | self.colon = True |
| 133 | else: |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 134 | function(COLON if self.colon else COMMA) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 135 | 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 Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 146 | def __str__(self): |
| 147 | return '%s, colon=%s' % (self.__class__.__name__, self.colon) |
| 148 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 149 | |
| 150 | class 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 170 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 171 | class TJSONProtocolBase(TProtocolBase): |
| 172 | |
| 173 | def __init__(self, trans): |
| 174 | TProtocolBase.__init__(self, trans) |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 175 | self.resetWriteContext() |
| 176 | self.resetReadContext() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 177 | |
| 178 | def resetWriteContext(self): |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 179 | self.context = JSONBaseContext(self) |
| 180 | self.contextStack = [self.context] |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 181 | |
| 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 Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 192 | if self.contextStack: |
| 193 | self.context = self.contextStack[-1] |
| 194 | else: |
| 195 | self.context = JSONBaseContext(self) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 196 | |
| 197 | def writeJSONString(self, string): |
| 198 | self.context.write() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 199 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 205 | |
Nobuaki Sukegawa | 228b328 | 2015-10-10 03:11:49 +0900 | [diff] [blame] | 206 | def writeJSONNumber(self, number, formatter='{}'): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 207 | self.context.write() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 208 | jsNumber = str(formatter.format(number)).encode('ascii') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 209 | if self.context.escapeNum(): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 210 | self.trans.write(QUOTE) |
| 211 | self.trans.write(jsNumber) |
| 212 | self.trans.write(QUOTE) |
| 213 | else: |
| 214 | self.trans.write(jsNumber) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 215 | |
| 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 Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 246 | 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 Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame^] | 252 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 267 | def readJSONString(self, skipContext): |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 268 | highSurrogate = None |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 269 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 277 | if ord(character) == ESCSEQ0: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 278 | character = self.reader.read() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 279 | if ord(character) == ESCSEQ1: |
Phongphan Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame^] | 280 | 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 Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 294 | else: |
Phongphan Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame^] | 295 | character = self._toChar(codeunit) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 296 | else: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 297 | if character not in ESCAPE_CHARS: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 298 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 299 | "Expected control char") |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 300 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 309 | string.append(character) |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 310 | |
| 311 | if highSurrogate: |
| 312 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 313 | "Expected low surrogate char") |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 314 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 330 | return b''.join(numeric).decode('ascii') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 331 | |
| 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 346 | string = self.readJSONString(True) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 347 | try: |
| 348 | double = float(string) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 349 | if (self.context.escapeNum is False and |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 350 | not math.isinf(double) and |
| 351 | not math.isnan(double)): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 352 | 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 363 | except ValueError: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 364 | 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 | |
| 390 | class 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 414 | ttype = 0 |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 415 | id = 0 |
| 416 | if character == RBRACE: |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 417 | ttype = TType.STOP |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 418 | else: |
| 419 | id = self.readJSONInteger() |
| 420 | self.readJSONObjectStart() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 421 | ttype = JTYPES[self.readJSONString(False)] |
| 422 | return (None, ttype, id) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 423 | |
| 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 443 | return (elemType, size) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 444 | 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 488 | def writeFieldBegin(self, name, ttype, id): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 489 | self.writeJSONNumber(id) |
| 490 | self.writeJSONObjectStart() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 491 | self.writeJSONString(CTYPES[ttype]) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 492 | |
| 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 509 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 510 | def writeListBegin(self, etype, size): |
| 511 | self.writeJSONArrayStart() |
| 512 | self.writeJSONString(CTYPES[etype]) |
| 513 | self.writeJSONNumber(size) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 514 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 515 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 522 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 523 | def writeSetEnd(self): |
| 524 | self.writeJSONArrayEnd() |
| 525 | |
| 526 | def writeBool(self, boolean): |
| 527 | self.writeJSONNumber(1 if boolean is True else 0) |
| 528 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 529 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 544 | |
| 545 | def writeDouble(self, dbl): |
Nobuaki Sukegawa | 228b328 | 2015-10-10 03:11:49 +0900 | [diff] [blame] | 546 | # 17 significant digits should be just enough for any double precision value. |
| 547 | self.writeJSONNumber(dbl, '{:.17g}') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 548 | |
| 549 | def writeString(self, string): |
| 550 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 551 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 552 | def writeBinary(self, binary): |
| 553 | self.writeJSONBase64(binary) |
| 554 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 555 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 556 | class TJSONProtocolFactory: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 557 | |
| 558 | def getProtocol(self, trans): |
| 559 | return TJSONProtocol(trans) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 560 | |
| 561 | |
| 562 | class TSimpleJSONProtocol(TJSONProtocolBase): |
| 563 | """Simple, readable, write-only JSON protocol. |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 564 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 565 | Useful for interacting with scripting languages. |
| 566 | """ |
| 567 | |
| 568 | def readMessageBegin(self): |
| 569 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 570 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 571 | def readMessageEnd(self): |
| 572 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 573 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 574 | def readStructBegin(self): |
| 575 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 576 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 577 | def readStructEnd(self): |
| 578 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 579 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 580 | def writeMessageBegin(self, name, request_type, seqid): |
| 581 | self.resetWriteContext() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 582 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 583 | def writeMessageEnd(self): |
| 584 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 585 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 586 | def writeStructBegin(self, name): |
| 587 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 588 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 589 | def writeStructEnd(self): |
| 590 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 591 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 592 | def writeFieldBegin(self, name, ttype, fid): |
| 593 | self.writeJSONString(name) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 594 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 595 | def writeFieldEnd(self): |
| 596 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 597 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 598 | def writeMapBegin(self, ktype, vtype, size): |
| 599 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 600 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 601 | def writeMapEnd(self): |
| 602 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 603 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 604 | def _writeCollectionBegin(self, etype, size): |
| 605 | self.writeJSONArrayStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 606 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 607 | def _writeCollectionEnd(self): |
| 608 | self.writeJSONArrayEnd() |
| 609 | writeListBegin = _writeCollectionBegin |
| 610 | writeListEnd = _writeCollectionEnd |
| 611 | writeSetBegin = _writeCollectionBegin |
| 612 | writeSetEnd = _writeCollectionEnd |
| 613 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 614 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 629 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 630 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 635 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 636 | def writeString(self, string): |
| 637 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 638 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 639 | def writeBinary(self, binary): |
| 640 | self.writeJSONBase64(binary) |
| 641 | |
| 642 | |
| 643 | class TSimpleJSONProtocolFactory(object): |
| 644 | |
| 645 | def getProtocol(self, trans): |
| 646 | return TSimpleJSONProtocol(trans) |