| 1 | package JSON::PP; | ||
| 2 | |||
| 3 | # JSON-2.0 | ||
| 4 | |||
| 5 | use 5.005; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | use Exporter (); | ||
| 9 | BEGIN { @JSON::PP::ISA = ('Exporter') } | ||
| 10 | |||
| 11 | use overload (); | ||
| 12 | use JSON::PP::Boolean; | ||
| 13 | |||
| 14 | use Carp (); | ||
| 15 | #use Devel::Peek; | ||
| 16 | |||
| 17 | $JSON::PP::VERSION = '2.97001'; | ||
| 18 | |||
| 19 | @JSON::PP::EXPORT = qw(encode_json decode_json from_json to_json); | ||
| 20 | |||
| 21 | # instead of hash-access, i tried index-access for speed. | ||
| 22 | # but this method is not faster than what i expected. so it will be changed. | ||
| 23 | |||
| 24 | use constant P_ASCII => 0; | ||
| 25 | use constant P_LATIN1 => 1; | ||
| 26 | use constant P_UTF8 => 2; | ||
| 27 | use constant P_INDENT => 3; | ||
| 28 | use constant P_CANONICAL => 4; | ||
| 29 | use constant P_SPACE_BEFORE => 5; | ||
| 30 | use constant P_SPACE_AFTER => 6; | ||
| 31 | use constant P_ALLOW_NONREF => 7; | ||
| 32 | use constant P_SHRINK => 8; | ||
| 33 | use constant P_ALLOW_BLESSED => 9; | ||
| 34 | use constant P_CONVERT_BLESSED => 10; | ||
| 35 | use constant P_RELAXED => 11; | ||
| 36 | |||
| 37 | use constant P_LOOSE => 12; | ||
| 38 | use constant P_ALLOW_BIGNUM => 13; | ||
| 39 | use constant P_ALLOW_BAREKEY => 14; | ||
| 40 | use constant P_ALLOW_SINGLEQUOTE => 15; | ||
| 41 | use constant P_ESCAPE_SLASH => 16; | ||
| 42 | use constant P_AS_NONBLESSED => 17; | ||
| 43 | |||
| 44 | use constant P_ALLOW_UNKNOWN => 18; | ||
| 45 | |||
| 46 | use constant OLD_PERL => $] < 5.008 ? 1 : 0; | ||
| 47 | use constant USE_B => 0; | ||
| 48 | |||
| 49 | BEGIN { | ||
| 50 | if (USE_B) { | ||
| 51 | require B; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | BEGIN { | ||
| 56 | my @xs_compati_bit_properties = qw( | ||
| 57 | latin1 ascii utf8 indent canonical space_before space_after allow_nonref shrink | ||
| 58 | allow_blessed convert_blessed relaxed allow_unknown | ||
| 59 | ); | ||
| 60 | my @pp_bit_properties = qw( | ||
| 61 | allow_singlequote allow_bignum loose | ||
| 62 | allow_barekey escape_slash as_nonblessed | ||
| 63 | ); | ||
| 64 | |||
| 65 | # Perl version check, Unicode handling is enabled? | ||
| 66 | # Helper module sets @JSON::PP::_properties. | ||
| 67 | if ( OLD_PERL ) { | ||
| 68 | my $helper = $] >= 5.006 ? 'JSON::PP::Compat5006' : 'JSON::PP::Compat5005'; | ||
| 69 | eval qq| require $helper |; | ||
| 70 | if ($@) { Carp::croak $@; } | ||
| 71 | } | ||
| 72 | |||
| 73 | for my $name (@xs_compati_bit_properties, @pp_bit_properties) { | ||
| 74 | my $property_id = 'P_' . uc($name); | ||
| 75 | |||
| 76 | eval qq/ | ||
| 77 | sub $name { | ||
| 78 | my \$enable = defined \$_[1] ? \$_[1] : 1; | ||
| 79 | |||
| 80 | if (\$enable) { | ||
| 81 | \$_[0]->{PROPS}->[$property_id] = 1; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | \$_[0]->{PROPS}->[$property_id] = 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | \$_[0]; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub get_$name { | ||
| 91 | \$_[0]->{PROPS}->[$property_id] ? 1 : ''; | ||
| 92 | } | ||
| 93 | /; | ||
| 94 | } | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | # Functions | ||
| 101 | |||
| 102 | my $JSON; # cache | ||
| 103 | |||
| 104 | sub encode_json ($) { # encode | ||
| 105 | ($JSON ||= __PACKAGE__->new->utf8)->encode(@_); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub decode_json { # decode | ||
| 110 | ($JSON ||= __PACKAGE__->new->utf8)->decode(@_); | ||
| 111 | } | ||
| 112 | |||
| 113 | # Obsoleted | ||
| 114 | |||
| 115 | sub to_json($) { | ||
| 116 | Carp::croak ("JSON::PP::to_json has been renamed to encode_json."); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub from_json($) { | ||
| 121 | Carp::croak ("JSON::PP::from_json has been renamed to decode_json."); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | # Methods | ||
| 126 | |||
| 127 | sub new { | ||
| 128 | my $class = shift; | ||
| 129 | my $self = { | ||
| 130 | max_depth => 512, | ||
| 131 | max_size => 0, | ||
| 132 | indent_length => 3, | ||
| 133 | }; | ||
| 134 | |||
| 135 | bless $self, $class; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | sub encode { | ||
| 140 | return $_[0]->PP_encode_json($_[1]); | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | sub decode { | ||
| 145 | return $_[0]->PP_decode_json($_[1], 0x00000000); | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub decode_prefix { | ||
| 150 | return $_[0]->PP_decode_json($_[1], 0x00000001); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | # accessor | ||
| 155 | |||
| 156 | |||
| 157 | # pretty printing | ||
| 158 | |||
| 159 | sub pretty { | ||
| 160 | my ($self, $v) = @_; | ||
| 161 | my $enable = defined $v ? $v : 1; | ||
| 162 | |||
| 163 | if ($enable) { # indent_length(3) for JSON::XS compatibility | ||
| 164 | $self->indent(1)->space_before(1)->space_after(1); | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | $self->indent(0)->space_before(0)->space_after(0); | ||
| 168 | } | ||
| 169 | |||
| 170 | $self; | ||
| 171 | } | ||
| 172 | |||
| 173 | # etc | ||
| 174 | |||
| 175 | sub max_depth { | ||
| 176 | my $max = defined $_[1] ? $_[1] : 0x80000000; | ||
| 177 | $_[0]->{max_depth} = $max; | ||
| 178 | $_[0]; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub get_max_depth { $_[0]->{max_depth}; } | ||
| 183 | |||
| 184 | |||
| 185 | sub max_size { | ||
| 186 | my $max = defined $_[1] ? $_[1] : 0; | ||
| 187 | $_[0]->{max_size} = $max; | ||
| 188 | $_[0]; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub get_max_size { $_[0]->{max_size}; } | ||
| 193 | |||
| 194 | |||
| 195 | sub filter_json_object { | ||
| 196 | if (defined $_[1] and ref $_[1] eq 'CODE') { | ||
| 197 | $_[0]->{cb_object} = $_[1]; | ||
| 198 | } else { | ||
| 199 | delete $_[0]->{cb_object}; | ||
| 200 | } | ||
| 201 | $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0; | ||
| 202 | $_[0]; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub filter_json_single_key_object { | ||
| 206 | if (@_ == 1 or @_ > 3) { | ||
| 207 | Carp::croak("Usage: JSON::PP::filter_json_single_key_object(self, key, callback = undef)"); | ||
| 208 | } | ||
| 209 | if (defined $_[2] and ref $_[2] eq 'CODE') { | ||
| 210 | $_[0]->{cb_sk_object}->{$_[1]} = $_[2]; | ||
| 211 | } else { | ||
| 212 | delete $_[0]->{cb_sk_object}->{$_[1]}; | ||
| 213 | delete $_[0]->{cb_sk_object} unless %{$_[0]->{cb_sk_object} || {}}; | ||
| 214 | } | ||
| 215 | $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0; | ||
| 216 | $_[0]; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub indent_length { | ||
| 220 | if (!defined $_[1] or $_[1] > 15 or $_[1] < 0) { | ||
| 221 | Carp::carp "The acceptable range of indent_length() is 0 to 15."; | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | $_[0]->{indent_length} = $_[1]; | ||
| 225 | } | ||
| 226 | $_[0]; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub get_indent_length { | ||
| 230 | $_[0]->{indent_length}; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub sort_by { | ||
| 234 | $_[0]->{sort_by} = defined $_[1] ? $_[1] : 1; | ||
| 235 | $_[0]; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub allow_bigint { | ||
| 239 | Carp::carp("allow_bigint() is obsoleted. use allow_bignum() instead."); | ||
| 240 | $_[0]->allow_bignum; | ||
| 241 | } | ||
| 242 | |||
| 243 | ############################### | ||
| 244 | |||
| 245 | ### | ||
| 246 | ### Perl => JSON | ||
| 247 | ### | ||
| 248 | |||
| 249 | |||
| 250 | { # Convert | ||
| 251 | |||
| 252 | my $max_depth; | ||
| 253 | my $indent; | ||
| 254 | my $ascii; | ||
| 255 | my $latin1; | ||
| 256 | my $utf8; | ||
| 257 | my $space_before; | ||
| 258 | my $space_after; | ||
| 259 | my $canonical; | ||
| 260 | my $allow_blessed; | ||
| 261 | my $convert_blessed; | ||
| 262 | |||
| 263 | my $indent_length; | ||
| 264 | my $escape_slash; | ||
| 265 | my $bignum; | ||
| 266 | my $as_nonblessed; | ||
| 267 | |||
| 268 | my $depth; | ||
| 269 | my $indent_count; | ||
| 270 | my $keysort; | ||
| 271 | |||
| 272 | |||
| 273 | sub PP_encode_json { | ||
| 274 | my $self = shift; | ||
| 275 | my $obj = shift; | ||
| 276 | |||
| 277 | $indent_count = 0; | ||
| 278 | $depth = 0; | ||
| 279 | |||
| 280 | my $props = $self->{PROPS}; | ||
| 281 | |||
| 282 | ($ascii, $latin1, $utf8, $indent, $canonical, $space_before, $space_after, $allow_blessed, | ||
| 283 | $convert_blessed, $escape_slash, $bignum, $as_nonblessed) | ||
| 284 | = @{$props}[P_ASCII .. P_SPACE_AFTER, P_ALLOW_BLESSED, P_CONVERT_BLESSED, | ||
| 285 | P_ESCAPE_SLASH, P_ALLOW_BIGNUM, P_AS_NONBLESSED]; | ||
| 286 | |||
| 287 | ($max_depth, $indent_length) = @{$self}{qw/max_depth indent_length/}; | ||
| 288 | |||
| 289 | $keysort = $canonical ? sub { $a cmp $b } : undef; | ||
| 290 | |||
| 291 | if ($self->{sort_by}) { | ||
| 292 | $keysort = ref($self->{sort_by}) eq 'CODE' ? $self->{sort_by} | ||
| 293 | : $self->{sort_by} =~ /\D+/ ? $self->{sort_by} | ||
| 294 | : sub { $a cmp $b }; | ||
| 295 | } | ||
| 296 | |||
| 297 | encode_error("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)") | ||
| 298 | if(!ref $obj and !$props->[ P_ALLOW_NONREF ]); | ||
| 299 | |||
| 300 | my $str = $self->object_to_json($obj); | ||
| 301 | |||
| 302 | $str .= "\n" if ( $indent ); # JSON::XS 2.26 compatible | ||
| 303 | |||
| 304 | unless ($ascii or $latin1 or $utf8) { | ||
| 305 | utf8::upgrade($str); | ||
| 306 | } | ||
| 307 | |||
| 308 | if ($props->[ P_SHRINK ]) { | ||
| 309 | utf8::downgrade($str, 1); | ||
| 310 | } | ||
| 311 | |||
| 312 | return $str; | ||
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | sub object_to_json { | ||
| 317 | my ($self, $obj) = @_; | ||
| 318 | my $type = ref($obj); | ||
| 319 | |||
| 320 | if($type eq 'HASH'){ | ||
| 321 | return $self->hash_to_json($obj); | ||
| 322 | } | ||
| 323 | elsif($type eq 'ARRAY'){ | ||
| 324 | return $self->array_to_json($obj); | ||
| 325 | } | ||
| 326 | elsif ($type) { # blessed object? | ||
| 327 | if (blessed($obj)) { | ||
| 328 | |||
| 329 | return $self->value_to_json($obj) if ( $obj->isa('JSON::PP::Boolean') ); | ||
| 330 | |||
| 331 | if ( $convert_blessed and $obj->can('TO_JSON') ) { | ||
| 332 | my $result = $obj->TO_JSON(); | ||
| 333 | if ( defined $result and ref( $result ) ) { | ||
| 334 | if ( refaddr( $obj ) eq refaddr( $result ) ) { | ||
| 335 | encode_error( sprintf( | ||
| 336 | "%s::TO_JSON method returned same object as was passed instead of a new one", | ||
| 337 | ref $obj | ||
| 338 | ) ); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | return $self->object_to_json( $result ); | ||
| 343 | } | ||
| 344 | |||
| 345 | return "$obj" if ( $bignum and _is_bignum($obj) ); | ||
| 346 | |||
| 347 | if ($allow_blessed) { | ||
| 348 | return $self->blessed_to_json($obj) if ($as_nonblessed); # will be removed. | ||
| 349 | return 'null'; | ||
| 350 | } | ||
| 351 | encode_error( sprintf("encountered object '%s', but neither allow_blessed " | ||
| 352 | . "nor convert_blessed settings are enabled", $obj) | ||
| 353 | ); | ||
| 354 | } | ||
| 355 | else { | ||
| 356 | return $self->value_to_json($obj); | ||
| 357 | } | ||
| 358 | } | ||
| 359 | else{ | ||
| 360 | return $self->value_to_json($obj); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | sub hash_to_json { | ||
| 366 | my ($self, $obj) = @_; | ||
| 367 | my @res; | ||
| 368 | |||
| 369 | encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)") | ||
| 370 | if (++$depth > $max_depth); | ||
| 371 | |||
| 372 | my ($pre, $post) = $indent ? $self->_up_indent() : ('', ''); | ||
| 373 | my $del = ($space_before ? ' ' : '') . ':' . ($space_after ? ' ' : ''); | ||
| 374 | |||
| 375 | for my $k ( _sort( $obj ) ) { | ||
| 376 | if ( OLD_PERL ) { utf8::decode($k) } # key for Perl 5.6 / be optimized | ||
| 377 | push @res, $self->string_to_json( $k ) | ||
| 378 | . $del | ||
| 379 | . ( ref $obj->{$k} ? $self->object_to_json( $obj->{$k} ) : $self->value_to_json( $obj->{$k} ) ); | ||
| 380 | } | ||
| 381 | |||
| 382 | --$depth; | ||
| 383 | $self->_down_indent() if ($indent); | ||
| 384 | |||
| 385 | return '{}' unless @res; | ||
| 386 | return '{' . $pre . join( ",$pre", @res ) . $post . '}'; | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | sub array_to_json { | ||
| 391 | my ($self, $obj) = @_; | ||
| 392 | my @res; | ||
| 393 | |||
| 394 | encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)") | ||
| 395 | if (++$depth > $max_depth); | ||
| 396 | |||
| 397 | my ($pre, $post) = $indent ? $self->_up_indent() : ('', ''); | ||
| 398 | |||
| 399 | for my $v (@$obj){ | ||
| 400 | push @res, ref($v) ? $self->object_to_json($v) : $self->value_to_json($v); | ||
| 401 | } | ||
| 402 | |||
| 403 | --$depth; | ||
| 404 | $self->_down_indent() if ($indent); | ||
| 405 | |||
| 406 | return '[]' unless @res; | ||
| 407 | return '[' . $pre . join( ",$pre", @res ) . $post . ']'; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub _looks_like_number { | ||
| 411 | my $value = shift; | ||
| 412 | if (USE_B) { | ||
| 413 | my $b_obj = B::svref_2object(\$value); | ||
| 414 | my $flags = $b_obj->FLAGS; | ||
| 415 | return 1 if $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() ); | ||
| 416 | return; | ||
| 417 | } else { | ||
| 418 | no warnings 'numeric'; | ||
| 419 | # if the utf8 flag is on, it almost certainly started as a string | ||
| 420 | return if utf8::is_utf8($value); | ||
| 421 | # detect numbers | ||
| 422 | # string & "" -> "" | ||
| 423 | # number & "" -> 0 (with warning) | ||
| 424 | # nan and inf can detect as numbers, so check with * 0 | ||
| 425 | return unless length((my $dummy = "") & $value); | ||
| 426 | return unless 0 + $value eq $value; | ||
| 427 | return 1 if $value * 0 == 0; | ||
| 428 | return -1; # inf/nan | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | sub value_to_json { | ||
| 433 | my ($self, $value) = @_; | ||
| 434 | |||
| 435 | return 'null' if(!defined $value); | ||
| 436 | |||
| 437 | my $type = ref($value); | ||
| 438 | |||
| 439 | if (!$type) { | ||
| 440 | if (_looks_like_number($value)) { | ||
| 441 | return $value; | ||
| 442 | } | ||
| 443 | return $self->string_to_json($value); | ||
| 444 | } | ||
| 445 | elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ){ | ||
| 446 | return $$value == 1 ? 'true' : 'false'; | ||
| 447 | } | ||
| 448 | else { | ||
| 449 | if ((overload::StrVal($value) =~ /=(\w+)/)[0]) { | ||
| 450 | return $self->value_to_json("$value"); | ||
| 451 | } | ||
| 452 | |||
| 453 | if ($type eq 'SCALAR' and defined $$value) { | ||
| 454 | return $$value eq '1' ? 'true' | ||
| 455 | : $$value eq '0' ? 'false' | ||
| 456 | : $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ? 'null' | ||
| 457 | : encode_error("cannot encode reference to scalar"); | ||
| 458 | } | ||
| 459 | |||
| 460 | if ( $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ) { | ||
| 461 | return 'null'; | ||
| 462 | } | ||
| 463 | else { | ||
| 464 | if ( $type eq 'SCALAR' or $type eq 'REF' ) { | ||
| 465 | encode_error("cannot encode reference to scalar"); | ||
| 466 | } | ||
| 467 | else { | ||
| 468 | encode_error("encountered $value, but JSON can only represent references to arrays or hashes"); | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | |||
| 476 | my %esc = ( | ||
| 477 | "\n" => '\n', | ||
| 478 | "\r" => '\r', | ||
| 479 | "\t" => '\t', | ||
| 480 | "\f" => '\f', | ||
| 481 | "\b" => '\b', | ||
| 482 | "\"" => '\"', | ||
| 483 | "\\" => '\\\\', | ||
| 484 | "\'" => '\\\'', | ||
| 485 | ); | ||
| 486 | |||
| 487 | |||
| 488 | sub string_to_json { | ||
| 489 | my ($self, $arg) = @_; | ||
| 490 | |||
| 491 | $arg =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g; | ||
| 492 | $arg =~ s/\//\\\//g if ($escape_slash); | ||
| 493 | $arg =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1)/eg; | ||
| 494 | |||
| 495 | if ($ascii) { | ||
| 496 | $arg = JSON_PP_encode_ascii($arg); | ||
| 497 | } | ||
| 498 | |||
| 499 | if ($latin1) { | ||
| 500 | $arg = JSON_PP_encode_latin1($arg); | ||
| 501 | } | ||
| 502 | |||
| 503 | if ($utf8) { | ||
| 504 | utf8::encode($arg); | ||
| 505 | } | ||
| 506 | |||
| 507 | return '"' . $arg . '"'; | ||
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | sub blessed_to_json { | ||
| 512 | my $reftype = reftype($_[1]) || ''; | ||
| 513 | if ($reftype eq 'HASH') { | ||
| 514 | return $_[0]->hash_to_json($_[1]); | ||
| 515 | } | ||
| 516 | elsif ($reftype eq 'ARRAY') { | ||
| 517 | return $_[0]->array_to_json($_[1]); | ||
| 518 | } | ||
| 519 | else { | ||
| 520 | return 'null'; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | |||
| 525 | sub encode_error { | ||
| 526 | my $error = shift; | ||
| 527 | Carp::croak "$error"; | ||
| 528 | } | ||
| 529 | |||
| 530 | |||
| 531 | sub _sort { | ||
| 532 | defined $keysort ? (sort $keysort (keys %{$_[0]})) : keys %{$_[0]}; | ||
| 533 | } | ||
| 534 | |||
| 535 | |||
| 536 | sub _up_indent { | ||
| 537 | my $self = shift; | ||
| 538 | my $space = ' ' x $indent_length; | ||
| 539 | |||
| 540 | my ($pre,$post) = ('',''); | ||
| 541 | |||
| 542 | $post = "\n" . $space x $indent_count; | ||
| 543 | |||
| 544 | $indent_count++; | ||
| 545 | |||
| 546 | $pre = "\n" . $space x $indent_count; | ||
| 547 | |||
| 548 | return ($pre,$post); | ||
| 549 | } | ||
| 550 | |||
| 551 | |||
| 552 | sub _down_indent { $indent_count--; } | ||
| 553 | |||
| 554 | |||
| 555 | sub PP_encode_box { | ||
| 556 | { | ||
| 557 | depth => $depth, | ||
| 558 | indent_count => $indent_count, | ||
| 559 | }; | ||
| 560 | } | ||
| 561 | |||
| 562 | } # Convert | ||
| 563 | |||
| 564 | |||
| 565 | sub _encode_ascii { | ||
| 566 | join('', | ||
| 567 | map { | ||
| 568 | $_ <= 127 ? | ||
| 569 | chr($_) : | ||
| 570 | $_ <= 65535 ? | ||
| 571 | sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_)); | ||
| 572 | } unpack('U*', $_[0]) | ||
| 573 | ); | ||
| 574 | } | ||
| 575 | |||
| 576 | |||
| 577 | sub _encode_latin1 { | ||
| 578 | join('', | ||
| 579 | map { | ||
| 580 | $_ <= 255 ? | ||
| 581 | chr($_) : | ||
| 582 | $_ <= 65535 ? | ||
| 583 | sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_)); | ||
| 584 | } unpack('U*', $_[0]) | ||
| 585 | ); | ||
| 586 | } | ||
| 587 | |||
| 588 | |||
| 589 | sub _encode_surrogates { # from perlunicode | ||
| 590 | my $uni = $_[0] - 0x10000; | ||
| 591 | return ($uni / 0x400 + 0xD800, $uni % 0x400 + 0xDC00); | ||
| 592 | } | ||
| 593 | |||
| 594 | |||
| 595 | sub _is_bignum { | ||
| 596 | $_[0]->isa('Math::BigInt') or $_[0]->isa('Math::BigFloat'); | ||
| 597 | } | ||
| 598 | |||
| 599 | |||
| 600 | |||
| 601 | # | ||
| 602 | # JSON => Perl | ||
| 603 | # | ||
| 604 | |||
| 605 | my $max_intsize; | ||
| 606 | |||
| 607 | BEGIN { | ||
| 608 | my $checkint = 1111; | ||
| 609 | for my $d (5..64) { | ||
| 610 | $checkint .= 1; | ||
| 611 | my $int = eval qq| $checkint |; | ||
| 612 | if ($int =~ /[eE]/) { | ||
| 613 | $max_intsize = $d - 1; | ||
| 614 | last; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | } | ||
| 618 | |||
| 619 | { # PARSE | ||
| 620 | |||
| 621 | my %escapes = ( # by Jeremy Muhlich <jmuhlich [at] bitflood.org> | ||
| 622 | b => "\x8", | ||
| 623 | t => "\x9", | ||
| 624 | n => "\xA", | ||
| 625 | f => "\xC", | ||
| 626 | r => "\xD", | ||
| 627 | '\\' => '\\', | ||
| 628 | '"' => '"', | ||
| 629 | '/' => '/', | ||
| 630 | ); | ||
| 631 | |||
| 632 | my $text; # json data | ||
| 633 | my $at; # offset | ||
| 634 | my $ch; # first character | ||
| 635 | my $len; # text length (changed according to UTF8 or NON UTF8) | ||
| 636 | # INTERNAL | ||
| 637 | my $depth; # nest counter | ||
| 638 | my $encoding; # json text encoding | ||
| 639 | my $is_valid_utf8; # temp variable | ||
| 640 | my $utf8_len; # utf8 byte length | ||
| 641 | # FLAGS | ||
| 642 | my $utf8; # must be utf8 | ||
| 643 | my $max_depth; # max nest number of objects and arrays | ||
| 644 | my $max_size; | ||
| 645 | my $relaxed; | ||
| 646 | my $cb_object; | ||
| 647 | my $cb_sk_object; | ||
| 648 | |||
| 649 | my $F_HOOK; | ||
| 650 | |||
| 651 | my $allow_bignum; # using Math::BigInt/BigFloat | ||
| 652 | my $singlequote; # loosely quoting | ||
| 653 | my $loose; # | ||
| 654 | my $allow_barekey; # bareKey | ||
| 655 | |||
| 656 | sub _detect_utf_encoding { | ||
| 657 | my $text = shift; | ||
| 658 | my @octets = unpack('C4', $text); | ||
| 659 | return 'unknown' unless defined $octets[3]; | ||
| 660 | return ( $octets[0] and $octets[1]) ? 'UTF-8' | ||
| 661 | : (!$octets[0] and $octets[1]) ? 'UTF-16BE' | ||
| 662 | : (!$octets[0] and !$octets[1]) ? 'UTF-32BE' | ||
| 663 | : ( $octets[2] ) ? 'UTF-16LE' | ||
| 664 | : (!$octets[2] ) ? 'UTF-32LE' | ||
| 665 | : 'unknown'; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub PP_decode_json { | ||
| 669 | my ($self, $want_offset); | ||
| 670 | |||
| 671 | ($self, $text, $want_offset) = @_; | ||
| 672 | |||
| 673 | ($at, $ch, $depth) = (0, '', 0); | ||
| 674 | |||
| 675 | if ( !defined $text or ref $text ) { | ||
| 676 | decode_error("malformed JSON string, neither array, object, number, string or atom"); | ||
| 677 | } | ||
| 678 | |||
| 679 | my $props = $self->{PROPS}; | ||
| 680 | |||
| 681 | ($utf8, $relaxed, $loose, $allow_bignum, $allow_barekey, $singlequote) | ||
| 682 | = @{$props}[P_UTF8, P_RELAXED, P_LOOSE .. P_ALLOW_SINGLEQUOTE]; | ||
| 683 | |||
| 684 | if ( $utf8 ) { | ||
| 685 | $encoding = _detect_utf_encoding($text); | ||
| 686 | if ($encoding ne 'UTF-8' and $encoding ne 'unknown') { | ||
| 687 | require Encode; | ||
| 688 | Encode::from_to($text, $encoding, 'utf-8'); | ||
| 689 | } else { | ||
| 690 | utf8::downgrade( $text, 1 ) or Carp::croak("Wide character in subroutine entry"); | ||
| 691 | } | ||
| 692 | } | ||
| 693 | else { | ||
| 694 | utf8::upgrade( $text ); | ||
| 695 | utf8::encode( $text ); | ||
| 696 | } | ||
| 697 | |||
| 698 | $len = length $text; | ||
| 699 | |||
| 700 | ($max_depth, $max_size, $cb_object, $cb_sk_object, $F_HOOK) | ||
| 701 | = @{$self}{qw/max_depth max_size cb_object cb_sk_object F_HOOK/}; | ||
| 702 | |||
| 703 | if ($max_size > 1) { | ||
| 704 | use bytes; | ||
| 705 | my $bytes = length $text; | ||
| 706 | decode_error( | ||
| 707 | sprintf("attempted decode of JSON text of %s bytes size, but max_size is set to %s" | ||
| 708 | , $bytes, $max_size), 1 | ||
| 709 | ) if ($bytes > $max_size); | ||
| 710 | } | ||
| 711 | |||
| 712 | white(); # remove head white space | ||
| 713 | |||
| 714 | decode_error("malformed JSON string, neither array, object, number, string or atom") unless defined $ch; # Is there a first character for JSON structure? | ||
| 715 | |||
| 716 | my $result = value(); | ||
| 717 | |||
| 718 | if ( !$props->[ P_ALLOW_NONREF ] and !ref $result ) { | ||
| 719 | decode_error( | ||
| 720 | 'JSON text must be an object or array (but found number, string, true, false or null,' | ||
| 721 | . ' use allow_nonref to allow this)', 1); | ||
| 722 | } | ||
| 723 | |||
| 724 | Carp::croak('something wrong.') if $len < $at; # we won't arrive here. | ||
| 725 | |||
| 726 | my $consumed = defined $ch ? $at - 1 : $at; # consumed JSON text length | ||
| 727 | |||
| 728 | white(); # remove tail white space | ||
| 729 | |||
| 730 | return ( $result, $consumed ) if $want_offset; # all right if decode_prefix | ||
| 731 | |||
| 732 | decode_error("garbage after JSON object") if defined $ch; | ||
| 733 | |||
| 734 | $result; | ||
| 735 | } | ||
| 736 | |||
| 737 | |||
| 738 | sub next_chr { | ||
| 739 | return $ch = undef if($at >= $len); | ||
| 740 | $ch = substr($text, $at++, 1); | ||
| 741 | } | ||
| 742 | |||
| 743 | |||
| 744 | sub value { | ||
| 745 | white(); | ||
| 746 | return if(!defined $ch); | ||
| 747 | return object() if($ch eq '{'); | ||
| 748 | return array() if($ch eq '['); | ||
| 749 | return string() if($ch eq '"' or ($singlequote and $ch eq "'")); | ||
| 750 | return number() if($ch =~ /[0-9]/ or $ch eq '-'); | ||
| 751 | return word(); | ||
| 752 | } | ||
| 753 | |||
| 754 | sub string { | ||
| 755 | my $utf16; | ||
| 756 | my $is_utf8; | ||
| 757 | |||
| 758 | ($is_valid_utf8, $utf8_len) = ('', 0); | ||
| 759 | |||
| 760 | my $s = ''; # basically UTF8 flag on | ||
| 761 | |||
| 762 | if($ch eq '"' or ($singlequote and $ch eq "'")){ | ||
| 763 | my $boundChar = $ch; | ||
| 764 | |||
| 765 | OUTER: while( defined(next_chr()) ){ | ||
| 766 | |||
| 767 | if($ch eq $boundChar){ | ||
| 768 | next_chr(); | ||
| 769 | |||
| 770 | if ($utf16) { | ||
| 771 | decode_error("missing low surrogate character in surrogate pair"); | ||
| 772 | } | ||
| 773 | |||
| 774 | utf8::decode($s) if($is_utf8); | ||
| 775 | |||
| 776 | return $s; | ||
| 777 | } | ||
| 778 | elsif($ch eq '\\'){ | ||
| 779 | next_chr(); | ||
| 780 | if(exists $escapes{$ch}){ | ||
| 781 | $s .= $escapes{$ch}; | ||
| 782 | } | ||
| 783 | elsif($ch eq 'u'){ # UNICODE handling | ||
| 784 | my $u = ''; | ||
| 785 | |||
| 786 | for(1..4){ | ||
| 787 | $ch = next_chr(); | ||
| 788 | last OUTER if($ch !~ /[0-9a-fA-F]/); | ||
| 789 | $u .= $ch; | ||
| 790 | } | ||
| 791 | |||
| 792 | # U+D800 - U+DBFF | ||
| 793 | if ($u =~ /^[dD][89abAB][0-9a-fA-F]{2}/) { # UTF-16 high surrogate? | ||
| 794 | $utf16 = $u; | ||
| 795 | } | ||
| 796 | # U+DC00 - U+DFFF | ||
| 797 | elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { # UTF-16 low surrogate? | ||
| 798 | unless (defined $utf16) { | ||
| 799 | decode_error("missing high surrogate character in surrogate pair"); | ||
| 800 | } | ||
| 801 | $is_utf8 = 1; | ||
| 802 | $s .= JSON_PP_decode_surrogates($utf16, $u) || next; | ||
| 803 | $utf16 = undef; | ||
| 804 | } | ||
| 805 | else { | ||
| 806 | if (defined $utf16) { | ||
| 807 | decode_error("surrogate pair expected"); | ||
| 808 | } | ||
| 809 | |||
| 810 | if ( ( my $hex = hex( $u ) ) > 127 ) { | ||
| 811 | $is_utf8 = 1; | ||
| 812 | $s .= JSON_PP_decode_unicode($u) || next; | ||
| 813 | } | ||
| 814 | else { | ||
| 815 | $s .= chr $hex; | ||
| 816 | } | ||
| 817 | } | ||
| 818 | |||
| 819 | } | ||
| 820 | else{ | ||
| 821 | unless ($loose) { | ||
| 822 | $at -= 2; | ||
| 823 | decode_error('illegal backslash escape sequence in string'); | ||
| 824 | } | ||
| 825 | $s .= $ch; | ||
| 826 | } | ||
| 827 | } | ||
| 828 | else{ | ||
| 829 | |||
| 830 | if ( ord $ch > 127 ) { | ||
| 831 | unless( $ch = is_valid_utf8($ch) ) { | ||
| 832 | $at -= 1; | ||
| 833 | decode_error("malformed UTF-8 character in JSON string"); | ||
| 834 | } | ||
| 835 | else { | ||
| 836 | $at += $utf8_len - 1; | ||
| 837 | } | ||
| 838 | |||
| 839 | $is_utf8 = 1; | ||
| 840 | } | ||
| 841 | |||
| 842 | if (!$loose) { | ||
| 843 | if ($ch =~ /[\x00-\x1f\x22\x5c]/) { # '/' ok | ||
| 844 | $at--; | ||
| 845 | decode_error('invalid character encountered while parsing JSON string'); | ||
| 846 | } | ||
| 847 | } | ||
| 848 | |||
| 849 | $s .= $ch; | ||
| 850 | } | ||
| 851 | } | ||
| 852 | } | ||
| 853 | |||
| 854 | decode_error("unexpected end of string while parsing JSON string"); | ||
| 855 | } | ||
| 856 | |||
| 857 | |||
| 858 | sub white { | ||
| 859 | while( defined $ch ){ | ||
| 860 | if($ch eq '' or $ch =~ /\A[ \t\r\n]\z/){ | ||
| 861 | next_chr(); | ||
| 862 | } | ||
| 863 | elsif($relaxed and $ch eq '/'){ | ||
| 864 | next_chr(); | ||
| 865 | if(defined $ch and $ch eq '/'){ | ||
| 866 | 1 while(defined(next_chr()) and $ch ne "\n" and $ch ne "\r"); | ||
| 867 | } | ||
| 868 | elsif(defined $ch and $ch eq '*'){ | ||
| 869 | next_chr(); | ||
| 870 | while(1){ | ||
| 871 | if(defined $ch){ | ||
| 872 | if($ch eq '*'){ | ||
| 873 | if(defined(next_chr()) and $ch eq '/'){ | ||
| 874 | next_chr(); | ||
| 875 | last; | ||
| 876 | } | ||
| 877 | } | ||
| 878 | else{ | ||
| 879 | next_chr(); | ||
| 880 | } | ||
| 881 | } | ||
| 882 | else{ | ||
| 883 | decode_error("Unterminated comment"); | ||
| 884 | } | ||
| 885 | } | ||
| 886 | next; | ||
| 887 | } | ||
| 888 | else{ | ||
| 889 | $at--; | ||
| 890 | decode_error("malformed JSON string, neither array, object, number, string or atom"); | ||
| 891 | } | ||
| 892 | } | ||
| 893 | else{ | ||
| 894 | if ($relaxed and $ch eq '#') { # correctly? | ||
| 895 | pos($text) = $at; | ||
| 896 | $text =~ /\G([^\n]*(?:\r\n|\r|\n|$))/g; | ||
| 897 | $at = pos($text); | ||
| 898 | next_chr; | ||
| 899 | next; | ||
| 900 | } | ||
| 901 | |||
| 902 | last; | ||
| 903 | } | ||
| 904 | } | ||
| 905 | } | ||
| 906 | |||
| 907 | |||
| 908 | sub array { | ||
| 909 | my $a = $_[0] || []; # you can use this code to use another array ref object. | ||
| 910 | |||
| 911 | decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)') | ||
| 912 | if (++$depth > $max_depth); | ||
| 913 | |||
| 914 | next_chr(); | ||
| 915 | white(); | ||
| 916 | |||
| 917 | if(defined $ch and $ch eq ']'){ | ||
| 918 | --$depth; | ||
| 919 | next_chr(); | ||
| 920 | return $a; | ||
| 921 | } | ||
| 922 | else { | ||
| 923 | while(defined($ch)){ | ||
| 924 | push @$a, value(); | ||
| 925 | |||
| 926 | white(); | ||
| 927 | |||
| 928 | if (!defined $ch) { | ||
| 929 | last; | ||
| 930 | } | ||
| 931 | |||
| 932 | if($ch eq ']'){ | ||
| 933 | --$depth; | ||
| 934 | next_chr(); | ||
| 935 | return $a; | ||
| 936 | } | ||
| 937 | |||
| 938 | if($ch ne ','){ | ||
| 939 | last; | ||
| 940 | } | ||
| 941 | |||
| 942 | next_chr(); | ||
| 943 | white(); | ||
| 944 | |||
| 945 | if ($relaxed and $ch eq ']') { | ||
| 946 | --$depth; | ||
| 947 | next_chr(); | ||
| 948 | return $a; | ||
| 949 | } | ||
| 950 | |||
| 951 | } | ||
| 952 | } | ||
| 953 | |||
| 954 | $at-- if defined $ch and $ch ne ''; | ||
| 955 | decode_error(", or ] expected while parsing array"); | ||
| 956 | } | ||
| 957 | |||
| 958 | |||
| 959 | sub object { | ||
| 960 | my $o = $_[0] || {}; # you can use this code to use another hash ref object. | ||
| 961 | my $k; | ||
| 962 | |||
| 963 | decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)') | ||
| 964 | if (++$depth > $max_depth); | ||
| 965 | next_chr(); | ||
| 966 | white(); | ||
| 967 | |||
| 968 | if(defined $ch and $ch eq '}'){ | ||
| 969 | --$depth; | ||
| 970 | next_chr(); | ||
| 971 | if ($F_HOOK) { | ||
| 972 | return _json_object_hook($o); | ||
| 973 | } | ||
| 974 | return $o; | ||
| 975 | } | ||
| 976 | else { | ||
| 977 | while (defined $ch) { | ||
| 978 | $k = ($allow_barekey and $ch ne '"' and $ch ne "'") ? bareKey() : string(); | ||
| 979 | white(); | ||
| 980 | |||
| 981 | if(!defined $ch or $ch ne ':'){ | ||
| 982 | $at--; | ||
| 983 | decode_error("':' expected"); | ||
| 984 | } | ||
| 985 | |||
| 986 | next_chr(); | ||
| 987 | $o->{$k} = value(); | ||
| 988 | white(); | ||
| 989 | |||
| 990 | last if (!defined $ch); | ||
| 991 | |||
| 992 | if($ch eq '}'){ | ||
| 993 | --$depth; | ||
| 994 | next_chr(); | ||
| 995 | if ($F_HOOK) { | ||
| 996 | return _json_object_hook($o); | ||
| 997 | } | ||
| 998 | return $o; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | if($ch ne ','){ | ||
| 1002 | last; | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | next_chr(); | ||
| 1006 | white(); | ||
| 1007 | |||
| 1008 | if ($relaxed and $ch eq '}') { | ||
| 1009 | --$depth; | ||
| 1010 | next_chr(); | ||
| 1011 | if ($F_HOOK) { | ||
| 1012 | return _json_object_hook($o); | ||
| 1013 | } | ||
| 1014 | return $o; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | } | ||
| 1018 | |||
| 1019 | } | ||
| 1020 | |||
| 1021 | $at-- if defined $ch and $ch ne ''; | ||
| 1022 | decode_error(", or } expected while parsing object/hash"); | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | |||
| 1026 | sub bareKey { # doesn't strictly follow Standard ECMA-262 3rd Edition | ||
| 1027 | my $key; | ||
| 1028 | while($ch =~ /[^\x00-\x23\x25-\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\x7F]/){ | ||
| 1029 | $key .= $ch; | ||
| 1030 | next_chr(); | ||
| 1031 | } | ||
| 1032 | return $key; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | |||
| 1036 | sub word { | ||
| 1037 | my $word = substr($text,$at-1,4); | ||
| 1038 | |||
| 1039 | if($word eq 'true'){ | ||
| 1040 | $at += 3; | ||
| 1041 | next_chr; | ||
| 1042 | return $JSON::PP::true; | ||
| 1043 | } | ||
| 1044 | elsif($word eq 'null'){ | ||
| 1045 | $at += 3; | ||
| 1046 | next_chr; | ||
| 1047 | return undef; | ||
| 1048 | } | ||
| 1049 | elsif($word eq 'fals'){ | ||
| 1050 | $at += 3; | ||
| 1051 | if(substr($text,$at,1) eq 'e'){ | ||
| 1052 | $at++; | ||
| 1053 | next_chr; | ||
| 1054 | return $JSON::PP::false; | ||
| 1055 | } | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | $at--; # for decode_error report | ||
| 1059 | |||
| 1060 | decode_error("'null' expected") if ($word =~ /^n/); | ||
| 1061 | decode_error("'true' expected") if ($word =~ /^t/); | ||
| 1062 | decode_error("'false' expected") if ($word =~ /^f/); | ||
| 1063 | decode_error("malformed JSON string, neither array, object, number, string or atom"); | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | |||
| 1067 | sub number { | ||
| 1068 | my $n = ''; | ||
| 1069 | my $v; | ||
| 1070 | my $is_dec; | ||
| 1071 | my $is_exp; | ||
| 1072 | |||
| 1073 | if($ch eq '-'){ | ||
| 1074 | $n = '-'; | ||
| 1075 | next_chr; | ||
| 1076 | if (!defined $ch or $ch !~ /\d/) { | ||
| 1077 | decode_error("malformed number (no digits after initial minus)"); | ||
| 1078 | } | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | # According to RFC4627, hex or oct digits are invalid. | ||
| 1082 | if($ch eq '0'){ | ||
| 1083 | my $peek = substr($text,$at,1); | ||
| 1084 | if($peek =~ /^[0-9a-dfA-DF]/){ # e may be valid (exponential) | ||
| 1085 | decode_error("malformed number (leading zero must not be followed by another digit)"); | ||
| 1086 | } | ||
| 1087 | $n .= $ch; | ||
| 1088 | next_chr; | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | while(defined $ch and $ch =~ /\d/){ | ||
| 1092 | $n .= $ch; | ||
| 1093 | next_chr; | ||
| 1094 | } | ||
| 1095 | |||
| 1096 | if(defined $ch and $ch eq '.'){ | ||
| 1097 | $n .= '.'; | ||
| 1098 | $is_dec = 1; | ||
| 1099 | |||
| 1100 | next_chr; | ||
| 1101 | if (!defined $ch or $ch !~ /\d/) { | ||
| 1102 | decode_error("malformed number (no digits after decimal point)"); | ||
| 1103 | } | ||
| 1104 | else { | ||
| 1105 | $n .= $ch; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | while(defined(next_chr) and $ch =~ /\d/){ | ||
| 1109 | $n .= $ch; | ||
| 1110 | } | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | if(defined $ch and ($ch eq 'e' or $ch eq 'E')){ | ||
| 1114 | $n .= $ch; | ||
| 1115 | $is_exp = 1; | ||
| 1116 | next_chr; | ||
| 1117 | |||
| 1118 | if(defined($ch) and ($ch eq '+' or $ch eq '-')){ | ||
| 1119 | $n .= $ch; | ||
| 1120 | next_chr; | ||
| 1121 | if (!defined $ch or $ch =~ /\D/) { | ||
| 1122 | decode_error("malformed number (no digits after exp sign)"); | ||
| 1123 | } | ||
| 1124 | $n .= $ch; | ||
| 1125 | } | ||
| 1126 | elsif(defined($ch) and $ch =~ /\d/){ | ||
| 1127 | $n .= $ch; | ||
| 1128 | } | ||
| 1129 | else { | ||
| 1130 | decode_error("malformed number (no digits after exp sign)"); | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | while(defined(next_chr) and $ch =~ /\d/){ | ||
| 1134 | $n .= $ch; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | } | ||
| 1138 | |||
| 1139 | $v .= $n; | ||
| 1140 | |||
| 1141 | if ($is_dec or $is_exp) { | ||
| 1142 | if ($allow_bignum) { | ||
| 1143 | require Math::BigFloat; | ||
| 1144 | return Math::BigFloat->new($v); | ||
| 1145 | } | ||
| 1146 | } else { | ||
| 1147 | if (length $v > $max_intsize) { | ||
| 1148 | if ($allow_bignum) { # from Adam Sussman | ||
| 1149 | require Math::BigInt; | ||
| 1150 | return Math::BigInt->new($v); | ||
| 1151 | } | ||
| 1152 | else { | ||
| 1153 | return "$v"; | ||
| 1154 | } | ||
| 1155 | } | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | return $is_dec ? $v/1.0 : 0+$v; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | |||
| 1162 | sub is_valid_utf8 { | ||
| 1163 | |||
| 1164 | $utf8_len = $_[0] =~ /[\x00-\x7F]/ ? 1 | ||
| 1165 | : $_[0] =~ /[\xC2-\xDF]/ ? 2 | ||
| 1166 | : $_[0] =~ /[\xE0-\xEF]/ ? 3 | ||
| 1167 | : $_[0] =~ /[\xF0-\xF4]/ ? 4 | ||
| 1168 | : 0 | ||
| 1169 | ; | ||
| 1170 | |||
| 1171 | return unless $utf8_len; | ||
| 1172 | |||
| 1173 | my $is_valid_utf8 = substr($text, $at - 1, $utf8_len); | ||
| 1174 | |||
| 1175 | return ( $is_valid_utf8 =~ /^(?: | ||
| 1176 | [\x00-\x7F] | ||
| 1177 | |[\xC2-\xDF][\x80-\xBF] | ||
| 1178 | |[\xE0][\xA0-\xBF][\x80-\xBF] | ||
| 1179 | |[\xE1-\xEC][\x80-\xBF][\x80-\xBF] | ||
| 1180 | |[\xED][\x80-\x9F][\x80-\xBF] | ||
| 1181 | |[\xEE-\xEF][\x80-\xBF][\x80-\xBF] | ||
| 1182 | |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF] | ||
| 1183 | |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF] | ||
| 1184 | |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF] | ||
| 1185 | )$/x ) ? $is_valid_utf8 : ''; | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | |||
| 1189 | sub decode_error { | ||
| 1190 | my $error = shift; | ||
| 1191 | my $no_rep = shift; | ||
| 1192 | my $str = defined $text ? substr($text, $at) : ''; | ||
| 1193 | my $mess = ''; | ||
| 1194 | my $type = 'U*'; | ||
| 1195 | |||
| 1196 | if ( OLD_PERL ) { | ||
| 1197 | my $type = $] < 5.006 ? 'C*' | ||
| 1198 | : utf8::is_utf8( $str ) ? 'U*' # 5.6 | ||
| 1199 | : 'C*' | ||
| 1200 | ; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | for my $c ( unpack( $type, $str ) ) { # emulate pv_uni_display() ? | ||
| 1204 | $mess .= $c == 0x07 ? '\a' | ||
| 1205 | : $c == 0x09 ? '\t' | ||
| 1206 | : $c == 0x0a ? '\n' | ||
| 1207 | : $c == 0x0d ? '\r' | ||
| 1208 | : $c == 0x0c ? '\f' | ||
| 1209 | : $c < 0x20 ? sprintf('\x{%x}', $c) | ||
| 1210 | : $c == 0x5c ? '\\\\' | ||
| 1211 | : $c < 0x80 ? chr($c) | ||
| 1212 | : sprintf('\x{%x}', $c) | ||
| 1213 | ; | ||
| 1214 | if ( length $mess >= 20 ) { | ||
| 1215 | $mess .= '...'; | ||
| 1216 | last; | ||
| 1217 | } | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | unless ( length $mess ) { | ||
| 1221 | $mess = '(end of string)'; | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | Carp::croak ( | ||
| 1225 | $no_rep ? "$error" : "$error, at character offset $at (before \"$mess\")" | ||
| 1226 | ); | ||
| 1227 | |||
| 1228 | } | ||
| 1229 | |||
| 1230 | |||
| 1231 | sub _json_object_hook { | ||
| 1232 | my $o = $_[0]; | ||
| 1233 | my @ks = keys %{$o}; | ||
| 1234 | |||
| 1235 | if ( $cb_sk_object and @ks == 1 and exists $cb_sk_object->{ $ks[0] } and ref $cb_sk_object->{ $ks[0] } ) { | ||
| 1236 | my @val = $cb_sk_object->{ $ks[0] }->( $o->{$ks[0]} ); | ||
| 1237 | if (@val == 1) { | ||
| 1238 | return $val[0]; | ||
| 1239 | } | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | my @val = $cb_object->($o) if ($cb_object); | ||
| 1243 | if (@val == 0 or @val > 1) { | ||
| 1244 | return $o; | ||
| 1245 | } | ||
| 1246 | else { | ||
| 1247 | return $val[0]; | ||
| 1248 | } | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | |||
| 1252 | sub PP_decode_box { | ||
| 1253 | { | ||
| 1254 | text => $text, | ||
| 1255 | at => $at, | ||
| 1256 | ch => $ch, | ||
| 1257 | len => $len, | ||
| 1258 | depth => $depth, | ||
| 1259 | encoding => $encoding, | ||
| 1260 | is_valid_utf8 => $is_valid_utf8, | ||
| 1261 | }; | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | } # PARSE | ||
| 1265 | |||
| 1266 | |||
| 1267 | sub _decode_surrogates { # from perlunicode | ||
| 1268 | my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00); | ||
| 1269 | my $un = pack('U*', $uni); | ||
| 1270 | utf8::encode( $un ); | ||
| 1271 | return $un; | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | |||
| 1275 | sub _decode_unicode { | ||
| 1276 | my $un = pack('U', hex shift); | ||
| 1277 | utf8::encode( $un ); | ||
| 1278 | return $un; | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | # | ||
| 1282 | # Setup for various Perl versions (the code from JSON::PP58) | ||
| 1283 | # | ||
| 1284 | |||
| 1285 | BEGIN { | ||
| 1286 | |||
| 1287 | unless ( defined &utf8::is_utf8 ) { | ||
| 1288 | require Encode; | ||
| 1289 | *utf8::is_utf8 = *Encode::is_utf8; | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | if ( !OLD_PERL ) { | ||
| 1293 | *JSON::PP::JSON_PP_encode_ascii = \&_encode_ascii; | ||
| 1294 | *JSON::PP::JSON_PP_encode_latin1 = \&_encode_latin1; | ||
| 1295 | *JSON::PP::JSON_PP_decode_surrogates = \&_decode_surrogates; | ||
| 1296 | *JSON::PP::JSON_PP_decode_unicode = \&_decode_unicode; | ||
| 1297 | |||
| 1298 | if ($] < 5.008003) { # join() in 5.8.0 - 5.8.2 is broken. | ||
| 1299 | package JSON::PP; | ||
| 1300 | require subs; | ||
| 1301 | subs->import('join'); | ||
| 1302 | eval q| | ||
| 1303 | sub join { | ||
| 1304 | return '' if (@_ < 2); | ||
| 1305 | my $j = shift; | ||
| 1306 | my $str = shift; | ||
| 1307 | for (@_) { $str .= $j . $_; } | ||
| 1308 | return $str; | ||
| 1309 | } | ||
| 1310 | |; | ||
| 1311 | } | ||
| 1312 | } | ||
| 1313 | |||
| 1314 | |||
| 1315 | sub JSON::PP::incr_parse { | ||
| 1316 | local $Carp::CarpLevel = 1; | ||
| 1317 | ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ ); | ||
| 1318 | } | ||
| 1319 | |||
| 1320 | |||
| 1321 | sub JSON::PP::incr_skip { | ||
| 1322 | ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip; | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | |||
| 1326 | sub JSON::PP::incr_reset { | ||
| 1327 | ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset; | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | eval q{ | ||
| 1331 | sub JSON::PP::incr_text : lvalue { | ||
| 1332 | $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new; | ||
| 1333 | |||
| 1334 | if ( $_[0]->{_incr_parser}->{incr_pos} ) { | ||
| 1335 | Carp::croak("incr_text cannot be called when the incremental parser already started parsing"); | ||
| 1336 | } | ||
| 1337 | $_[0]->{_incr_parser}->{incr_text}; | ||
| 1338 | } | ||
| 1339 | } if ( $] >= 5.006 ); | ||
| 1340 | |||
| 1341 | } # Setup for various Perl versions (the code from JSON::PP58) | ||
| 1342 | |||
| 1343 | |||
| 1344 | ############################### | ||
| 1345 | # Utilities | ||
| 1346 | # | ||
| 1347 | |||
| 1348 | BEGIN { | ||
| 1349 | eval 'require Scalar::Util'; | ||
| 1350 | unless($@){ | ||
| 1351 | *JSON::PP::blessed = \&Scalar::Util::blessed; | ||
| 1352 | *JSON::PP::reftype = \&Scalar::Util::reftype; | ||
| 1353 | *JSON::PP::refaddr = \&Scalar::Util::refaddr; | ||
| 1354 | } | ||
| 1355 | else{ # This code is from Scalar::Util. | ||
| 1356 | # warn $@; | ||
| 1357 | eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }'; | ||
| 1358 | *JSON::PP::blessed = sub { | ||
| 1359 | local($@, $SIG{__DIE__}, $SIG{__WARN__}); | ||
| 1360 | ref($_[0]) ? eval { $_[0]->a_sub_not_likely_to_be_here } : undef; | ||
| 1361 | }; | ||
| 1362 | require B; | ||
| 1363 | my %tmap = qw( | ||
| 1364 | B::NULL SCALAR | ||
| 1365 | B::HV HASH | ||
| 1366 | B::AV ARRAY | ||
| 1367 | B::CV CODE | ||
| 1368 | B::IO IO | ||
| 1369 | B::GV GLOB | ||
| 1370 | B::REGEXP REGEXP | ||
| 1371 | ); | ||
| 1372 | *JSON::PP::reftype = sub { | ||
| 1373 | my $r = shift; | ||
| 1374 | |||
| 1375 | return undef unless length(ref($r)); | ||
| 1376 | |||
| 1377 | my $t = ref(B::svref_2object($r)); | ||
| 1378 | |||
| 1379 | return | ||
| 1380 | exists $tmap{$t} ? $tmap{$t} | ||
| 1381 | : length(ref($$r)) ? 'REF' | ||
| 1382 | : 'SCALAR'; | ||
| 1383 | }; | ||
| 1384 | *JSON::PP::refaddr = sub { | ||
| 1385 | return undef unless length(ref($_[0])); | ||
| 1386 | |||
| 1387 | my $addr; | ||
| 1388 | if(defined(my $pkg = blessed($_[0]))) { | ||
| 1389 | $addr .= bless $_[0], 'Scalar::Util::Fake'; | ||
| 1390 | bless $_[0], $pkg; | ||
| 1391 | } | ||
| 1392 | else { | ||
| 1393 | $addr .= $_[0] | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | $addr =~ /0x(\w+)/; | ||
| 1397 | local $^W; | ||
| 1398 | #no warnings 'portable'; | ||
| 1399 | hex($1); | ||
| 1400 | } | ||
| 1401 | } | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | |||
| 1405 | # shamelessly copied and modified from JSON::XS code. | ||
| 1406 | |||
| 1407 | $JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" }; | ||
| 1408 | $JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" }; | ||
| 1409 | |||
| 1410 | sub is_bool { blessed $_[0] and $_[0]->isa("JSON::PP::Boolean"); } | ||
| 1411 | |||
| 1412 | sub true { $JSON::PP::true } | ||
| 1413 | sub false { $JSON::PP::false } | ||
| 1414 | sub null { undef; } | ||
| 1415 | |||
| 1416 | ############################### | ||
| 1417 | |||
| 1418 | package JSON::PP::IncrParser; | ||
| 1419 | |||
| 1420 | use strict; | ||
| 1421 | |||
| 1422 | use constant INCR_M_WS => 0; # initial whitespace skipping | ||
| 1423 | use constant INCR_M_STR => 1; # inside string | ||
| 1424 | use constant INCR_M_BS => 2; # inside backslash | ||
| 1425 | use constant INCR_M_JSON => 3; # outside anything, count nesting | ||
| 1426 | use constant INCR_M_C0 => 4; | ||
| 1427 | use constant INCR_M_C1 => 5; | ||
| 1428 | |||
| 1429 | $JSON::PP::IncrParser::VERSION = '1.01'; | ||
| 1430 | |||
| 1431 | sub new { | ||
| 1432 | my ( $class ) = @_; | ||
| 1433 | |||
| 1434 | bless { | ||
| 1435 | incr_nest => 0, | ||
| 1436 | incr_text => undef, | ||
| 1437 | incr_pos => 0, | ||
| 1438 | incr_mode => 0, | ||
| 1439 | }, $class; | ||
| 1440 | } | ||
| 1441 | |||
| 1442 | |||
| 1443 | sub incr_parse { | ||
| 1444 | my ( $self, $coder, $text ) = @_; | ||
| 1445 | |||
| 1446 | $self->{incr_text} = '' unless ( defined $self->{incr_text} ); | ||
| 1447 | |||
| 1448 | if ( defined $text ) { | ||
| 1449 | if ( utf8::is_utf8( $text ) and !utf8::is_utf8( $self->{incr_text} ) ) { | ||
| 1450 | utf8::upgrade( $self->{incr_text} ) ; | ||
| 1451 | utf8::decode( $self->{incr_text} ) ; | ||
| 1452 | } | ||
| 1453 | $self->{incr_text} .= $text; | ||
| 1454 | } | ||
| 1455 | |||
| 1456 | if ( defined wantarray ) { | ||
| 1457 | my $max_size = $coder->get_max_size; | ||
| 1458 | my $p = $self->{incr_pos}; | ||
| 1459 | my @ret; | ||
| 1460 | { | ||
| 1461 | do { | ||
| 1462 | unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) { | ||
| 1463 | $self->_incr_parse( $coder ); | ||
| 1464 | |||
| 1465 | if ( $max_size and $self->{incr_pos} > $max_size ) { | ||
| 1466 | Carp::croak("attempted decode of JSON text of $self->{incr_pos} bytes size, but max_size is set to $max_size"); | ||
| 1467 | } | ||
| 1468 | unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) { | ||
| 1469 | # as an optimisation, do not accumulate white space in the incr buffer | ||
| 1470 | if ( $self->{incr_mode} == INCR_M_WS and $self->{incr_pos} ) { | ||
| 1471 | $self->{incr_pos} = 0; | ||
| 1472 | $self->{incr_text} = ''; | ||
| 1473 | } | ||
| 1474 | last; | ||
| 1475 | } | ||
| 1476 | } | ||
| 1477 | |||
| 1478 | my ($obj, $offset) = $coder->PP_decode_json( $self->{incr_text}, 0x00000001 ); | ||
| 1479 | push @ret, $obj; | ||
| 1480 | use bytes; | ||
| 1481 | $self->{incr_text} = substr( $self->{incr_text}, $offset || 0 ); | ||
| 1482 | $self->{incr_pos} = 0; | ||
| 1483 | $self->{incr_nest} = 0; | ||
| 1484 | $self->{incr_mode} = 0; | ||
| 1485 | last unless wantarray; | ||
| 1486 | } while ( wantarray ); | ||
| 1487 | } | ||
| 1488 | |||
| 1489 | if ( wantarray ) { | ||
| 1490 | return @ret; | ||
| 1491 | } | ||
| 1492 | else { # in scalar context | ||
| 1493 | return $ret[0] ? $ret[0] : undef; | ||
| 1494 | } | ||
| 1495 | } | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | |||
| 1499 | sub _incr_parse { | ||
| 1500 | my ($self, $coder) = @_; | ||
| 1501 | my $text = $self->{incr_text}; | ||
| 1502 | my $len = length $text; | ||
| 1503 | my $p = $self->{incr_pos}; | ||
| 1504 | |||
| 1505 | INCR_PARSE: | ||
| 1506 | while ( $len > $p ) { | ||
| 1507 | my $s = substr( $text, $p, 1 ); | ||
| 1508 | last INCR_PARSE unless defined $s; | ||
| 1509 | my $mode = $self->{incr_mode}; | ||
| 1510 | |||
| 1511 | if ( $mode == INCR_M_WS ) { | ||
| 1512 | while ( $len > $p ) { | ||
| 1513 | $s = substr( $text, $p, 1 ); | ||
| 1514 | last INCR_PARSE unless defined $s; | ||
| 1515 | if ( ord($s) > 0x20 ) { | ||
| 1516 | if ( $s eq '#' ) { | ||
| 1517 | $self->{incr_mode} = INCR_M_C0; | ||
| 1518 | redo INCR_PARSE; | ||
| 1519 | } else { | ||
| 1520 | $self->{incr_mode} = INCR_M_JSON; | ||
| 1521 | redo INCR_PARSE; | ||
| 1522 | } | ||
| 1523 | } | ||
| 1524 | $p++; | ||
| 1525 | } | ||
| 1526 | } elsif ( $mode == INCR_M_BS ) { | ||
| 1527 | $p++; | ||
| 1528 | $self->{incr_mode} = INCR_M_STR; | ||
| 1529 | redo INCR_PARSE; | ||
| 1530 | } elsif ( $mode == INCR_M_C0 or $mode == INCR_M_C1 ) { | ||
| 1531 | while ( $len > $p ) { | ||
| 1532 | $s = substr( $text, $p, 1 ); | ||
| 1533 | last INCR_PARSE unless defined $s; | ||
| 1534 | if ( $s eq "\n" ) { | ||
| 1535 | $self->{incr_mode} = $self->{incr_mode} == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; | ||
| 1536 | last; | ||
| 1537 | } | ||
| 1538 | $p++; | ||
| 1539 | } | ||
| 1540 | next; | ||
| 1541 | } elsif ( $mode == INCR_M_STR ) { | ||
| 1542 | while ( $len > $p ) { | ||
| 1543 | $s = substr( $text, $p, 1 ); | ||
| 1544 | last INCR_PARSE unless defined $s; | ||
| 1545 | if ( $s eq '"' ) { | ||
| 1546 | $p++; | ||
| 1547 | $self->{incr_mode} = INCR_M_JSON; | ||
| 1548 | |||
| 1549 | last INCR_PARSE unless $self->{incr_nest}; | ||
| 1550 | redo INCR_PARSE; | ||
| 1551 | } | ||
| 1552 | elsif ( $s eq '\\' ) { | ||
| 1553 | $p++; | ||
| 1554 | if ( !defined substr($text, $p, 1) ) { | ||
| 1555 | $self->{incr_mode} = INCR_M_BS; | ||
| 1556 | last INCR_PARSE; | ||
| 1557 | } | ||
| 1558 | } | ||
| 1559 | $p++; | ||
| 1560 | } | ||
| 1561 | } elsif ( $mode == INCR_M_JSON ) { | ||
| 1562 | while ( $len > $p ) { | ||
| 1563 | $s = substr( $text, $p++, 1 ); | ||
| 1564 | if ( $s eq "\x00" ) { | ||
| 1565 | $p--; | ||
| 1566 | last INCR_PARSE; | ||
| 1567 | } elsif ( $s eq "\x09" or $s eq "\x0a" or $s eq "\x0d" or $s eq "\x20" ) { | ||
| 1568 | if ( !$self->{incr_nest} ) { | ||
| 1569 | $p--; # do not eat the whitespace, let the next round do it | ||
| 1570 | last INCR_PARSE; | ||
| 1571 | } | ||
| 1572 | next; | ||
| 1573 | } elsif ( $s eq '"' ) { | ||
| 1574 | $self->{incr_mode} = INCR_M_STR; | ||
| 1575 | redo INCR_PARSE; | ||
| 1576 | } elsif ( $s eq '[' or $s eq '{' ) { | ||
| 1577 | if ( ++$self->{incr_nest} > $coder->get_max_depth ) { | ||
| 1578 | Carp::croak('json text or perl structure exceeds maximum nesting level (max_depth set too low?)'); | ||
| 1579 | } | ||
| 1580 | next; | ||
| 1581 | } elsif ( $s eq ']' or $s eq '}' ) { | ||
| 1582 | if ( --$self->{incr_nest} <= 0 ) { | ||
| 1583 | last INCR_PARSE; | ||
| 1584 | } | ||
| 1585 | } elsif ( $s eq '#' ) { | ||
| 1586 | $self->{incr_mode} = INCR_M_C1; | ||
| 1587 | redo INCR_PARSE; | ||
| 1588 | } | ||
| 1589 | } | ||
| 1590 | } | ||
| 1591 | } | ||
| 1592 | |||
| 1593 | $self->{incr_pos} = $p; | ||
| 1594 | $self->{incr_parsing} = $p ? 1 : 0; # for backward compatibility | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | |||
| 1598 | sub incr_text { | ||
| 1599 | if ( $_[0]->{incr_pos} ) { | ||
| 1600 | Carp::croak("incr_text cannot be called when the incremental parser already started parsing"); | ||
| 1601 | } | ||
| 1602 | $_[0]->{incr_text}; | ||
| 1603 | } | ||
| 1604 | |||
| 1605 | |||
| 1606 | sub incr_skip { | ||
| 1607 | my $self = shift; | ||
| 1608 | $self->{incr_text} = substr( $self->{incr_text}, $self->{incr_pos} ); | ||
| 1609 | $self->{incr_pos} = 0; | ||
| 1610 | $self->{incr_mode} = 0; | ||
| 1611 | $self->{incr_nest} = 0; | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | |||
| 1615 | sub incr_reset { | ||
| 1616 | my $self = shift; | ||
| 1617 | $self->{incr_text} = undef; | ||
| 1618 | $self->{incr_pos} = 0; | ||
| 1619 | $self->{incr_mode} = 0; | ||
| 1620 | $self->{incr_nest} = 0; | ||
| 1621 | } | ||
| 1622 | |||
| 1623 | ############################### | ||
| 1624 | |||
| 1625 | |||
| 1626 | 1; | ||
| 1627 | __END__ |