1package JSON::PP; 2 3# JSON-2.0 4 5use 5.005; 6use strict; 7 8use Exporter (); 9BEGIN { @JSON::PP::ISA = ('Exporter') } 10 11use overload (); 12use JSON::PP::Boolean; 13 14use Carp (); 15#use Devel::Peek; 16 17$JSON::PP::VERSION = '4.07'; 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 24use constant P_ASCII => 0; 25use constant P_LATIN1 => 1; 26use constant P_UTF8 => 2; 27use constant P_INDENT => 3; 28use constant P_CANONICAL => 4; 29use constant P_SPACE_BEFORE => 5; 30use constant P_SPACE_AFTER => 6; 31use constant P_ALLOW_NONREF => 7; 32use constant P_SHRINK => 8; 33use constant P_ALLOW_BLESSED => 9; 34use constant P_CONVERT_BLESSED => 10; 35use constant P_RELAXED => 11; 36 37use constant P_LOOSE => 12; 38use constant P_ALLOW_BIGNUM => 13; 39use constant P_ALLOW_BAREKEY => 14; 40use constant P_ALLOW_SINGLEQUOTE => 15; 41use constant P_ESCAPE_SLASH => 16; 42use constant P_AS_NONBLESSED => 17; 43 44use constant P_ALLOW_UNKNOWN => 18; 45use constant P_ALLOW_TAGS => 19; 46 47use constant OLD_PERL => $] < 5.008 ? 1 : 0; 48use constant USE_B => $ENV{PERL_JSON_PP_USE_B} || 0; 49 50BEGIN { 51 if (USE_B) { 52 require B; 53 } 54} 55 56BEGIN { 57 my @xs_compati_bit_properties = qw( 58 latin1 ascii utf8 indent canonical space_before space_after allow_nonref shrink 59 allow_blessed convert_blessed relaxed allow_unknown 60 allow_tags 61 ); 62 my @pp_bit_properties = qw( 63 allow_singlequote allow_bignum loose 64 allow_barekey escape_slash as_nonblessed 65 ); 66 67 # Perl version check, Unicode handling is enabled? 68 # Helper module sets @JSON::PP::_properties. 69 if ( OLD_PERL ) { 70 my $helper = $] >= 5.006 ? 'JSON::PP::Compat5006' : 'JSON::PP::Compat5005'; 71 eval qq| require $helper |; 72 if ($@) { Carp::croak $@; } 73 } 74 75 for my $name (@xs_compati_bit_properties, @pp_bit_properties) { 76 my $property_id = 'P_' . uc($name); 77 78 eval qq/ 79 sub $name { 80 my \$enable = defined \$_[1] ? \$_[1] : 1; 81 82 if (\$enable) { 83 \$_[0]->{PROPS}->[$property_id] = 1; 84 } 85 else { 86 \$_[0]->{PROPS}->[$property_id] = 0; 87 } 88 89 \$_[0]; 90 } 91 92 sub get_$name { 93 \$_[0]->{PROPS}->[$property_id] ? 1 : ''; 94 } 95 /; 96 } 97 98} 99 100 101 102# Functions 103 104my $JSON; # cache 105 106sub encode_json ($) { # encode 107 ($JSON ||= __PACKAGE__->new->utf8)->encode(@_); 108} 109 110 111sub decode_json { # decode 112 ($JSON ||= __PACKAGE__->new->utf8)->decode(@_); 113} 114 115# Obsoleted 116 117sub to_json($) { 118 Carp::croak ("JSON::PP::to_json has been renamed to encode_json."); 119} 120 121 122sub from_json($) { 123 Carp::croak ("JSON::PP::from_json has been renamed to decode_json."); 124} 125 126 127# Methods 128 129sub new { 130 my $class = shift; 131 my $self = { 132 max_depth => 512, 133 max_size => 0, 134 indent_length => 3, 135 }; 136 137 $self->{PROPS}[P_ALLOW_NONREF] = 1; 138 139 bless $self, $class; 140} 141 142 143sub encode { 144 return $_[0]->PP_encode_json($_[1]); 145} 146 147 148sub decode { 149 return $_[0]->PP_decode_json($_[1], 0x00000000); 150} 151 152 153sub decode_prefix { 154 return $_[0]->PP_decode_json($_[1], 0x00000001); 155} 156 157 158# accessor 159 160 161# pretty printing 162 163sub pretty { 164 my ($self, $v) = @_; 165 my $enable = defined $v ? $v : 1; 166 167 if ($enable) { # indent_length(3) for JSON::XS compatibility 168 $self->indent(1)->space_before(1)->space_after(1); 169 } 170 else { 171 $self->indent(0)->space_before(0)->space_after(0); 172 } 173 174 $self; 175} 176 177# etc 178 179sub max_depth { 180 my $max = defined $_[1] ? $_[1] : 0x80000000; 181 $_[0]->{max_depth} = $max; 182 $_[0]; 183} 184 185 186sub get_max_depth { $_[0]->{max_depth}; } 187 188 189sub max_size { 190 my $max = defined $_[1] ? $_[1] : 0; 191 $_[0]->{max_size} = $max; 192 $_[0]; 193} 194 195 196sub get_max_size { $_[0]->{max_size}; } 197 198sub boolean_values { 199 my $self = shift; 200 if (@_) { 201 my ($false, $true) = @_; 202 $self->{false} = $false; 203 $self->{true} = $true; 204 } else { 205 delete $self->{false}; 206 delete $self->{true}; 207 } 208 return $self; 209} 210 211sub get_boolean_values { 212 my $self = shift; 213 if (exists $self->{true} and exists $self->{false}) { 214 return @$self{qw/false true/}; 215 } 216 return; 217} 218 219sub filter_json_object { 220 if (defined $_[1] and ref $_[1] eq 'CODE') { 221 $_[0]->{cb_object} = $_[1]; 222 } else { 223 delete $_[0]->{cb_object}; 224 } 225 $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0; 226 $_[0]; 227} 228 229sub filter_json_single_key_object { 230 if (@_ == 1 or @_ > 3) { 231 Carp::croak("Usage: JSON::PP::filter_json_single_key_object(self, key, callback = undef)"); 232 } 233 if (defined $_[2] and ref $_[2] eq 'CODE') { 234 $_[0]->{cb_sk_object}->{$_[1]} = $_[2]; 235 } else { 236 delete $_[0]->{cb_sk_object}->{$_[1]}; 237 delete $_[0]->{cb_sk_object} unless %{$_[0]->{cb_sk_object} || {}}; 238 } 239 $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0; 240 $_[0]; 241} 242 243sub indent_length { 244 if (!defined $_[1] or $_[1] > 15 or $_[1] < 0) { 245 Carp::carp "The acceptable range of indent_length() is 0 to 15."; 246 } 247 else { 248 $_[0]->{indent_length} = $_[1]; 249 } 250 $_[0]; 251} 252 253sub get_indent_length { 254 $_[0]->{indent_length}; 255} 256 257sub sort_by { 258 $_[0]->{sort_by} = defined $_[1] ? $_[1] : 1; 259 $_[0]; 260} 261 262sub allow_bigint { 263 Carp::carp("allow_bigint() is obsoleted. use allow_bignum() instead."); 264 $_[0]->allow_bignum; 265} 266 267############################### 268 269### 270### Perl => JSON 271### 272 273 274{ # Convert 275 276 my $max_depth; 277 my $indent; 278 my $ascii; 279 my $latin1; 280 my $utf8; 281 my $space_before; 282 my $space_after; 283 my $canonical; 284 my $allow_blessed; 285 my $convert_blessed; 286 287 my $indent_length; 288 my $escape_slash; 289 my $bignum; 290 my $as_nonblessed; 291 my $allow_tags; 292 293 my $depth; 294 my $indent_count; 295 my $keysort; 296 297 298 sub PP_encode_json { 299 my $self = shift; 300 my $obj = shift; 301 302 $indent_count = 0; 303 $depth = 0; 304 305 my $props = $self->{PROPS}; 306 307 ($ascii, $latin1, $utf8, $indent, $canonical, $space_before, $space_after, $allow_blessed, 308 $convert_blessed, $escape_slash, $bignum, $as_nonblessed, $allow_tags) 309 = @{$props}[P_ASCII .. P_SPACE_AFTER, P_ALLOW_BLESSED, P_CONVERT_BLESSED, 310 P_ESCAPE_SLASH, P_ALLOW_BIGNUM, P_AS_NONBLESSED, P_ALLOW_TAGS]; 311 312 ($max_depth, $indent_length) = @{$self}{qw/max_depth indent_length/}; 313 314 $keysort = $canonical ? sub { $a cmp $b } : undef; 315 316 if ($self->{sort_by}) { 317 $keysort = ref($self->{sort_by}) eq 'CODE' ? $self->{sort_by} 318 : $self->{sort_by} =~ /\D+/ ? $self->{sort_by} 319 : sub { $a cmp $b }; 320 } 321 322 encode_error("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)") 323 if(!ref $obj and !$props->[ P_ALLOW_NONREF ]); 324 325 my $str = $self->object_to_json($obj); 326 327 $str .= "\n" if ( $indent ); # JSON::XS 2.26 compatible 328 329 unless ($ascii or $latin1 or $utf8) { 330 utf8::upgrade($str); 331 } 332 333 if ($props->[ P_SHRINK ]) { 334 utf8::downgrade($str, 1); 335 } 336 337 return $str; 338 } 339 340 341 sub object_to_json { 342 my ($self, $obj) = @_; 343 my $type = ref($obj); 344 345 if($type eq 'HASH'){ 346 return $self->hash_to_json($obj); 347 } 348 elsif($type eq 'ARRAY'){ 349 return $self->array_to_json($obj); 350 } 351 elsif ($type) { # blessed object? 352 if (blessed($obj)) { 353 354 return $self->value_to_json($obj) if ( $obj->isa('JSON::PP::Boolean') ); 355 356 if ( $allow_tags and $obj->can('FREEZE') ) { 357 my $obj_class = ref $obj || $obj; 358 $obj = bless $obj, $obj_class; 359 my @results = $obj->FREEZE('JSON'); 360 if ( @results and ref $results[0] ) { 361 if ( refaddr( $obj ) eq refaddr( $results[0] ) ) { 362 encode_error( sprintf( 363 "%s::FREEZE method returned same object as was passed instead of a new one", 364 ref $obj 365 ) ); 366 } 367 } 368 return '("'.$obj_class.'")['.join(',', @results).']'; 369 } 370 371 if ( $convert_blessed and $obj->can('TO_JSON') ) { 372 my $result = $obj->TO_JSON(); 373 if ( defined $result and ref( $result ) ) { 374 if ( refaddr( $obj ) eq refaddr( $result ) ) { 375 encode_error( sprintf( 376 "%s::TO_JSON method returned same object as was passed instead of a new one", 377 ref $obj 378 ) ); 379 } 380 } 381 382 return $self->object_to_json( $result ); 383 } 384 385 return "$obj" if ( $bignum and _is_bignum($obj) ); 386 387 if ($allow_blessed) { 388 return $self->blessed_to_json($obj) if ($as_nonblessed); # will be removed. 389 return 'null'; 390 } 391 encode_error( sprintf("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", $obj) 392 ); 393 } 394 else { 395 return $self->value_to_json($obj); 396 } 397 } 398 else{ 399 return $self->value_to_json($obj); 400 } 401 } 402 403 404 sub hash_to_json { 405 my ($self, $obj) = @_; 406 my @res; 407 408 encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)") 409 if (++$depth > $max_depth); 410 411 my ($pre, $post) = $indent ? $self->_up_indent() : ('', ''); 412 my $del = ($space_before ? ' ' : '') . ':' . ($space_after ? ' ' : ''); 413 414 for my $k ( _sort( $obj ) ) { 415 if ( OLD_PERL ) { utf8::decode($k) } # key for Perl 5.6 / be optimized 416 push @res, $self->string_to_json( $k ) 417 . $del 418 . ( ref $obj->{$k} ? $self->object_to_json( $obj->{$k} ) : $self->value_to_json( $obj->{$k} ) ); 419 } 420 421 --$depth; 422 $self->_down_indent() if ($indent); 423 424 return '{}' unless @res; 425 return '{' . $pre . join( ",$pre", @res ) . $post . '}'; 426 } 427 428 429 sub array_to_json { 430 my ($self, $obj) = @_; 431 my @res; 432 433 encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)") 434 if (++$depth > $max_depth); 435 436 my ($pre, $post) = $indent ? $self->_up_indent() : ('', ''); 437 438 for my $v (@$obj){ 439 push @res, ref($v) ? $self->object_to_json($v) : $self->value_to_json($v); 440 } 441 442 --$depth; 443 $self->_down_indent() if ($indent); 444 445 return '[]' unless @res; 446 return '[' . $pre . join( ",$pre", @res ) . $post . ']'; 447 } 448 449 sub _looks_like_number { 450 my $value = shift; 451 if (USE_B) { 452 my $b_obj = B::svref_2object(\$value); 453 my $flags = $b_obj->FLAGS; 454 return 1 if $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() ); 455 return; 456 } else { 457 no warnings 'numeric'; 458 # if the utf8 flag is on, it almost certainly started as a string 459 return if utf8::is_utf8($value); 460 # detect numbers 461 # string & "" -> "" 462 # number & "" -> 0 (with warning) 463 # nan and inf can detect as numbers, so check with * 0 464 return unless length((my $dummy = "") & $value); 465 return unless 0 + $value eq $value; 466 return 1 if $value * 0 == 0; 467 return -1; # inf/nan 468 } 469 } 470 471 sub value_to_json { 472 my ($self, $value) = @_; 473 474 return 'null' if(!defined $value); 475 476 my $type = ref($value); 477 478 if (!$type) { 479 if (_looks_like_number($value)) { 480 return $value; 481 } 482 return $self->string_to_json($value); 483 } 484 elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ){ 485 return $$value == 1 ? 'true' : 'false'; 486 } 487 else { 488 if ((overload::StrVal($value) =~ /=(\w+)/)[0]) { 489 return $self->value_to_json("$value"); 490 } 491 492 if ($type eq 'SCALAR' and defined $$value) { 493 return $$value eq '1' ? 'true' 494 : $$value eq '0' ? 'false' 495 : $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ? 'null' 496 : encode_error("cannot encode reference to scalar"); 497 } 498 499 if ( $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ) { 500 return 'null'; 501 } 502 else { 503 if ( $type eq 'SCALAR' or $type eq 'REF' ) { 504 encode_error("cannot encode reference to scalar"); 505 } 506 else { 507 encode_error("encountered $value, but JSON can only represent references to arrays or hashes"); 508 } 509 } 510 511 } 512 } 513 514 515 my %esc = ( 516 "\n" => '\n', 517 "\r" => '\r', 518 "\t" => '\t', 519 "\f" => '\f', 520 "\b" => '\b', 521 "\"" => '\"', 522 "\\" => '\\\\', 523 "\'" => '\\\'', 524 ); 525 526 527 sub string_to_json { 528 my ($self, $arg) = @_; 529 530 $arg =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g; 531 $arg =~ s/\//\\\//g if ($escape_slash); 532 $arg =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1)/eg; 533 534 if ($ascii) { 535 $arg = JSON_PP_encode_ascii($arg); 536 } 537 538 if ($latin1) { 539 $arg = JSON_PP_encode_latin1($arg); 540 } 541 542 if ($utf8) { 543 utf8::encode($arg); 544 } 545 546 return '"' . $arg . '"'; 547 } 548 549 550 sub blessed_to_json { 551 my $reftype = reftype($_[1]) || ''; 552 if ($reftype eq 'HASH') { 553 return $_[0]->hash_to_json($_[1]); 554 } 555 elsif ($reftype eq 'ARRAY') { 556 return $_[0]->array_to_json($_[1]); 557 } 558 else { 559 return 'null'; 560 } 561 } 562 563 564 sub encode_error { 565 my $error = shift; 566 Carp::croak "$error"; 567 } 568 569 570 sub _sort { 571 defined $keysort ? (sort $keysort (keys %{$_[0]})) : keys %{$_[0]}; 572 } 573 574 575 sub _up_indent { 576 my $self = shift; 577 my $space = ' ' x $indent_length; 578 579 my ($pre,$post) = ('',''); 580 581 $post = "\n" . $space x $indent_count; 582 583 $indent_count++; 584 585 $pre = "\n" . $space x $indent_count; 586 587 return ($pre,$post); 588 } 589 590 591 sub _down_indent { $indent_count--; } 592 593 594 sub PP_encode_box { 595 { 596 depth => $depth, 597 indent_count => $indent_count, 598 }; 599 } 600 601} # Convert 602 603 604sub _encode_ascii { 605 join('', 606 map { 607 $_ <= 127 ? 608 chr($_) : 609 $_ <= 65535 ? 610 sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_)); 611 } unpack('U*', $_[0]) 612 ); 613} 614 615 616sub _encode_latin1 { 617 join('', 618 map { 619 $_ <= 255 ? 620 chr($_) : 621 $_ <= 65535 ? 622 sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_)); 623 } unpack('U*', $_[0]) 624 ); 625} 626 627 628sub _encode_surrogates { # from perlunicode 629 my $uni = $_[0] - 0x10000; 630 return ($uni / 0x400 + 0xD800, $uni % 0x400 + 0xDC00); 631} 632 633 634sub _is_bignum { 635 $_[0]->isa('Math::BigInt') or $_[0]->isa('Math::BigFloat'); 636} 637 638 639 640# 641# JSON => Perl 642# 643 644my $max_intsize; 645 646BEGIN { 647 my $checkint = 1111; 648 for my $d (5..64) { 649 $checkint .= 1; 650 my $int = eval qq| $checkint |; 651 if ($int =~ /[eE]/) { 652 $max_intsize = $d - 1; 653 last; 654 } 655 } 656} 657 658{ # PARSE 659 660 my %escapes = ( # by Jeremy Muhlich <jmuhlich [at] bitflood.org> 661 b => "\x8", 662 t => "\x9", 663 n => "\xA", 664 f => "\xC", 665 r => "\xD", 666 '\\' => '\\', 667 '"' => '"', 668 '/' => '/', 669 ); 670 671 my $text; # json data 672 my $at; # offset 673 my $ch; # first character 674 my $len; # text length (changed according to UTF8 or NON UTF8) 675 # INTERNAL 676 my $depth; # nest counter 677 my $encoding; # json text encoding 678 my $is_valid_utf8; # temp variable 679 my $utf8_len; # utf8 byte length 680 # FLAGS 681 my $utf8; # must be utf8 682 my $max_depth; # max nest number of objects and arrays 683 my $max_size; 684 my $relaxed; 685 my $cb_object; 686 my $cb_sk_object; 687 688 my $F_HOOK; 689 690 my $allow_bignum; # using Math::BigInt/BigFloat 691 my $singlequote; # loosely quoting 692 my $loose; # 693 my $allow_barekey; # bareKey 694 my $allow_tags; 695 696 my $alt_true; 697 my $alt_false; 698 699 sub _detect_utf_encoding { 700 my $text = shift; 701 my @octets = unpack('C4', $text); 702 return 'unknown' unless defined $octets[3]; 703 return ( $octets[0] and $octets[1]) ? 'UTF-8' 704 : (!$octets[0] and $octets[1]) ? 'UTF-16BE' 705 : (!$octets[0] and !$octets[1]) ? 'UTF-32BE' 706 : ( $octets[2] ) ? 'UTF-16LE' 707 : (!$octets[2] ) ? 'UTF-32LE' 708 : 'unknown'; 709 } 710 711 sub PP_decode_json { 712 my ($self, $want_offset); 713 714 ($self, $text, $want_offset) = @_; 715 716 ($at, $ch, $depth) = (0, '', 0); 717 718 if ( !defined $text or ref $text ) { 719 decode_error("malformed JSON string, neither array, object, number, string or atom"); 720 } 721 722 my $props = $self->{PROPS}; 723 724 ($utf8, $relaxed, $loose, $allow_bignum, $allow_barekey, $singlequote, $allow_tags) 725 = @{$props}[P_UTF8, P_RELAXED, P_LOOSE .. P_ALLOW_SINGLEQUOTE, P_ALLOW_TAGS]; 726 727 ($alt_true, $alt_false) = @$self{qw/true false/}; 728 729 if ( $utf8 ) { 730 $encoding = _detect_utf_encoding($text); 731 if ($encoding ne 'UTF-8' and $encoding ne 'unknown') { 732 require Encode; 733 Encode::from_to($text, $encoding, 'utf-8'); 734 } else { 735 utf8::downgrade( $text, 1 ) or Carp::croak("Wide character in subroutine entry"); 736 } 737 } 738 else { 739 utf8::upgrade( $text ); 740 utf8::encode( $text ); 741 } 742 743 $len = length $text; 744 745 ($max_depth, $max_size, $cb_object, $cb_sk_object, $F_HOOK) 746 = @{$self}{qw/max_depth max_size cb_object cb_sk_object F_HOOK/}; 747 748 if ($max_size > 1) { 749 use bytes; 750 my $bytes = length $text; 751 decode_error( 752 sprintf("attempted decode of JSON text of %s bytes size, but max_size is set to %s" 753 , $bytes, $max_size), 1 754 ) if ($bytes > $max_size); 755 } 756 757 white(); # remove head white space 758 759 decode_error("malformed JSON string, neither array, object, number, string or atom") unless defined $ch; # Is there a first character for JSON structure? 760 761 my $result = value(); 762 763 if ( !$props->[ P_ALLOW_NONREF ] and !ref $result ) { 764 decode_error( 765 'JSON text must be an object or array (but found number, string, true, false or null,' 766 . ' use allow_nonref to allow this)', 1); 767 } 768 769 Carp::croak('something wrong.') if $len < $at; # we won't arrive here. 770 771 my $consumed = defined $ch ? $at - 1 : $at; # consumed JSON text length 772 773 white(); # remove tail white space 774 775 return ( $result, $consumed ) if $want_offset; # all right if decode_prefix 776 777 decode_error("garbage after JSON object") if defined $ch; 778 779 $result; 780 } 781 782 783 sub next_chr { 784 return $ch = undef if($at >= $len); 785 $ch = substr($text, $at++, 1); 786 } 787 788 789 sub value { 790 white(); 791 return if(!defined $ch); 792 return object() if($ch eq '{'); 793 return array() if($ch eq '['); 794 return tag() if($ch eq '('); 795 return string() if($ch eq '"' or ($singlequote and $ch eq "'")); 796 return number() if($ch =~ /[0-9]/ or $ch eq '-'); 797 return word(); 798 } 799 800 sub string { 801 my $utf16; 802 my $is_utf8; 803 804 ($is_valid_utf8, $utf8_len) = ('', 0); 805 806 my $s = ''; # basically UTF8 flag on 807 808 if($ch eq '"' or ($singlequote and $ch eq "'")){ 809 my $boundChar = $ch; 810 811 OUTER: while( defined(next_chr()) ){ 812 813 if($ch eq $boundChar){ 814 next_chr(); 815 816 if ($utf16) { 817 decode_error("missing low surrogate character in surrogate pair"); 818 } 819 820 utf8::decode($s) if($is_utf8); 821 822 return $s; 823 } 824 elsif($ch eq '\\'){ 825 next_chr(); 826 if(exists $escapes{$ch}){ 827 $s .= $escapes{$ch}; 828 } 829 elsif($ch eq 'u'){ # UNICODE handling 830 my $u = ''; 831 832 for(1..4){ 833 $ch = next_chr(); 834 last OUTER if($ch !~ /[0-9a-fA-F]/); 835 $u .= $ch; 836 } 837 838 # U+D800 - U+DBFF 839 if ($u =~ /^[dD][89abAB][0-9a-fA-F]{2}/) { # UTF-16 high surrogate? 840 $utf16 = $u; 841 } 842 # U+DC00 - U+DFFF 843 elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { # UTF-16 low surrogate? 844 unless (defined $utf16) { 845 decode_error("missing high surrogate character in surrogate pair"); 846 } 847 $is_utf8 = 1; 848 $s .= JSON_PP_decode_surrogates($utf16, $u) || next; 849 $utf16 = undef; 850 } 851 else { 852 if (defined $utf16) { 853 decode_error("surrogate pair expected"); 854 } 855 856 if ( ( my $hex = hex( $u ) ) > 127 ) { 857 $is_utf8 = 1; 858 $s .= JSON_PP_decode_unicode($u) || next; 859 } 860 else { 861 $s .= chr $hex; 862 } 863 } 864 865 } 866 else{ 867 unless ($loose) { 868 $at -= 2; 869 decode_error('illegal backslash escape sequence in string'); 870 } 871 $s .= $ch; 872 } 873 } 874 else{ 875 876 if ( ord $ch > 127 ) { 877 unless( $ch = is_valid_utf8($ch) ) { 878 $at -= 1; 879 decode_error("malformed UTF-8 character in JSON string"); 880 } 881 else { 882 $at += $utf8_len - 1; 883 } 884 885 $is_utf8 = 1; 886 } 887 888 if (!$loose) { 889 if ($ch =~ /[\x00-\x1f\x22\x5c]/) { # '/' ok 890 if (!$relaxed or $ch ne "\t") { 891 $at--; 892 decode_error('invalid character encountered while parsing JSON string'); 893 } 894 } 895 } 896 897 $s .= $ch; 898 } 899 } 900 } 901 902 decode_error("unexpected end of string while parsing JSON string"); 903 } 904 905 906 sub white { 907 while( defined $ch ){ 908 if($ch eq '' or $ch =~ /\A[ \t\r\n]\z/){ 909 next_chr(); 910 } 911 elsif($relaxed and $ch eq '/'){ 912 next_chr(); 913 if(defined $ch and $ch eq '/'){ 914 1 while(defined(next_chr()) and $ch ne "\n" and $ch ne "\r"); 915 } 916 elsif(defined $ch and $ch eq '*'){ 917 next_chr(); 918 while(1){ 919 if(defined $ch){ 920 if($ch eq '*'){ 921 if(defined(next_chr()) and $ch eq '/'){ 922 next_chr(); 923 last; 924 } 925 } 926 else{ 927 next_chr(); 928 } 929 } 930 else{ 931 decode_error("Unterminated comment"); 932 } 933 } 934 next; 935 } 936 else{ 937 $at--; 938 decode_error("malformed JSON string, neither array, object, number, string or atom"); 939 } 940 } 941 else{ 942 if ($relaxed and $ch eq '#') { # correctly? 943 pos($text) = $at; 944 $text =~ /\G([^\n]*(?:\r\n|\r|\n|$))/g; 945 $at = pos($text); 946 next_chr; 947 next; 948 } 949 950 last; 951 } 952 } 953 } 954 955 956 sub array { 957 my $a = $_[0] || []; # you can use this code to use another array ref object. 958 959 decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)') 960 if (++$depth > $max_depth); 961 962 next_chr(); 963 white(); 964 965 if(defined $ch and $ch eq ']'){ 966 --$depth; 967 next_chr(); 968 return $a; 969 } 970 else { 971 while(defined($ch)){ 972 push @$a, value(); 973 974 white(); 975 976 if (!defined $ch) { 977 last; 978 } 979 980 if($ch eq ']'){ 981 --$depth; 982 next_chr(); 983 return $a; 984 } 985 986 if($ch ne ','){ 987 last; 988 } 989 990 next_chr(); 991 white(); 992 993 if ($relaxed and $ch eq ']') { 994 --$depth; 995 next_chr(); 996 return $a; 997 } 998 999 } 1000 } 1001 1002 $at-- if defined $ch and $ch ne ''; 1003 decode_error(", or ] expected while parsing array"); 1004 } 1005 1006 sub tag { 1007 decode_error('malformed JSON string, neither array, object, number, string or atom') unless $allow_tags; 1008 1009 next_chr(); 1010 white(); 1011 1012 my $tag = value(); 1013 return unless defined $tag; 1014 decode_error('malformed JSON string, (tag) must be a string') if ref $tag; 1015 1016 white(); 1017 1018 if (!defined $ch or $ch ne ')') { 1019 decode_error(') expected after tag'); 1020 } 1021 1022 next_chr(); 1023 white(); 1024 1025 my $val = value(); 1026 return unless defined $val; 1027 decode_error('malformed JSON string, tag value must be an array') unless ref $val eq 'ARRAY'; 1028 1029 if (!eval { $tag->can('THAW') }) { 1030 decode_error('cannot decode perl-object (package does not exist)') if $@; 1031 decode_error('cannot decode perl-object (package does not have a THAW method)'); 1032 } 1033 $tag->THAW('JSON', @$val); 1034 } 1035 1036 sub object { 1037 my $o = $_[0] || {}; # you can use this code to use another hash ref object. 1038 my $k; 1039 1040 decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)') 1041 if (++$depth > $max_depth); 1042 next_chr(); 1043 white(); 1044 1045 if(defined $ch and $ch eq '}'){ 1046 --$depth; 1047 next_chr(); 1048 if ($F_HOOK) { 1049 return _json_object_hook($o); 1050 } 1051 return $o; 1052 } 1053 else { 1054 while (defined $ch) { 1055 $k = ($allow_barekey and $ch ne '"' and $ch ne "'") ? bareKey() : string(); 1056 white(); 1057 1058 if(!defined $ch or $ch ne ':'){ 1059 $at--; 1060 decode_error("':' expected"); 1061 } 1062 1063 next_chr(); 1064 $o->{$k} = value(); 1065 white(); 1066 1067 last if (!defined $ch); 1068 1069 if($ch eq '}'){ 1070 --$depth; 1071 next_chr(); 1072 if ($F_HOOK) { 1073 return _json_object_hook($o); 1074 } 1075 return $o; 1076 } 1077 1078 if($ch ne ','){ 1079 last; 1080 } 1081 1082 next_chr(); 1083 white(); 1084 1085 if ($relaxed and $ch eq '}') { 1086 --$depth; 1087 next_chr(); 1088 if ($F_HOOK) { 1089 return _json_object_hook($o); 1090 } 1091 return $o; 1092 } 1093 1094 } 1095 1096 } 1097 1098 $at-- if defined $ch and $ch ne ''; 1099 decode_error(", or } expected while parsing object/hash"); 1100 } 1101 1102 1103 sub bareKey { # doesn't strictly follow Standard ECMA-262 3rd Edition 1104 my $key; 1105 while($ch =~ /[^\x00-\x23\x25-\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\x7F]/){ 1106 $key .= $ch; 1107 next_chr(); 1108 } 1109 return $key; 1110 } 1111 1112 1113 sub word { 1114 my $word = substr($text,$at-1,4); 1115 1116 if($word eq 'true'){ 1117 $at += 3; 1118 next_chr; 1119 return defined $alt_true ? $alt_true : $JSON::PP::true; 1120 } 1121 elsif($word eq 'null'){ 1122 $at += 3; 1123 next_chr; 1124 return undef; 1125 } 1126 elsif($word eq 'fals'){ 1127 $at += 3; 1128 if(substr($text,$at,1) eq 'e'){ 1129 $at++; 1130 next_chr; 1131 return defined $alt_false ? $alt_false : $JSON::PP::false; 1132 } 1133 } 1134 1135 $at--; # for decode_error report 1136 1137 decode_error("'null' expected") if ($word =~ /^n/); 1138 decode_error("'true' expected") if ($word =~ /^t/); 1139 decode_error("'false' expected") if ($word =~ /^f/); 1140 decode_error("malformed JSON string, neither array, object, number, string or atom"); 1141 } 1142 1143 1144 sub number { 1145 my $n = ''; 1146 my $v; 1147 my $is_dec; 1148 my $is_exp; 1149 1150 if($ch eq '-'){ 1151 $n = '-'; 1152 next_chr; 1153 if (!defined $ch or $ch !~ /\d/) { 1154 decode_error("malformed number (no digits after initial minus)"); 1155 } 1156 } 1157 1158 # According to RFC4627, hex or oct digits are invalid. 1159 if($ch eq '0'){ 1160 my $peek = substr($text,$at,1); 1161 if($peek =~ /^[0-9a-dfA-DF]/){ # e may be valid (exponential) 1162 decode_error("malformed number (leading zero must not be followed by another digit)"); 1163 } 1164 $n .= $ch; 1165 next_chr; 1166 } 1167 1168 while(defined $ch and $ch =~ /\d/){ 1169 $n .= $ch; 1170 next_chr; 1171 } 1172 1173 if(defined $ch and $ch eq '.'){ 1174 $n .= '.'; 1175 $is_dec = 1; 1176 1177 next_chr; 1178 if (!defined $ch or $ch !~ /\d/) { 1179 decode_error("malformed number (no digits after decimal point)"); 1180 } 1181 else { 1182 $n .= $ch; 1183 } 1184 1185 while(defined(next_chr) and $ch =~ /\d/){ 1186 $n .= $ch; 1187 } 1188 } 1189 1190 if(defined $ch and ($ch eq 'e' or $ch eq 'E')){ 1191 $n .= $ch; 1192 $is_exp = 1; 1193 next_chr; 1194 1195 if(defined($ch) and ($ch eq '+' or $ch eq '-')){ 1196 $n .= $ch; 1197 next_chr; 1198 if (!defined $ch or $ch =~ /\D/) { 1199 decode_error("malformed number (no digits after exp sign)"); 1200 } 1201 $n .= $ch; 1202 } 1203 elsif(defined($ch) and $ch =~ /\d/){ 1204 $n .= $ch; 1205 } 1206 else { 1207 decode_error("malformed number (no digits after exp sign)"); 1208 } 1209 1210 while(defined(next_chr) and $ch =~ /\d/){ 1211 $n .= $ch; 1212 } 1213 1214 } 1215 1216 $v .= $n; 1217 1218 if ($is_dec or $is_exp) { 1219 if ($allow_bignum) { 1220 require Math::BigFloat; 1221 return Math::BigFloat->new($v); 1222 } 1223 } else { 1224 if (length $v > $max_intsize) { 1225 if ($allow_bignum) { # from Adam Sussman 1226 require Math::BigInt; 1227 return Math::BigInt->new($v); 1228 } 1229 else { 1230 return "$v"; 1231 } 1232 } 1233 } 1234 1235 return $is_dec ? $v/1.0 : 0+$v; 1236 } 1237 1238 1239 sub is_valid_utf8 { 1240 1241 $utf8_len = $_[0] =~ /[\x00-\x7F]/ ? 1 1242 : $_[0] =~ /[\xC2-\xDF]/ ? 2 1243 : $_[0] =~ /[\xE0-\xEF]/ ? 3 1244 : $_[0] =~ /[\xF0-\xF4]/ ? 4 1245 : 0 1246 ; 1247 1248 return unless $utf8_len; 1249 1250 my $is_valid_utf8 = substr($text, $at - 1, $utf8_len); 1251 1252 return ( $is_valid_utf8 =~ /^(?: 1253 [\x00-\x7F] 1254 |[\xC2-\xDF][\x80-\xBF] 1255 |[\xE0][\xA0-\xBF][\x80-\xBF] 1256 |[\xE1-\xEC][\x80-\xBF][\x80-\xBF] 1257 |[\xED][\x80-\x9F][\x80-\xBF] 1258 |[\xEE-\xEF][\x80-\xBF][\x80-\xBF] 1259 |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF] 1260 |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF] 1261 |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF] 1262 )$/x ) ? $is_valid_utf8 : ''; 1263 } 1264 1265 1266 sub decode_error { 1267 my $error = shift; 1268 my $no_rep = shift; 1269 my $str = defined $text ? substr($text, $at) : ''; 1270 my $mess = ''; 1271 my $type = 'U*'; 1272 1273 if ( OLD_PERL ) { 1274 my $type = $] < 5.006 ? 'C*' 1275 : utf8::is_utf8( $str ) ? 'U*' # 5.6 1276 : 'C*' 1277 ; 1278 } 1279 1280 for my $c ( unpack( $type, $str ) ) { # emulate pv_uni_display() ? 1281 $mess .= $c == 0x07 ? '\a' 1282 : $c == 0x09 ? '\t' 1283 : $c == 0x0a ? '\n' 1284 : $c == 0x0d ? '\r' 1285 : $c == 0x0c ? '\f' 1286 : $c < 0x20 ? sprintf('\x{%x}', $c) 1287 : $c == 0x5c ? '\\\\' 1288 : $c < 0x80 ? chr($c) 1289 : sprintf('\x{%x}', $c) 1290 ; 1291 if ( length $mess >= 20 ) { 1292 $mess .= '...'; 1293 last; 1294 } 1295 } 1296 1297 unless ( length $mess ) { 1298 $mess = '(end of string)'; 1299 } 1300 1301 Carp::croak ( 1302 $no_rep ? "$error" : "$error, at character offset $at (before \"$mess\")" 1303 ); 1304 1305 } 1306 1307 1308 sub _json_object_hook { 1309 my $o = $_[0]; 1310 my @ks = keys %{$o}; 1311 1312 if ( $cb_sk_object and @ks == 1 and exists $cb_sk_object->{ $ks[0] } and ref $cb_sk_object->{ $ks[0] } ) { 1313 my @val = $cb_sk_object->{ $ks[0] }->( $o->{$ks[0]} ); 1314 if (@val == 0) { 1315 return $o; 1316 } 1317 elsif (@val == 1) { 1318 return $val[0]; 1319 } 1320 else { 1321 Carp::croak("filter_json_single_key_object callbacks must not return more than one scalar"); 1322 } 1323 } 1324 1325 my @val = $cb_object->($o) if ($cb_object); 1326 if (@val == 0) { 1327 return $o; 1328 } 1329 elsif (@val == 1) { 1330 return $val[0]; 1331 } 1332 else { 1333 Carp::croak("filter_json_object callbacks must not return more than one scalar"); 1334 } 1335 } 1336 1337 1338 sub PP_decode_box { 1339 { 1340 text => $text, 1341 at => $at, 1342 ch => $ch, 1343 len => $len, 1344 depth => $depth, 1345 encoding => $encoding, 1346 is_valid_utf8 => $is_valid_utf8, 1347 }; 1348 } 1349 1350} # PARSE 1351 1352 1353sub _decode_surrogates { # from perlunicode 1354 my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00); 1355 my $un = pack('U*', $uni); 1356 utf8::encode( $un ); 1357 return $un; 1358} 1359 1360 1361sub _decode_unicode { 1362 my $un = pack('U', hex shift); 1363 utf8::encode( $un ); 1364 return $un; 1365} 1366 1367# 1368# Setup for various Perl versions (the code from JSON::PP58) 1369# 1370 1371BEGIN { 1372 1373 unless ( defined &utf8::is_utf8 ) { 1374 require Encode; 1375 *utf8::is_utf8 = *Encode::is_utf8; 1376 } 1377 1378 if ( !OLD_PERL ) { 1379 *JSON::PP::JSON_PP_encode_ascii = \&_encode_ascii; 1380 *JSON::PP::JSON_PP_encode_latin1 = \&_encode_latin1; 1381 *JSON::PP::JSON_PP_decode_surrogates = \&_decode_surrogates; 1382 *JSON::PP::JSON_PP_decode_unicode = \&_decode_unicode; 1383 1384 if ($] < 5.008003) { # join() in 5.8.0 - 5.8.2 is broken. 1385 package JSON::PP; 1386 require subs; 1387 subs->import('join'); 1388 eval q| 1389 sub join { 1390 return '' if (@_ < 2); 1391 my $j = shift; 1392 my $str = shift; 1393 for (@_) { $str .= $j . $_; } 1394 return $str; 1395 } 1396 |; 1397 } 1398 } 1399 1400 1401 sub JSON::PP::incr_parse { 1402 local $Carp::CarpLevel = 1; 1403 ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ ); 1404 } 1405 1406 1407 sub JSON::PP::incr_skip { 1408 ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip; 1409 } 1410 1411 1412 sub JSON::PP::incr_reset { 1413 ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset; 1414 } 1415 1416 eval q{ 1417 sub JSON::PP::incr_text : lvalue { 1418 $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new; 1419 1420 if ( $_[0]->{_incr_parser}->{incr_pos} ) { 1421 Carp::croak("incr_text cannot be called when the incremental parser already started parsing"); 1422 } 1423 $_[0]->{_incr_parser}->{incr_text}; 1424 } 1425 } if ( $] >= 5.006 ); 1426 1427} # Setup for various Perl versions (the code from JSON::PP58) 1428 1429 1430############################### 1431# Utilities 1432# 1433 1434BEGIN { 1435 eval 'require Scalar::Util'; 1436 unless($@){ 1437 *JSON::PP::blessed = \&Scalar::Util::blessed; 1438 *JSON::PP::reftype = \&Scalar::Util::reftype; 1439 *JSON::PP::refaddr = \&Scalar::Util::refaddr; 1440 } 1441 else{ # This code is from Scalar::Util. 1442 # warn $@; 1443 eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }'; 1444 *JSON::PP::blessed = sub { 1445 local($@, $SIG{__DIE__}, $SIG{__WARN__}); 1446 ref($_[0]) ? eval { $_[0]->a_sub_not_likely_to_be_here } : undef; 1447 }; 1448 require B; 1449 my %tmap = qw( 1450 B::NULL SCALAR 1451 B::HV HASH 1452 B::AV ARRAY 1453 B::CV CODE 1454 B::IO IO 1455 B::GV GLOB 1456 B::REGEXP REGEXP 1457 ); 1458 *JSON::PP::reftype = sub { 1459 my $r = shift; 1460 1461 return undef unless length(ref($r)); 1462 1463 my $t = ref(B::svref_2object($r)); 1464 1465 return 1466 exists $tmap{$t} ? $tmap{$t} 1467 : length(ref($$r)) ? 'REF' 1468 : 'SCALAR'; 1469 }; 1470 *JSON::PP::refaddr = sub { 1471 return undef unless length(ref($_[0])); 1472 1473 my $addr; 1474 if(defined(my $pkg = blessed($_[0]))) { 1475 $addr .= bless $_[0], 'Scalar::Util::Fake'; 1476 bless $_[0], $pkg; 1477 } 1478 else { 1479 $addr .= $_[0] 1480 } 1481 1482 $addr =~ /0x(\w+)/; 1483 local $^W; 1484 #no warnings 'portable'; 1485 hex($1); 1486 } 1487 } 1488} 1489 1490 1491# shamelessly copied and modified from JSON::XS code. 1492 1493$JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" }; 1494$JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" }; 1495 1496sub is_bool { blessed $_[0] and ( $_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Types::Serialiser::BooleanBase") or $_[0]->isa("JSON::XS::Boolean") ); } 1497 1498sub true { $JSON::PP::true } 1499sub false { $JSON::PP::false } 1500sub null { undef; } 1501 1502############################### 1503 1504package JSON::PP::IncrParser; 1505 1506use strict; 1507 1508use constant INCR_M_WS => 0; # initial whitespace skipping 1509use constant INCR_M_STR => 1; # inside string 1510use constant INCR_M_BS => 2; # inside backslash 1511use constant INCR_M_JSON => 3; # outside anything, count nesting 1512use constant INCR_M_C0 => 4; 1513use constant INCR_M_C1 => 5; 1514use constant INCR_M_TFN => 6; 1515use constant INCR_M_NUM => 7; 1516 1517$JSON::PP::IncrParser::VERSION = '1.01'; 1518 1519sub new { 1520 my ( $class ) = @_; 1521 1522 bless { 1523 incr_nest => 0, 1524 incr_text => undef, 1525 incr_pos => 0, 1526 incr_mode => 0, 1527 }, $class; 1528} 1529 1530 1531sub incr_parse { 1532 my ( $self, $coder, $text ) = @_; 1533 1534 $self->{incr_text} = '' unless ( defined $self->{incr_text} ); 1535 1536 if ( defined $text ) { 1537 if ( utf8::is_utf8( $text ) and !utf8::is_utf8( $self->{incr_text} ) ) { 1538 utf8::upgrade( $self->{incr_text} ) ; 1539 utf8::decode( $self->{incr_text} ) ; 1540 } 1541 $self->{incr_text} .= $text; 1542 } 1543 1544 if ( defined wantarray ) { 1545 my $max_size = $coder->get_max_size; 1546 my $p = $self->{incr_pos}; 1547 my @ret; 1548 { 1549 do { 1550 unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) { 1551 $self->_incr_parse( $coder ); 1552 1553 if ( $max_size and $self->{incr_pos} > $max_size ) { 1554 Carp::croak("attempted decode of JSON text of $self->{incr_pos} bytes size, but max_size is set to $max_size"); 1555 } 1556 unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) { 1557 # as an optimisation, do not accumulate white space in the incr buffer 1558 if ( $self->{incr_mode} == INCR_M_WS and $self->{incr_pos} ) { 1559 $self->{incr_pos} = 0; 1560 $self->{incr_text} = ''; 1561 } 1562 last; 1563 } 1564 } 1565 1566 unless ( $coder->get_utf8 ) { 1567 utf8::upgrade( $self->{incr_text} ); 1568 utf8::decode( $self->{incr_text} ); 1569 } 1570 1571 my ($obj, $offset) = $coder->PP_decode_json( $self->{incr_text}, 0x00000001 ); 1572 push @ret, $obj; 1573 use bytes; 1574 $self->{incr_text} = substr( $self->{incr_text}, $offset || 0 ); 1575 $self->{incr_pos} = 0; 1576 $self->{incr_nest} = 0; 1577 $self->{incr_mode} = 0; 1578 last unless wantarray; 1579 } while ( wantarray ); 1580 } 1581 1582 if ( wantarray ) { 1583 return @ret; 1584 } 1585 else { # in scalar context 1586 return defined $ret[0] ? $ret[0] : undef; 1587 } 1588 } 1589} 1590 1591 1592sub _incr_parse { 1593 my ($self, $coder) = @_; 1594 my $text = $self->{incr_text}; 1595 my $len = length $text; 1596 my $p = $self->{incr_pos}; 1597 1598INCR_PARSE: 1599 while ( $len > $p ) { 1600 my $s = substr( $text, $p, 1 ); 1601 last INCR_PARSE unless defined $s; 1602 my $mode = $self->{incr_mode}; 1603 1604 if ( $mode == INCR_M_WS ) { 1605 while ( $len > $p ) { 1606 $s = substr( $text, $p, 1 ); 1607 last INCR_PARSE unless defined $s; 1608 if ( ord($s) > 0x20 ) { 1609 if ( $s eq '#' ) { 1610 $self->{incr_mode} = INCR_M_C0; 1611 redo INCR_PARSE; 1612 } else { 1613 $self->{incr_mode} = INCR_M_JSON; 1614 redo INCR_PARSE; 1615 } 1616 } 1617 $p++; 1618 } 1619 } elsif ( $mode == INCR_M_BS ) { 1620 $p++; 1621 $self->{incr_mode} = INCR_M_STR; 1622 redo INCR_PARSE; 1623 } elsif ( $mode == INCR_M_C0 or $mode == INCR_M_C1 ) { 1624 while ( $len > $p ) { 1625 $s = substr( $text, $p, 1 ); 1626 last INCR_PARSE unless defined $s; 1627 if ( $s eq "\n" ) { 1628 $self->{incr_mode} = $self->{incr_mode} == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; 1629 last; 1630 } 1631 $p++; 1632 } 1633 next; 1634 } elsif ( $mode == INCR_M_TFN ) { 1635 while ( $len > $p ) { 1636 $s = substr( $text, $p++, 1 ); 1637 next if defined $s and $s =~ /[rueals]/; 1638 last; 1639 } 1640 $p--; 1641 $self->{incr_mode} = INCR_M_JSON; 1642 1643 last INCR_PARSE unless $self->{incr_nest}; 1644 redo INCR_PARSE; 1645 } elsif ( $mode == INCR_M_NUM ) { 1646 while ( $len > $p ) { 1647 $s = substr( $text, $p++, 1 ); 1648 next if defined $s and $s =~ /[0-9eE.+\-]/; 1649 last; 1650 } 1651 $p--; 1652 $self->{incr_mode} = INCR_M_JSON; 1653 1654 last INCR_PARSE unless $self->{incr_nest}; 1655 redo INCR_PARSE; 1656 } elsif ( $mode == INCR_M_STR ) { 1657 while ( $len > $p ) { 1658 $s = substr( $text, $p, 1 ); 1659 last INCR_PARSE unless defined $s; 1660 if ( $s eq '"' ) { 1661 $p++; 1662 $self->{incr_mode} = INCR_M_JSON; 1663 1664 last INCR_PARSE unless $self->{incr_nest}; 1665 redo INCR_PARSE; 1666 } 1667 elsif ( $s eq '\\' ) { 1668 $p++; 1669 if ( !defined substr($text, $p, 1) ) { 1670 $self->{incr_mode} = INCR_M_BS; 1671 last INCR_PARSE; 1672 } 1673 } 1674 $p++; 1675 } 1676 } elsif ( $mode == INCR_M_JSON ) { 1677 while ( $len > $p ) { 1678 $s = substr( $text, $p++, 1 ); 1679 if ( $s eq "\x00" ) { 1680 $p--; 1681 last INCR_PARSE; 1682 } elsif ( $s eq "\x09" or $s eq "\x0a" or $s eq "\x0d" or $s eq "\x20" ) { 1683 if ( !$self->{incr_nest} ) { 1684 $p--; # do not eat the whitespace, let the next round do it 1685 last INCR_PARSE; 1686 } 1687 next; 1688 } elsif ( $s eq 't' or $s eq 'f' or $s eq 'n' ) { 1689 $self->{incr_mode} = INCR_M_TFN; 1690 redo INCR_PARSE; 1691 } elsif ( $s =~ /^[0-9\-]$/ ) { 1692 $self->{incr_mode} = INCR_M_NUM; 1693 redo INCR_PARSE; 1694 } elsif ( $s eq '"' ) { 1695 $self->{incr_mode} = INCR_M_STR; 1696 redo INCR_PARSE; 1697 } elsif ( $s eq '[' or $s eq '{' ) { 1698 if ( ++$self->{incr_nest} > $coder->get_max_depth ) { 1699 Carp::croak('json text or perl structure exceeds maximum nesting level (max_depth set too low?)'); 1700 } 1701 next; 1702 } elsif ( $s eq ']' or $s eq '}' ) { 1703 if ( --$self->{incr_nest} <= 0 ) { 1704 last INCR_PARSE; 1705 } 1706 } elsif ( $s eq '#' ) { 1707 $self->{incr_mode} = INCR_M_C1; 1708 redo INCR_PARSE; 1709 } 1710 } 1711 } 1712 } 1713 1714 $self->{incr_pos} = $p; 1715 $self->{incr_parsing} = $p ? 1 : 0; # for backward compatibility 1716} 1717 1718 1719sub incr_text { 1720 if ( $_[0]->{incr_pos} ) { 1721 Carp::croak("incr_text cannot be called when the incremental parser already started parsing"); 1722 } 1723 $_[0]->{incr_text}; 1724} 1725 1726 1727sub incr_skip { 1728 my $self = shift; 1729 $self->{incr_text} = substr( $self->{incr_text}, $self->{incr_pos} ); 1730 $self->{incr_pos} = 0; 1731 $self->{incr_mode} = 0; 1732 $self->{incr_nest} = 0; 1733} 1734 1735 1736sub incr_reset { 1737 my $self = shift; 1738 $self->{incr_text} = undef; 1739 $self->{incr_pos} = 0; 1740 $self->{incr_mode} = 0; 1741 $self->{incr_nest} = 0; 1742} 1743 1744############################### 1745 1746 17471; 1748__END__ 1749=pod 1750 1751=head1 NAME 1752 1753JSON::PP - JSON::XS compatible pure-Perl module. 1754 1755=head1 SYNOPSIS 1756 1757 use JSON::PP; 1758 1759 # exported functions, they croak on error 1760 # and expect/generate UTF-8 1761 1762 $utf8_encoded_json_text = encode_json $perl_hash_or_arrayref; 1763 $perl_hash_or_arrayref = decode_json $utf8_encoded_json_text; 1764 1765 # OO-interface 1766 1767 $json = JSON::PP->new->ascii->pretty->allow_nonref; 1768 1769 $pretty_printed_json_text = $json->encode( $perl_scalar ); 1770 $perl_scalar = $json->decode( $json_text ); 1771 1772 # Note that JSON version 2.0 and above will automatically use 1773 # JSON::XS or JSON::PP, so you should be able to just: 1774 1775 use JSON; 1776 1777 1778=head1 DESCRIPTION 1779 1780JSON::PP is a pure perl JSON decoder/encoder, and (almost) compatible to much 1781faster L<JSON::XS> written by Marc Lehmann in C. JSON::PP works as 1782a fallback module when you use L<JSON> module without having 1783installed JSON::XS. 1784 1785Because of this fallback feature of JSON.pm, JSON::PP tries not to 1786be more JavaScript-friendly than JSON::XS (i.e. not to escape extra 1787characters such as U+2028 and U+2029, etc), 1788in order for you not to lose such JavaScript-friendliness silently 1789when you use JSON.pm and install JSON::XS for speed or by accident. 1790If you need JavaScript-friendly RFC7159-compliant pure perl module, 1791try L<JSON::Tiny>, which is derived from L<Mojolicious> web 1792framework and is also smaller and faster than JSON::PP. 1793 1794JSON::PP has been in the Perl core since Perl 5.14, mainly for 1795CPAN toolchain modules to parse META.json. 1796 1797=head1 FUNCTIONAL INTERFACE 1798 1799This section is taken from JSON::XS almost verbatim. C<encode_json> 1800and C<decode_json> are exported by default. 1801 1802=head2 encode_json 1803 1804 $json_text = encode_json $perl_scalar 1805 1806Converts the given Perl data structure to a UTF-8 encoded, binary string 1807(that is, the string contains octets only). Croaks on error. 1808 1809This function call is functionally identical to: 1810 1811 $json_text = JSON::PP->new->utf8->encode($perl_scalar) 1812 1813Except being faster. 1814 1815=head2 decode_json 1816 1817 $perl_scalar = decode_json $json_text 1818 1819The opposite of C<encode_json>: expects an UTF-8 (binary) string and tries 1820to parse that as an UTF-8 encoded JSON text, returning the resulting 1821reference. Croaks on error. 1822 1823This function call is functionally identical to: 1824 1825 $perl_scalar = JSON::PP->new->utf8->decode($json_text) 1826 1827Except being faster. 1828 1829=head2 JSON::PP::is_bool 1830 1831 $is_boolean = JSON::PP::is_bool($scalar) 1832 1833Returns true if the passed scalar represents either JSON::PP::true or 1834JSON::PP::false, two constants that act like C<1> and C<0> respectively 1835and are also used to represent JSON C<true> and C<false> in Perl strings. 1836 1837See L<MAPPING>, below, for more information on how JSON values are mapped to 1838Perl. 1839 1840=head1 OBJECT-ORIENTED INTERFACE 1841 1842This section is also taken from JSON::XS. 1843 1844The object oriented interface lets you configure your own encoding or 1845decoding style, within the limits of supported formats. 1846 1847=head2 new 1848 1849 $json = JSON::PP->new 1850 1851Creates a new JSON::PP object that can be used to de/encode JSON 1852strings. All boolean flags described below are by default I<disabled> 1853(with the exception of C<allow_nonref>, which defaults to I<enabled> since 1854version C<4.0>). 1855 1856The mutators for flags all return the JSON::PP object again and thus calls can 1857be chained: 1858 1859 my $json = JSON::PP->new->utf8->space_after->encode({a => [1,2]}) 1860 => {"a": [1, 2]} 1861 1862=head2 ascii 1863 1864 $json = $json->ascii([$enable]) 1865 1866 $enabled = $json->get_ascii 1867 1868If C<$enable> is true (or missing), then the C<encode> method will not 1869generate characters outside the code range C<0..127> (which is ASCII). Any 1870Unicode characters outside that range will be escaped using either a 1871single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, 1872as per RFC4627. The resulting encoded JSON text can be treated as a native 1873Unicode string, an ascii-encoded, latin1-encoded or UTF-8 encoded string, 1874or any other superset of ASCII. 1875 1876If C<$enable> is false, then the C<encode> method will not escape Unicode 1877characters unless required by the JSON syntax or other flags. This results 1878in a faster and more compact format. 1879 1880See also the section I<ENCODING/CODESET FLAG NOTES> later in this document. 1881 1882The main use for this flag is to produce JSON texts that can be 1883transmitted over a 7-bit channel, as the encoded JSON texts will not 1884contain any 8 bit characters. 1885 1886 JSON::PP->new->ascii(1)->encode([chr 0x10401]) 1887 => ["\ud801\udc01"] 1888 1889=head2 latin1 1890 1891 $json = $json->latin1([$enable]) 1892 1893 $enabled = $json->get_latin1 1894 1895If C<$enable> is true (or missing), then the C<encode> method will encode 1896the resulting JSON text as latin1 (or iso-8859-1), escaping any characters 1897outside the code range C<0..255>. The resulting string can be treated as a 1898latin1-encoded JSON text or a native Unicode string. The C<decode> method 1899will not be affected in any way by this flag, as C<decode> by default 1900expects Unicode, which is a strict superset of latin1. 1901 1902If C<$enable> is false, then the C<encode> method will not escape Unicode 1903characters unless required by the JSON syntax or other flags. 1904 1905See also the section I<ENCODING/CODESET FLAG NOTES> later in this document. 1906 1907The main use for this flag is efficiently encoding binary data as JSON 1908text, as most octets will not be escaped, resulting in a smaller encoded 1909size. The disadvantage is that the resulting JSON text is encoded 1910in latin1 (and must correctly be treated as such when storing and 1911transferring), a rare encoding for JSON. It is therefore most useful when 1912you want to store data structures known to contain binary data efficiently 1913in files or databases, not when talking to other JSON encoders/decoders. 1914 1915 JSON::PP->new->latin1->encode (["\x{89}\x{abc}"] 1916 => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) 1917 1918=head2 utf8 1919 1920 $json = $json->utf8([$enable]) 1921 1922 $enabled = $json->get_utf8 1923 1924If C<$enable> is true (or missing), then the C<encode> method will encode 1925the JSON result into UTF-8, as required by many protocols, while the 1926C<decode> method expects to be handled an UTF-8-encoded string. Please 1927note that UTF-8-encoded strings do not contain any characters outside the 1928range C<0..255>, they are thus useful for bytewise/binary I/O. In future 1929versions, enabling this option might enable autodetection of the UTF-16 1930and UTF-32 encoding families, as described in RFC4627. 1931 1932If C<$enable> is false, then the C<encode> method will return the JSON 1933string as a (non-encoded) Unicode string, while C<decode> expects thus a 1934Unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs 1935to be done yourself, e.g. using the Encode module. 1936 1937See also the section I<ENCODING/CODESET FLAG NOTES> later in this document. 1938 1939Example, output UTF-16BE-encoded JSON: 1940 1941 use Encode; 1942 $jsontext = encode "UTF-16BE", JSON::PP->new->encode ($object); 1943 1944Example, decode UTF-32LE-encoded JSON: 1945 1946 use Encode; 1947 $object = JSON::PP->new->decode (decode "UTF-32LE", $jsontext); 1948 1949=head2 pretty 1950 1951 $json = $json->pretty([$enable]) 1952 1953This enables (or disables) all of the C<indent>, C<space_before> and 1954C<space_after> (and in the future possibly more) flags in one call to 1955generate the most readable (or most compact) form possible. 1956 1957=head2 indent 1958 1959 $json = $json->indent([$enable]) 1960 1961 $enabled = $json->get_indent 1962 1963If C<$enable> is true (or missing), then the C<encode> method will use a multiline 1964format as output, putting every array member or object/hash key-value pair 1965into its own line, indenting them properly. 1966 1967If C<$enable> is false, no newlines or indenting will be produced, and the 1968resulting JSON text is guaranteed not to contain any C<newlines>. 1969 1970This setting has no effect when decoding JSON texts. 1971 1972The default indent space length is three. 1973You can use C<indent_length> to change the length. 1974 1975=head2 space_before 1976 1977 $json = $json->space_before([$enable]) 1978 1979 $enabled = $json->get_space_before 1980 1981If C<$enable> is true (or missing), then the C<encode> method will add an extra 1982optional space before the C<:> separating keys from values in JSON objects. 1983 1984If C<$enable> is false, then the C<encode> method will not add any extra 1985space at those places. 1986 1987This setting has no effect when decoding JSON texts. You will also 1988most likely combine this setting with C<space_after>. 1989 1990Example, space_before enabled, space_after and indent disabled: 1991 1992 {"key" :"value"} 1993 1994=head2 space_after 1995 1996 $json = $json->space_after([$enable]) 1997 1998 $enabled = $json->get_space_after 1999 2000If C<$enable> is true (or missing), then the C<encode> method will add an extra 2001optional space after the C<:> separating keys from values in JSON objects 2002and extra whitespace after the C<,> separating key-value pairs and array 2003members. 2004 2005If C<$enable> is false, then the C<encode> method will not add any extra 2006space at those places. 2007 2008This setting has no effect when decoding JSON texts. 2009 2010Example, space_before and indent disabled, space_after enabled: 2011 2012 {"key": "value"} 2013 2014=head2 relaxed 2015 2016 $json = $json->relaxed([$enable]) 2017 2018 $enabled = $json->get_relaxed 2019 2020If C<$enable> is true (or missing), then C<decode> will accept some 2021extensions to normal JSON syntax (see below). C<encode> will not be 2022affected in anyway. I<Be aware that this option makes you accept invalid 2023JSON texts as if they were valid!>. I suggest only to use this option to 2024parse application-specific files written by humans (configuration files, 2025resource files etc.) 2026 2027If C<$enable> is false (the default), then C<decode> will only accept 2028valid JSON texts. 2029 2030Currently accepted extensions are: 2031 2032=over 4 2033 2034=item * list items can have an end-comma 2035 2036JSON I<separates> array elements and key-value pairs with commas. This 2037can be annoying if you write JSON texts manually and want to be able to 2038quickly append elements, so this extension accepts comma at the end of 2039such items not just between them: 2040 2041 [ 2042 1, 2043 2, <- this comma not normally allowed 2044 ] 2045 { 2046 "k1": "v1", 2047 "k2": "v2", <- this comma not normally allowed 2048 } 2049 2050=item * shell-style '#'-comments 2051 2052Whenever JSON allows whitespace, shell-style comments are additionally 2053allowed. They are terminated by the first carriage-return or line-feed 2054character, after which more white-space and comments are allowed. 2055 2056 [ 2057 1, # this comment not allowed in JSON 2058 # neither this one... 2059 ] 2060 2061=item * C-style multiple-line '/* */'-comments (JSON::PP only) 2062 2063Whenever JSON allows whitespace, C-style multiple-line comments are additionally 2064allowed. Everything between C</*> and C<*/> is a comment, after which 2065more white-space and comments are allowed. 2066 2067 [ 2068 1, /* this comment not allowed in JSON */ 2069 /* neither this one... */ 2070 ] 2071 2072=item * C++-style one-line '//'-comments (JSON::PP only) 2073 2074Whenever JSON allows whitespace, C++-style one-line comments are additionally 2075allowed. They are terminated by the first carriage-return or line-feed 2076character, after which more white-space and comments are allowed. 2077 2078 [ 2079 1, // this comment not allowed in JSON 2080 // neither this one... 2081 ] 2082 2083=item * literal ASCII TAB characters in strings 2084 2085Literal ASCII TAB characters are now allowed in strings (and treated as 2086C<\t>). 2087 2088 [ 2089 "Hello\tWorld", 2090 "Hello<TAB>World", # literal <TAB> would not normally be allowed 2091 ] 2092 2093=back 2094 2095=head2 canonical 2096 2097 $json = $json->canonical([$enable]) 2098 2099 $enabled = $json->get_canonical 2100 2101If C<$enable> is true (or missing), then the C<encode> method will output JSON objects 2102by sorting their keys. This is adding a comparatively high overhead. 2103 2104If C<$enable> is false, then the C<encode> method will output key-value 2105pairs in the order Perl stores them (which will likely change between runs 2106of the same script, and can change even within the same run from 5.18 2107onwards). 2108 2109This option is useful if you want the same data structure to be encoded as 2110the same JSON text (given the same overall settings). If it is disabled, 2111the same hash might be encoded differently even if contains the same data, 2112as key-value pairs have no inherent ordering in Perl. 2113 2114This setting has no effect when decoding JSON texts. 2115 2116This setting has currently no effect on tied hashes. 2117 2118=head2 allow_nonref 2119 2120 $json = $json->allow_nonref([$enable]) 2121 2122 $enabled = $json->get_allow_nonref 2123 2124Unlike other boolean options, this opotion is enabled by default beginning 2125with version C<4.0>. 2126 2127If C<$enable> is true (or missing), then the C<encode> method can convert a 2128non-reference into its corresponding string, number or null JSON value, 2129which is an extension to RFC4627. Likewise, C<decode> will accept those JSON 2130values instead of croaking. 2131 2132If C<$enable> is false, then the C<encode> method will croak if it isn't 2133passed an arrayref or hashref, as JSON texts must either be an object 2134or array. Likewise, C<decode> will croak if given something that is not a 2135JSON object or array. 2136 2137Example, encode a Perl scalar as JSON value without enabled C<allow_nonref>, 2138resulting in an error: 2139 2140 JSON::PP->new->allow_nonref(0)->encode ("Hello, World!") 2141 => hash- or arrayref expected... 2142 2143=head2 allow_unknown 2144 2145 $json = $json->allow_unknown([$enable]) 2146 2147 $enabled = $json->get_allow_unknown 2148 2149If C<$enable> is true (or missing), then C<encode> will I<not> throw an 2150exception when it encounters values it cannot represent in JSON (for 2151example, filehandles) but instead will encode a JSON C<null> value. Note 2152that blessed objects are not included here and are handled separately by 2153c<allow_blessed>. 2154 2155If C<$enable> is false (the default), then C<encode> will throw an 2156exception when it encounters anything it cannot encode as JSON. 2157 2158This option does not affect C<decode> in any way, and it is recommended to 2159leave it off unless you know your communications partner. 2160 2161=head2 allow_blessed 2162 2163 $json = $json->allow_blessed([$enable]) 2164 2165 $enabled = $json->get_allow_blessed 2166 2167See L<OBJECT SERIALISATION> for details. 2168 2169If C<$enable> is true (or missing), then the C<encode> method will not 2170barf when it encounters a blessed reference that it cannot convert 2171otherwise. Instead, a JSON C<null> value is encoded instead of the object. 2172 2173If C<$enable> is false (the default), then C<encode> will throw an 2174exception when it encounters a blessed object that it cannot convert 2175otherwise. 2176 2177This setting has no effect on C<decode>. 2178 2179=head2 convert_blessed 2180 2181 $json = $json->convert_blessed([$enable]) 2182 2183 $enabled = $json->get_convert_blessed 2184 2185See L<OBJECT SERIALISATION> for details. 2186 2187If C<$enable> is true (or missing), then C<encode>, upon encountering a 2188blessed object, will check for the availability of the C<TO_JSON> method 2189on the object's class. If found, it will be called in scalar context and 2190the resulting scalar will be encoded instead of the object. 2191 2192The C<TO_JSON> method may safely call die if it wants. If C<TO_JSON> 2193returns other blessed objects, those will be handled in the same 2194way. C<TO_JSON> must take care of not causing an endless recursion cycle 2195(== crash) in this case. The name of C<TO_JSON> was chosen because other 2196methods called by the Perl core (== not by the user of the object) are 2197usually in upper case letters and to avoid collisions with any C<to_json> 2198function or method. 2199 2200If C<$enable> is false (the default), then C<encode> will not consider 2201this type of conversion. 2202 2203This setting has no effect on C<decode>. 2204 2205=head2 allow_tags 2206 2207 $json = $json->allow_tags([$enable]) 2208 2209 $enabled = $json->get_allow_tags 2210 2211See L<OBJECT SERIALISATION> for details. 2212 2213If C<$enable> is true (or missing), then C<encode>, upon encountering a 2214blessed object, will check for the availability of the C<FREEZE> method on 2215the object's class. If found, it will be used to serialise the object into 2216a nonstandard tagged JSON value (that JSON decoders cannot decode). 2217 2218It also causes C<decode> to parse such tagged JSON values and deserialise 2219them via a call to the C<THAW> method. 2220 2221If C<$enable> is false (the default), then C<encode> will not consider 2222this type of conversion, and tagged JSON values will cause a parse error 2223in C<decode>, as if tags were not part of the grammar. 2224 2225=head2 boolean_values 2226 2227 $json->boolean_values([$false, $true]) 2228 2229 ($false, $true) = $json->get_boolean_values 2230 2231By default, JSON booleans will be decoded as overloaded 2232C<$JSON::PP::false> and C<$JSON::PP::true> objects. 2233 2234With this method you can specify your own boolean values for decoding - 2235on decode, JSON C<false> will be decoded as a copy of C<$false>, and JSON 2236C<true> will be decoded as C<$true> ("copy" here is the same thing as 2237assigning a value to another variable, i.e. C<$copy = $false>). 2238 2239This is useful when you want to pass a decoded data structure directly 2240to other serialisers like YAML, Data::MessagePack and so on. 2241 2242Note that this works only when you C<decode>. You can set incompatible 2243boolean objects (like L<boolean>), but when you C<encode> a data structure 2244with such boolean objects, you still need to enable C<convert_blessed> 2245(and add a C<TO_JSON> method if necessary). 2246 2247Calling this method without any arguments will reset the booleans 2248to their default values. 2249 2250C<get_boolean_values> will return both C<$false> and C<$true> values, or 2251the empty list when they are set to the default. 2252 2253=head2 filter_json_object 2254 2255 $json = $json->filter_json_object([$coderef]) 2256 2257When C<$coderef> is specified, it will be called from C<decode> each 2258time it decodes a JSON object. The only argument is a reference to 2259the newly-created hash. If the code references returns a single scalar 2260(which need not be a reference), this value (or rather a copy of it) is 2261inserted into the deserialised data structure. If it returns an empty 2262list (NOTE: I<not> C<undef>, which is a valid scalar), the original 2263deserialised hash will be inserted. This setting can slow down decoding 2264considerably. 2265 2266When C<$coderef> is omitted or undefined, any existing callback will 2267be removed and C<decode> will not change the deserialised hash in any 2268way. 2269 2270Example, convert all JSON objects into the integer 5: 2271 2272 my $js = JSON::PP->new->filter_json_object(sub { 5 }); 2273 # returns [5] 2274 $js->decode('[{}]'); 2275 # returns 5 2276 $js->decode('{"a":1, "b":2}'); 2277 2278=head2 filter_json_single_key_object 2279 2280 $json = $json->filter_json_single_key_object($key [=> $coderef]) 2281 2282Works remotely similar to C<filter_json_object>, but is only called for 2283JSON objects having a single key named C<$key>. 2284 2285This C<$coderef> is called before the one specified via 2286C<filter_json_object>, if any. It gets passed the single value in the JSON 2287object. If it returns a single value, it will be inserted into the data 2288structure. If it returns nothing (not even C<undef> but the empty list), 2289the callback from C<filter_json_object> will be called next, as if no 2290single-key callback were specified. 2291 2292If C<$coderef> is omitted or undefined, the corresponding callback will be 2293disabled. There can only ever be one callback for a given key. 2294 2295As this callback gets called less often then the C<filter_json_object> 2296one, decoding speed will not usually suffer as much. Therefore, single-key 2297objects make excellent targets to serialise Perl objects into, especially 2298as single-key JSON objects are as close to the type-tagged value concept 2299as JSON gets (it's basically an ID/VALUE tuple). Of course, JSON does not 2300support this in any way, so you need to make sure your data never looks 2301like a serialised Perl hash. 2302 2303Typical names for the single object key are C<__class_whatever__>, or 2304C<$__dollars_are_rarely_used__$> or C<}ugly_brace_placement>, or even 2305things like C<__class_md5sum(classname)__>, to reduce the risk of clashing 2306with real hashes. 2307 2308Example, decode JSON objects of the form C<< { "__widget__" => <id> } >> 2309into the corresponding C<< $WIDGET{<id>} >> object: 2310 2311 # return whatever is in $WIDGET{5}: 2312 JSON::PP 2313 ->new 2314 ->filter_json_single_key_object (__widget__ => sub { 2315 $WIDGET{ $_[0] } 2316 }) 2317 ->decode ('{"__widget__": 5') 2318 2319 # this can be used with a TO_JSON method in some "widget" class 2320 # for serialisation to json: 2321 sub WidgetBase::TO_JSON { 2322 my ($self) = @_; 2323 2324 unless ($self->{id}) { 2325 $self->{id} = ..get..some..id..; 2326 $WIDGET{$self->{id}} = $self; 2327 } 2328 2329 { __widget__ => $self->{id} } 2330 } 2331 2332=head2 shrink 2333 2334 $json = $json->shrink([$enable]) 2335 2336 $enabled = $json->get_shrink 2337 2338If C<$enable> is true (or missing), the string returned by C<encode> will 2339be shrunk (i.e. downgraded if possible). 2340 2341The actual definition of what shrink does might change in future versions, 2342but it will always try to save space at the expense of time. 2343 2344If C<$enable> is false, then JSON::PP does nothing. 2345 2346=head2 max_depth 2347 2348 $json = $json->max_depth([$maximum_nesting_depth]) 2349 2350 $max_depth = $json->get_max_depth 2351 2352Sets the maximum nesting level (default C<512>) accepted while encoding 2353or decoding. If a higher nesting level is detected in JSON text or a Perl 2354data structure, then the encoder and decoder will stop and croak at that 2355point. 2356 2357Nesting level is defined by number of hash- or arrayrefs that the encoder 2358needs to traverse to reach a given point or the number of C<{> or C<[> 2359characters without their matching closing parenthesis crossed to reach a 2360given character in a string. 2361 2362Setting the maximum depth to one disallows any nesting, so that ensures 2363that the object is only a single hash/object or array. 2364 2365If no argument is given, the highest possible setting will be used, which 2366is rarely useful. 2367 2368See L<JSON::XS/SECURITY CONSIDERATIONS> for more info on why this is useful. 2369 2370=head2 max_size 2371 2372 $json = $json->max_size([$maximum_string_size]) 2373 2374 $max_size = $json->get_max_size 2375 2376Set the maximum length a JSON text may have (in bytes) where decoding is 2377being attempted. The default is C<0>, meaning no limit. When C<decode> 2378is called on a string that is longer then this many bytes, it will not 2379attempt to decode the string but throw an exception. This setting has no 2380effect on C<encode> (yet). 2381 2382If no argument is given, the limit check will be deactivated (same as when 2383C<0> is specified). 2384 2385See L<JSON::XS/SECURITY CONSIDERATIONS> for more info on why this is useful. 2386 2387=head2 encode 2388 2389 $json_text = $json->encode($perl_scalar) 2390 2391Converts the given Perl value or data structure to its JSON 2392representation. Croaks on error. 2393 2394=head2 decode 2395 2396 $perl_scalar = $json->decode($json_text) 2397 2398The opposite of C<encode>: expects a JSON text and tries to parse it, 2399returning the resulting simple scalar or reference. Croaks on error. 2400 2401=head2 decode_prefix 2402 2403 ($perl_scalar, $characters) = $json->decode_prefix($json_text) 2404 2405This works like the C<decode> method, but instead of raising an exception 2406when there is trailing garbage after the first JSON object, it will 2407silently stop parsing there and return the number of characters consumed 2408so far. 2409 2410This is useful if your JSON texts are not delimited by an outer protocol 2411and you need to know where the JSON text ends. 2412 2413 JSON::PP->new->decode_prefix ("[1] the tail") 2414 => ([1], 3) 2415 2416=head1 FLAGS FOR JSON::PP ONLY 2417 2418The following flags and properties are for JSON::PP only. If you use 2419any of these, you can't make your application run faster by replacing 2420JSON::PP with JSON::XS. If you need these and also speed boost, 2421you might want to try L<Cpanel::JSON::XS>, a fork of JSON::XS by 2422Reini Urban, which supports some of these (with a different set of 2423incompatibilities). Most of these historical flags are only kept 2424for backward compatibility, and should not be used in a new application. 2425 2426=head2 allow_singlequote 2427 2428 $json = $json->allow_singlequote([$enable]) 2429 $enabled = $json->get_allow_singlequote 2430 2431If C<$enable> is true (or missing), then C<decode> will accept 2432invalid JSON texts that contain strings that begin and end with 2433single quotation marks. C<encode> will not be affected in any way. 2434I<Be aware that this option makes you accept invalid JSON texts 2435as if they were valid!>. I suggest only to use this option to 2436parse application-specific files written by humans (configuration 2437files, resource files etc.) 2438 2439If C<$enable> is false (the default), then C<decode> will only accept 2440valid JSON texts. 2441 2442 $json->allow_singlequote->decode(qq|{"foo":'bar'}|); 2443 $json->allow_singlequote->decode(qq|{'foo':"bar"}|); 2444 $json->allow_singlequote->decode(qq|{'foo':'bar'}|); 2445 2446=head2 allow_barekey 2447 2448 $json = $json->allow_barekey([$enable]) 2449 $enabled = $json->get_allow_barekey 2450 2451If C<$enable> is true (or missing), then C<decode> will accept 2452invalid JSON texts that contain JSON objects whose names don't 2453begin and end with quotation marks. C<encode> will not be affected 2454in any way. I<Be aware that this option makes you accept invalid JSON 2455texts as if they were valid!>. I suggest only to use this option to 2456parse application-specific files written by humans (configuration 2457files, resource files etc.) 2458 2459If C<$enable> is false (the default), then C<decode> will only accept 2460valid JSON texts. 2461 2462 $json->allow_barekey->decode(qq|{foo:"bar"}|); 2463 2464=head2 allow_bignum 2465 2466 $json = $json->allow_bignum([$enable]) 2467 $enabled = $json->get_allow_bignum 2468 2469If C<$enable> is true (or missing), then C<decode> will convert 2470big integers Perl cannot handle as integer into L<Math::BigInt> 2471objects and convert floating numbers into L<Math::BigFloat> 2472objects. C<encode> will convert C<Math::BigInt> and C<Math::BigFloat> 2473objects into JSON numbers. 2474 2475 $json->allow_nonref->allow_bignum; 2476 $bigfloat = $json->decode('2.000000000000000000000000001'); 2477 print $json->encode($bigfloat); 2478 # => 2.000000000000000000000000001 2479 2480See also L<MAPPING>. 2481 2482=head2 loose 2483 2484 $json = $json->loose([$enable]) 2485 $enabled = $json->get_loose 2486 2487If C<$enable> is true (or missing), then C<decode> will accept 2488invalid JSON texts that contain unescaped [\x00-\x1f\x22\x5c] 2489characters. C<encode> will not be affected in any way. 2490I<Be aware that this option makes you accept invalid JSON texts 2491as if they were valid!>. I suggest only to use this option to 2492parse application-specific files written by humans (configuration 2493files, resource files etc.) 2494 2495If C<$enable> is false (the default), then C<decode> will only accept 2496valid JSON texts. 2497 2498 $json->loose->decode(qq|["abc 2499 def"]|); 2500 2501=head2 escape_slash 2502 2503 $json = $json->escape_slash([$enable]) 2504 $enabled = $json->get_escape_slash 2505 2506If C<$enable> is true (or missing), then C<encode> will explicitly 2507escape I<slash> (solidus; C<U+002F>) characters to reduce the risk of 2508XSS (cross site scripting) that may be caused by C<< </script> >> 2509in a JSON text, with the cost of bloating the size of JSON texts. 2510 2511This option may be useful when you embed JSON in HTML, but embedding 2512arbitrary JSON in HTML (by some HTML template toolkit or by string 2513interpolation) is risky in general. You must escape necessary 2514characters in correct order, depending on the context. 2515 2516C<decode> will not be affected in any way. 2517 2518=head2 indent_length 2519 2520 $json = $json->indent_length($number_of_spaces) 2521 $length = $json->get_indent_length 2522 2523This option is only useful when you also enable C<indent> or C<pretty>. 2524 2525JSON::XS indents with three spaces when you C<encode> (if requested 2526by C<indent> or C<pretty>), and the number cannot be changed. 2527JSON::PP allows you to change/get the number of indent spaces with these 2528mutator/accessor. The default number of spaces is three (the same as 2529JSON::XS), and the acceptable range is from C<0> (no indentation; 2530it'd be better to disable indentation by C<indent(0)>) to C<15>. 2531 2532=head2 sort_by 2533 2534 $json = $json->sort_by($code_ref) 2535 $json = $json->sort_by($subroutine_name) 2536 2537If you just want to sort keys (names) in JSON objects when you 2538C<encode>, enable C<canonical> option (see above) that allows you to 2539sort object keys alphabetically. 2540 2541If you do need to sort non-alphabetically for whatever reasons, 2542you can give a code reference (or a subroutine name) to C<sort_by>, 2543then the argument will be passed to Perl's C<sort> built-in function. 2544 2545As the sorting is done in the JSON::PP scope, you usually need to 2546prepend C<JSON::PP::> to the subroutine name, and the special variables 2547C<$a> and C<$b> used in the subrontine used by C<sort> function. 2548 2549Example: 2550 2551 my %ORDER = (id => 1, class => 2, name => 3); 2552 $json->sort_by(sub { 2553 ($ORDER{$JSON::PP::a} // 999) <=> ($ORDER{$JSON::PP::b} // 999) 2554 or $JSON::PP::a cmp $JSON::PP::b 2555 }); 2556 print $json->encode([ 2557 {name => 'CPAN', id => 1, href => 'http://cpan.org'} 2558 ]); 2559 # [{"id":1,"name":"CPAN","href":"http://cpan.org"}] 2560 2561Note that C<sort_by> affects all the plain hashes in the data structure. 2562If you need finer control, C<tie> necessary hashes with a module that 2563implements ordered hash (such as L<Hash::Ordered> and L<Tie::IxHash>). 2564C<canonical> and C<sort_by> don't affect the key order in C<tie>d 2565hashes. 2566 2567 use Hash::Ordered; 2568 tie my %hash, 'Hash::Ordered', 2569 (name => 'CPAN', id => 1, href => 'http://cpan.org'); 2570 print $json->encode([\%hash]); 2571 # [{"name":"CPAN","id":1,"href":"http://cpan.org"}] # order is kept 2572 2573=head1 INCREMENTAL PARSING 2574 2575This section is also taken from JSON::XS. 2576 2577In some cases, there is the need for incremental parsing of JSON 2578texts. While this module always has to keep both JSON text and resulting 2579Perl data structure in memory at one time, it does allow you to parse a 2580JSON stream incrementally. It does so by accumulating text until it has 2581a full JSON object, which it then can decode. This process is similar to 2582using C<decode_prefix> to see if a full JSON object is available, but 2583is much more efficient (and can be implemented with a minimum of method 2584calls). 2585 2586JSON::PP will only attempt to parse the JSON text once it is sure it 2587has enough text to get a decisive result, using a very simple but 2588truly incremental parser. This means that it sometimes won't stop as 2589early as the full parser, for example, it doesn't detect mismatched 2590parentheses. The only thing it guarantees is that it starts decoding as 2591soon as a syntactically valid JSON text has been seen. This means you need 2592to set resource limits (e.g. C<max_size>) to ensure the parser will stop 2593parsing in the presence if syntax errors. 2594 2595The following methods implement this incremental parser. 2596 2597=head2 incr_parse 2598 2599 $json->incr_parse( [$string] ) # void context 2600 2601 $obj_or_undef = $json->incr_parse( [$string] ) # scalar context 2602 2603 @obj_or_empty = $json->incr_parse( [$string] ) # list context 2604 2605This is the central parsing function. It can both append new text and 2606extract objects from the stream accumulated so far (both of these 2607functions are optional). 2608 2609If C<$string> is given, then this string is appended to the already 2610existing JSON fragment stored in the C<$json> object. 2611 2612After that, if the function is called in void context, it will simply 2613return without doing anything further. This can be used to add more text 2614in as many chunks as you want. 2615 2616If the method is called in scalar context, then it will try to extract 2617exactly I<one> JSON object. If that is successful, it will return this 2618object, otherwise it will return C<undef>. If there is a parse error, 2619this method will croak just as C<decode> would do (one can then use 2620C<incr_skip> to skip the erroneous part). This is the most common way of 2621using the method. 2622 2623And finally, in list context, it will try to extract as many objects 2624from the stream as it can find and return them, or the empty list 2625otherwise. For this to work, there must be no separators (other than 2626whitespace) between the JSON objects or arrays, instead they must be 2627concatenated back-to-back. If an error occurs, an exception will be 2628raised as in the scalar context case. Note that in this case, any 2629previously-parsed JSON texts will be lost. 2630 2631Example: Parse some JSON arrays/objects in a given string and return 2632them. 2633 2634 my @objs = JSON::PP->new->incr_parse ("[5][7][1,2]"); 2635 2636=head2 incr_text 2637 2638 $lvalue_string = $json->incr_text 2639 2640This method returns the currently stored JSON fragment as an lvalue, that 2641is, you can manipulate it. This I<only> works when a preceding call to 2642C<incr_parse> in I<scalar context> successfully returned an object. Under 2643all other circumstances you must not call this function (I mean it. 2644although in simple tests it might actually work, it I<will> fail under 2645real world conditions). As a special exception, you can also call this 2646method before having parsed anything. 2647 2648That means you can only use this function to look at or manipulate text 2649before or after complete JSON objects, not while the parser is in the 2650middle of parsing a JSON object. 2651 2652This function is useful in two cases: a) finding the trailing text after a 2653JSON object or b) parsing multiple JSON objects separated by non-JSON text 2654(such as commas). 2655 2656=head2 incr_skip 2657 2658 $json->incr_skip 2659 2660This will reset the state of the incremental parser and will remove 2661the parsed text from the input buffer so far. This is useful after 2662C<incr_parse> died, in which case the input buffer and incremental parser 2663state is left unchanged, to skip the text parsed so far and to reset the 2664parse state. 2665 2666The difference to C<incr_reset> is that only text until the parse error 2667occurred is removed. 2668 2669=head2 incr_reset 2670 2671 $json->incr_reset 2672 2673This completely resets the incremental parser, that is, after this call, 2674it will be as if the parser had never parsed anything. 2675 2676This is useful if you want to repeatedly parse JSON objects and want to 2677ignore any trailing data, which means you have to reset the parser after 2678each successful decode. 2679 2680=head1 MAPPING 2681 2682Most of this section is also taken from JSON::XS. 2683 2684This section describes how JSON::PP maps Perl values to JSON values and 2685vice versa. These mappings are designed to "do the right thing" in most 2686circumstances automatically, preserving round-tripping characteristics 2687(what you put in comes out as something equivalent). 2688 2689For the more enlightened: note that in the following descriptions, 2690lowercase I<perl> refers to the Perl interpreter, while uppercase I<Perl> 2691refers to the abstract Perl language itself. 2692 2693=head2 JSON -> PERL 2694 2695=over 4 2696 2697=item object 2698 2699A JSON object becomes a reference to a hash in Perl. No ordering of object 2700keys is preserved (JSON does not preserve object key ordering itself). 2701 2702=item array 2703 2704A JSON array becomes a reference to an array in Perl. 2705 2706=item string 2707 2708A JSON string becomes a string scalar in Perl - Unicode codepoints in JSON 2709are represented by the same codepoints in the Perl string, so no manual 2710decoding is necessary. 2711 2712=item number 2713 2714A JSON number becomes either an integer, numeric (floating point) or 2715string scalar in perl, depending on its range and any fractional parts. On 2716the Perl level, there is no difference between those as Perl handles all 2717the conversion details, but an integer may take slightly less memory and 2718might represent more values exactly than floating point numbers. 2719 2720If the number consists of digits only, JSON::PP will try to represent 2721it as an integer value. If that fails, it will try to represent it as 2722a numeric (floating point) value if that is possible without loss of 2723precision. Otherwise it will preserve the number as a string value (in 2724which case you lose roundtripping ability, as the JSON number will be 2725re-encoded to a JSON string). 2726 2727Numbers containing a fractional or exponential part will always be 2728represented as numeric (floating point) values, possibly at a loss of 2729precision (in which case you might lose perfect roundtripping ability, but 2730the JSON number will still be re-encoded as a JSON number). 2731 2732Note that precision is not accuracy - binary floating point values cannot 2733represent most decimal fractions exactly, and when converting from and to 2734floating point, JSON::PP only guarantees precision up to but not including 2735the least significant bit. 2736 2737When C<allow_bignum> is enabled, big integer values and any numeric 2738values will be converted into L<Math::BigInt> and L<Math::BigFloat> 2739objects respectively, without becoming string scalars or losing 2740precision. 2741 2742=item true, false 2743 2744These JSON atoms become C<JSON::PP::true> and C<JSON::PP::false>, 2745respectively. They are overloaded to act almost exactly like the numbers 2746C<1> and C<0>. You can check whether a scalar is a JSON boolean by using 2747the C<JSON::PP::is_bool> function. 2748 2749=item null 2750 2751A JSON null atom becomes C<undef> in Perl. 2752 2753=item shell-style comments (C<< # I<text> >>) 2754 2755As a nonstandard extension to the JSON syntax that is enabled by the 2756C<relaxed> setting, shell-style comments are allowed. They can start 2757anywhere outside strings and go till the end of the line. 2758 2759=item tagged values (C<< (I<tag>)I<value> >>). 2760 2761Another nonstandard extension to the JSON syntax, enabled with the 2762C<allow_tags> setting, are tagged values. In this implementation, the 2763I<tag> must be a perl package/class name encoded as a JSON string, and the 2764I<value> must be a JSON array encoding optional constructor arguments. 2765 2766See L<OBJECT SERIALISATION>, below, for details. 2767 2768=back 2769 2770 2771=head2 PERL -> JSON 2772 2773The mapping from Perl to JSON is slightly more difficult, as Perl is a 2774truly typeless language, so we can only guess which JSON type is meant by 2775a Perl value. 2776 2777=over 4 2778 2779=item hash references 2780 2781Perl hash references become JSON objects. As there is no inherent 2782ordering in hash keys (or JSON objects), they will usually be encoded 2783in a pseudo-random order. JSON::PP can optionally sort the hash keys 2784(determined by the I<canonical> flag and/or I<sort_by> property), so 2785the same data structure will serialise to the same JSON text (given 2786same settings and version of JSON::PP), but this incurs a runtime 2787overhead and is only rarely useful, e.g. when you want to compare some 2788JSON text against another for equality. 2789 2790=item array references 2791 2792Perl array references become JSON arrays. 2793 2794=item other references 2795 2796Other unblessed references are generally not allowed and will cause an 2797exception to be thrown, except for references to the integers C<0> and 2798C<1>, which get turned into C<false> and C<true> atoms in JSON. You can 2799also use C<JSON::PP::false> and C<JSON::PP::true> to improve 2800readability. 2801 2802 to_json [\0, JSON::PP::true] # yields [false,true] 2803 2804=item JSON::PP::true, JSON::PP::false 2805 2806These special values become JSON true and JSON false values, 2807respectively. You can also use C<\1> and C<\0> directly if you want. 2808 2809=item JSON::PP::null 2810 2811This special value becomes JSON null. 2812 2813=item blessed objects 2814 2815Blessed objects are not directly representable in JSON, but C<JSON::PP> 2816allows various ways of handling objects. See L<OBJECT SERIALISATION>, 2817below, for details. 2818 2819=item simple scalars 2820 2821Simple Perl scalars (any scalar that is not a reference) are the most 2822difficult objects to encode: JSON::PP will encode undefined scalars as 2823JSON C<null> values, scalars that have last been used in a string context 2824before encoding as JSON strings, and anything else as number value: 2825 2826 # dump as number 2827 encode_json [2] # yields [2] 2828 encode_json [-3.0e17] # yields [-3e+17] 2829 my $value = 5; encode_json [$value] # yields [5] 2830 2831 # used as string, so dump as string 2832 print $value; 2833 encode_json [$value] # yields ["5"] 2834 2835 # undef becomes null 2836 encode_json [undef] # yields [null] 2837 2838You can force the type to be a JSON string by stringifying it: 2839 2840 my $x = 3.1; # some variable containing a number 2841 "$x"; # stringified 2842 $x .= ""; # another, more awkward way to stringify 2843 print $x; # perl does it for you, too, quite often 2844 # (but for older perls) 2845 2846You can force the type to be a JSON number by numifying it: 2847 2848 my $x = "3"; # some variable containing a string 2849 $x += 0; # numify it, ensuring it will be dumped as a number 2850 $x *= 1; # same thing, the choice is yours. 2851 2852You can not currently force the type in other, less obscure, ways. 2853 2854Since version 2.91_01, JSON::PP uses a different number detection logic 2855that converts a scalar that is possible to turn into a number safely. 2856The new logic is slightly faster, and tends to help people who use older 2857perl or who want to encode complicated data structure. However, this may 2858results in a different JSON text from the one JSON::XS encodes (and 2859thus may break tests that compare entire JSON texts). If you do 2860need the previous behavior for compatibility or for finer control, 2861set PERL_JSON_PP_USE_B environmental variable to true before you 2862C<use> JSON::PP (or JSON.pm). 2863 2864Note that numerical precision has the same meaning as under Perl (so 2865binary to decimal conversion follows the same rules as in Perl, which 2866can differ to other languages). Also, your perl interpreter might expose 2867extensions to the floating point numbers of your platform, such as 2868infinities or NaN's - these cannot be represented in JSON, and it is an 2869error to pass those in. 2870 2871JSON::PP (and JSON::XS) trusts what you pass to C<encode> method 2872(or C<encode_json> function) is a clean, validated data structure with 2873values that can be represented as valid JSON values only, because it's 2874not from an external data source (as opposed to JSON texts you pass to 2875C<decode> or C<decode_json>, which JSON::PP considers tainted and 2876doesn't trust). As JSON::PP doesn't know exactly what you and consumers 2877of your JSON texts want the unexpected values to be (you may want to 2878convert them into null, or to stringify them with or without 2879normalisation (string representation of infinities/NaN may vary 2880depending on platforms), or to croak without conversion), you're advised 2881to do what you and your consumers need before you encode, and also not 2882to numify values that may start with values that look like a number 2883(including infinities/NaN), without validating. 2884 2885=back 2886 2887=head2 OBJECT SERIALISATION 2888 2889As JSON cannot directly represent Perl objects, you have to choose between 2890a pure JSON representation (without the ability to deserialise the object 2891automatically again), and a nonstandard extension to the JSON syntax, 2892tagged values. 2893 2894=head3 SERIALISATION 2895 2896What happens when C<JSON::PP> encounters a Perl object depends on the 2897C<allow_blessed>, C<convert_blessed>, C<allow_tags> and C<allow_bignum> 2898settings, which are used in this order: 2899 2900=over 4 2901 2902=item 1. C<allow_tags> is enabled and the object has a C<FREEZE> method. 2903 2904In this case, C<JSON::PP> creates a tagged JSON value, using a nonstandard 2905extension to the JSON syntax. 2906 2907This works by invoking the C<FREEZE> method on the object, with the first 2908argument being the object to serialise, and the second argument being the 2909constant string C<JSON> to distinguish it from other serialisers. 2910 2911The C<FREEZE> method can return any number of values (i.e. zero or 2912more). These values and the paclkage/classname of the object will then be 2913encoded as a tagged JSON value in the following format: 2914 2915 ("classname")[FREEZE return values...] 2916 2917e.g.: 2918 2919 ("URI")["http://www.google.com/"] 2920 ("MyDate")[2013,10,29] 2921 ("ImageData::JPEG")["Z3...VlCg=="] 2922 2923For example, the hypothetical C<My::Object> C<FREEZE> method might use the 2924objects C<type> and C<id> members to encode the object: 2925 2926 sub My::Object::FREEZE { 2927 my ($self, $serialiser) = @_; 2928 2929 ($self->{type}, $self->{id}) 2930 } 2931 2932=item 2. C<convert_blessed> is enabled and the object has a C<TO_JSON> method. 2933 2934In this case, the C<TO_JSON> method of the object is invoked in scalar 2935context. It must return a single scalar that can be directly encoded into 2936JSON. This scalar replaces the object in the JSON text. 2937 2938For example, the following C<TO_JSON> method will convert all L<URI> 2939objects to JSON strings when serialised. The fact that these values 2940originally were L<URI> objects is lost. 2941 2942 sub URI::TO_JSON { 2943 my ($uri) = @_; 2944 $uri->as_string 2945 } 2946 2947=item 3. C<allow_bignum> is enabled and the object is a C<Math::BigInt> or C<Math::BigFloat>. 2948 2949The object will be serialised as a JSON number value. 2950 2951=item 4. C<allow_blessed> is enabled. 2952 2953The object will be serialised as a JSON null value. 2954 2955=item 5. none of the above 2956 2957If none of the settings are enabled or the respective methods are missing, 2958C<JSON::PP> throws an exception. 2959 2960=back 2961 2962=head3 DESERIALISATION 2963 2964For deserialisation there are only two cases to consider: either 2965nonstandard tagging was used, in which case C<allow_tags> decides, 2966or objects cannot be automatically be deserialised, in which 2967case you can use postprocessing or the C<filter_json_object> or 2968C<filter_json_single_key_object> callbacks to get some real objects our of 2969your JSON. 2970 2971This section only considers the tagged value case: a tagged JSON object 2972is encountered during decoding and C<allow_tags> is disabled, a parse 2973error will result (as if tagged values were not part of the grammar). 2974 2975If C<allow_tags> is enabled, C<JSON::PP> will look up the C<THAW> method 2976of the package/classname used during serialisation (it will not attempt 2977to load the package as a Perl module). If there is no such method, the 2978decoding will fail with an error. 2979 2980Otherwise, the C<THAW> method is invoked with the classname as first 2981argument, the constant string C<JSON> as second argument, and all the 2982values from the JSON array (the values originally returned by the 2983C<FREEZE> method) as remaining arguments. 2984 2985The method must then return the object. While technically you can return 2986any Perl scalar, you might have to enable the C<allow_nonref> setting to 2987make that work in all cases, so better return an actual blessed reference. 2988 2989As an example, let's implement a C<THAW> function that regenerates the 2990C<My::Object> from the C<FREEZE> example earlier: 2991 2992 sub My::Object::THAW { 2993 my ($class, $serialiser, $type, $id) = @_; 2994 2995 $class->new (type => $type, id => $id) 2996 } 2997 2998 2999=head1 ENCODING/CODESET FLAG NOTES 3000 3001This section is taken from JSON::XS. 3002 3003The interested reader might have seen a number of flags that signify 3004encodings or codesets - C<utf8>, C<latin1> and C<ascii>. There seems to be 3005some confusion on what these do, so here is a short comparison: 3006 3007C<utf8> controls whether the JSON text created by C<encode> (and expected 3008by C<decode>) is UTF-8 encoded or not, while C<latin1> and C<ascii> only 3009control whether C<encode> escapes character values outside their respective 3010codeset range. Neither of these flags conflict with each other, although 3011some combinations make less sense than others. 3012 3013Care has been taken to make all flags symmetrical with respect to 3014C<encode> and C<decode>, that is, texts encoded with any combination of 3015these flag values will be correctly decoded when the same flags are used 3016- in general, if you use different flag settings while encoding vs. when 3017decoding you likely have a bug somewhere. 3018 3019Below comes a verbose discussion of these flags. Note that a "codeset" is 3020simply an abstract set of character-codepoint pairs, while an encoding 3021takes those codepoint numbers and I<encodes> them, in our case into 3022octets. Unicode is (among other things) a codeset, UTF-8 is an encoding, 3023and ISO-8859-1 (= latin 1) and ASCII are both codesets I<and> encodings at 3024the same time, which can be confusing. 3025 3026=over 4 3027 3028=item C<utf8> flag disabled 3029 3030When C<utf8> is disabled (the default), then C<encode>/C<decode> generate 3031and expect Unicode strings, that is, characters with high ordinal Unicode 3032values (> 255) will be encoded as such characters, and likewise such 3033characters are decoded as-is, no changes to them will be done, except 3034"(re-)interpreting" them as Unicode codepoints or Unicode characters, 3035respectively (to Perl, these are the same thing in strings unless you do 3036funny/weird/dumb stuff). 3037 3038This is useful when you want to do the encoding yourself (e.g. when you 3039want to have UTF-16 encoded JSON texts) or when some other layer does 3040the encoding for you (for example, when printing to a terminal using a 3041filehandle that transparently encodes to UTF-8 you certainly do NOT want 3042to UTF-8 encode your data first and have Perl encode it another time). 3043 3044=item C<utf8> flag enabled 3045 3046If the C<utf8>-flag is enabled, C<encode>/C<decode> will encode all 3047characters using the corresponding UTF-8 multi-byte sequence, and will 3048expect your input strings to be encoded as UTF-8, that is, no "character" 3049of the input string must have any value > 255, as UTF-8 does not allow 3050that. 3051 3052The C<utf8> flag therefore switches between two modes: disabled means you 3053will get a Unicode string in Perl, enabled means you get an UTF-8 encoded 3054octet/binary string in Perl. 3055 3056=item C<latin1> or C<ascii> flags enabled 3057 3058With C<latin1> (or C<ascii>) enabled, C<encode> will escape characters 3059with ordinal values > 255 (> 127 with C<ascii>) and encode the remaining 3060characters as specified by the C<utf8> flag. 3061 3062If C<utf8> is disabled, then the result is also correctly encoded in those 3063character sets (as both are proper subsets of Unicode, meaning that a 3064Unicode string with all character values < 256 is the same thing as a 3065ISO-8859-1 string, and a Unicode string with all character values < 128 is 3066the same thing as an ASCII string in Perl). 3067 3068If C<utf8> is enabled, you still get a correct UTF-8-encoded string, 3069regardless of these flags, just some more characters will be escaped using 3070C<\uXXXX> then before. 3071 3072Note that ISO-8859-1-I<encoded> strings are not compatible with UTF-8 3073encoding, while ASCII-encoded strings are. That is because the ISO-8859-1 3074encoding is NOT a subset of UTF-8 (despite the ISO-8859-1 I<codeset> being 3075a subset of Unicode), while ASCII is. 3076 3077Surprisingly, C<decode> will ignore these flags and so treat all input 3078values as governed by the C<utf8> flag. If it is disabled, this allows you 3079to decode ISO-8859-1- and ASCII-encoded strings, as both strict subsets of 3080Unicode. If it is enabled, you can correctly decode UTF-8 encoded strings. 3081 3082So neither C<latin1> nor C<ascii> are incompatible with the C<utf8> flag - 3083they only govern when the JSON output engine escapes a character or not. 3084 3085The main use for C<latin1> is to relatively efficiently store binary data 3086as JSON, at the expense of breaking compatibility with most JSON decoders. 3087 3088The main use for C<ascii> is to force the output to not contain characters 3089with values > 127, which means you can interpret the resulting string 3090as UTF-8, ISO-8859-1, ASCII, KOI8-R or most about any character set and 30918-bit-encoding, and still get the same data structure back. This is useful 3092when your channel for JSON transfer is not 8-bit clean or the encoding 3093might be mangled in between (e.g. in mail), and works because ASCII is a 3094proper subset of most 8-bit and multibyte encodings in use in the world. 3095 3096=back 3097 3098=head1 BUGS 3099 3100Please report bugs on a specific behavior of this module to RT or GitHub 3101issues (preferred): 3102 3103L<https://github.com/makamaka/JSON-PP/issues> 3104 3105L<https://rt.cpan.org/Public/Dist/Display.html?Queue=JSON-PP> 3106 3107As for new features and requests to change common behaviors, please 3108ask the author of JSON::XS (Marc Lehmann, E<lt>schmorp[at]schmorp.deE<gt>) 3109first, by email (important!), to keep compatibility among JSON.pm backends. 3110 3111Generally speaking, if you need something special for you, you are advised 3112to create a new module, maybe based on L<JSON::Tiny>, which is smaller and 3113written in a much cleaner way than this module. 3114 3115=head1 SEE ALSO 3116 3117The F<json_pp> command line utility for quick experiments. 3118 3119L<JSON::XS>, L<Cpanel::JSON::XS>, and L<JSON::Tiny> for faster alternatives. 3120L<JSON> and L<JSON::MaybeXS> for easy migration. 3121 3122L<JSON::PP::Compat5005> and L<JSON::PP::Compat5006> for older perl users. 3123 3124RFC4627 (L<http://www.ietf.org/rfc/rfc4627.txt>) 3125 3126RFC7159 (L<http://www.ietf.org/rfc/rfc7159.txt>) 3127 3128RFC8259 (L<http://www.ietf.org/rfc/rfc8259.txt>) 3129 3130=head1 AUTHOR 3131 3132Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt> 3133 3134=head1 CURRENT MAINTAINER 3135 3136Kenichi Ishigaki, E<lt>ishigaki[at]cpan.orgE<gt> 3137 3138=head1 COPYRIGHT AND LICENSE 3139 3140Copyright 2007-2016 by Makamaka Hannyaharamitu 3141 3142Most of the documentation is taken from JSON::XS by Marc Lehmann 3143 3144This library is free software; you can redistribute it and/or modify 3145it under the same terms as Perl itself. 3146 3147=cut 3148