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: |
Nobuaki Sukegawa | 7f404fd | 2015-11-14 17:05:42 +0900 | [diff] [blame] | 254 | codepoint = high |
Phongphan Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame] | 255 | else: |
| 256 | codepoint = (1 << 16) + ((high & 0x3ff) << 10) |
| 257 | codepoint += low & 0x3ff |
Nobuaki Sukegawa | 7f404fd | 2015-11-14 17:05:42 +0900 | [diff] [blame] | 258 | if sys.version_info[0] == 2: |
| 259 | return unichr(codepoint).encode('utf-8') |
| 260 | else: |
| 261 | return chr(codepoint) |
Phongphan Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame] | 262 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 263 | def readJSONString(self, skipContext): |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 264 | highSurrogate = None |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 265 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 273 | if ord(character) == ESCSEQ0: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 274 | character = self.reader.read() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 275 | if ord(character) == ESCSEQ1: |
Phongphan Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame] | 276 | 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 Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 290 | else: |
Phongphan Phuttha | 369d62e | 2015-11-09 02:05:09 +0700 | [diff] [blame] | 291 | character = self._toChar(codeunit) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 292 | else: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 293 | if character not in ESCAPE_CHARS: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 294 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 295 | "Expected control char") |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 296 | 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 Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 305 | string.append(character) |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 306 | |
| 307 | if highSurrogate: |
| 308 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 309 | "Expected low surrogate char") |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 310 | 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 326 | return b''.join(numeric).decode('ascii') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 327 | |
| 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 Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 342 | string = self.readJSONString(True) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 343 | try: |
| 344 | double = float(string) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 345 | if (self.context.escapeNum is False and |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 346 | not math.isinf(double) and |
| 347 | not math.isnan(double)): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 348 | 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 Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 359 | except ValueError: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 360 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 361 | "Bad data encounted in numeric data") |
| 362 | |
| 363 | def readJSONBase64(self): |
| 364 | string = self.readJSONString(False) |
Nobuaki Sukegawa | bc7e480 | 2015-11-14 19:23:27 +0900 | [diff] [blame] | 365 | size = len(string) |
| 366 | m = size % 4 |
| 367 | # Force padding since b64encode method does not allow it |
| 368 | if m != 0: |
| 369 | for i in range(4 - m): |
| 370 | string += '=' |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 371 | return base64.b64decode(string) |
| 372 | |
| 373 | def readJSONObjectStart(self): |
| 374 | self.context.read() |
| 375 | self.readJSONSyntaxChar(LBRACE) |
| 376 | self.pushContext(JSONPairContext(self)) |
| 377 | |
| 378 | def readJSONObjectEnd(self): |
| 379 | self.readJSONSyntaxChar(RBRACE) |
| 380 | self.popContext() |
| 381 | |
| 382 | def readJSONArrayStart(self): |
| 383 | self.context.read() |
| 384 | self.readJSONSyntaxChar(LBRACKET) |
| 385 | self.pushContext(JSONListContext(self)) |
| 386 | |
| 387 | def readJSONArrayEnd(self): |
| 388 | self.readJSONSyntaxChar(RBRACKET) |
| 389 | self.popContext() |
| 390 | |
| 391 | |
| 392 | class TJSONProtocol(TJSONProtocolBase): |
| 393 | |
| 394 | def readMessageBegin(self): |
| 395 | self.resetReadContext() |
| 396 | self.readJSONArrayStart() |
| 397 | if self.readJSONInteger() != VERSION: |
| 398 | raise TProtocolException(TProtocolException.BAD_VERSION, |
| 399 | "Message contained bad version.") |
| 400 | name = self.readJSONString(False) |
| 401 | typen = self.readJSONInteger() |
| 402 | seqid = self.readJSONInteger() |
| 403 | return (name, typen, seqid) |
| 404 | |
| 405 | def readMessageEnd(self): |
| 406 | self.readJSONArrayEnd() |
| 407 | |
| 408 | def readStructBegin(self): |
| 409 | self.readJSONObjectStart() |
| 410 | |
| 411 | def readStructEnd(self): |
| 412 | self.readJSONObjectEnd() |
| 413 | |
| 414 | def readFieldBegin(self): |
| 415 | character = self.reader.peek() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 416 | ttype = 0 |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 417 | id = 0 |
| 418 | if character == RBRACE: |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 419 | ttype = TType.STOP |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 420 | else: |
| 421 | id = self.readJSONInteger() |
| 422 | self.readJSONObjectStart() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 423 | ttype = JTYPES[self.readJSONString(False)] |
| 424 | return (None, ttype, id) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 425 | |
| 426 | def readFieldEnd(self): |
| 427 | self.readJSONObjectEnd() |
| 428 | |
| 429 | def readMapBegin(self): |
| 430 | self.readJSONArrayStart() |
| 431 | keyType = JTYPES[self.readJSONString(False)] |
| 432 | valueType = JTYPES[self.readJSONString(False)] |
| 433 | size = self.readJSONInteger() |
| 434 | self.readJSONObjectStart() |
| 435 | return (keyType, valueType, size) |
| 436 | |
| 437 | def readMapEnd(self): |
| 438 | self.readJSONObjectEnd() |
| 439 | self.readJSONArrayEnd() |
| 440 | |
| 441 | def readCollectionBegin(self): |
| 442 | self.readJSONArrayStart() |
| 443 | elemType = JTYPES[self.readJSONString(False)] |
| 444 | size = self.readJSONInteger() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 445 | return (elemType, size) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 446 | readListBegin = readCollectionBegin |
| 447 | readSetBegin = readCollectionBegin |
| 448 | |
| 449 | def readCollectionEnd(self): |
| 450 | self.readJSONArrayEnd() |
| 451 | readSetEnd = readCollectionEnd |
| 452 | readListEnd = readCollectionEnd |
| 453 | |
| 454 | def readBool(self): |
| 455 | return (False if self.readJSONInteger() == 0 else True) |
| 456 | |
| 457 | def readNumber(self): |
| 458 | return self.readJSONInteger() |
| 459 | readByte = readNumber |
| 460 | readI16 = readNumber |
| 461 | readI32 = readNumber |
| 462 | readI64 = readNumber |
| 463 | |
| 464 | def readDouble(self): |
| 465 | return self.readJSONDouble() |
| 466 | |
| 467 | def readString(self): |
| 468 | return self.readJSONString(False) |
| 469 | |
| 470 | def readBinary(self): |
| 471 | return self.readJSONBase64() |
| 472 | |
| 473 | def writeMessageBegin(self, name, request_type, seqid): |
| 474 | self.resetWriteContext() |
| 475 | self.writeJSONArrayStart() |
| 476 | self.writeJSONNumber(VERSION) |
| 477 | self.writeJSONString(name) |
| 478 | self.writeJSONNumber(request_type) |
| 479 | self.writeJSONNumber(seqid) |
| 480 | |
| 481 | def writeMessageEnd(self): |
| 482 | self.writeJSONArrayEnd() |
| 483 | |
| 484 | def writeStructBegin(self, name): |
| 485 | self.writeJSONObjectStart() |
| 486 | |
| 487 | def writeStructEnd(self): |
| 488 | self.writeJSONObjectEnd() |
| 489 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 490 | def writeFieldBegin(self, name, ttype, id): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 491 | self.writeJSONNumber(id) |
| 492 | self.writeJSONObjectStart() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 493 | self.writeJSONString(CTYPES[ttype]) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 494 | |
| 495 | def writeFieldEnd(self): |
| 496 | self.writeJSONObjectEnd() |
| 497 | |
| 498 | def writeFieldStop(self): |
| 499 | pass |
| 500 | |
| 501 | def writeMapBegin(self, ktype, vtype, size): |
| 502 | self.writeJSONArrayStart() |
| 503 | self.writeJSONString(CTYPES[ktype]) |
| 504 | self.writeJSONString(CTYPES[vtype]) |
| 505 | self.writeJSONNumber(size) |
| 506 | self.writeJSONObjectStart() |
| 507 | |
| 508 | def writeMapEnd(self): |
| 509 | self.writeJSONObjectEnd() |
| 510 | self.writeJSONArrayEnd() |
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 writeListBegin(self, etype, size): |
| 513 | self.writeJSONArrayStart() |
| 514 | self.writeJSONString(CTYPES[etype]) |
| 515 | self.writeJSONNumber(size) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 516 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 517 | def writeListEnd(self): |
| 518 | self.writeJSONArrayEnd() |
| 519 | |
| 520 | def writeSetBegin(self, etype, size): |
| 521 | self.writeJSONArrayStart() |
| 522 | self.writeJSONString(CTYPES[etype]) |
| 523 | self.writeJSONNumber(size) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 524 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 525 | def writeSetEnd(self): |
| 526 | self.writeJSONArrayEnd() |
| 527 | |
| 528 | def writeBool(self, boolean): |
| 529 | self.writeJSONNumber(1 if boolean is True else 0) |
| 530 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 531 | def writeByte(self, byte): |
| 532 | checkIntegerLimits(byte, 8) |
| 533 | self.writeJSONNumber(byte) |
| 534 | |
| 535 | def writeI16(self, i16): |
| 536 | checkIntegerLimits(i16, 16) |
| 537 | self.writeJSONNumber(i16) |
| 538 | |
| 539 | def writeI32(self, i32): |
| 540 | checkIntegerLimits(i32, 32) |
| 541 | self.writeJSONNumber(i32) |
| 542 | |
| 543 | def writeI64(self, i64): |
| 544 | checkIntegerLimits(i64, 64) |
| 545 | self.writeJSONNumber(i64) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 546 | |
| 547 | def writeDouble(self, dbl): |
Nobuaki Sukegawa | 228b328 | 2015-10-10 03:11:49 +0900 | [diff] [blame] | 548 | # 17 significant digits should be just enough for any double precision value. |
| 549 | self.writeJSONNumber(dbl, '{:.17g}') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 550 | |
| 551 | def writeString(self, string): |
| 552 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 553 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 554 | def writeBinary(self, binary): |
| 555 | self.writeJSONBase64(binary) |
| 556 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 557 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 558 | class TJSONProtocolFactory: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 559 | |
| 560 | def getProtocol(self, trans): |
| 561 | return TJSONProtocol(trans) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 562 | |
| 563 | |
| 564 | class TSimpleJSONProtocol(TJSONProtocolBase): |
| 565 | """Simple, readable, write-only JSON protocol. |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 566 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 567 | Useful for interacting with scripting languages. |
| 568 | """ |
| 569 | |
| 570 | def readMessageBegin(self): |
| 571 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 572 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 573 | def readMessageEnd(self): |
| 574 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 575 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 576 | def readStructBegin(self): |
| 577 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 578 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 579 | def readStructEnd(self): |
| 580 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 581 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 582 | def writeMessageBegin(self, name, request_type, seqid): |
| 583 | self.resetWriteContext() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 584 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 585 | def writeMessageEnd(self): |
| 586 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 587 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 588 | def writeStructBegin(self, name): |
| 589 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 590 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 591 | def writeStructEnd(self): |
| 592 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 593 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 594 | def writeFieldBegin(self, name, ttype, fid): |
| 595 | self.writeJSONString(name) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 596 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 597 | def writeFieldEnd(self): |
| 598 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 599 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 600 | def writeMapBegin(self, ktype, vtype, size): |
| 601 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 602 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 603 | def writeMapEnd(self): |
| 604 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 605 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 606 | def _writeCollectionBegin(self, etype, size): |
| 607 | self.writeJSONArrayStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 608 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 609 | def _writeCollectionEnd(self): |
| 610 | self.writeJSONArrayEnd() |
| 611 | writeListBegin = _writeCollectionBegin |
| 612 | writeListEnd = _writeCollectionEnd |
| 613 | writeSetBegin = _writeCollectionBegin |
| 614 | writeSetEnd = _writeCollectionEnd |
| 615 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 616 | def writeByte(self, byte): |
| 617 | checkIntegerLimits(byte, 8) |
| 618 | self.writeJSONNumber(byte) |
| 619 | |
| 620 | def writeI16(self, i16): |
| 621 | checkIntegerLimits(i16, 16) |
| 622 | self.writeJSONNumber(i16) |
| 623 | |
| 624 | def writeI32(self, i32): |
| 625 | checkIntegerLimits(i32, 32) |
| 626 | self.writeJSONNumber(i32) |
| 627 | |
| 628 | def writeI64(self, i64): |
| 629 | checkIntegerLimits(i64, 64) |
| 630 | self.writeJSONNumber(i64) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 631 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 632 | def writeBool(self, boolean): |
| 633 | self.writeJSONNumber(1 if boolean is True else 0) |
| 634 | |
| 635 | def writeDouble(self, dbl): |
| 636 | self.writeJSONNumber(dbl) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 637 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 638 | def writeString(self, string): |
| 639 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 640 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 641 | def writeBinary(self, binary): |
| 642 | self.writeJSONBase64(binary) |
| 643 | |
| 644 | |
| 645 | class TSimpleJSONProtocolFactory(object): |
| 646 | |
| 647 | def getProtocol(self, trans): |
| 648 | return TSimpleJSONProtocol(trans) |