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 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 252 | def readJSONString(self, skipContext): |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 253 | highSurrogate = None |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 254 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 262 | if ord(character) == ESCSEQ0: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 263 | character = self.reader.read() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 264 | if ord(character) == ESCSEQ1: |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 265 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 285 | else: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 286 | if character not in ESCAPE_CHARS: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 287 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 288 | "Expected control char") |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 289 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 298 | string.append(character) |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 299 | |
| 300 | if highSurrogate: |
| 301 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 302 | "Expected low surrogate char") |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 303 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 319 | return b''.join(numeric).decode('ascii') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 320 | |
| 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 335 | string = self.readJSONString(True) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 336 | try: |
| 337 | double = float(string) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 338 | if (self.context.escapeNum is False and |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 339 | not math.isinf(double) and |
| 340 | not math.isnan(double)): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 341 | 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 352 | except ValueError: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 353 | 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 | |
| 379 | class 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 403 | ttype = 0 |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 404 | id = 0 |
| 405 | if character == RBRACE: |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 406 | ttype = TType.STOP |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 407 | else: |
| 408 | id = self.readJSONInteger() |
| 409 | self.readJSONObjectStart() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 410 | ttype = JTYPES[self.readJSONString(False)] |
| 411 | return (None, ttype, id) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 412 | |
| 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 432 | return (elemType, size) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 433 | 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 477 | def writeFieldBegin(self, name, ttype, id): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 478 | self.writeJSONNumber(id) |
| 479 | self.writeJSONObjectStart() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 480 | self.writeJSONString(CTYPES[ttype]) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 481 | |
| 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 498 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 499 | def writeListBegin(self, etype, size): |
| 500 | self.writeJSONArrayStart() |
| 501 | self.writeJSONString(CTYPES[etype]) |
| 502 | self.writeJSONNumber(size) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 503 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 504 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 511 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 512 | def writeSetEnd(self): |
| 513 | self.writeJSONArrayEnd() |
| 514 | |
| 515 | def writeBool(self, boolean): |
| 516 | self.writeJSONNumber(1 if boolean is True else 0) |
| 517 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 518 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 533 | |
| 534 | def writeDouble(self, dbl): |
Nobuaki Sukegawa | 228b328 | 2015-10-10 03:11:49 +0900 | [diff] [blame] | 535 | # 17 significant digits should be just enough for any double precision value. |
| 536 | self.writeJSONNumber(dbl, '{:.17g}') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 537 | |
| 538 | def writeString(self, string): |
| 539 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 540 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 541 | def writeBinary(self, binary): |
| 542 | self.writeJSONBase64(binary) |
| 543 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 544 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 545 | class TJSONProtocolFactory: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 546 | |
| 547 | def getProtocol(self, trans): |
| 548 | return TJSONProtocol(trans) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 549 | |
| 550 | |
| 551 | class TSimpleJSONProtocol(TJSONProtocolBase): |
| 552 | """Simple, readable, write-only JSON protocol. |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 553 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 554 | Useful for interacting with scripting languages. |
| 555 | """ |
| 556 | |
| 557 | def readMessageBegin(self): |
| 558 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 559 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 560 | def readMessageEnd(self): |
| 561 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 562 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 563 | def readStructBegin(self): |
| 564 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 565 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 566 | def readStructEnd(self): |
| 567 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 568 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 569 | def writeMessageBegin(self, name, request_type, seqid): |
| 570 | self.resetWriteContext() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 571 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 572 | def writeMessageEnd(self): |
| 573 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 574 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 575 | def writeStructBegin(self, name): |
| 576 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 577 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 578 | def writeStructEnd(self): |
| 579 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 580 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 581 | def writeFieldBegin(self, name, ttype, fid): |
| 582 | self.writeJSONString(name) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 583 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 584 | def writeFieldEnd(self): |
| 585 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 586 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 587 | def writeMapBegin(self, ktype, vtype, size): |
| 588 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 589 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 590 | def writeMapEnd(self): |
| 591 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 592 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 593 | def _writeCollectionBegin(self, etype, size): |
| 594 | self.writeJSONArrayStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 595 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 596 | def _writeCollectionEnd(self): |
| 597 | self.writeJSONArrayEnd() |
| 598 | writeListBegin = _writeCollectionBegin |
| 599 | writeListEnd = _writeCollectionEnd |
| 600 | writeSetBegin = _writeCollectionBegin |
| 601 | writeSetEnd = _writeCollectionEnd |
| 602 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 603 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 618 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 619 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 624 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 625 | def writeString(self, string): |
| 626 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 627 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 628 | def writeBinary(self, binary): |
| 629 | self.writeJSONBase64(binary) |
| 630 | |
| 631 | |
| 632 | class TSimpleJSONProtocolFactory(object): |
| 633 | |
| 634 | def getProtocol(self, trans): |
| 635 | return TSimpleJSONProtocol(trans) |