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 | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 20 | from .TProtocol import (TType, TProtocolBase, TProtocolException, |
James E. King III | e44f6a9 | 2019-02-07 17:11:21 -0500 | [diff] [blame^] | 21 | TProtocolFactory, checkIntegerLimits) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 22 | import base64 |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 23 | import math |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 24 | import sys |
| 25 | |
| 26 | from ..compat import str_to_binary |
| 27 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 28 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 29 | __all__ = ['TJSONProtocol', |
| 30 | 'TJSONProtocolFactory', |
| 31 | 'TSimpleJSONProtocol', |
| 32 | 'TSimpleJSONProtocolFactory'] |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 33 | |
| 34 | VERSION = 1 |
| 35 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 36 | COMMA = b',' |
| 37 | COLON = b':' |
| 38 | LBRACE = b'{' |
| 39 | RBRACE = b'}' |
| 40 | LBRACKET = b'[' |
| 41 | RBRACKET = b']' |
| 42 | QUOTE = b'"' |
| 43 | BACKSLASH = b'\\' |
| 44 | ZERO = b'0' |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 45 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 46 | ESCSEQ0 = ord('\\') |
| 47 | ESCSEQ1 = ord('u') |
| 48 | ESCAPE_CHAR_VALS = { |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 49 | '"': '\\"', |
| 50 | '\\': '\\\\', |
| 51 | '\b': '\\b', |
| 52 | '\f': '\\f', |
| 53 | '\n': '\\n', |
| 54 | '\r': '\\r', |
| 55 | '\t': '\\t', |
| 56 | # '/': '\\/', |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 57 | } |
| 58 | ESCAPE_CHARS = { |
| 59 | b'"': '"', |
| 60 | b'\\': '\\', |
| 61 | b'b': '\b', |
| 62 | b'f': '\f', |
| 63 | b'n': '\n', |
| 64 | b'r': '\r', |
| 65 | b't': '\t', |
| 66 | b'/': '/', |
| 67 | } |
| 68 | NUMERIC_CHAR = b'+-.0123456789Ee' |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | CTYPES = { |
| 71 | TType.BOOL: 'tf', |
| 72 | TType.BYTE: 'i8', |
| 73 | TType.I16: 'i16', |
| 74 | TType.I32: 'i32', |
| 75 | TType.I64: 'i64', |
| 76 | TType.DOUBLE: 'dbl', |
| 77 | TType.STRING: 'str', |
| 78 | TType.STRUCT: 'rec', |
| 79 | TType.LIST: 'lst', |
| 80 | TType.SET: 'set', |
| 81 | TType.MAP: 'map', |
| 82 | } |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 83 | |
| 84 | JTYPES = {} |
| 85 | for key in CTYPES.keys(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 86 | JTYPES[CTYPES[key]] = key |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 87 | |
| 88 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 89 | class JSONBaseContext(object): |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 90 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 91 | def __init__(self, protocol): |
| 92 | self.protocol = protocol |
| 93 | self.first = True |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 94 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 95 | def doIO(self, function): |
| 96 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 97 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 98 | def write(self): |
| 99 | pass |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 100 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 101 | def read(self): |
| 102 | pass |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 103 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 104 | def escapeNum(self): |
| 105 | return False |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 106 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 107 | def __str__(self): |
| 108 | return self.__class__.__name__ |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 109 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 110 | |
| 111 | class JSONListContext(JSONBaseContext): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 112 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 113 | def doIO(self, function): |
| 114 | if self.first is True: |
| 115 | self.first = False |
| 116 | else: |
| 117 | function(COMMA) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 118 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 119 | def write(self): |
| 120 | self.doIO(self.protocol.trans.write) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 121 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 122 | def read(self): |
| 123 | self.doIO(self.protocol.readJSONSyntaxChar) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | class JSONPairContext(JSONBaseContext): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 127 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 128 | def __init__(self, protocol): |
| 129 | super(JSONPairContext, self).__init__(protocol) |
| 130 | self.colon = True |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 131 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 132 | def doIO(self, function): |
| 133 | if self.first: |
| 134 | self.first = False |
| 135 | self.colon = True |
| 136 | else: |
| 137 | function(COLON if self.colon else COMMA) |
| 138 | self.colon = not self.colon |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 139 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 140 | def write(self): |
| 141 | self.doIO(self.protocol.trans.write) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 142 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 143 | def read(self): |
| 144 | self.doIO(self.protocol.readJSONSyntaxChar) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 145 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 146 | def escapeNum(self): |
| 147 | return self.colon |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 148 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | def __str__(self): |
| 150 | return '%s, colon=%s' % (self.__class__.__name__, self.colon) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 151 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 152 | |
| 153 | class LookaheadReader(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 154 | hasData = False |
| 155 | data = '' |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 156 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 157 | def __init__(self, protocol): |
| 158 | self.protocol = protocol |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 159 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 160 | def read(self): |
| 161 | if self.hasData is True: |
| 162 | self.hasData = False |
| 163 | else: |
| 164 | self.data = self.protocol.trans.read(1) |
| 165 | return self.data |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 166 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 167 | def peek(self): |
| 168 | if self.hasData is False: |
| 169 | self.data = self.protocol.trans.read(1) |
| 170 | self.hasData = True |
| 171 | return self.data |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 172 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 173 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 174 | class TJSONProtocolBase(TProtocolBase): |
| 175 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 176 | def __init__(self, trans): |
| 177 | TProtocolBase.__init__(self, trans) |
| 178 | self.resetWriteContext() |
| 179 | self.resetReadContext() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 180 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 181 | # We don't have length limit implementation for JSON protocols |
| 182 | @property |
| 183 | def string_length_limit(senf): |
| 184 | return None |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 185 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 186 | @property |
| 187 | def container_length_limit(senf): |
| 188 | return None |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 189 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 190 | def resetWriteContext(self): |
| 191 | self.context = JSONBaseContext(self) |
| 192 | self.contextStack = [self.context] |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 193 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 194 | def resetReadContext(self): |
| 195 | self.resetWriteContext() |
| 196 | self.reader = LookaheadReader(self) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 197 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 198 | def pushContext(self, ctx): |
| 199 | self.contextStack.append(ctx) |
| 200 | self.context = ctx |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 201 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 202 | def popContext(self): |
| 203 | self.contextStack.pop() |
| 204 | if self.contextStack: |
| 205 | self.context = self.contextStack[-1] |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 206 | else: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 207 | self.context = JSONBaseContext(self) |
| 208 | |
| 209 | def writeJSONString(self, string): |
| 210 | self.context.write() |
| 211 | json_str = ['"'] |
| 212 | for s in string: |
| 213 | escaped = ESCAPE_CHAR_VALS.get(s, s) |
| 214 | json_str.append(escaped) |
| 215 | json_str.append('"') |
| 216 | self.trans.write(str_to_binary(''.join(json_str))) |
| 217 | |
| 218 | def writeJSONNumber(self, number, formatter='{0}'): |
| 219 | self.context.write() |
| 220 | jsNumber = str(formatter.format(number)).encode('ascii') |
| 221 | if self.context.escapeNum(): |
| 222 | self.trans.write(QUOTE) |
| 223 | self.trans.write(jsNumber) |
| 224 | self.trans.write(QUOTE) |
| 225 | else: |
| 226 | self.trans.write(jsNumber) |
| 227 | |
| 228 | def writeJSONBase64(self, binary): |
| 229 | self.context.write() |
| 230 | self.trans.write(QUOTE) |
| 231 | self.trans.write(base64.b64encode(binary)) |
| 232 | self.trans.write(QUOTE) |
| 233 | |
| 234 | def writeJSONObjectStart(self): |
| 235 | self.context.write() |
| 236 | self.trans.write(LBRACE) |
| 237 | self.pushContext(JSONPairContext(self)) |
| 238 | |
| 239 | def writeJSONObjectEnd(self): |
| 240 | self.popContext() |
| 241 | self.trans.write(RBRACE) |
| 242 | |
| 243 | def writeJSONArrayStart(self): |
| 244 | self.context.write() |
| 245 | self.trans.write(LBRACKET) |
| 246 | self.pushContext(JSONListContext(self)) |
| 247 | |
| 248 | def writeJSONArrayEnd(self): |
| 249 | self.popContext() |
| 250 | self.trans.write(RBRACKET) |
| 251 | |
| 252 | def readJSONSyntaxChar(self, character): |
| 253 | current = self.reader.read() |
| 254 | if character != current: |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 255 | raise TProtocolException(TProtocolException.INVALID_DATA, |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 256 | "Unexpected character: %s" % current) |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 257 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 258 | def _isHighSurrogate(self, codeunit): |
| 259 | return codeunit >= 0xd800 and codeunit <= 0xdbff |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 260 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 261 | def _isLowSurrogate(self, codeunit): |
| 262 | return codeunit >= 0xdc00 and codeunit <= 0xdfff |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 263 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 264 | def _toChar(self, high, low=None): |
| 265 | if not low: |
| 266 | if sys.version_info[0] == 2: |
| 267 | return ("\\u%04x" % high).decode('unicode-escape') \ |
| 268 | .encode('utf-8') |
| 269 | else: |
| 270 | return chr(high) |
| 271 | else: |
| 272 | codepoint = (1 << 16) + ((high & 0x3ff) << 10) |
| 273 | codepoint += low & 0x3ff |
| 274 | if sys.version_info[0] == 2: |
| 275 | s = "\\U%08x" % codepoint |
| 276 | return s.decode('unicode-escape').encode('utf-8') |
| 277 | else: |
| 278 | return chr(codepoint) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 279 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 280 | def readJSONString(self, skipContext): |
| 281 | highSurrogate = None |
| 282 | string = [] |
| 283 | if skipContext is False: |
| 284 | self.context.read() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 285 | self.readJSONSyntaxChar(QUOTE) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 286 | while True: |
| 287 | character = self.reader.read() |
| 288 | if character == QUOTE: |
| 289 | break |
| 290 | if ord(character) == ESCSEQ0: |
| 291 | character = self.reader.read() |
| 292 | if ord(character) == ESCSEQ1: |
| 293 | character = self.trans.read(4).decode('ascii') |
| 294 | codeunit = int(character, 16) |
| 295 | if self._isHighSurrogate(codeunit): |
| 296 | if highSurrogate: |
| 297 | raise TProtocolException( |
| 298 | TProtocolException.INVALID_DATA, |
| 299 | "Expected low surrogate char") |
| 300 | highSurrogate = codeunit |
| 301 | continue |
| 302 | elif self._isLowSurrogate(codeunit): |
| 303 | if not highSurrogate: |
| 304 | raise TProtocolException( |
| 305 | TProtocolException.INVALID_DATA, |
| 306 | "Expected high surrogate char") |
| 307 | character = self._toChar(highSurrogate, codeunit) |
| 308 | highSurrogate = None |
| 309 | else: |
| 310 | character = self._toChar(codeunit) |
| 311 | else: |
| 312 | if character not in ESCAPE_CHARS: |
| 313 | raise TProtocolException( |
| 314 | TProtocolException.INVALID_DATA, |
| 315 | "Expected control char") |
| 316 | character = ESCAPE_CHARS[character] |
| 317 | elif character in ESCAPE_CHAR_VALS: |
| 318 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 319 | "Unescaped control char") |
| 320 | elif sys.version_info[0] > 2: |
| 321 | utf8_bytes = bytearray([ord(character)]) |
| 322 | while ord(self.reader.peek()) >= 0x80: |
| 323 | utf8_bytes.append(ord(self.reader.read())) |
| 324 | character = utf8_bytes.decode('utf8') |
| 325 | string.append(character) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 326 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 327 | if highSurrogate: |
| 328 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 329 | "Expected low surrogate char") |
| 330 | return ''.join(string) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 331 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 332 | def isJSONNumeric(self, character): |
| 333 | return (True if NUMERIC_CHAR.find(character) != - 1 else False) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 334 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 335 | def readJSONQuotes(self): |
| 336 | if (self.context.escapeNum()): |
| 337 | self.readJSONSyntaxChar(QUOTE) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 338 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 339 | def readJSONNumericChars(self): |
| 340 | numeric = [] |
| 341 | while True: |
| 342 | character = self.reader.peek() |
| 343 | if self.isJSONNumeric(character) is False: |
| 344 | break |
| 345 | numeric.append(self.reader.read()) |
| 346 | return b''.join(numeric).decode('ascii') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 347 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 348 | def readJSONInteger(self): |
| 349 | self.context.read() |
| 350 | self.readJSONQuotes() |
| 351 | numeric = self.readJSONNumericChars() |
| 352 | self.readJSONQuotes() |
| 353 | try: |
| 354 | return int(numeric) |
| 355 | except ValueError: |
| 356 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 357 | "Bad data encounted in numeric data") |
| 358 | |
| 359 | def readJSONDouble(self): |
| 360 | self.context.read() |
| 361 | if self.reader.peek() == QUOTE: |
| 362 | string = self.readJSONString(True) |
| 363 | try: |
| 364 | double = float(string) |
| 365 | if (self.context.escapeNum is False and |
| 366 | not math.isinf(double) and |
| 367 | not math.isnan(double)): |
| 368 | raise TProtocolException( |
| 369 | TProtocolException.INVALID_DATA, |
| 370 | "Numeric data unexpectedly quoted") |
| 371 | return double |
| 372 | except ValueError: |
| 373 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 374 | "Bad data encounted in numeric data") |
| 375 | else: |
| 376 | if self.context.escapeNum() is True: |
| 377 | self.readJSONSyntaxChar(QUOTE) |
| 378 | try: |
| 379 | return float(self.readJSONNumericChars()) |
| 380 | except ValueError: |
| 381 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 382 | "Bad data encounted in numeric data") |
| 383 | |
| 384 | def readJSONBase64(self): |
| 385 | string = self.readJSONString(False) |
| 386 | size = len(string) |
| 387 | m = size % 4 |
| 388 | # Force padding since b64encode method does not allow it |
| 389 | if m != 0: |
| 390 | for i in range(4 - m): |
| 391 | string += '=' |
| 392 | return base64.b64decode(string) |
| 393 | |
| 394 | def readJSONObjectStart(self): |
| 395 | self.context.read() |
| 396 | self.readJSONSyntaxChar(LBRACE) |
| 397 | self.pushContext(JSONPairContext(self)) |
| 398 | |
| 399 | def readJSONObjectEnd(self): |
| 400 | self.readJSONSyntaxChar(RBRACE) |
| 401 | self.popContext() |
| 402 | |
| 403 | def readJSONArrayStart(self): |
| 404 | self.context.read() |
| 405 | self.readJSONSyntaxChar(LBRACKET) |
| 406 | self.pushContext(JSONListContext(self)) |
| 407 | |
| 408 | def readJSONArrayEnd(self): |
| 409 | self.readJSONSyntaxChar(RBRACKET) |
| 410 | self.popContext() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 411 | |
| 412 | |
| 413 | class TJSONProtocol(TJSONProtocolBase): |
| 414 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 415 | def readMessageBegin(self): |
| 416 | self.resetReadContext() |
| 417 | self.readJSONArrayStart() |
| 418 | if self.readJSONInteger() != VERSION: |
| 419 | raise TProtocolException(TProtocolException.BAD_VERSION, |
| 420 | "Message contained bad version.") |
| 421 | name = self.readJSONString(False) |
| 422 | typen = self.readJSONInteger() |
| 423 | seqid = self.readJSONInteger() |
| 424 | return (name, typen, seqid) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 425 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 426 | def readMessageEnd(self): |
| 427 | self.readJSONArrayEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 428 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 429 | def readStructBegin(self): |
| 430 | self.readJSONObjectStart() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 431 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 432 | def readStructEnd(self): |
| 433 | self.readJSONObjectEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 434 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 435 | def readFieldBegin(self): |
| 436 | character = self.reader.peek() |
| 437 | ttype = 0 |
| 438 | id = 0 |
| 439 | if character == RBRACE: |
| 440 | ttype = TType.STOP |
| 441 | else: |
| 442 | id = self.readJSONInteger() |
| 443 | self.readJSONObjectStart() |
| 444 | ttype = JTYPES[self.readJSONString(False)] |
| 445 | return (None, ttype, id) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 446 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 447 | def readFieldEnd(self): |
| 448 | self.readJSONObjectEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 449 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 450 | def readMapBegin(self): |
| 451 | self.readJSONArrayStart() |
| 452 | keyType = JTYPES[self.readJSONString(False)] |
| 453 | valueType = JTYPES[self.readJSONString(False)] |
| 454 | size = self.readJSONInteger() |
| 455 | self.readJSONObjectStart() |
| 456 | return (keyType, valueType, size) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 457 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 458 | def readMapEnd(self): |
| 459 | self.readJSONObjectEnd() |
| 460 | self.readJSONArrayEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 461 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 462 | def readCollectionBegin(self): |
| 463 | self.readJSONArrayStart() |
| 464 | elemType = JTYPES[self.readJSONString(False)] |
| 465 | size = self.readJSONInteger() |
| 466 | return (elemType, size) |
| 467 | readListBegin = readCollectionBegin |
| 468 | readSetBegin = readCollectionBegin |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 469 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 470 | def readCollectionEnd(self): |
| 471 | self.readJSONArrayEnd() |
| 472 | readSetEnd = readCollectionEnd |
| 473 | readListEnd = readCollectionEnd |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 474 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 475 | def readBool(self): |
| 476 | return (False if self.readJSONInteger() == 0 else True) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 477 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 478 | def readNumber(self): |
| 479 | return self.readJSONInteger() |
| 480 | readByte = readNumber |
| 481 | readI16 = readNumber |
| 482 | readI32 = readNumber |
| 483 | readI64 = readNumber |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 484 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 485 | def readDouble(self): |
| 486 | return self.readJSONDouble() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 487 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 488 | def readString(self): |
| 489 | return self.readJSONString(False) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 490 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 491 | def readBinary(self): |
| 492 | return self.readJSONBase64() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 493 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 494 | def writeMessageBegin(self, name, request_type, seqid): |
| 495 | self.resetWriteContext() |
| 496 | self.writeJSONArrayStart() |
| 497 | self.writeJSONNumber(VERSION) |
| 498 | self.writeJSONString(name) |
| 499 | self.writeJSONNumber(request_type) |
| 500 | self.writeJSONNumber(seqid) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 501 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 502 | def writeMessageEnd(self): |
| 503 | self.writeJSONArrayEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 504 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 505 | def writeStructBegin(self, name): |
| 506 | self.writeJSONObjectStart() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 507 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 508 | def writeStructEnd(self): |
| 509 | self.writeJSONObjectEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 510 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 511 | def writeFieldBegin(self, name, ttype, id): |
| 512 | self.writeJSONNumber(id) |
| 513 | self.writeJSONObjectStart() |
| 514 | self.writeJSONString(CTYPES[ttype]) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 515 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 516 | def writeFieldEnd(self): |
| 517 | self.writeJSONObjectEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 518 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 519 | def writeFieldStop(self): |
| 520 | pass |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 521 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 522 | def writeMapBegin(self, ktype, vtype, size): |
| 523 | self.writeJSONArrayStart() |
| 524 | self.writeJSONString(CTYPES[ktype]) |
| 525 | self.writeJSONString(CTYPES[vtype]) |
| 526 | self.writeJSONNumber(size) |
| 527 | self.writeJSONObjectStart() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 528 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 529 | def writeMapEnd(self): |
| 530 | self.writeJSONObjectEnd() |
| 531 | self.writeJSONArrayEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 532 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 533 | def writeListBegin(self, etype, size): |
| 534 | self.writeJSONArrayStart() |
| 535 | self.writeJSONString(CTYPES[etype]) |
| 536 | self.writeJSONNumber(size) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 537 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 538 | def writeListEnd(self): |
| 539 | self.writeJSONArrayEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 540 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 541 | def writeSetBegin(self, etype, size): |
| 542 | self.writeJSONArrayStart() |
| 543 | self.writeJSONString(CTYPES[etype]) |
| 544 | self.writeJSONNumber(size) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 545 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 546 | def writeSetEnd(self): |
| 547 | self.writeJSONArrayEnd() |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 548 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 549 | def writeBool(self, boolean): |
| 550 | self.writeJSONNumber(1 if boolean is True else 0) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 551 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 552 | def writeByte(self, byte): |
| 553 | checkIntegerLimits(byte, 8) |
| 554 | self.writeJSONNumber(byte) |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 555 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 556 | def writeI16(self, i16): |
| 557 | checkIntegerLimits(i16, 16) |
| 558 | self.writeJSONNumber(i16) |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 559 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 560 | def writeI32(self, i32): |
| 561 | checkIntegerLimits(i32, 32) |
| 562 | self.writeJSONNumber(i32) |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 563 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 564 | def writeI64(self, i64): |
| 565 | checkIntegerLimits(i64, 64) |
| 566 | self.writeJSONNumber(i64) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 567 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 568 | def writeDouble(self, dbl): |
| 569 | # 17 significant digits should be just enough for any double precision |
| 570 | # value. |
| 571 | self.writeJSONNumber(dbl, '{0:.17g}') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 572 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 573 | def writeString(self, string): |
| 574 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 575 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 576 | def writeBinary(self, binary): |
| 577 | self.writeJSONBase64(binary) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 578 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 579 | |
James E. King III | e44f6a9 | 2019-02-07 17:11:21 -0500 | [diff] [blame^] | 580 | class TJSONProtocolFactory(TProtocolFactory): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 581 | def getProtocol(self, trans): |
| 582 | return TJSONProtocol(trans) |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 583 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 584 | @property |
| 585 | def string_length_limit(senf): |
| 586 | return None |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 587 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 588 | @property |
| 589 | def container_length_limit(senf): |
| 590 | return None |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 591 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 592 | |
| 593 | class TSimpleJSONProtocol(TJSONProtocolBase): |
| 594 | """Simple, readable, write-only JSON protocol. |
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 | Useful for interacting with scripting languages. |
| 597 | """ |
| 598 | |
| 599 | def readMessageBegin(self): |
| 600 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 601 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 602 | def readMessageEnd(self): |
| 603 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 604 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 605 | def readStructBegin(self): |
| 606 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 607 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 608 | def readStructEnd(self): |
| 609 | raise NotImplementedError() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 610 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 611 | def writeMessageBegin(self, name, request_type, seqid): |
| 612 | self.resetWriteContext() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 613 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 614 | def writeMessageEnd(self): |
| 615 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 616 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 617 | def writeStructBegin(self, name): |
| 618 | self.writeJSONObjectStart() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 619 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 620 | def writeStructEnd(self): |
| 621 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 622 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 623 | def writeFieldBegin(self, name, ttype, fid): |
| 624 | self.writeJSONString(name) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 625 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 626 | def writeFieldEnd(self): |
| 627 | pass |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 628 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 629 | def writeMapBegin(self, ktype, vtype, size): |
| 630 | self.writeJSONObjectStart() |
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 writeMapEnd(self): |
| 633 | self.writeJSONObjectEnd() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 634 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 635 | def _writeCollectionBegin(self, etype, size): |
| 636 | self.writeJSONArrayStart() |
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 _writeCollectionEnd(self): |
| 639 | self.writeJSONArrayEnd() |
| 640 | writeListBegin = _writeCollectionBegin |
| 641 | writeListEnd = _writeCollectionEnd |
| 642 | writeSetBegin = _writeCollectionBegin |
| 643 | writeSetEnd = _writeCollectionEnd |
| 644 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 645 | def writeByte(self, byte): |
| 646 | checkIntegerLimits(byte, 8) |
| 647 | self.writeJSONNumber(byte) |
| 648 | |
| 649 | def writeI16(self, i16): |
| 650 | checkIntegerLimits(i16, 16) |
| 651 | self.writeJSONNumber(i16) |
| 652 | |
| 653 | def writeI32(self, i32): |
| 654 | checkIntegerLimits(i32, 32) |
| 655 | self.writeJSONNumber(i32) |
| 656 | |
| 657 | def writeI64(self, i64): |
| 658 | checkIntegerLimits(i64, 64) |
| 659 | self.writeJSONNumber(i64) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 660 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 661 | def writeBool(self, boolean): |
| 662 | self.writeJSONNumber(1 if boolean is True else 0) |
| 663 | |
| 664 | def writeDouble(self, dbl): |
| 665 | self.writeJSONNumber(dbl) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 666 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 667 | def writeString(self, string): |
| 668 | self.writeJSONString(string) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 669 | |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 670 | def writeBinary(self, binary): |
| 671 | self.writeJSONBase64(binary) |
| 672 | |
| 673 | |
James E. King III | e44f6a9 | 2019-02-07 17:11:21 -0500 | [diff] [blame^] | 674 | class TSimpleJSONProtocolFactory(TProtocolFactory): |
Roger Meier | 0895dfe | 2012-12-26 22:09:55 +0100 | [diff] [blame] | 675 | |
| 676 | def getProtocol(self, trans): |
| 677 | return TSimpleJSONProtocol(trans) |