| 1 | package Thrift::JSONProtocol; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Thrift::JSONProtocol | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | JSON protocol implementation for thrift. | ||
| 10 | |||
| 11 | This is a full-featured protocol supporting write and read. | ||
| 12 | |||
| 13 | This code was adapted from the Java implementation. | ||
| 14 | |||
| 15 | Please see the C++ class header for a detailed description of the protocol's wire format. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Thrift; | ||
| 22 | use Thrift::Protocol; | ||
| 23 | use base qw(Thrift::Protocol Class::Accessor); | ||
| 24 | |||
| 25 | use utf8; | ||
| 26 | use Encode; | ||
| 27 | use MIME::Base64; | ||
| 28 | |||
| 29 | __PACKAGE__->mk_accessors(qw(trans context_ reader_)); | ||
| 30 | sub transport { shift->{trans} } | ||
| 31 | |||
| 32 | use constant { | ||
| 33 | COMMA => ',', | ||
| 34 | COLON => ':', | ||
| 35 | LBRACE => '{', | ||
| 36 | RBRACE => '}', | ||
| 37 | LBRACKET => '[', | ||
| 38 | RBRACKET => ']', | ||
| 39 | QUOTE => '"', | ||
| 40 | BACKSLASH => '\\', | ||
| 41 | ZERO => '0', | ||
| 42 | |||
| 43 | ESCSEQ => join('','\\','u','0','0'), | ||
| 44 | VERSION => 1, | ||
| 45 | |||
| 46 | JSON_CHAR_TABLE => [ | ||
| 47 | # 0 1 2 3 4 5 6 7 8 9 A B C D E F | ||
| 48 | 0, 0, 0, 0, 0, 0, 0, 0,'b','t','n', 0,'f','r', 0, 0, # 0 | ||
| 49 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 1 | ||
| 50 | 1, 1,'"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 2 | ||
| 51 | ], | ||
| 52 | |||
| 53 | ESCAPE_CHARS => "\"\\bfnrt", | ||
| 54 | ESCAPE_CHAR_VALS => [ '"', '\\', "\b", "\f", "\n", "\r", "\t", ], | ||
| 55 | |||
| 56 | NAME_BOOL => 'tf', | ||
| 57 | NAME_BYTE => 'i8', | ||
| 58 | NAME_I16 => 'i16', | ||
| 59 | NAME_I32 => 'i32', | ||
| 60 | NAME_I64 => 'i64', | ||
| 61 | NAME_DOUBLE => 'dbl', | ||
| 62 | NAME_STRUCT => 'rec', | ||
| 63 | NAME_STRING => 'str', | ||
| 64 | NAME_MAP => 'map', | ||
| 65 | NAME_LIST => 'lst', | ||
| 66 | NAME_SET => 'set', | ||
| 67 | }; | ||
| 68 | |||
| 69 | my %_getTypeNameForTypeID = ( | ||
| 70 | TType::BOOL => NAME_BOOL, | ||
| 71 | TType::BYTE => NAME_BYTE, | ||
| 72 | TType::I16 => NAME_I16, | ||
| 73 | TType::I32 => NAME_I32, | ||
| 74 | TType::I64 => NAME_I64, | ||
| 75 | TType::DOUBLE => NAME_DOUBLE, | ||
| 76 | TType::STRING => NAME_STRING, | ||
| 77 | TType::STRUCT => NAME_STRUCT, | ||
| 78 | TType::MAP => NAME_MAP, | ||
| 79 | TType::SET => NAME_SET, | ||
| 80 | TType::LIST => NAME_LIST, | ||
| 81 | ); | ||
| 82 | |||
| 83 | ## | ||
| 84 | ## Class methods | ||
| 85 | ## | ||
| 86 | |||
| 87 | sub new { | ||
| 88 | my $class = shift; | ||
| 89 | |||
| 90 | my $self = $class->SUPER::new(@_); | ||
| 91 | |||
| 92 | # Stack of nested contexts that we may be in | ||
| 93 | $self->{contextStack_} = []; | ||
| 94 | |||
| 95 | # Current context that we are in | ||
| 96 | $self->{context_} = Thrift::JSONProtocol::JSONBaseContext->new( protocol => $self ); | ||
| 97 | |||
| 98 | # Reader that manages a 1-byte buffer | ||
| 99 | $self->{reader_} = Thrift::JSONProtocol::LookaheadReader->new( protocol => $self ); | ||
| 100 | |||
| 101 | return $self; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub getTypeNameForTypeID { | ||
| 105 | my ($typeID) = @_; | ||
| 106 | |||
| 107 | if (my $typeName = $_getTypeNameForTypeID{$typeID}) { | ||
| 108 | return $typeName; | ||
| 109 | } | ||
| 110 | die TProtocolException->new( "Unrecognized type $typeID", TProtocolException::UNKNOWN ) | ||
| 111 | } | ||
| 112 | |||
| 113 | sub getTypeIDForTypeName { | ||
| 114 | my ($name) = @_; | ||
| 115 | my $result = TType::STOP; | ||
| 116 | my @name = split //, $name; | ||
| 117 | if (int(@name) > 1) { | ||
| 118 | if ($name[0] eq 'd') { $result = TType::DOUBLE } | ||
| 119 | elsif ($name[0] eq 'i') { | ||
| 120 | if ($name[1] eq '8') { $result = TType::BYTE } | ||
| 121 | elsif ($name[1] eq '1') { $result = TType::I16 } | ||
| 122 | elsif ($name[1] eq '3') { $result = TType::I32 } | ||
| 123 | elsif ($name[1] eq '6') { $result = TType::I64 } | ||
| 124 | } | ||
| 125 | elsif ($name[0] eq 'l') { $result = TType::LIST } | ||
| 126 | elsif ($name[0] eq 'm') { $result = TType::MAP } | ||
| 127 | elsif ($name[0] eq 'r') { $result = TType::STRUCT } | ||
| 128 | elsif ($name[0] eq 's') { | ||
| 129 | if ($name[1] eq 't') { $result = TType::STRING } | ||
| 130 | elsif ($name[1] eq 'e') { $result = TType::SET } | ||
| 131 | } | ||
| 132 | elsif ($name[0] eq 't') { $result = TType::BOOL } | ||
| 133 | } | ||
| 134 | if ($result == TType::STOP) { | ||
| 135 | die TProtocolException->new("Unrecognized type", TProtocolException::UNKNOWN); | ||
| 136 | } | ||
| 137 | return $result; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub check_utf8 { | ||
| 141 | my ($string_ref) = @_; | ||
| 142 | |||
| 143 | return if ! utf8::is_utf8($$string_ref); | ||
| 144 | $$string_ref = Encode::encode_utf8($$string_ref); | ||
| 145 | } | ||
| 146 | |||
| 147 | ## | ||
| 148 | ## Object methods | ||
| 149 | ## | ||
| 150 | |||
| 151 | # | ||
| 152 | # Helper methods | ||
| 153 | # | ||
| 154 | |||
| 155 | # Push a new JSON context onto the stack. | ||
| 156 | sub pushContext { | ||
| 157 | my ($self, $context) = @_; | ||
| 158 | push @{ $self->{contextStack_} }, delete $self->{context_}; | ||
| 159 | $self->{context_} = $context; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Pop the last JSON context off the stack | ||
| 163 | sub popContext { | ||
| 164 | my ($self) = @_; | ||
| 165 | my $context = pop @{ $self->{contextStack_} }; | ||
| 166 | $self->{context_} = $context; | ||
| 167 | return $context; | ||
| 168 | } | ||
| 169 | |||
| 170 | # Read a byte that must match $expected; otherwise an excpetion is thrown. | ||
| 171 | sub readJSONSyntaxChar { | ||
| 172 | my ($self, $expected) = @_; | ||
| 173 | my $got = $self->{reader_}->read(); | ||
| 174 | if ($got ne $expected) { | ||
| 175 | die TProtocolException->new("Unexpected character: $got", TProtocolException::INVALID_DATA); | ||
| 176 | } | ||
| 177 | return length $got; | ||
| 178 | } | ||
| 179 | |||
| 180 | # Convenience method for writing and getting the length of the string written | ||
| 181 | sub write { | ||
| 182 | my ($self, $string) = @_; | ||
| 183 | $self->transport->write($string); | ||
| 184 | return length $string; | ||
| 185 | } | ||
| 186 | |||
| 187 | # | ||
| 188 | # Read/write JSON methods | ||
| 189 | # | ||
| 190 | |||
| 191 | # Write the bytes in array buf as a JSON characters, escaping as needed | ||
| 192 | sub writeJSONString { | ||
| 193 | my ($self, $string) = @_; | ||
| 194 | my @b = split //, $string; | ||
| 195 | |||
| 196 | my $xfer = 0; | ||
| 197 | |||
| 198 | $xfer += $self->context_->write(); | ||
| 199 | |||
| 200 | $xfer += $self->write(QUOTE); | ||
| 201 | |||
| 202 | my $len = int @b; | ||
| 203 | for (my $i = 0; $i < $len; $i++) { | ||
| 204 | my $ord = ord($b[$i]); | ||
| 205 | if (($ord & 0x00FF) >= 0x30) { | ||
| 206 | if ($b[$i] eq BACKSLASH) { | ||
| 207 | $xfer += $self->write(BACKSLASH . BACKSLASH); | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | $xfer += $self->write($b[$i]); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | my $tmp = JSON_CHAR_TABLE->[$ord]; | ||
| 215 | if ($tmp eq '1') { | ||
| 216 | $xfer += $self->write($b[$i]); | ||
| 217 | } | ||
| 218 | elsif ($tmp eq '0') { | ||
| 219 | my $hex = unpack 'H*', chr($ord); | ||
| 220 | $xfer += $self->write(ESCSEQ . $hex); | ||
| 221 | } | ||
| 222 | else { | ||
| 223 | $xfer += $self->write(BACKSLASH . $tmp); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | $xfer += $self->write(QUOTE); | ||
| 229 | |||
| 230 | return $xfer; | ||
| 231 | } | ||
| 232 | |||
| 233 | # Read in a JSON string, unescaping as appropriate.. Skip reading from the | ||
| 234 | # context if skipContext is true. | ||
| 235 | sub readJSONString { | ||
| 236 | my ($self, $string, $skipContext) = @_; | ||
| 237 | |||
| 238 | my $xfer = 0; | ||
| 239 | |||
| 240 | $xfer += $self->context_->read() if ! $skipContext; | ||
| 241 | |||
| 242 | $xfer += $self->readJSONSyntaxChar(QUOTE); | ||
| 243 | |||
| 244 | $$string = ''; | ||
| 245 | |||
| 246 | while (1) { | ||
| 247 | my $ch = $self->reader_->read(); | ||
| 248 | $xfer++; | ||
| 249 | if ($ch eq QUOTE) { | ||
| 250 | last; | ||
| 251 | } | ||
| 252 | if ($ch eq substr ESCSEQ, 0, 1) { | ||
| 253 | $ch = $self->reader_->read(); | ||
| 254 | $xfer++; | ||
| 255 | if ($ch eq substr ESCSEQ, 1, 1) { | ||
| 256 | $xfer += $self->readJSONSyntaxChar(ZERO); | ||
| 257 | $xfer += $self->readJSONSyntaxChar(ZERO); | ||
| 258 | my $tmp = $self->transport->readAll(2); | ||
| 259 | $ch = chr(hex($tmp)); | ||
| 260 | $xfer += 2; | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | my $off = index ESCAPE_CHARS, $ch; | ||
| 264 | if ($off == -1) { | ||
| 265 | die TProtocolException->new("Expected control char, got '$ch'", TProtocolException::INVALID_DATA); | ||
| 266 | } | ||
| 267 | $ch = ESCAPE_CHAR_VALS->[$off]; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | $$string .= $ch; | ||
| 271 | } | ||
| 272 | |||
| 273 | return $xfer; | ||
| 274 | } | ||
| 275 | |||
| 276 | # Write out number as a JSON value. If the context dictates so, it will be | ||
| 277 | # wrapped in quotes to output as a JSON string. | ||
| 278 | |||
| 279 | sub writeJSONInteger { | ||
| 280 | my ($self, $num) = @_; | ||
| 281 | my $xfer = 0; | ||
| 282 | $xfer += $self->context_->write; | ||
| 283 | my $str = $num . ''; | ||
| 284 | check_utf8(\$str); | ||
| 285 | my $escapeNum = $self->context_->escapeNum(); | ||
| 286 | |||
| 287 | $xfer += $self->write(QUOTE) if $escapeNum; | ||
| 288 | $xfer += $self->write($str); | ||
| 289 | $xfer += $self->write(QUOTE) if $escapeNum; | ||
| 290 | return $xfer; | ||
| 291 | } | ||
| 292 | |||
| 293 | # Return true if the given byte could be a valid part of a JSON number. | ||
| 294 | sub isJSONNumeric { | ||
| 295 | my ($char) = @_; | ||
| 296 | return $char =~ m{^[-+.0-9Ee]$} ? 1 : 0; | ||
| 297 | } | ||
| 298 | |||
| 299 | # Read in a sequence of characters that are all valid in JSON numbers. Does | ||
| 300 | # not do a complete regex check to validate that this is actually a number. | ||
| 301 | sub readJSONNumericChars { | ||
| 302 | my ($self, $str) = @_; | ||
| 303 | my $xfer = 0; | ||
| 304 | while (1) { | ||
| 305 | my $ch; | ||
| 306 | eval { | ||
| 307 | $ch = $self->reader_->peek(); | ||
| 308 | }; | ||
| 309 | if (my $ex = $@) { | ||
| 310 | if ($ex->isa('TTransportException') && $ex->{code} == TTransportException::END_OF_FILE) { | ||
| 311 | last; | ||
| 312 | } | ||
| 313 | die $ex; | ||
| 314 | } | ||
| 315 | if (! isJSONNumeric($ch)) { | ||
| 316 | last; | ||
| 317 | } | ||
| 318 | $$str .= $self->reader_->read(); | ||
| 319 | $xfer++; | ||
| 320 | } | ||
| 321 | return $xfer; | ||
| 322 | } | ||
| 323 | |||
| 324 | # Read in a JSON number. If the context dictates, read in enclosing quotes. | ||
| 325 | sub readJSONInteger { | ||
| 326 | my ($self, $int) = @_; | ||
| 327 | my $xfer = 0; | ||
| 328 | $xfer += $self->context_->read(); | ||
| 329 | my $escapeNum = $self->context_->escapeNum(); | ||
| 330 | |||
| 331 | my $str; | ||
| 332 | $xfer += $self->readJSONSyntaxChar(QUOTE) if $escapeNum; | ||
| 333 | $xfer += $self->readJSONNumericChars(\$str); | ||
| 334 | $xfer += $self->readJSONSyntaxChar(QUOTE) if $escapeNum; | ||
| 335 | |||
| 336 | $$int = $str * 1; | ||
| 337 | return $xfer; | ||
| 338 | } | ||
| 339 | |||
| 340 | # Write out a double as a JSON value. If it is NaN or infinity or if the | ||
| 341 | # context dictates escaping, write out as JSON string. | ||
| 342 | sub writeJSONDouble { | ||
| 343 | my ($self, $num) = @_; | ||
| 344 | my $xfer = 0; | ||
| 345 | $xfer += $self->context_->write(); | ||
| 346 | my $str = $num . ''; | ||
| 347 | check_utf8(\$str); | ||
| 348 | my $special = $str =~ m{^-?(N|I)} ? 1 : 0; | ||
| 349 | my $escapeNum = $special || $self->context_->escapeNum; | ||
| 350 | |||
| 351 | $xfer += $self->write(QUOTE) if $escapeNum; | ||
| 352 | $xfer += $self->write($str); | ||
| 353 | $xfer += $self->write(QUOTE) if $escapeNum; | ||
| 354 | return $xfer; | ||
| 355 | } | ||
| 356 | |||
| 357 | # Read in a JSON double value. Throw if the value is not wrapped in quotes | ||
| 358 | # when expected or if wrapped in quotes when not expected. | ||
| 359 | sub readJSONDouble { | ||
| 360 | my ($self, $dub) = @_; | ||
| 361 | my $xfer = 0; | ||
| 362 | $xfer += $self->context_->read(); | ||
| 363 | |||
| 364 | if ($self->reader_->peek() eq QUOTE) { | ||
| 365 | my $str; | ||
| 366 | $xfer += $self->readJSONString(\$str, 1); | ||
| 367 | my $special = $str =~ m{^-?(N|I)} ? 1 : 0; | ||
| 368 | if (! $self->context_->escapeNum && ! $special) { | ||
| 369 | # Throw exception -- we should not be in a string in this case | ||
| 370 | die TProtocolException->new( | ||
| 371 | "Numeric data unexpectedly quoted", | ||
| 372 | TProtocolException::INVALID_DATA, | ||
| 373 | ); | ||
| 374 | } | ||
| 375 | $$dub = $str; | ||
| 376 | } | ||
| 377 | else { | ||
| 378 | if ($self->context_->escapeNum()) { | ||
| 379 | # This will throw - we should have had a quote if escapeNum == true | ||
| 380 | $xfer += $self->readJSONSyntaxChar(QUOTE); | ||
| 381 | } | ||
| 382 | $xfer += $self->readJSONNumericChars($dub); | ||
| 383 | } | ||
| 384 | return $xfer; | ||
| 385 | } | ||
| 386 | |||
| 387 | # Write out contents of byte array b as a JSON string with base-64 encoded | ||
| 388 | # data | ||
| 389 | sub writeJSONBase64 { | ||
| 390 | my ($self, $string) = @_; | ||
| 391 | return $self->writeJSONString( encode_base64($string, '') ); | ||
| 392 | } | ||
| 393 | |||
| 394 | # Read in a JSON string containing base-64 encoded data and decode it. | ||
| 395 | sub readJSONBase64 { | ||
| 396 | my ($self, $string) = @_; | ||
| 397 | |||
| 398 | my $xfer = $self->readJSONString($string); | ||
| 399 | my $tmp = decode_base64($$string); | ||
| 400 | $$string = $tmp; | ||
| 401 | return $xfer; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub writeJSONObjectStart { | ||
| 405 | my ($self) = @_; | ||
| 406 | my $xfer = 0; | ||
| 407 | $xfer += $self->context_->write(); | ||
| 408 | $xfer += $self->write(LBRACE); | ||
| 409 | $self->pushContext( | ||
| 410 | Thrift::JSONProtocol::JSONPairContext->new( protocol => $self ) | ||
| 411 | ); | ||
| 412 | return $xfer; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub readJSONObjectStart { | ||
| 416 | my ($self) = @_; | ||
| 417 | my $xfer = 0; | ||
| 418 | $xfer += $self->context_->read(); | ||
| 419 | $xfer += $self->readJSONSyntaxChar(LBRACE); | ||
| 420 | $self->pushContext( | ||
| 421 | Thrift::JSONProtocol::JSONPairContext->new( protocol => $self ) | ||
| 422 | ); | ||
| 423 | return $xfer; | ||
| 424 | } | ||
| 425 | |||
| 426 | sub writeJSONObjectEnd { | ||
| 427 | my ($self) = @_; | ||
| 428 | $self->popContext(); | ||
| 429 | return $self->write(RBRACE); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub readJSONObjectEnd { | ||
| 433 | my ($self) = @_; | ||
| 434 | $self->popContext(); | ||
| 435 | return $self->readJSONSyntaxChar(RBRACE); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub writeJSONArrayStart { | ||
| 439 | my ($self) = @_; | ||
| 440 | my $xfer = 0; | ||
| 441 | $xfer += $self->context_->write(); | ||
| 442 | $xfer += $self->write(LBRACKET); | ||
| 443 | $self->pushContext( | ||
| 444 | Thrift::JSONProtocol::JSONListContext->new( protocol => $self ) | ||
| 445 | ); | ||
| 446 | return $xfer; | ||
| 447 | } | ||
| 448 | |||
| 449 | sub readJSONArrayStart { | ||
| 450 | my ($self) = @_; | ||
| 451 | my $xfer = 0; | ||
| 452 | $xfer += $self->context_->read(); | ||
| 453 | $xfer += $self->readJSONSyntaxChar(LBRACKET); | ||
| 454 | $self->pushContext( | ||
| 455 | Thrift::JSONProtocol::JSONListContext->new( protocol => $self ) | ||
| 456 | ); | ||
| 457 | return $xfer; | ||
| 458 | } | ||
| 459 | |||
| 460 | sub writeJSONArrayEnd { | ||
| 461 | my ($self) = @_; | ||
| 462 | $self->popContext(); | ||
| 463 | return $self->write(RBRACKET); | ||
| 464 | } | ||
| 465 | |||
| 466 | sub readJSONArrayEnd { | ||
| 467 | my ($self) = @_; | ||
| 468 | $self->popContext(); | ||
| 469 | return $self->readJSONSyntaxChar(RBRACKET); | ||
| 470 | } | ||
| 471 | |||
| 472 | # | ||
| 473 | # Thrift::Protocol methods | ||
| 474 | # | ||
| 475 | |||
| 476 | sub writeMessageBegin { | ||
| 477 | my ($self, $name, $type, $seqid) = @_; | ||
| 478 | |||
| 479 | check_utf8(\$name); | ||
| 480 | |||
| 481 | my $xfer = 0; | ||
| 482 | $xfer += $self->writeJSONArrayStart(); | ||
| 483 | $xfer += $self->writeJSONInteger(VERSION); | ||
| 484 | $xfer += $self->writeJSONString($name); | ||
| 485 | $xfer += $self->writeJSONInteger($type); | ||
| 486 | $xfer += $self->writeJSONInteger($seqid); | ||
| 487 | return $xfer; | ||
| 488 | } | ||
| 489 | |||
| 490 | sub readMessageBegin { | ||
| 491 | my ($self, $name, $type, $seqid) = @_; | ||
| 492 | |||
| 493 | my $xfer = 0; | ||
| 494 | |||
| 495 | $xfer += $self->readJSONArrayStart(); | ||
| 496 | my $version; | ||
| 497 | $xfer += $self->readJSONInteger(\$version); | ||
| 498 | |||
| 499 | if ($version != VERSION) { | ||
| 500 | die TProtocolException->new("Message contained bad version.", TProtocolException::BAD_VERSION); | ||
| 501 | } | ||
| 502 | |||
| 503 | $xfer += $self->readJSONString($name); | ||
| 504 | $xfer += $self->readJSONInteger($type); | ||
| 505 | $xfer += $self->readJSONInteger($seqid); | ||
| 506 | return $xfer; | ||
| 507 | } | ||
| 508 | |||
| 509 | sub writeMessageEnd { | ||
| 510 | my ($self) = @_; | ||
| 511 | $self->writeJSONArrayEnd(); | ||
| 512 | } | ||
| 513 | |||
| 514 | sub readMessageEnd { | ||
| 515 | my ($self) = @_; | ||
| 516 | $self->readJSONArrayEnd(); | ||
| 517 | } | ||
| 518 | |||
| 519 | sub writeStructBegin { | ||
| 520 | my ($self) = @_; | ||
| 521 | $self->writeJSONObjectStart(); | ||
| 522 | } | ||
| 523 | |||
| 524 | sub readStructBegin { | ||
| 525 | my ($self, $name) = @_; | ||
| 526 | $self->readJSONObjectStart(); | ||
| 527 | } | ||
| 528 | |||
| 529 | sub writeStructEnd { | ||
| 530 | my ($self) = @_; | ||
| 531 | $self->writeJSONObjectEnd(); | ||
| 532 | } | ||
| 533 | |||
| 534 | sub readStructEnd { | ||
| 535 | my ($self) = @_; | ||
| 536 | $self->readJSONObjectEnd(); | ||
| 537 | } | ||
| 538 | |||
| 539 | sub writeFieldBegin { | ||
| 540 | my ($self, $fieldName, $fieldType, $fieldId) = @_; | ||
| 541 | my $xfer = 0; | ||
| 542 | $xfer += $self->writeJSONInteger($fieldId); | ||
| 543 | $xfer += $self->writeJSONObjectStart(); | ||
| 544 | $xfer += $self->writeJSONString(getTypeNameForTypeID($fieldType)); | ||
| 545 | return $xfer; | ||
| 546 | } | ||
| 547 | |||
| 548 | sub readFieldBegin { | ||
| 549 | my ($self, $name, $fieldType, $fieldId) = @_; | ||
| 550 | |||
| 551 | my $xfer = 0; | ||
| 552 | my $ch = $self->reader_->peek(); | ||
| 553 | if ($ch eq RBRACE) { | ||
| 554 | $$fieldType = TType::STOP; | ||
| 555 | } | ||
| 556 | else { | ||
| 557 | $xfer += $self->readJSONInteger($fieldId); | ||
| 558 | $xfer += $self->readJSONObjectStart(); | ||
| 559 | my $type; | ||
| 560 | $xfer += $self->readJSONString(\$type); | ||
| 561 | $$fieldType = getTypeIDForTypeName($type); | ||
| 562 | } | ||
| 563 | return $xfer; | ||
| 564 | } | ||
| 565 | |||
| 566 | sub writeFieldEnd { | ||
| 567 | my ($self) = @_; | ||
| 568 | $self->writeJSONObjectEnd(); | ||
| 569 | } | ||
| 570 | |||
| 571 | sub readFieldEnd { | ||
| 572 | my ($self) = @_; | ||
| 573 | $self->readJSONObjectEnd(); | ||
| 574 | } | ||
| 575 | |||
| 576 | sub writeFieldStop { 0 } | ||
| 577 | |||
| 578 | sub writeMapBegin { | ||
| 579 | my ($self, $keyType, $valType, $size) = @_; | ||
| 580 | my $xfer = 0; | ||
| 581 | $xfer += $self->writeJSONArrayStart(); | ||
| 582 | $xfer += $self->writeJSONString(getTypeNameForTypeID($keyType)); | ||
| 583 | $xfer += $self->writeJSONString(getTypeNameForTypeID($valType)); | ||
| 584 | $xfer += $self->writeJSONInteger($size); | ||
| 585 | $xfer += $self->writeJSONObjectStart(); | ||
| 586 | return $xfer; | ||
| 587 | } | ||
| 588 | |||
| 589 | sub readMapBegin { | ||
| 590 | my ($self, $keyType, $valType, $size) = @_; | ||
| 591 | my $xfer = 0; | ||
| 592 | $xfer += $self->readJSONArrayStart(); | ||
| 593 | |||
| 594 | $xfer += $self->readJSONString($keyType); | ||
| 595 | $$keyType = getTypeIDForTypeName($$keyType); | ||
| 596 | |||
| 597 | $xfer += $self->readJSONString($valType); | ||
| 598 | $$valType = getTypeIDForTypeName($$valType); | ||
| 599 | |||
| 600 | $xfer += $self->readJSONInteger($size); | ||
| 601 | $xfer += $self->readJSONObjectStart(); | ||
| 602 | return $xfer; | ||
| 603 | } | ||
| 604 | |||
| 605 | sub writeMapEnd { | ||
| 606 | my ($self) = @_; | ||
| 607 | return $self->writeJSONObjectEnd() + $self->writeJSONArrayEnd(); | ||
| 608 | } | ||
| 609 | |||
| 610 | sub readMapEnd { | ||
| 611 | my ($self) = @_; | ||
| 612 | return $self->readJSONObjectEnd() + $self->readJSONArrayEnd(); | ||
| 613 | } | ||
| 614 | |||
| 615 | sub writeListBegin { | ||
| 616 | my ($self, $elemType, $size) = @_; | ||
| 617 | my $xfer = 0; | ||
| 618 | $xfer += $self->writeJSONArrayStart(); | ||
| 619 | $xfer += $self->writeJSONString(getTypeNameForTypeID($elemType)); | ||
| 620 | $xfer += $self->writeJSONInteger($size); | ||
| 621 | return $xfer; | ||
| 622 | } | ||
| 623 | |||
| 624 | sub readListBegin { | ||
| 625 | my ($self, $elemType, $size) = @_; | ||
| 626 | my $xfer = 0; | ||
| 627 | |||
| 628 | $xfer += $self->readJSONArrayStart(); | ||
| 629 | |||
| 630 | $xfer += $self->readJSONString($elemType); | ||
| 631 | $$elemType = getTypeIDForTypeName($$elemType); | ||
| 632 | |||
| 633 | $xfer += $self->readJSONInteger($size); | ||
| 634 | |||
| 635 | return $xfer; | ||
| 636 | } | ||
| 637 | |||
| 638 | sub writeListEnd { | ||
| 639 | my ($self) = @_; | ||
| 640 | $self->writeJSONArrayEnd(); | ||
| 641 | } | ||
| 642 | |||
| 643 | sub readListEnd { | ||
| 644 | my ($self) = @_; | ||
| 645 | $self->readJSONArrayEnd(); | ||
| 646 | } | ||
| 647 | |||
| 648 | sub writeSetBegin { | ||
| 649 | my $self = shift; | ||
| 650 | $self->writeListBegin(@_); | ||
| 651 | } | ||
| 652 | |||
| 653 | sub readSetBegin { | ||
| 654 | my $self = shift; | ||
| 655 | $self->readListBegin(@_); | ||
| 656 | } | ||
| 657 | |||
| 658 | sub writeSetEnd { | ||
| 659 | my ($self) = @_; | ||
| 660 | $self->writeListEnd(); | ||
| 661 | } | ||
| 662 | |||
| 663 | sub readSetEnd { | ||
| 664 | my ($self) = @_; | ||
| 665 | $self->readListEnd(); | ||
| 666 | } | ||
| 667 | |||
| 668 | sub writeBool { | ||
| 669 | my ($self, $b) = @_; | ||
| 670 | $self->writeJSONInteger($b ? 1 : 0); | ||
| 671 | } | ||
| 672 | |||
| 673 | sub readBool { | ||
| 674 | my ($self, $b) = @_; | ||
| 675 | my $xfer = $self->readJSONInteger($b); | ||
| 676 | $$b = $$b ? 1 : 0; | ||
| 677 | return $xfer; | ||
| 678 | } | ||
| 679 | |||
| 680 | sub writeByte { | ||
| 681 | my ($self, $b) = @_; | ||
| 682 | $self->writeJSONInteger(ord($b)); | ||
| 683 | } | ||
| 684 | |||
| 685 | sub readByte { | ||
| 686 | my ($self, $b) = @_; | ||
| 687 | my $xfer = $self->readJSONInteger($b); | ||
| 688 | $$b = chr($$b); | ||
| 689 | return $xfer; | ||
| 690 | } | ||
| 691 | |||
| 692 | sub writeI16 { | ||
| 693 | my ($self, $i16) = @_; | ||
| 694 | $self->writeJSONInteger($i16); | ||
| 695 | } | ||
| 696 | |||
| 697 | sub readI16 { | ||
| 698 | my ($self, $i16) = @_; | ||
| 699 | $self->readJSONInteger($i16); | ||
| 700 | } | ||
| 701 | |||
| 702 | sub writeI32 { | ||
| 703 | my ($self, $i32) = @_; | ||
| 704 | $self->writeJSONInteger($i32); | ||
| 705 | } | ||
| 706 | |||
| 707 | sub readI32 { | ||
| 708 | my ($self, $i32) = @_; | ||
| 709 | $self->readJSONInteger($i32); | ||
| 710 | } | ||
| 711 | |||
| 712 | sub writeI64 { | ||
| 713 | my ($self, $i64) = @_; | ||
| 714 | $self->writeJSONInteger($i64); | ||
| 715 | } | ||
| 716 | |||
| 717 | sub readI64 { | ||
| 718 | my ($self, $i64) = @_; | ||
| 719 | $self->readJSONInteger($i64); | ||
| 720 | } | ||
| 721 | |||
| 722 | sub writeDouble { | ||
| 723 | my ($self, $dub) = @_; | ||
| 724 | $self->writeJSONDouble($dub); | ||
| 725 | } | ||
| 726 | |||
| 727 | sub readDouble { | ||
| 728 | my ($self, $dub) = @_; | ||
| 729 | $self->readJSONDouble($dub); | ||
| 730 | } | ||
| 731 | |||
| 732 | sub writeString { | ||
| 733 | my ($self, $str) = @_; | ||
| 734 | check_utf8(\$str); | ||
| 735 | $self->writeJSONString($str); | ||
| 736 | } | ||
| 737 | |||
| 738 | sub readString { | ||
| 739 | my ($self, $str) = @_; | ||
| 740 | $self->readJSONString($str); | ||
| 741 | } | ||
| 742 | |||
| 743 | sub writeBinary { | ||
| 744 | my ($self, $str) = @_; | ||
| 745 | $self->writeJSONBase64($str); | ||
| 746 | } | ||
| 747 | |||
| 748 | sub readBinary { | ||
| 749 | my ($self, $str) = @_; | ||
| 750 | $self->readJSONBase64($str); | ||
| 751 | } | ||
| 752 | # | ||
| 753 | # Other related packages | ||
| 754 | # | ||
| 755 | |||
| 756 | { | ||
| 757 | package Thrift::JSONProtocolFactory; | ||
| 758 | |||
| 759 | use strict; | ||
| 760 | use warnings; | ||
| 761 | use base qw(TProtocolFactory); | ||
| 762 | |||
| 763 | sub getProtocol { | ||
| 764 | my ($self, $transport) = @_; | ||
| 765 | return Thrift::JSONProtocol->new($transport); | ||
| 766 | } | ||
| 767 | } | ||
| 768 | |||
| 769 | { | ||
| 770 | # Base class for tracking JSON contexts that may require inserting/reading | ||
| 771 | # additional JSON syntax characters | ||
| 772 | # This base context does nothing. | ||
| 773 | package Thrift::JSONProtocol::JSONBaseContext; | ||
| 774 | |||
| 775 | use strict; | ||
| 776 | use warnings; | ||
| 777 | use base qw(Class::Accessor); | ||
| 778 | BEGIN { | ||
| 779 | __PACKAGE__->mk_accessors(qw(protocol)); | ||
| 780 | }; | ||
| 781 | |||
| 782 | sub new { | ||
| 783 | my ($class, %self) = @_; | ||
| 784 | return bless \%self, $class; | ||
| 785 | } | ||
| 786 | |||
| 787 | sub write { 0 } | ||
| 788 | sub read { 0 } | ||
| 789 | sub escapeNum { 0 } | ||
| 790 | } | ||
| 791 | |||
| 792 | { | ||
| 793 | # Context for JSON lists. Will insert/read commas before each item except | ||
| 794 | # for the first one | ||
| 795 | package Thrift::JSONProtocol::JSONListContext; | ||
| 796 | |||
| 797 | use strict; | ||
| 798 | use warnings; | ||
| 799 | use base qw(Thrift::JSONProtocol::JSONBaseContext); | ||
| 800 | BEGIN { | ||
| 801 | __PACKAGE__->mk_accessors(qw(first_)); | ||
| 802 | }; | ||
| 803 | |||
| 804 | sub new { | ||
| 805 | my $class = shift; | ||
| 806 | my $self = $class->SUPER::new(@_); | ||
| 807 | $self->first_(1); | ||
| 808 | return $self; | ||
| 809 | } | ||
| 810 | |||
| 811 | sub write { | ||
| 812 | my ($self) = @_; | ||
| 813 | if ($self->first_) { | ||
| 814 | $self->first_(0); | ||
| 815 | return 0; | ||
| 816 | } | ||
| 817 | else { | ||
| 818 | $self->protocol->transport->write(Thrift::JSONProtocol::COMMA); | ||
| 819 | return length Thrift::JSONProtocol::COMMA; | ||
| 820 | } | ||
| 821 | } | ||
| 822 | |||
| 823 | sub read { | ||
| 824 | my ($self) = @_; | ||
| 825 | if ($self->first_) { | ||
| 826 | $self->first_(0); | ||
| 827 | return 0; | ||
| 828 | } | ||
| 829 | else { | ||
| 830 | $self->protocol->readJSONSyntaxChar(Thrift::JSONProtocol::COMMA); | ||
| 831 | return length Thrift::JSONProtocol::COMMA; | ||
| 832 | } | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | { | ||
| 837 | # Context for JSON records. Will insert/read colons before the value portion | ||
| 838 | # of each record pair, and commas before each key except the first. In | ||
| 839 | # addition, will indicate that numbers in the key position need to be | ||
| 840 | # escaped in quotes (since JSON keys must be strings). | ||
| 841 | package Thrift::JSONProtocol::JSONPairContext; | ||
| 842 | |||
| 843 | use strict; | ||
| 844 | use warnings; | ||
| 845 | use base qw(Thrift::JSONProtocol::JSONBaseContext); | ||
| 846 | BEGIN { | ||
| 847 | __PACKAGE__->mk_accessors(qw(first_ colon_)); | ||
| 848 | }; | ||
| 849 | |||
| 850 | sub new { | ||
| 851 | my $class = shift; | ||
| 852 | my $self = $class->SUPER::new(@_); | ||
| 853 | $self->first_(1); | ||
| 854 | $self->colon_(1); | ||
| 855 | return $self; | ||
| 856 | } | ||
| 857 | |||
| 858 | sub write { | ||
| 859 | my ($self) = @_; | ||
| 860 | |||
| 861 | if ($self->first_) { | ||
| 862 | $self->first_(0); | ||
| 863 | $self->colon_(1); | ||
| 864 | return 0; | ||
| 865 | } | ||
| 866 | else { | ||
| 867 | my $string = $self->colon_ ? Thrift::JSONProtocol::COLON : Thrift::JSONProtocol::COMMA; | ||
| 868 | $self->protocol->transport->write($string); | ||
| 869 | $self->colon_($self->colon_ ? 0 : 1); | ||
| 870 | return length $string; | ||
| 871 | } | ||
| 872 | } | ||
| 873 | |||
| 874 | sub read { | ||
| 875 | my ($self) = @_; | ||
| 876 | |||
| 877 | if ($self->first_) { | ||
| 878 | $self->first_(0); | ||
| 879 | $self->colon_(1); | ||
| 880 | return 0; | ||
| 881 | } | ||
| 882 | else { | ||
| 883 | my $string = $self->colon_ ? Thrift::JSONProtocol::COLON : Thrift::JSONProtocol::COMMA; | ||
| 884 | $self->protocol->readJSONSyntaxChar($string); | ||
| 885 | $self->colon_($self->colon_ ? 0 : 1); | ||
| 886 | return length $string; | ||
| 887 | } | ||
| 888 | } | ||
| 889 | |||
| 890 | sub escapeNum { | ||
| 891 | my ($self) = @_; | ||
| 892 | return $self->colon_; | ||
| 893 | } | ||
| 894 | } | ||
| 895 | |||
| 896 | { | ||
| 897 | # Holds up to one byte from the transport | ||
| 898 | package Thrift::JSONProtocol::LookaheadReader; | ||
| 899 | |||
| 900 | use strict; | ||
| 901 | use warnings; | ||
| 902 | use base qw(Class::Accessor); | ||
| 903 | BEGIN { | ||
| 904 | __PACKAGE__->mk_accessors(qw(protocol hasData_ data_)); | ||
| 905 | }; | ||
| 906 | |||
| 907 | sub new { | ||
| 908 | my ($class, %self) = @_; | ||
| 909 | return bless \%self, $class; | ||
| 910 | } | ||
| 911 | |||
| 912 | # Return and consume the next byte to be read, either taking it from the | ||
| 913 | # data buffer if present or getting it from the transport otherwise. | ||
| 914 | sub read { | ||
| 915 | my ($self) = @_; | ||
| 916 | if ($self->hasData_) { | ||
| 917 | $self->hasData_(0); | ||
| 918 | } | ||
| 919 | else { | ||
| 920 | $self->data_( $self->protocol->transport->readAll(1) ); | ||
| 921 | } | ||
| 922 | return $self->data_; | ||
| 923 | } | ||
| 924 | |||
| 925 | # Return the next byte to be read without consuming, filling the data | ||
| 926 | # buffer if it has not been filled already. | ||
| 927 | sub peek { | ||
| 928 | my ($self) = @_; | ||
| 929 | if (! $self->hasData_) { | ||
| 930 | $self->data_( $self->protocol->transport->readAll(1) ); | ||
| 931 | } | ||
| 932 | $self->hasData_(1); | ||
| 933 | return $self->data_; | ||
| 934 | } | ||
| 935 | } | ||
| 936 | |||
| 937 | 1; |