| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | * or more contributor license agreements. See the NOTICE file |
| 6 | * distributed with this work for additional information |
| 7 | * regarding copyright ownership. The ASF licenses this file |
| 8 | * to you under the Apache License, Version 2.0 (the |
| 9 | * "License"); you may not use this file except in compliance |
| 10 | * with the License. You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, |
| 15 | * software distributed under the License is distributed on an |
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | * KIND, either express or implied. See the License for the |
| 18 | * specific language governing permissions and limitations |
| 19 | * under the License. |
| 20 | * |
| 21 | * @package thrift.protocol |
| 22 | */ |
| 23 | |
| 24 | namespace Thrift\Protocol; |
| 25 | |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 26 | use Thrift\Exception\TException; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 27 | use Thrift\Type\TType; |
| 28 | use Thrift\Exception\TProtocolException; |
| 29 | use Thrift\Protocol\JSON\BaseContext; |
| 30 | use Thrift\Protocol\JSON\LookaheadReader; |
| 31 | use Thrift\Protocol\JSON\PairContext; |
| 32 | use Thrift\Protocol\JSON\ListContext; |
| 33 | |
| 34 | /** |
| 35 | * JSON implementation of thrift protocol, ported from Java. |
| 36 | */ |
| 37 | class TJSONProtocol extends TProtocol |
| 38 | { |
| 39 | const COMMA = ','; |
| 40 | const COLON = ':'; |
| 41 | const LBRACE = '{'; |
| 42 | const RBRACE = '}'; |
| 43 | const LBRACKET = '['; |
| 44 | const RBRACKET = ']'; |
| 45 | const QUOTE = '"'; |
| 46 | const BACKSLASH = '\\'; |
| 47 | const ZERO = '0'; |
| 48 | const ESCSEQ = '\\'; |
| 49 | const DOUBLEESC = '__DOUBLE_ESCAPE_SEQUENCE__'; |
| 50 | |
| 51 | const VERSION = 1; |
| 52 | |
| 53 | public static $JSON_CHAR_TABLE = array( |
| 54 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 55 | 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, // 0 |
| 56 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1 |
| 57 | 1, 1, '"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 58 | ); |
| 59 | |
| Jens Geyer | 7975950 | 2013-12-26 18:56:54 +0100 | [diff] [blame] | 60 | public static $ESCAPE_CHARS = array('"', '\\', '/', "b", "f", "n", "r", "t"); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 61 | |
| 62 | public static $ESCAPE_CHAR_VALS = array( |
| Jens Geyer | 7975950 | 2013-12-26 18:56:54 +0100 | [diff] [blame] | 63 | '"', '\\', '/', "\x08", "\f", "\n", "\r", "\t", |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 64 | ); |
| 65 | |
| 66 | const NAME_BOOL = "tf"; |
| 67 | const NAME_BYTE = "i8"; |
| 68 | const NAME_I16 = "i16"; |
| 69 | const NAME_I32 = "i32"; |
| 70 | const NAME_I64 = "i64"; |
| 71 | const NAME_DOUBLE = "dbl"; |
| 72 | const NAME_STRUCT = "rec"; |
| 73 | const NAME_STRING = "str"; |
| 74 | const NAME_MAP = "map"; |
| 75 | const NAME_LIST = "lst"; |
| 76 | const NAME_SET = "set"; |
| 77 | |
| 78 | private function getTypeNameForTypeID($typeID) |
| 79 | { |
| 80 | switch ($typeID) { |
| 81 | case TType::BOOL: |
| 82 | return self::NAME_BOOL; |
| 83 | case TType::BYTE: |
| 84 | return self::NAME_BYTE; |
| 85 | case TType::I16: |
| 86 | return self::NAME_I16; |
| 87 | case TType::I32: |
| 88 | return self::NAME_I32; |
| 89 | case TType::I64: |
| 90 | return self::NAME_I64; |
| 91 | case TType::DOUBLE: |
| 92 | return self::NAME_DOUBLE; |
| 93 | case TType::STRING: |
| 94 | return self::NAME_STRING; |
| 95 | case TType::STRUCT: |
| 96 | return self::NAME_STRUCT; |
| 97 | case TType::MAP: |
| 98 | return self::NAME_MAP; |
| 99 | case TType::SET: |
| 100 | return self::NAME_SET; |
| 101 | case TType::LST: |
| 102 | return self::NAME_LIST; |
| 103 | default: |
| 104 | throw new TProtocolException("Unrecognized type", TProtocolException::UNKNOWN); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private function getTypeIDForTypeName($name) |
| 109 | { |
| 110 | $result = TType::STOP; |
| 111 | |
| Pavel Kvach | 8238364 | 2024-01-14 15:53:17 +0200 | [diff] [blame] | 112 | if (strlen((string) $name) > 1) { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 113 | switch (substr($name, 0, 1)) { |
| 114 | case 'd': |
| 115 | $result = TType::DOUBLE; |
| 116 | break; |
| 117 | case 'i': |
| 118 | switch (substr($name, 1, 1)) { |
| 119 | case '8': |
| 120 | $result = TType::BYTE; |
| 121 | break; |
| 122 | case '1': |
| 123 | $result = TType::I16; |
| 124 | break; |
| 125 | case '3': |
| 126 | $result = TType::I32; |
| 127 | break; |
| 128 | case '6': |
| 129 | $result = TType::I64; |
| 130 | break; |
| 131 | } |
| 132 | break; |
| 133 | case 'l': |
| 134 | $result = TType::LST; |
| 135 | break; |
| 136 | case 'm': |
| 137 | $result = TType::MAP; |
| 138 | break; |
| 139 | case 'r': |
| 140 | $result = TType::STRUCT; |
| 141 | break; |
| 142 | case 's': |
| 143 | if (substr($name, 1, 1) == 't') { |
| 144 | $result = TType::STRING; |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 145 | } elseif (substr($name, 1, 1) == 'e') { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 146 | $result = TType::SET; |
| 147 | } |
| 148 | break; |
| 149 | case 't': |
| 150 | $result = TType::BOOL; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | if ($result == TType::STOP) { |
| 155 | throw new TProtocolException("Unrecognized type", TProtocolException::INVALID_DATA); |
| 156 | } |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 157 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 158 | return $result; |
| 159 | } |
| 160 | |
| 161 | public $contextStack_ = array(); |
| 162 | public $context_; |
| 163 | public $reader_; |
| 164 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 165 | private function pushContext($c) |
| 166 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 167 | array_push($this->contextStack_, $this->context_); |
| 168 | $this->context_ = $c; |
| 169 | } |
| 170 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 171 | private function popContext() |
| 172 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 173 | $this->context_ = array_pop($this->contextStack_); |
| 174 | } |
| 175 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 176 | public function __construct($trans) |
| 177 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 178 | parent::__construct($trans); |
| 179 | $this->context_ = new BaseContext(); |
| 180 | $this->reader_ = new LookaheadReader($this); |
| 181 | } |
| 182 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 183 | public function reset() |
| 184 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 185 | $this->contextStack_ = array(); |
| 186 | $this->context_ = new BaseContext(); |
| 187 | $this->reader_ = new LookaheadReader($this); |
| 188 | } |
| 189 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 190 | public function readJSONSyntaxChar($b) |
| 191 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 192 | $ch = $this->reader_->read(); |
| 193 | |
| 194 | if (substr($ch, 0, 1) != $b) { |
| 195 | throw new TProtocolException("Unexpected character: " . $ch, TProtocolException::INVALID_DATA); |
| 196 | } |
| 197 | } |
| 198 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 199 | private function writeJSONString($b) |
| 200 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 201 | $this->context_->write(); |
| 202 | |
| 203 | if (is_numeric($b) && $this->context_->escapeNum()) { |
| 204 | $this->trans_->write(self::QUOTE); |
| 205 | } |
| 206 | |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 207 | $this->trans_->write(json_encode($b, JSON_UNESCAPED_UNICODE)); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 208 | |
| 209 | if (is_numeric($b) && $this->context_->escapeNum()) { |
| 210 | $this->trans_->write(self::QUOTE); |
| 211 | } |
| 212 | } |
| 213 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 214 | private function writeJSONInteger($num) |
| 215 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 216 | $this->context_->write(); |
| 217 | |
| 218 | if ($this->context_->escapeNum()) { |
| 219 | $this->trans_->write(self::QUOTE); |
| 220 | } |
| 221 | |
| 222 | $this->trans_->write($num); |
| 223 | |
| 224 | if ($this->context_->escapeNum()) { |
| 225 | $this->trans_->write(self::QUOTE); |
| 226 | } |
| 227 | } |
| 228 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 229 | private function writeJSONDouble($num) |
| 230 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 231 | $this->context_->write(); |
| 232 | |
| 233 | if ($this->context_->escapeNum()) { |
| 234 | $this->trans_->write(self::QUOTE); |
| 235 | } |
| 236 | |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 237 | #TODO add compatibility with NAN and INF |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 238 | $this->trans_->write(json_encode($num)); |
| 239 | |
| 240 | if ($this->context_->escapeNum()) { |
| 241 | $this->trans_->write(self::QUOTE); |
| 242 | } |
| 243 | } |
| 244 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 245 | private function writeJSONObjectStart() |
| 246 | { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 247 | $this->context_->write(); |
| 248 | $this->trans_->write(self::LBRACE); |
| 249 | $this->pushContext(new PairContext($this)); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 252 | private function writeJSONObjectEnd() |
| 253 | { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 254 | $this->popContext(); |
| 255 | $this->trans_->write(self::RBRACE); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 258 | private function writeJSONArrayStart() |
| 259 | { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 260 | $this->context_->write(); |
| 261 | $this->trans_->write(self::LBRACKET); |
| 262 | $this->pushContext(new ListContext($this)); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 265 | private function writeJSONArrayEnd() |
| 266 | { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 267 | $this->popContext(); |
| 268 | $this->trans_->write(self::RBRACKET); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 271 | private function readJSONString($skipContext) |
| 272 | { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 273 | if (!$skipContext) { |
| 274 | $this->context_->read(); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 275 | } |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 276 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 277 | $jsonString = ''; |
| 278 | $lastChar = null; |
| 279 | while (true) { |
| 280 | $ch = $this->reader_->read(); |
| 281 | $jsonString .= $ch; |
| 282 | if ($ch == self::QUOTE && |
| 283 | $lastChar !== null && |
| 284 | $lastChar !== self::ESCSEQ) { |
| 285 | break; |
| 286 | } |
| 287 | if ($ch == self::ESCSEQ && $lastChar == self::ESCSEQ) { |
| 288 | $lastChar = self::DOUBLEESC; |
| 289 | } else { |
| 290 | $lastChar = $ch; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return json_decode($jsonString); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 297 | private function isJSONNumeric($b) |
| 298 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 299 | switch ($b) { |
| 300 | case '+': |
| 301 | case '-': |
| 302 | case '.': |
| 303 | case '0': |
| 304 | case '1': |
| 305 | case '2': |
| 306 | case '3': |
| 307 | case '4': |
| 308 | case '5': |
| 309 | case '6': |
| 310 | case '7': |
| 311 | case '8': |
| 312 | case '9': |
| 313 | case 'E': |
| 314 | case 'e': |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 315 | return true; |
| 316 | } |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 317 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 321 | private function readJSONNumericChars() |
| 322 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 323 | $strbld = array(); |
| 324 | |
| 325 | while (true) { |
| 326 | $ch = $this->reader_->peek(); |
| 327 | |
| 328 | if (!$this->isJSONNumeric($ch)) { |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | $strbld[] = $this->reader_->read(); |
| 333 | } |
| 334 | |
| 335 | return implode("", $strbld); |
| 336 | } |
| 337 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 338 | private function readJSONInteger() |
| 339 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 340 | $this->context_->read(); |
| 341 | |
| 342 | if ($this->context_->escapeNum()) { |
| 343 | $this->readJSONSyntaxChar(self::QUOTE); |
| 344 | } |
| 345 | |
| 346 | $str = $this->readJSONNumericChars(); |
| 347 | |
| 348 | if ($this->context_->escapeNum()) { |
| 349 | $this->readJSONSyntaxChar(self::QUOTE); |
| 350 | } |
| 351 | |
| 352 | if (!is_numeric($str)) { |
| 353 | throw new TProtocolException("Invalid data in numeric: " . $str, TProtocolException::INVALID_DATA); |
| 354 | } |
| 355 | |
| 356 | return intval($str); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Identical to readJSONInteger but without the final cast. |
| 361 | * Needed for proper handling of i64 on 32 bit machines. Why a |
| 362 | * separate function? So we don't have to force the rest of the |
| 363 | * use cases through the extra conditional. |
| 364 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 365 | private function readJSONIntegerAsString() |
| 366 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 367 | $this->context_->read(); |
| 368 | |
| 369 | if ($this->context_->escapeNum()) { |
| 370 | $this->readJSONSyntaxChar(self::QUOTE); |
| 371 | } |
| 372 | |
| 373 | $str = $this->readJSONNumericChars(); |
| 374 | |
| 375 | if ($this->context_->escapeNum()) { |
| 376 | $this->readJSONSyntaxChar(self::QUOTE); |
| 377 | } |
| 378 | |
| 379 | if (!is_numeric($str)) { |
| 380 | throw new TProtocolException("Invalid data in numeric: " . $str, TProtocolException::INVALID_DATA); |
| 381 | } |
| 382 | |
| 383 | return $str; |
| 384 | } |
| 385 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 386 | private function readJSONDouble() |
| 387 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 388 | $this->context_->read(); |
| 389 | |
| 390 | if (substr($this->reader_->peek(), 0, 1) == self::QUOTE) { |
| 391 | $arr = $this->readJSONString(true); |
| 392 | |
| 393 | if ($arr == "NaN") { |
| 394 | return NAN; |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 395 | } elseif ($arr == "Infinity") { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 396 | return INF; |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 397 | } elseif (!$this->context_->escapeNum()) { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 398 | throw new TProtocolException( |
| 399 | "Numeric data unexpectedly quoted " . $arr, |
| 400 | TProtocolException::INVALID_DATA |
| 401 | ); |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | return floatval($arr); |
| 405 | } else { |
| 406 | if ($this->context_->escapeNum()) { |
| 407 | $this->readJSONSyntaxChar(self::QUOTE); |
| 408 | } |
| 409 | |
| 410 | return floatval($this->readJSONNumericChars()); |
| 411 | } |
| 412 | } |
| 413 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 414 | private function readJSONObjectStart() |
| 415 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 416 | $this->context_->read(); |
| 417 | $this->readJSONSyntaxChar(self::LBRACE); |
| 418 | $this->pushContext(new PairContext($this)); |
| 419 | } |
| 420 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 421 | private function readJSONObjectEnd() |
| 422 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 423 | $this->readJSONSyntaxChar(self::RBRACE); |
| 424 | $this->popContext(); |
| 425 | } |
| 426 | |
| 427 | private function readJSONArrayStart() |
| 428 | { |
| 429 | $this->context_->read(); |
| 430 | $this->readJSONSyntaxChar(self::LBRACKET); |
| 431 | $this->pushContext(new ListContext($this)); |
| 432 | } |
| 433 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 434 | private function readJSONArrayEnd() |
| 435 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 436 | $this->readJSONSyntaxChar(self::RBRACKET); |
| 437 | $this->popContext(); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Writes the message header |
| 442 | * |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 443 | * @param string $name Function name |
| 444 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY |
| 445 | * @param int $seqid The sequence id of this message |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 446 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 447 | public function writeMessageBegin($name, $type, $seqid) |
| 448 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 449 | $this->writeJSONArrayStart(); |
| 450 | $this->writeJSONInteger(self::VERSION); |
| 451 | $this->writeJSONString($name); |
| 452 | $this->writeJSONInteger($type); |
| 453 | $this->writeJSONInteger($seqid); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Close the message |
| 458 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 459 | public function writeMessageEnd() |
| 460 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 461 | $this->writeJSONArrayEnd(); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Writes a struct header. |
| 466 | * |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 467 | * @param string $name Struct name |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 468 | * @throws TException on write error |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 469 | * @return int How many bytes written |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 470 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 471 | public function writeStructBegin($name) |
| 472 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 473 | $this->writeJSONObjectStart(); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Close a struct. |
| 478 | * |
| 479 | * @throws TException on write error |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 480 | * @return int How many bytes written |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 481 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 482 | public function writeStructEnd() |
| 483 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 484 | $this->writeJSONObjectEnd(); |
| 485 | } |
| 486 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 487 | public function writeFieldBegin($fieldName, $fieldType, $fieldId) |
| 488 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 489 | $this->writeJSONInteger($fieldId); |
| 490 | $this->writeJSONObjectStart(); |
| 491 | $this->writeJSONString($this->getTypeNameForTypeID($fieldType)); |
| 492 | } |
| 493 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 494 | public function writeFieldEnd() |
| 495 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 496 | $this->writeJsonObjectEnd(); |
| 497 | } |
| 498 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 499 | public function writeFieldStop() |
| 500 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 503 | public function writeMapBegin($keyType, $valType, $size) |
| 504 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 505 | $this->writeJSONArrayStart(); |
| 506 | $this->writeJSONString($this->getTypeNameForTypeID($keyType)); |
| 507 | $this->writeJSONString($this->getTypeNameForTypeID($valType)); |
| 508 | $this->writeJSONInteger($size); |
| 509 | $this->writeJSONObjectStart(); |
| 510 | } |
| 511 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 512 | public function writeMapEnd() |
| 513 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 514 | $this->writeJSONObjectEnd(); |
| 515 | $this->writeJSONArrayEnd(); |
| 516 | } |
| 517 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 518 | public function writeListBegin($elemType, $size) |
| 519 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 520 | $this->writeJSONArrayStart(); |
| 521 | $this->writeJSONString($this->getTypeNameForTypeID($elemType)); |
| 522 | $this->writeJSONInteger($size); |
| 523 | } |
| 524 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 525 | public function writeListEnd() |
| 526 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 527 | $this->writeJSONArrayEnd(); |
| 528 | } |
| 529 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 530 | public function writeSetBegin($elemType, $size) |
| 531 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 532 | $this->writeJSONArrayStart(); |
| 533 | $this->writeJSONString($this->getTypeNameForTypeID($elemType)); |
| 534 | $this->writeJSONInteger($size); |
| 535 | } |
| 536 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 537 | public function writeSetEnd() |
| 538 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 539 | $this->writeJSONArrayEnd(); |
| 540 | } |
| 541 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 542 | public function writeBool($bool) |
| 543 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 544 | $this->writeJSONInteger($bool ? 1 : 0); |
| 545 | } |
| 546 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 547 | public function writeByte($byte) |
| 548 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 549 | $this->writeJSONInteger($byte); |
| 550 | } |
| 551 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 552 | public function writeI16($i16) |
| 553 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 554 | $this->writeJSONInteger($i16); |
| 555 | } |
| 556 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 557 | public function writeI32($i32) |
| 558 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 559 | $this->writeJSONInteger($i32); |
| 560 | } |
| 561 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 562 | public function writeI64($i64) |
| 563 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 564 | $this->writeJSONInteger($i64); |
| 565 | } |
| 566 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 567 | public function writeDouble($dub) |
| 568 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 569 | $this->writeJSONDouble($dub); |
| 570 | } |
| 571 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 572 | public function writeString($str) |
| 573 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 574 | $this->writeJSONString($str); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Reads the message header |
| 579 | * |
| 580 | * @param string $name Function name |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 581 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 582 | * @parem int $seqid The sequence id of this message |
| 583 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 584 | public function readMessageBegin(&$name, &$type, &$seqid) |
| 585 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 586 | $this->readJSONArrayStart(); |
| 587 | |
| 588 | if ($this->readJSONInteger() != self::VERSION) { |
| 589 | throw new TProtocolException("Message contained bad version", TProtocolException::BAD_VERSION); |
| 590 | } |
| 591 | |
| 592 | $name = $this->readJSONString(false); |
| 593 | $type = $this->readJSONInteger(); |
| 594 | $seqid = $this->readJSONInteger(); |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Read the close of message |
| 601 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 602 | public function readMessageEnd() |
| 603 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 604 | $this->readJSONArrayEnd(); |
| 605 | } |
| 606 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 607 | public function readStructBegin(&$name) |
| 608 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 609 | $this->readJSONObjectStart(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 610 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 614 | public function readStructEnd() |
| 615 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 616 | $this->readJSONObjectEnd(); |
| 617 | } |
| 618 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 619 | public function readFieldBegin(&$name, &$fieldType, &$fieldId) |
| 620 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 621 | $ch = $this->reader_->peek(); |
| 622 | $name = ""; |
| 623 | |
| 624 | if (substr($ch, 0, 1) == self::RBRACE) { |
| 625 | $fieldType = TType::STOP; |
| 626 | } else { |
| 627 | $fieldId = $this->readJSONInteger(); |
| 628 | $this->readJSONObjectStart(); |
| 629 | $fieldType = $this->getTypeIDForTypeName($this->readJSONString(false)); |
| 630 | } |
| 631 | } |
| 632 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 633 | public function readFieldEnd() |
| 634 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 635 | $this->readJSONObjectEnd(); |
| 636 | } |
| 637 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 638 | public function readMapBegin(&$keyType, &$valType, &$size) |
| 639 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 640 | $this->readJSONArrayStart(); |
| 641 | $keyType = $this->getTypeIDForTypeName($this->readJSONString(false)); |
| 642 | $valType = $this->getTypeIDForTypeName($this->readJSONString(false)); |
| 643 | $size = $this->readJSONInteger(); |
| 644 | $this->readJSONObjectStart(); |
| 645 | } |
| 646 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 647 | public function readMapEnd() |
| 648 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 649 | $this->readJSONObjectEnd(); |
| 650 | $this->readJSONArrayEnd(); |
| 651 | } |
| 652 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 653 | public function readListBegin(&$elemType, &$size) |
| 654 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 655 | $this->readJSONArrayStart(); |
| 656 | $elemType = $this->getTypeIDForTypeName($this->readJSONString(false)); |
| 657 | $size = $this->readJSONInteger(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 658 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 659 | return true; |
| 660 | } |
| 661 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 662 | public function readListEnd() |
| 663 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 664 | $this->readJSONArrayEnd(); |
| 665 | } |
| 666 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 667 | public function readSetBegin(&$elemType, &$size) |
| 668 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 669 | $this->readJSONArrayStart(); |
| 670 | $elemType = $this->getTypeIDForTypeName($this->readJSONString(false)); |
| 671 | $size = $this->readJSONInteger(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 672 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 673 | return true; |
| 674 | } |
| 675 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 676 | public function readSetEnd() |
| 677 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 678 | $this->readJSONArrayEnd(); |
| 679 | } |
| 680 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 681 | public function readBool(&$bool) |
| 682 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 683 | $bool = $this->readJSONInteger() == 0 ? false : true; |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 684 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 688 | public function readByte(&$byte) |
| 689 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 690 | $byte = $this->readJSONInteger(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 691 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 692 | return true; |
| 693 | } |
| 694 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 695 | public function readI16(&$i16) |
| 696 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 697 | $i16 = $this->readJSONInteger(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 698 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 699 | return true; |
| 700 | } |
| 701 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 702 | public function readI32(&$i32) |
| 703 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 704 | $i32 = $this->readJSONInteger(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 705 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 706 | return true; |
| 707 | } |
| 708 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 709 | public function readI64(&$i64) |
| 710 | { |
| 711 | if (PHP_INT_SIZE === 4) { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 712 | $i64 = $this->readJSONIntegerAsString(); |
| 713 | } else { |
| 714 | $i64 = $this->readJSONInteger(); |
| 715 | } |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 716 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 720 | public function readDouble(&$dub) |
| 721 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 722 | $dub = $this->readJSONDouble(); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 723 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 724 | return true; |
| 725 | } |
| 726 | |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 727 | public function readString(&$str) |
| 728 | { |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 729 | $str = $this->readJSONString(false); |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 730 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 731 | return true; |
| 732 | } |
| 733 | } |