1898184e3Ssthenpackage JSON::PP; 2898184e3Ssthen 3898184e3Ssthen# JSON-2.0 4898184e3Ssthen 5*e0680481Safresh1use 5.008; 6898184e3Ssthenuse strict; 79f11ffb7Safresh1 89f11ffb7Safresh1use Exporter (); 9*e0680481Safresh1BEGIN { our @ISA = ('Exporter') } 109f11ffb7Safresh1 11898184e3Ssthenuse overload (); 129f11ffb7Safresh1use JSON::PP::Boolean; 13898184e3Ssthen 14898184e3Ssthenuse Carp (); 15*e0680481Safresh1use Scalar::Util qw(blessed reftype refaddr); 16898184e3Ssthen#use Devel::Peek; 17898184e3Ssthen 18*e0680481Safresh1our $VERSION = '4.16'; 19898184e3Ssthen 20*e0680481Safresh1our @EXPORT = qw(encode_json decode_json from_json to_json); 21898184e3Ssthen 22898184e3Ssthen# instead of hash-access, i tried index-access for speed. 23898184e3Ssthen# but this method is not faster than what i expected. so it will be changed. 24898184e3Ssthen 25898184e3Ssthenuse constant P_ASCII => 0; 26898184e3Ssthenuse constant P_LATIN1 => 1; 27898184e3Ssthenuse constant P_UTF8 => 2; 28898184e3Ssthenuse constant P_INDENT => 3; 29898184e3Ssthenuse constant P_CANONICAL => 4; 30898184e3Ssthenuse constant P_SPACE_BEFORE => 5; 31898184e3Ssthenuse constant P_SPACE_AFTER => 6; 32898184e3Ssthenuse constant P_ALLOW_NONREF => 7; 33898184e3Ssthenuse constant P_SHRINK => 8; 34898184e3Ssthenuse constant P_ALLOW_BLESSED => 9; 35898184e3Ssthenuse constant P_CONVERT_BLESSED => 10; 36898184e3Ssthenuse constant P_RELAXED => 11; 37898184e3Ssthen 38898184e3Ssthenuse constant P_LOOSE => 12; 39898184e3Ssthenuse constant P_ALLOW_BIGNUM => 13; 40898184e3Ssthenuse constant P_ALLOW_BAREKEY => 14; 41898184e3Ssthenuse constant P_ALLOW_SINGLEQUOTE => 15; 42898184e3Ssthenuse constant P_ESCAPE_SLASH => 16; 43898184e3Ssthenuse constant P_AS_NONBLESSED => 17; 44898184e3Ssthen 45898184e3Ssthenuse constant P_ALLOW_UNKNOWN => 18; 46b46d8ef2Safresh1use constant P_ALLOW_TAGS => 19; 47898184e3Ssthen 48b46d8ef2Safresh1use constant USE_B => $ENV{PERL_JSON_PP_USE_B} || 0; 49*e0680481Safresh1use constant CORE_BOOL => defined &builtin::is_bool; 50*e0680481Safresh1 51*e0680481Safresh1my $invalid_char_re; 52*e0680481Safresh1 53*e0680481Safresh1BEGIN { 54*e0680481Safresh1 $invalid_char_re = "["; 55*e0680481Safresh1 for my $i (0 .. 0x01F, 0x22, 0x5c) { # '/' is ok 56*e0680481Safresh1 $invalid_char_re .= quotemeta chr utf8::unicode_to_native($i); 57*e0680481Safresh1 } 58*e0680481Safresh1 59*e0680481Safresh1 $invalid_char_re = qr/$invalid_char_re]/; 60*e0680481Safresh1} 619f11ffb7Safresh1 629f11ffb7Safresh1BEGIN { 639f11ffb7Safresh1 if (USE_B) { 649f11ffb7Safresh1 require B; 659f11ffb7Safresh1 } 669f11ffb7Safresh1} 67898184e3Ssthen 68898184e3SsthenBEGIN { 69898184e3Ssthen my @xs_compati_bit_properties = qw( 70898184e3Ssthen latin1 ascii utf8 indent canonical space_before space_after allow_nonref shrink 71898184e3Ssthen allow_blessed convert_blessed relaxed allow_unknown 72b46d8ef2Safresh1 allow_tags 73898184e3Ssthen ); 74898184e3Ssthen my @pp_bit_properties = qw( 75898184e3Ssthen allow_singlequote allow_bignum loose 76898184e3Ssthen allow_barekey escape_slash as_nonblessed 77898184e3Ssthen ); 78898184e3Ssthen 79898184e3Ssthen for my $name (@xs_compati_bit_properties, @pp_bit_properties) { 809f11ffb7Safresh1 my $property_id = 'P_' . uc($name); 81898184e3Ssthen 82898184e3Ssthen eval qq/ 83898184e3Ssthen sub $name { 84898184e3Ssthen my \$enable = defined \$_[1] ? \$_[1] : 1; 85898184e3Ssthen 86898184e3Ssthen if (\$enable) { 879f11ffb7Safresh1 \$_[0]->{PROPS}->[$property_id] = 1; 88898184e3Ssthen } 89898184e3Ssthen else { 909f11ffb7Safresh1 \$_[0]->{PROPS}->[$property_id] = 0; 91898184e3Ssthen } 92898184e3Ssthen 93898184e3Ssthen \$_[0]; 94898184e3Ssthen } 95898184e3Ssthen 96898184e3Ssthen sub get_$name { 979f11ffb7Safresh1 \$_[0]->{PROPS}->[$property_id] ? 1 : ''; 98898184e3Ssthen } 99898184e3Ssthen /; 100898184e3Ssthen } 101898184e3Ssthen 102898184e3Ssthen} 103898184e3Ssthen 104898184e3Ssthen 105898184e3Ssthen 106898184e3Ssthen# Functions 107898184e3Ssthen 108898184e3Ssthenmy $JSON; # cache 109898184e3Ssthen 110898184e3Ssthensub encode_json ($) { # encode 111898184e3Ssthen ($JSON ||= __PACKAGE__->new->utf8)->encode(@_); 112898184e3Ssthen} 113898184e3Ssthen 114898184e3Ssthen 115898184e3Ssthensub decode_json { # decode 116898184e3Ssthen ($JSON ||= __PACKAGE__->new->utf8)->decode(@_); 117898184e3Ssthen} 118898184e3Ssthen 119898184e3Ssthen# Obsoleted 120898184e3Ssthen 121898184e3Ssthensub to_json($) { 122898184e3Ssthen Carp::croak ("JSON::PP::to_json has been renamed to encode_json."); 123898184e3Ssthen} 124898184e3Ssthen 125898184e3Ssthen 126898184e3Ssthensub from_json($) { 127898184e3Ssthen Carp::croak ("JSON::PP::from_json has been renamed to decode_json."); 128898184e3Ssthen} 129898184e3Ssthen 130898184e3Ssthen 131898184e3Ssthen# Methods 132898184e3Ssthen 133898184e3Ssthensub new { 134898184e3Ssthen my $class = shift; 135898184e3Ssthen my $self = { 136898184e3Ssthen max_depth => 512, 137898184e3Ssthen max_size => 0, 138898184e3Ssthen indent_length => 3, 139898184e3Ssthen }; 140898184e3Ssthen 141b46d8ef2Safresh1 $self->{PROPS}[P_ALLOW_NONREF] = 1; 142b46d8ef2Safresh1 143898184e3Ssthen bless $self, $class; 144898184e3Ssthen} 145898184e3Ssthen 146898184e3Ssthen 147898184e3Ssthensub encode { 148898184e3Ssthen return $_[0]->PP_encode_json($_[1]); 149898184e3Ssthen} 150898184e3Ssthen 151898184e3Ssthen 152898184e3Ssthensub decode { 153898184e3Ssthen return $_[0]->PP_decode_json($_[1], 0x00000000); 154898184e3Ssthen} 155898184e3Ssthen 156898184e3Ssthen 157898184e3Ssthensub decode_prefix { 158898184e3Ssthen return $_[0]->PP_decode_json($_[1], 0x00000001); 159898184e3Ssthen} 160898184e3Ssthen 161898184e3Ssthen 162898184e3Ssthen# accessor 163898184e3Ssthen 164898184e3Ssthen 165898184e3Ssthen# pretty printing 166898184e3Ssthen 167898184e3Ssthensub pretty { 168898184e3Ssthen my ($self, $v) = @_; 169898184e3Ssthen my $enable = defined $v ? $v : 1; 170898184e3Ssthen 171898184e3Ssthen if ($enable) { # indent_length(3) for JSON::XS compatibility 1729f11ffb7Safresh1 $self->indent(1)->space_before(1)->space_after(1); 173898184e3Ssthen } 174898184e3Ssthen else { 175898184e3Ssthen $self->indent(0)->space_before(0)->space_after(0); 176898184e3Ssthen } 177898184e3Ssthen 178898184e3Ssthen $self; 179898184e3Ssthen} 180898184e3Ssthen 181898184e3Ssthen# etc 182898184e3Ssthen 183898184e3Ssthensub max_depth { 184898184e3Ssthen my $max = defined $_[1] ? $_[1] : 0x80000000; 185898184e3Ssthen $_[0]->{max_depth} = $max; 186898184e3Ssthen $_[0]; 187898184e3Ssthen} 188898184e3Ssthen 189898184e3Ssthen 190898184e3Ssthensub get_max_depth { $_[0]->{max_depth}; } 191898184e3Ssthen 192898184e3Ssthen 193898184e3Ssthensub max_size { 194898184e3Ssthen my $max = defined $_[1] ? $_[1] : 0; 195898184e3Ssthen $_[0]->{max_size} = $max; 196898184e3Ssthen $_[0]; 197898184e3Ssthen} 198898184e3Ssthen 199898184e3Ssthen 200898184e3Ssthensub get_max_size { $_[0]->{max_size}; } 201898184e3Ssthen 202b46d8ef2Safresh1sub boolean_values { 203b46d8ef2Safresh1 my $self = shift; 204b46d8ef2Safresh1 if (@_) { 205b46d8ef2Safresh1 my ($false, $true) = @_; 206b46d8ef2Safresh1 $self->{false} = $false; 207b46d8ef2Safresh1 $self->{true} = $true; 208*e0680481Safresh1 if (CORE_BOOL) { 209*e0680481Safresh1 BEGIN { CORE_BOOL and warnings->unimport(qw(experimental::builtin)) } 210*e0680481Safresh1 if (builtin::is_bool($true) && builtin::is_bool($false) && $true && !$false) { 211*e0680481Safresh1 $self->{core_bools} = !!1; 212*e0680481Safresh1 } 213*e0680481Safresh1 else { 214*e0680481Safresh1 delete $self->{core_bools}; 215*e0680481Safresh1 } 216*e0680481Safresh1 } 217b46d8ef2Safresh1 } else { 218b46d8ef2Safresh1 delete $self->{false}; 219b46d8ef2Safresh1 delete $self->{true}; 220*e0680481Safresh1 delete $self->{core_bools}; 221b46d8ef2Safresh1 } 222eac174f2Safresh1 return $self; 223b46d8ef2Safresh1} 224b46d8ef2Safresh1 225*e0680481Safresh1sub core_bools { 226*e0680481Safresh1 my $self = shift; 227*e0680481Safresh1 my $core_bools = defined $_[0] ? $_[0] : 1; 228*e0680481Safresh1 if ($core_bools) { 229*e0680481Safresh1 $self->{true} = !!1; 230*e0680481Safresh1 $self->{false} = !!0; 231*e0680481Safresh1 $self->{core_bools} = !!1; 232*e0680481Safresh1 } 233*e0680481Safresh1 else { 234*e0680481Safresh1 $self->{true} = $JSON::PP::true; 235*e0680481Safresh1 $self->{false} = $JSON::PP::false; 236*e0680481Safresh1 $self->{core_bools} = !!0; 237*e0680481Safresh1 } 238*e0680481Safresh1 return $self; 239*e0680481Safresh1} 240*e0680481Safresh1 241*e0680481Safresh1sub get_core_bools { 242*e0680481Safresh1 my $self = shift; 243*e0680481Safresh1 return !!$self->{core_bools}; 244*e0680481Safresh1} 245*e0680481Safresh1 246*e0680481Safresh1sub unblessed_bool { 247*e0680481Safresh1 my $self = shift; 248*e0680481Safresh1 return $self->core_bools(@_); 249*e0680481Safresh1} 250*e0680481Safresh1 251*e0680481Safresh1sub get_unblessed_bool { 252*e0680481Safresh1 my $self = shift; 253*e0680481Safresh1 return $self->get_core_bools(@_); 254*e0680481Safresh1} 255*e0680481Safresh1 256b46d8ef2Safresh1sub get_boolean_values { 257b46d8ef2Safresh1 my $self = shift; 258b46d8ef2Safresh1 if (exists $self->{true} and exists $self->{false}) { 259b46d8ef2Safresh1 return @$self{qw/false true/}; 260b46d8ef2Safresh1 } 261b46d8ef2Safresh1 return; 262b46d8ef2Safresh1} 263898184e3Ssthen 264898184e3Ssthensub filter_json_object { 2659f11ffb7Safresh1 if (defined $_[1] and ref $_[1] eq 'CODE') { 2669f11ffb7Safresh1 $_[0]->{cb_object} = $_[1]; 2679f11ffb7Safresh1 } else { 2689f11ffb7Safresh1 delete $_[0]->{cb_object}; 2699f11ffb7Safresh1 } 270898184e3Ssthen $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0; 271898184e3Ssthen $_[0]; 272898184e3Ssthen} 273898184e3Ssthen 274898184e3Ssthensub filter_json_single_key_object { 2759f11ffb7Safresh1 if (@_ == 1 or @_ > 3) { 2769f11ffb7Safresh1 Carp::croak("Usage: JSON::PP::filter_json_single_key_object(self, key, callback = undef)"); 2779f11ffb7Safresh1 } 2789f11ffb7Safresh1 if (defined $_[2] and ref $_[2] eq 'CODE') { 279898184e3Ssthen $_[0]->{cb_sk_object}->{$_[1]} = $_[2]; 2809f11ffb7Safresh1 } else { 2819f11ffb7Safresh1 delete $_[0]->{cb_sk_object}->{$_[1]}; 2829f11ffb7Safresh1 delete $_[0]->{cb_sk_object} unless %{$_[0]->{cb_sk_object} || {}}; 283898184e3Ssthen } 284898184e3Ssthen $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0; 285898184e3Ssthen $_[0]; 286898184e3Ssthen} 287898184e3Ssthen 288898184e3Ssthensub indent_length { 289898184e3Ssthen if (!defined $_[1] or $_[1] > 15 or $_[1] < 0) { 290898184e3Ssthen Carp::carp "The acceptable range of indent_length() is 0 to 15."; 291898184e3Ssthen } 292898184e3Ssthen else { 293898184e3Ssthen $_[0]->{indent_length} = $_[1]; 294898184e3Ssthen } 295898184e3Ssthen $_[0]; 296898184e3Ssthen} 297898184e3Ssthen 298898184e3Ssthensub get_indent_length { 299898184e3Ssthen $_[0]->{indent_length}; 300898184e3Ssthen} 301898184e3Ssthen 302898184e3Ssthensub sort_by { 303898184e3Ssthen $_[0]->{sort_by} = defined $_[1] ? $_[1] : 1; 304898184e3Ssthen $_[0]; 305898184e3Ssthen} 306898184e3Ssthen 307898184e3Ssthensub allow_bigint { 3089f11ffb7Safresh1 Carp::carp("allow_bigint() is obsoleted. use allow_bignum() instead."); 3099f11ffb7Safresh1 $_[0]->allow_bignum; 310898184e3Ssthen} 311898184e3Ssthen 312898184e3Ssthen############################### 313898184e3Ssthen 314898184e3Ssthen### 315898184e3Ssthen### Perl => JSON 316898184e3Ssthen### 317898184e3Ssthen 318898184e3Ssthen 319898184e3Ssthen{ # Convert 320898184e3Ssthen 321898184e3Ssthen my $max_depth; 322898184e3Ssthen my $indent; 323898184e3Ssthen my $ascii; 324898184e3Ssthen my $latin1; 325898184e3Ssthen my $utf8; 326898184e3Ssthen my $space_before; 327898184e3Ssthen my $space_after; 328898184e3Ssthen my $canonical; 329898184e3Ssthen my $allow_blessed; 330898184e3Ssthen my $convert_blessed; 331898184e3Ssthen 332898184e3Ssthen my $indent_length; 333898184e3Ssthen my $escape_slash; 334898184e3Ssthen my $bignum; 335898184e3Ssthen my $as_nonblessed; 336b46d8ef2Safresh1 my $allow_tags; 337898184e3Ssthen 338898184e3Ssthen my $depth; 339898184e3Ssthen my $indent_count; 340898184e3Ssthen my $keysort; 341898184e3Ssthen 342898184e3Ssthen 343898184e3Ssthen sub PP_encode_json { 344898184e3Ssthen my $self = shift; 345898184e3Ssthen my $obj = shift; 346898184e3Ssthen 347898184e3Ssthen $indent_count = 0; 348898184e3Ssthen $depth = 0; 349898184e3Ssthen 3509f11ffb7Safresh1 my $props = $self->{PROPS}; 351898184e3Ssthen 352898184e3Ssthen ($ascii, $latin1, $utf8, $indent, $canonical, $space_before, $space_after, $allow_blessed, 353b46d8ef2Safresh1 $convert_blessed, $escape_slash, $bignum, $as_nonblessed, $allow_tags) 3549f11ffb7Safresh1 = @{$props}[P_ASCII .. P_SPACE_AFTER, P_ALLOW_BLESSED, P_CONVERT_BLESSED, 355b46d8ef2Safresh1 P_ESCAPE_SLASH, P_ALLOW_BIGNUM, P_AS_NONBLESSED, P_ALLOW_TAGS]; 356898184e3Ssthen 357898184e3Ssthen ($max_depth, $indent_length) = @{$self}{qw/max_depth indent_length/}; 358898184e3Ssthen 359898184e3Ssthen $keysort = $canonical ? sub { $a cmp $b } : undef; 360898184e3Ssthen 361898184e3Ssthen if ($self->{sort_by}) { 362898184e3Ssthen $keysort = ref($self->{sort_by}) eq 'CODE' ? $self->{sort_by} 363898184e3Ssthen : $self->{sort_by} =~ /\D+/ ? $self->{sort_by} 364898184e3Ssthen : sub { $a cmp $b }; 365898184e3Ssthen } 366898184e3Ssthen 367898184e3Ssthen encode_error("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)") 3689f11ffb7Safresh1 if(!ref $obj and !$props->[ P_ALLOW_NONREF ]); 369898184e3Ssthen 370898184e3Ssthen my $str = $self->object_to_json($obj); 371898184e3Ssthen 372898184e3Ssthen $str .= "\n" if ( $indent ); # JSON::XS 2.26 compatible 373898184e3Ssthen 374898184e3Ssthen return $str; 375898184e3Ssthen } 376898184e3Ssthen 377898184e3Ssthen 378898184e3Ssthen sub object_to_json { 379898184e3Ssthen my ($self, $obj) = @_; 380898184e3Ssthen my $type = ref($obj); 381898184e3Ssthen 382898184e3Ssthen if($type eq 'HASH'){ 383898184e3Ssthen return $self->hash_to_json($obj); 384898184e3Ssthen } 385898184e3Ssthen elsif($type eq 'ARRAY'){ 386898184e3Ssthen return $self->array_to_json($obj); 387898184e3Ssthen } 388898184e3Ssthen elsif ($type) { # blessed object? 389898184e3Ssthen if (blessed($obj)) { 390898184e3Ssthen 391898184e3Ssthen return $self->value_to_json($obj) if ( $obj->isa('JSON::PP::Boolean') ); 392898184e3Ssthen 393b46d8ef2Safresh1 if ( $allow_tags and $obj->can('FREEZE') ) { 394b46d8ef2Safresh1 my $obj_class = ref $obj || $obj; 395b46d8ef2Safresh1 $obj = bless $obj, $obj_class; 396b46d8ef2Safresh1 my @results = $obj->FREEZE('JSON'); 397b46d8ef2Safresh1 if ( @results and ref $results[0] ) { 398b46d8ef2Safresh1 if ( refaddr( $obj ) eq refaddr( $results[0] ) ) { 399b46d8ef2Safresh1 encode_error( sprintf( 400b46d8ef2Safresh1 "%s::FREEZE method returned same object as was passed instead of a new one", 401b46d8ef2Safresh1 ref $obj 402b46d8ef2Safresh1 ) ); 403b46d8ef2Safresh1 } 404b46d8ef2Safresh1 } 405b46d8ef2Safresh1 return '("'.$obj_class.'")['.join(',', @results).']'; 406b46d8ef2Safresh1 } 407b46d8ef2Safresh1 408898184e3Ssthen if ( $convert_blessed and $obj->can('TO_JSON') ) { 409898184e3Ssthen my $result = $obj->TO_JSON(); 410898184e3Ssthen if ( defined $result and ref( $result ) ) { 411898184e3Ssthen if ( refaddr( $obj ) eq refaddr( $result ) ) { 412898184e3Ssthen encode_error( sprintf( 413898184e3Ssthen "%s::TO_JSON method returned same object as was passed instead of a new one", 414898184e3Ssthen ref $obj 415898184e3Ssthen ) ); 416898184e3Ssthen } 417898184e3Ssthen } 418898184e3Ssthen 419898184e3Ssthen return $self->object_to_json( $result ); 420898184e3Ssthen } 421898184e3Ssthen 422898184e3Ssthen return "$obj" if ( $bignum and _is_bignum($obj) ); 423898184e3Ssthen 4249f11ffb7Safresh1 if ($allow_blessed) { 4259f11ffb7Safresh1 return $self->blessed_to_json($obj) if ($as_nonblessed); # will be removed. 4269f11ffb7Safresh1 return 'null'; 4279f11ffb7Safresh1 } 428b46d8ef2Safresh1 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) 4299f11ffb7Safresh1 ); 430898184e3Ssthen } 431898184e3Ssthen else { 432898184e3Ssthen return $self->value_to_json($obj); 433898184e3Ssthen } 434898184e3Ssthen } 435898184e3Ssthen else{ 436898184e3Ssthen return $self->value_to_json($obj); 437898184e3Ssthen } 438898184e3Ssthen } 439898184e3Ssthen 440898184e3Ssthen 441898184e3Ssthen sub hash_to_json { 442898184e3Ssthen my ($self, $obj) = @_; 443898184e3Ssthen my @res; 444898184e3Ssthen 445898184e3Ssthen encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)") 446898184e3Ssthen if (++$depth > $max_depth); 447898184e3Ssthen 448898184e3Ssthen my ($pre, $post) = $indent ? $self->_up_indent() : ('', ''); 449898184e3Ssthen my $del = ($space_before ? ' ' : '') . ':' . ($space_after ? ' ' : ''); 450898184e3Ssthen 451898184e3Ssthen for my $k ( _sort( $obj ) ) { 4529f11ffb7Safresh1 push @res, $self->string_to_json( $k ) 453898184e3Ssthen . $del 4549f11ffb7Safresh1 . ( ref $obj->{$k} ? $self->object_to_json( $obj->{$k} ) : $self->value_to_json( $obj->{$k} ) ); 455898184e3Ssthen } 456898184e3Ssthen 457898184e3Ssthen --$depth; 458898184e3Ssthen $self->_down_indent() if ($indent); 459898184e3Ssthen 4609f11ffb7Safresh1 return '{}' unless @res; 4619f11ffb7Safresh1 return '{' . $pre . join( ",$pre", @res ) . $post . '}'; 462898184e3Ssthen } 463898184e3Ssthen 464898184e3Ssthen 465898184e3Ssthen sub array_to_json { 466898184e3Ssthen my ($self, $obj) = @_; 467898184e3Ssthen my @res; 468898184e3Ssthen 469898184e3Ssthen encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)") 470898184e3Ssthen if (++$depth > $max_depth); 471898184e3Ssthen 472898184e3Ssthen my ($pre, $post) = $indent ? $self->_up_indent() : ('', ''); 473898184e3Ssthen 474898184e3Ssthen for my $v (@$obj){ 4759f11ffb7Safresh1 push @res, ref($v) ? $self->object_to_json($v) : $self->value_to_json($v); 476898184e3Ssthen } 477898184e3Ssthen 478898184e3Ssthen --$depth; 479898184e3Ssthen $self->_down_indent() if ($indent); 480898184e3Ssthen 4819f11ffb7Safresh1 return '[]' unless @res; 4829f11ffb7Safresh1 return '[' . $pre . join( ",$pre", @res ) . $post . ']'; 483898184e3Ssthen } 484898184e3Ssthen 4859f11ffb7Safresh1 sub _looks_like_number { 4869f11ffb7Safresh1 my $value = shift; 4879f11ffb7Safresh1 if (USE_B) { 4889f11ffb7Safresh1 my $b_obj = B::svref_2object(\$value); 4899f11ffb7Safresh1 my $flags = $b_obj->FLAGS; 4909f11ffb7Safresh1 return 1 if $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() ); 4919f11ffb7Safresh1 return; 4929f11ffb7Safresh1 } else { 4939f11ffb7Safresh1 no warnings 'numeric'; 4949f11ffb7Safresh1 # if the utf8 flag is on, it almost certainly started as a string 4959f11ffb7Safresh1 return if utf8::is_utf8($value); 4969f11ffb7Safresh1 # detect numbers 4979f11ffb7Safresh1 # string & "" -> "" 4989f11ffb7Safresh1 # number & "" -> 0 (with warning) 4999f11ffb7Safresh1 # nan and inf can detect as numbers, so check with * 0 5009f11ffb7Safresh1 return unless length((my $dummy = "") & $value); 5019f11ffb7Safresh1 return unless 0 + $value eq $value; 5029f11ffb7Safresh1 return 1 if $value * 0 == 0; 5039f11ffb7Safresh1 return -1; # inf/nan 5049f11ffb7Safresh1 } 5059f11ffb7Safresh1 } 506898184e3Ssthen 507898184e3Ssthen sub value_to_json { 508898184e3Ssthen my ($self, $value) = @_; 509898184e3Ssthen 510898184e3Ssthen return 'null' if(!defined $value); 511898184e3Ssthen 512898184e3Ssthen my $type = ref($value); 513898184e3Ssthen 514898184e3Ssthen if (!$type) { 515*e0680481Safresh1 BEGIN { CORE_BOOL and warnings->unimport('experimental::builtin') } 516*e0680481Safresh1 if (CORE_BOOL && builtin::is_bool($value)) { 517*e0680481Safresh1 return $value ? 'true' : 'false'; 518*e0680481Safresh1 } 519*e0680481Safresh1 elsif (_looks_like_number($value)) { 5209f11ffb7Safresh1 return $value; 5219f11ffb7Safresh1 } 5229f11ffb7Safresh1 return $self->string_to_json($value); 523898184e3Ssthen } 524898184e3Ssthen elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ){ 525898184e3Ssthen return $$value == 1 ? 'true' : 'false'; 526898184e3Ssthen } 5279f11ffb7Safresh1 else { 528898184e3Ssthen if ((overload::StrVal($value) =~ /=(\w+)/)[0]) { 529898184e3Ssthen return $self->value_to_json("$value"); 530898184e3Ssthen } 531898184e3Ssthen 532898184e3Ssthen if ($type eq 'SCALAR' and defined $$value) { 533898184e3Ssthen return $$value eq '1' ? 'true' 534898184e3Ssthen : $$value eq '0' ? 'false' 535898184e3Ssthen : $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ? 'null' 536898184e3Ssthen : encode_error("cannot encode reference to scalar"); 537898184e3Ssthen } 538898184e3Ssthen 539898184e3Ssthen if ( $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ) { 540898184e3Ssthen return 'null'; 541898184e3Ssthen } 542898184e3Ssthen else { 543898184e3Ssthen if ( $type eq 'SCALAR' or $type eq 'REF' ) { 544898184e3Ssthen encode_error("cannot encode reference to scalar"); 545898184e3Ssthen } 546898184e3Ssthen else { 547898184e3Ssthen encode_error("encountered $value, but JSON can only represent references to arrays or hashes"); 548898184e3Ssthen } 549898184e3Ssthen } 550898184e3Ssthen 551898184e3Ssthen } 552898184e3Ssthen } 553898184e3Ssthen 554898184e3Ssthen 555898184e3Ssthen my %esc = ( 556898184e3Ssthen "\n" => '\n', 557898184e3Ssthen "\r" => '\r', 558898184e3Ssthen "\t" => '\t', 559898184e3Ssthen "\f" => '\f', 560898184e3Ssthen "\b" => '\b', 561898184e3Ssthen "\"" => '\"', 562898184e3Ssthen "\\" => '\\\\', 563898184e3Ssthen "\'" => '\\\'', 564898184e3Ssthen ); 565898184e3Ssthen 566898184e3Ssthen 567898184e3Ssthen sub string_to_json { 568898184e3Ssthen my ($self, $arg) = @_; 569898184e3Ssthen 570*e0680481Safresh1 $arg =~ s/(["\\\n\r\t\f\b])/$esc{$1}/g; 571898184e3Ssthen $arg =~ s/\//\\\//g if ($escape_slash); 572*e0680481Safresh1 573*e0680481Safresh1 # On ASCII platforms, matches [\x00-\x08\x0b\x0e-\x1f] 574*e0680481Safresh1 $arg =~ s/([^\n\t\c?[:^cntrl:][:^ascii:]])/'\\u00' . unpack('H2', $1)/eg; 575898184e3Ssthen 576898184e3Ssthen if ($ascii) { 577*e0680481Safresh1 $arg = _encode_ascii($arg); 578898184e3Ssthen } 579898184e3Ssthen 580898184e3Ssthen if ($latin1) { 581*e0680481Safresh1 $arg = _encode_latin1($arg); 582898184e3Ssthen } 583898184e3Ssthen 584898184e3Ssthen if ($utf8) { 585898184e3Ssthen utf8::encode($arg); 586898184e3Ssthen } 587898184e3Ssthen 588898184e3Ssthen return '"' . $arg . '"'; 589898184e3Ssthen } 590898184e3Ssthen 591898184e3Ssthen 592898184e3Ssthen sub blessed_to_json { 593898184e3Ssthen my $reftype = reftype($_[1]) || ''; 594898184e3Ssthen if ($reftype eq 'HASH') { 595898184e3Ssthen return $_[0]->hash_to_json($_[1]); 596898184e3Ssthen } 597898184e3Ssthen elsif ($reftype eq 'ARRAY') { 598898184e3Ssthen return $_[0]->array_to_json($_[1]); 599898184e3Ssthen } 600898184e3Ssthen else { 601898184e3Ssthen return 'null'; 602898184e3Ssthen } 603898184e3Ssthen } 604898184e3Ssthen 605898184e3Ssthen 606898184e3Ssthen sub encode_error { 607898184e3Ssthen my $error = shift; 608898184e3Ssthen Carp::croak "$error"; 609898184e3Ssthen } 610898184e3Ssthen 611898184e3Ssthen 612898184e3Ssthen sub _sort { 613898184e3Ssthen defined $keysort ? (sort $keysort (keys %{$_[0]})) : keys %{$_[0]}; 614898184e3Ssthen } 615898184e3Ssthen 616898184e3Ssthen 617898184e3Ssthen sub _up_indent { 618898184e3Ssthen my $self = shift; 619898184e3Ssthen my $space = ' ' x $indent_length; 620898184e3Ssthen 621898184e3Ssthen my ($pre,$post) = ('',''); 622898184e3Ssthen 623898184e3Ssthen $post = "\n" . $space x $indent_count; 624898184e3Ssthen 625898184e3Ssthen $indent_count++; 626898184e3Ssthen 627898184e3Ssthen $pre = "\n" . $space x $indent_count; 628898184e3Ssthen 629898184e3Ssthen return ($pre,$post); 630898184e3Ssthen } 631898184e3Ssthen 632898184e3Ssthen 633898184e3Ssthen sub _down_indent { $indent_count--; } 634898184e3Ssthen 635898184e3Ssthen 636898184e3Ssthen sub PP_encode_box { 637898184e3Ssthen { 638898184e3Ssthen depth => $depth, 639898184e3Ssthen indent_count => $indent_count, 640898184e3Ssthen }; 641898184e3Ssthen } 642898184e3Ssthen 643898184e3Ssthen} # Convert 644898184e3Ssthen 645898184e3Ssthen 646898184e3Ssthensub _encode_ascii { 647898184e3Ssthen join('', 648898184e3Ssthen map { 649*e0680481Safresh1 chr($_) =~ /[[:ascii:]]/ ? 650898184e3Ssthen chr($_) : 651898184e3Ssthen $_ <= 65535 ? 652898184e3Ssthen sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_)); 653898184e3Ssthen } unpack('U*', $_[0]) 654898184e3Ssthen ); 655898184e3Ssthen} 656898184e3Ssthen 657898184e3Ssthen 658898184e3Ssthensub _encode_latin1 { 659898184e3Ssthen join('', 660898184e3Ssthen map { 661898184e3Ssthen $_ <= 255 ? 662898184e3Ssthen chr($_) : 663898184e3Ssthen $_ <= 65535 ? 664898184e3Ssthen sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_)); 665898184e3Ssthen } unpack('U*', $_[0]) 666898184e3Ssthen ); 667898184e3Ssthen} 668898184e3Ssthen 669898184e3Ssthen 670898184e3Ssthensub _encode_surrogates { # from perlunicode 671898184e3Ssthen my $uni = $_[0] - 0x10000; 672898184e3Ssthen return ($uni / 0x400 + 0xD800, $uni % 0x400 + 0xDC00); 673898184e3Ssthen} 674898184e3Ssthen 675898184e3Ssthen 676898184e3Ssthensub _is_bignum { 677898184e3Ssthen $_[0]->isa('Math::BigInt') or $_[0]->isa('Math::BigFloat'); 678898184e3Ssthen} 679898184e3Ssthen 680898184e3Ssthen 681898184e3Ssthen 682898184e3Ssthen# 683898184e3Ssthen# JSON => Perl 684898184e3Ssthen# 685898184e3Ssthen 686898184e3Ssthenmy $max_intsize; 687898184e3Ssthen 688898184e3SsthenBEGIN { 689898184e3Ssthen my $checkint = 1111; 690898184e3Ssthen for my $d (5..64) { 691898184e3Ssthen $checkint .= 1; 692898184e3Ssthen my $int = eval qq| $checkint |; 693898184e3Ssthen if ($int =~ /[eE]/) { 694898184e3Ssthen $max_intsize = $d - 1; 695898184e3Ssthen last; 696898184e3Ssthen } 697898184e3Ssthen } 698898184e3Ssthen} 699898184e3Ssthen 700898184e3Ssthen{ # PARSE 701898184e3Ssthen 702898184e3Ssthen my %escapes = ( # by Jeremy Muhlich <jmuhlich [at] bitflood.org> 703*e0680481Safresh1 b => "\b", 704*e0680481Safresh1 t => "\t", 705*e0680481Safresh1 n => "\n", 706*e0680481Safresh1 f => "\f", 707*e0680481Safresh1 r => "\r", 708898184e3Ssthen '\\' => '\\', 709898184e3Ssthen '"' => '"', 710898184e3Ssthen '/' => '/', 711898184e3Ssthen ); 712898184e3Ssthen 713898184e3Ssthen my $text; # json data 714898184e3Ssthen my $at; # offset 7159f11ffb7Safresh1 my $ch; # first character 716898184e3Ssthen my $len; # text length (changed according to UTF8 or NON UTF8) 717898184e3Ssthen # INTERNAL 718898184e3Ssthen my $depth; # nest counter 719898184e3Ssthen my $encoding; # json text encoding 720898184e3Ssthen my $is_valid_utf8; # temp variable 721898184e3Ssthen my $utf8_len; # utf8 byte length 722898184e3Ssthen # FLAGS 723898184e3Ssthen my $utf8; # must be utf8 7249f11ffb7Safresh1 my $max_depth; # max nest number of objects and arrays 725898184e3Ssthen my $max_size; 726898184e3Ssthen my $relaxed; 727898184e3Ssthen my $cb_object; 728898184e3Ssthen my $cb_sk_object; 729898184e3Ssthen 730898184e3Ssthen my $F_HOOK; 731898184e3Ssthen 7329f11ffb7Safresh1 my $allow_bignum; # using Math::BigInt/BigFloat 733898184e3Ssthen my $singlequote; # loosely quoting 734898184e3Ssthen my $loose; # 735898184e3Ssthen my $allow_barekey; # bareKey 736b46d8ef2Safresh1 my $allow_tags; 737b46d8ef2Safresh1 738b46d8ef2Safresh1 my $alt_true; 739b46d8ef2Safresh1 my $alt_false; 740898184e3Ssthen 7419f11ffb7Safresh1 sub _detect_utf_encoding { 7429f11ffb7Safresh1 my $text = shift; 7439f11ffb7Safresh1 my @octets = unpack('C4', $text); 7449f11ffb7Safresh1 return 'unknown' unless defined $octets[3]; 7459f11ffb7Safresh1 return ( $octets[0] and $octets[1]) ? 'UTF-8' 7469f11ffb7Safresh1 : (!$octets[0] and $octets[1]) ? 'UTF-16BE' 7479f11ffb7Safresh1 : (!$octets[0] and !$octets[1]) ? 'UTF-32BE' 7489f11ffb7Safresh1 : ( $octets[2] ) ? 'UTF-16LE' 7499f11ffb7Safresh1 : (!$octets[2] ) ? 'UTF-32LE' 7509f11ffb7Safresh1 : 'unknown'; 7519f11ffb7Safresh1 } 752898184e3Ssthen 753898184e3Ssthen sub PP_decode_json { 7549f11ffb7Safresh1 my ($self, $want_offset); 755898184e3Ssthen 7569f11ffb7Safresh1 ($self, $text, $want_offset) = @_; 757898184e3Ssthen 758898184e3Ssthen ($at, $ch, $depth) = (0, '', 0); 759898184e3Ssthen 760898184e3Ssthen if ( !defined $text or ref $text ) { 761898184e3Ssthen decode_error("malformed JSON string, neither array, object, number, string or atom"); 762898184e3Ssthen } 763898184e3Ssthen 7649f11ffb7Safresh1 my $props = $self->{PROPS}; 765898184e3Ssthen 766b46d8ef2Safresh1 ($utf8, $relaxed, $loose, $allow_bignum, $allow_barekey, $singlequote, $allow_tags) 767b46d8ef2Safresh1 = @{$props}[P_UTF8, P_RELAXED, P_LOOSE .. P_ALLOW_SINGLEQUOTE, P_ALLOW_TAGS]; 768b46d8ef2Safresh1 769b46d8ef2Safresh1 ($alt_true, $alt_false) = @$self{qw/true false/}; 770898184e3Ssthen 771898184e3Ssthen if ( $utf8 ) { 7729f11ffb7Safresh1 $encoding = _detect_utf_encoding($text); 7739f11ffb7Safresh1 if ($encoding ne 'UTF-8' and $encoding ne 'unknown') { 7749f11ffb7Safresh1 require Encode; 7759f11ffb7Safresh1 Encode::from_to($text, $encoding, 'utf-8'); 7769f11ffb7Safresh1 } else { 777898184e3Ssthen utf8::downgrade( $text, 1 ) or Carp::croak("Wide character in subroutine entry"); 778898184e3Ssthen } 7799f11ffb7Safresh1 } 780898184e3Ssthen else { 781b8851fccSafresh1 utf8::encode( $text ); 782898184e3Ssthen } 783898184e3Ssthen 784898184e3Ssthen $len = length $text; 785898184e3Ssthen 786898184e3Ssthen ($max_depth, $max_size, $cb_object, $cb_sk_object, $F_HOOK) 787898184e3Ssthen = @{$self}{qw/max_depth max_size cb_object cb_sk_object F_HOOK/}; 788898184e3Ssthen 789898184e3Ssthen if ($max_size > 1) { 790898184e3Ssthen use bytes; 791898184e3Ssthen my $bytes = length $text; 792898184e3Ssthen decode_error( 793898184e3Ssthen sprintf("attempted decode of JSON text of %s bytes size, but max_size is set to %s" 794898184e3Ssthen , $bytes, $max_size), 1 795898184e3Ssthen ) if ($bytes > $max_size); 796898184e3Ssthen } 797898184e3Ssthen 798898184e3Ssthen white(); # remove head white space 799898184e3Ssthen 8009f11ffb7Safresh1 decode_error("malformed JSON string, neither array, object, number, string or atom") unless defined $ch; # Is there a first character for JSON structure? 801898184e3Ssthen 802898184e3Ssthen my $result = value(); 803898184e3Ssthen 8049f11ffb7Safresh1 if ( !$props->[ P_ALLOW_NONREF ] and !ref $result ) { 805898184e3Ssthen decode_error( 806898184e3Ssthen 'JSON text must be an object or array (but found number, string, true, false or null,' 807898184e3Ssthen . ' use allow_nonref to allow this)', 1); 808898184e3Ssthen } 809898184e3Ssthen 810898184e3Ssthen Carp::croak('something wrong.') if $len < $at; # we won't arrive here. 811898184e3Ssthen 812898184e3Ssthen my $consumed = defined $ch ? $at - 1 : $at; # consumed JSON text length 813898184e3Ssthen 814898184e3Ssthen white(); # remove tail white space 815898184e3Ssthen 8169f11ffb7Safresh1 return ( $result, $consumed ) if $want_offset; # all right if decode_prefix 817898184e3Ssthen 8189f11ffb7Safresh1 decode_error("garbage after JSON object") if defined $ch; 8199f11ffb7Safresh1 8209f11ffb7Safresh1 $result; 821898184e3Ssthen } 822898184e3Ssthen 823898184e3Ssthen 824898184e3Ssthen sub next_chr { 825898184e3Ssthen return $ch = undef if($at >= $len); 826898184e3Ssthen $ch = substr($text, $at++, 1); 827898184e3Ssthen } 828898184e3Ssthen 829898184e3Ssthen 830898184e3Ssthen sub value { 831898184e3Ssthen white(); 832898184e3Ssthen return if(!defined $ch); 833898184e3Ssthen return object() if($ch eq '{'); 834898184e3Ssthen return array() if($ch eq '['); 835b46d8ef2Safresh1 return tag() if($ch eq '('); 836898184e3Ssthen return string() if($ch eq '"' or ($singlequote and $ch eq "'")); 837898184e3Ssthen return number() if($ch =~ /[0-9]/ or $ch eq '-'); 838898184e3Ssthen return word(); 839898184e3Ssthen } 840898184e3Ssthen 841898184e3Ssthen sub string { 842898184e3Ssthen my $utf16; 843898184e3Ssthen my $is_utf8; 844898184e3Ssthen 845898184e3Ssthen ($is_valid_utf8, $utf8_len) = ('', 0); 846898184e3Ssthen 8479f11ffb7Safresh1 my $s = ''; # basically UTF8 flag on 848898184e3Ssthen 849898184e3Ssthen if($ch eq '"' or ($singlequote and $ch eq "'")){ 850898184e3Ssthen my $boundChar = $ch; 851898184e3Ssthen 852898184e3Ssthen OUTER: while( defined(next_chr()) ){ 853898184e3Ssthen 854898184e3Ssthen if($ch eq $boundChar){ 855898184e3Ssthen next_chr(); 856898184e3Ssthen 857898184e3Ssthen if ($utf16) { 858898184e3Ssthen decode_error("missing low surrogate character in surrogate pair"); 859898184e3Ssthen } 860898184e3Ssthen 861898184e3Ssthen utf8::decode($s) if($is_utf8); 862898184e3Ssthen 863898184e3Ssthen return $s; 864898184e3Ssthen } 865898184e3Ssthen elsif($ch eq '\\'){ 866898184e3Ssthen next_chr(); 867898184e3Ssthen if(exists $escapes{$ch}){ 868898184e3Ssthen $s .= $escapes{$ch}; 869898184e3Ssthen } 870898184e3Ssthen elsif($ch eq 'u'){ # UNICODE handling 871898184e3Ssthen my $u = ''; 872898184e3Ssthen 873898184e3Ssthen for(1..4){ 874898184e3Ssthen $ch = next_chr(); 875898184e3Ssthen last OUTER if($ch !~ /[0-9a-fA-F]/); 876898184e3Ssthen $u .= $ch; 877898184e3Ssthen } 878898184e3Ssthen 879898184e3Ssthen # U+D800 - U+DBFF 880898184e3Ssthen if ($u =~ /^[dD][89abAB][0-9a-fA-F]{2}/) { # UTF-16 high surrogate? 881898184e3Ssthen $utf16 = $u; 882898184e3Ssthen } 883898184e3Ssthen # U+DC00 - U+DFFF 884898184e3Ssthen elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { # UTF-16 low surrogate? 885898184e3Ssthen unless (defined $utf16) { 886898184e3Ssthen decode_error("missing high surrogate character in surrogate pair"); 887898184e3Ssthen } 888898184e3Ssthen $is_utf8 = 1; 889*e0680481Safresh1 $s .= _decode_surrogates($utf16, $u) || next; 890898184e3Ssthen $utf16 = undef; 891898184e3Ssthen } 892898184e3Ssthen else { 893898184e3Ssthen if (defined $utf16) { 894898184e3Ssthen decode_error("surrogate pair expected"); 895898184e3Ssthen } 896898184e3Ssthen 897*e0680481Safresh1 my $hex = hex( $u ); 898*e0680481Safresh1 if ( chr $u =~ /[[:^ascii:]]/ ) { 899898184e3Ssthen $is_utf8 = 1; 900*e0680481Safresh1 $s .= _decode_unicode($u) || next; 901898184e3Ssthen } 902898184e3Ssthen else { 903898184e3Ssthen $s .= chr $hex; 904898184e3Ssthen } 905898184e3Ssthen } 906898184e3Ssthen 907898184e3Ssthen } 908898184e3Ssthen else{ 909898184e3Ssthen unless ($loose) { 910898184e3Ssthen $at -= 2; 911898184e3Ssthen decode_error('illegal backslash escape sequence in string'); 912898184e3Ssthen } 913898184e3Ssthen $s .= $ch; 914898184e3Ssthen } 915898184e3Ssthen } 916898184e3Ssthen else{ 917898184e3Ssthen 918*e0680481Safresh1 if ( $ch =~ /[[:^ascii:]]/ ) { 919898184e3Ssthen unless( $ch = is_valid_utf8($ch) ) { 920898184e3Ssthen $at -= 1; 921898184e3Ssthen decode_error("malformed UTF-8 character in JSON string"); 922898184e3Ssthen } 923898184e3Ssthen else { 924898184e3Ssthen $at += $utf8_len - 1; 925898184e3Ssthen } 926898184e3Ssthen 927898184e3Ssthen $is_utf8 = 1; 928898184e3Ssthen } 929898184e3Ssthen 930898184e3Ssthen if (!$loose) { 931*e0680481Safresh1 if ($ch =~ $invalid_char_re) { # '/' ok 932b46d8ef2Safresh1 if (!$relaxed or $ch ne "\t") { 933898184e3Ssthen $at--; 934*e0680481Safresh1 decode_error(sprintf "invalid character 0x%X" 935*e0680481Safresh1 . " encountered while parsing JSON string", 936*e0680481Safresh1 ord $ch); 937898184e3Ssthen } 938898184e3Ssthen } 939b46d8ef2Safresh1 } 940898184e3Ssthen 941898184e3Ssthen $s .= $ch; 942898184e3Ssthen } 943898184e3Ssthen } 944898184e3Ssthen } 945898184e3Ssthen 946898184e3Ssthen decode_error("unexpected end of string while parsing JSON string"); 947898184e3Ssthen } 948898184e3Ssthen 949898184e3Ssthen 950898184e3Ssthen sub white { 951898184e3Ssthen while( defined $ch ){ 9529f11ffb7Safresh1 if($ch eq '' or $ch =~ /\A[ \t\r\n]\z/){ 953898184e3Ssthen next_chr(); 954898184e3Ssthen } 9559f11ffb7Safresh1 elsif($relaxed and $ch eq '/'){ 956898184e3Ssthen next_chr(); 957898184e3Ssthen if(defined $ch and $ch eq '/'){ 958898184e3Ssthen 1 while(defined(next_chr()) and $ch ne "\n" and $ch ne "\r"); 959898184e3Ssthen } 960898184e3Ssthen elsif(defined $ch and $ch eq '*'){ 961898184e3Ssthen next_chr(); 962898184e3Ssthen while(1){ 963898184e3Ssthen if(defined $ch){ 964898184e3Ssthen if($ch eq '*'){ 965898184e3Ssthen if(defined(next_chr()) and $ch eq '/'){ 966898184e3Ssthen next_chr(); 967898184e3Ssthen last; 968898184e3Ssthen } 969898184e3Ssthen } 970898184e3Ssthen else{ 971898184e3Ssthen next_chr(); 972898184e3Ssthen } 973898184e3Ssthen } 974898184e3Ssthen else{ 975898184e3Ssthen decode_error("Unterminated comment"); 976898184e3Ssthen } 977898184e3Ssthen } 978898184e3Ssthen next; 979898184e3Ssthen } 980898184e3Ssthen else{ 981898184e3Ssthen $at--; 982898184e3Ssthen decode_error("malformed JSON string, neither array, object, number, string or atom"); 983898184e3Ssthen } 984898184e3Ssthen } 985898184e3Ssthen else{ 986898184e3Ssthen if ($relaxed and $ch eq '#') { # correctly? 987898184e3Ssthen pos($text) = $at; 988898184e3Ssthen $text =~ /\G([^\n]*(?:\r\n|\r|\n|$))/g; 989898184e3Ssthen $at = pos($text); 990898184e3Ssthen next_chr; 991898184e3Ssthen next; 992898184e3Ssthen } 993898184e3Ssthen 994898184e3Ssthen last; 995898184e3Ssthen } 996898184e3Ssthen } 997898184e3Ssthen } 998898184e3Ssthen 999898184e3Ssthen 1000898184e3Ssthen sub array { 1001898184e3Ssthen my $a = $_[0] || []; # you can use this code to use another array ref object. 1002898184e3Ssthen 1003898184e3Ssthen decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)') 1004898184e3Ssthen if (++$depth > $max_depth); 1005898184e3Ssthen 1006898184e3Ssthen next_chr(); 1007898184e3Ssthen white(); 1008898184e3Ssthen 1009898184e3Ssthen if(defined $ch and $ch eq ']'){ 1010898184e3Ssthen --$depth; 1011898184e3Ssthen next_chr(); 1012898184e3Ssthen return $a; 1013898184e3Ssthen } 1014898184e3Ssthen else { 1015898184e3Ssthen while(defined($ch)){ 1016898184e3Ssthen push @$a, value(); 1017898184e3Ssthen 1018898184e3Ssthen white(); 1019898184e3Ssthen 1020898184e3Ssthen if (!defined $ch) { 1021898184e3Ssthen last; 1022898184e3Ssthen } 1023898184e3Ssthen 1024898184e3Ssthen if($ch eq ']'){ 1025898184e3Ssthen --$depth; 1026898184e3Ssthen next_chr(); 1027898184e3Ssthen return $a; 1028898184e3Ssthen } 1029898184e3Ssthen 1030898184e3Ssthen if($ch ne ','){ 1031898184e3Ssthen last; 1032898184e3Ssthen } 1033898184e3Ssthen 1034898184e3Ssthen next_chr(); 1035898184e3Ssthen white(); 1036898184e3Ssthen 1037898184e3Ssthen if ($relaxed and $ch eq ']') { 1038898184e3Ssthen --$depth; 1039898184e3Ssthen next_chr(); 1040898184e3Ssthen return $a; 1041898184e3Ssthen } 1042898184e3Ssthen 1043898184e3Ssthen } 1044898184e3Ssthen } 1045898184e3Ssthen 10469f11ffb7Safresh1 $at-- if defined $ch and $ch ne ''; 1047898184e3Ssthen decode_error(", or ] expected while parsing array"); 1048898184e3Ssthen } 1049898184e3Ssthen 1050b46d8ef2Safresh1 sub tag { 1051b46d8ef2Safresh1 decode_error('malformed JSON string, neither array, object, number, string or atom') unless $allow_tags; 1052b46d8ef2Safresh1 1053b46d8ef2Safresh1 next_chr(); 1054b46d8ef2Safresh1 white(); 1055b46d8ef2Safresh1 1056b46d8ef2Safresh1 my $tag = value(); 1057b46d8ef2Safresh1 return unless defined $tag; 1058b46d8ef2Safresh1 decode_error('malformed JSON string, (tag) must be a string') if ref $tag; 1059b46d8ef2Safresh1 1060b46d8ef2Safresh1 white(); 1061b46d8ef2Safresh1 1062b46d8ef2Safresh1 if (!defined $ch or $ch ne ')') { 1063b46d8ef2Safresh1 decode_error(') expected after tag'); 1064b46d8ef2Safresh1 } 1065b46d8ef2Safresh1 1066b46d8ef2Safresh1 next_chr(); 1067b46d8ef2Safresh1 white(); 1068b46d8ef2Safresh1 1069b46d8ef2Safresh1 my $val = value(); 1070b46d8ef2Safresh1 return unless defined $val; 1071b46d8ef2Safresh1 decode_error('malformed JSON string, tag value must be an array') unless ref $val eq 'ARRAY'; 1072b46d8ef2Safresh1 1073b46d8ef2Safresh1 if (!eval { $tag->can('THAW') }) { 1074b46d8ef2Safresh1 decode_error('cannot decode perl-object (package does not exist)') if $@; 1075b46d8ef2Safresh1 decode_error('cannot decode perl-object (package does not have a THAW method)'); 1076b46d8ef2Safresh1 } 1077b46d8ef2Safresh1 $tag->THAW('JSON', @$val); 1078b46d8ef2Safresh1 } 1079898184e3Ssthen 1080898184e3Ssthen sub object { 1081898184e3Ssthen my $o = $_[0] || {}; # you can use this code to use another hash ref object. 1082898184e3Ssthen my $k; 1083898184e3Ssthen 1084898184e3Ssthen decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)') 1085898184e3Ssthen if (++$depth > $max_depth); 1086898184e3Ssthen next_chr(); 1087898184e3Ssthen white(); 1088898184e3Ssthen 1089898184e3Ssthen if(defined $ch and $ch eq '}'){ 1090898184e3Ssthen --$depth; 1091898184e3Ssthen next_chr(); 1092898184e3Ssthen if ($F_HOOK) { 1093898184e3Ssthen return _json_object_hook($o); 1094898184e3Ssthen } 1095898184e3Ssthen return $o; 1096898184e3Ssthen } 1097898184e3Ssthen else { 1098898184e3Ssthen while (defined $ch) { 1099898184e3Ssthen $k = ($allow_barekey and $ch ne '"' and $ch ne "'") ? bareKey() : string(); 1100898184e3Ssthen white(); 1101898184e3Ssthen 1102898184e3Ssthen if(!defined $ch or $ch ne ':'){ 1103898184e3Ssthen $at--; 1104898184e3Ssthen decode_error("':' expected"); 1105898184e3Ssthen } 1106898184e3Ssthen 1107898184e3Ssthen next_chr(); 1108898184e3Ssthen $o->{$k} = value(); 1109898184e3Ssthen white(); 1110898184e3Ssthen 1111898184e3Ssthen last if (!defined $ch); 1112898184e3Ssthen 1113898184e3Ssthen if($ch eq '}'){ 1114898184e3Ssthen --$depth; 1115898184e3Ssthen next_chr(); 1116898184e3Ssthen if ($F_HOOK) { 1117898184e3Ssthen return _json_object_hook($o); 1118898184e3Ssthen } 1119898184e3Ssthen return $o; 1120898184e3Ssthen } 1121898184e3Ssthen 1122898184e3Ssthen if($ch ne ','){ 1123898184e3Ssthen last; 1124898184e3Ssthen } 1125898184e3Ssthen 1126898184e3Ssthen next_chr(); 1127898184e3Ssthen white(); 1128898184e3Ssthen 1129898184e3Ssthen if ($relaxed and $ch eq '}') { 1130898184e3Ssthen --$depth; 1131898184e3Ssthen next_chr(); 1132898184e3Ssthen if ($F_HOOK) { 1133898184e3Ssthen return _json_object_hook($o); 1134898184e3Ssthen } 1135898184e3Ssthen return $o; 1136898184e3Ssthen } 1137898184e3Ssthen 1138898184e3Ssthen } 1139898184e3Ssthen 1140898184e3Ssthen } 1141898184e3Ssthen 11429f11ffb7Safresh1 $at-- if defined $ch and $ch ne ''; 1143898184e3Ssthen decode_error(", or } expected while parsing object/hash"); 1144898184e3Ssthen } 1145898184e3Ssthen 1146898184e3Ssthen 1147898184e3Ssthen sub bareKey { # doesn't strictly follow Standard ECMA-262 3rd Edition 1148898184e3Ssthen my $key; 1149*e0680481Safresh1 while($ch =~ /[\$\w[:^ascii:]]/){ 1150898184e3Ssthen $key .= $ch; 1151898184e3Ssthen next_chr(); 1152898184e3Ssthen } 1153898184e3Ssthen return $key; 1154898184e3Ssthen } 1155898184e3Ssthen 1156898184e3Ssthen 1157898184e3Ssthen sub word { 1158898184e3Ssthen my $word = substr($text,$at-1,4); 1159898184e3Ssthen 1160898184e3Ssthen if($word eq 'true'){ 1161898184e3Ssthen $at += 3; 1162898184e3Ssthen next_chr; 1163b46d8ef2Safresh1 return defined $alt_true ? $alt_true : $JSON::PP::true; 1164898184e3Ssthen } 1165898184e3Ssthen elsif($word eq 'null'){ 1166898184e3Ssthen $at += 3; 1167898184e3Ssthen next_chr; 1168898184e3Ssthen return undef; 1169898184e3Ssthen } 1170898184e3Ssthen elsif($word eq 'fals'){ 1171898184e3Ssthen $at += 3; 1172898184e3Ssthen if(substr($text,$at,1) eq 'e'){ 1173898184e3Ssthen $at++; 1174898184e3Ssthen next_chr; 1175b46d8ef2Safresh1 return defined $alt_false ? $alt_false : $JSON::PP::false; 1176898184e3Ssthen } 1177898184e3Ssthen } 1178898184e3Ssthen 1179898184e3Ssthen $at--; # for decode_error report 1180898184e3Ssthen 1181898184e3Ssthen decode_error("'null' expected") if ($word =~ /^n/); 1182898184e3Ssthen decode_error("'true' expected") if ($word =~ /^t/); 1183898184e3Ssthen decode_error("'false' expected") if ($word =~ /^f/); 1184898184e3Ssthen decode_error("malformed JSON string, neither array, object, number, string or atom"); 1185898184e3Ssthen } 1186898184e3Ssthen 1187898184e3Ssthen 1188898184e3Ssthen sub number { 1189898184e3Ssthen my $n = ''; 1190898184e3Ssthen my $v; 11919f11ffb7Safresh1 my $is_dec; 11929f11ffb7Safresh1 my $is_exp; 1193898184e3Ssthen 1194898184e3Ssthen if($ch eq '-'){ 1195898184e3Ssthen $n = '-'; 1196898184e3Ssthen next_chr; 1197898184e3Ssthen if (!defined $ch or $ch !~ /\d/) { 1198898184e3Ssthen decode_error("malformed number (no digits after initial minus)"); 1199898184e3Ssthen } 1200898184e3Ssthen } 1201898184e3Ssthen 12029f11ffb7Safresh1 # According to RFC4627, hex or oct digits are invalid. 12039f11ffb7Safresh1 if($ch eq '0'){ 12049f11ffb7Safresh1 my $peek = substr($text,$at,1); 12059f11ffb7Safresh1 if($peek =~ /^[0-9a-dfA-DF]/){ # e may be valid (exponential) 12069f11ffb7Safresh1 decode_error("malformed number (leading zero must not be followed by another digit)"); 12079f11ffb7Safresh1 } 12089f11ffb7Safresh1 $n .= $ch; 12099f11ffb7Safresh1 next_chr; 12109f11ffb7Safresh1 } 12119f11ffb7Safresh1 1212898184e3Ssthen while(defined $ch and $ch =~ /\d/){ 1213898184e3Ssthen $n .= $ch; 1214898184e3Ssthen next_chr; 1215898184e3Ssthen } 1216898184e3Ssthen 1217898184e3Ssthen if(defined $ch and $ch eq '.'){ 1218898184e3Ssthen $n .= '.'; 12199f11ffb7Safresh1 $is_dec = 1; 1220898184e3Ssthen 1221898184e3Ssthen next_chr; 1222898184e3Ssthen if (!defined $ch or $ch !~ /\d/) { 1223898184e3Ssthen decode_error("malformed number (no digits after decimal point)"); 1224898184e3Ssthen } 1225898184e3Ssthen else { 1226898184e3Ssthen $n .= $ch; 1227898184e3Ssthen } 1228898184e3Ssthen 1229898184e3Ssthen while(defined(next_chr) and $ch =~ /\d/){ 1230898184e3Ssthen $n .= $ch; 1231898184e3Ssthen } 1232898184e3Ssthen } 1233898184e3Ssthen 1234898184e3Ssthen if(defined $ch and ($ch eq 'e' or $ch eq 'E')){ 1235898184e3Ssthen $n .= $ch; 12369f11ffb7Safresh1 $is_exp = 1; 1237898184e3Ssthen next_chr; 1238898184e3Ssthen 1239898184e3Ssthen if(defined($ch) and ($ch eq '+' or $ch eq '-')){ 1240898184e3Ssthen $n .= $ch; 1241898184e3Ssthen next_chr; 1242898184e3Ssthen if (!defined $ch or $ch =~ /\D/) { 1243898184e3Ssthen decode_error("malformed number (no digits after exp sign)"); 1244898184e3Ssthen } 1245898184e3Ssthen $n .= $ch; 1246898184e3Ssthen } 1247898184e3Ssthen elsif(defined($ch) and $ch =~ /\d/){ 1248898184e3Ssthen $n .= $ch; 1249898184e3Ssthen } 1250898184e3Ssthen else { 1251898184e3Ssthen decode_error("malformed number (no digits after exp sign)"); 1252898184e3Ssthen } 1253898184e3Ssthen 1254898184e3Ssthen while(defined(next_chr) and $ch =~ /\d/){ 1255898184e3Ssthen $n .= $ch; 1256898184e3Ssthen } 1257898184e3Ssthen 1258898184e3Ssthen } 1259898184e3Ssthen 1260898184e3Ssthen $v .= $n; 1261898184e3Ssthen 12629f11ffb7Safresh1 if ($is_dec or $is_exp) { 12639f11ffb7Safresh1 if ($allow_bignum) { 12649f11ffb7Safresh1 require Math::BigFloat; 12659f11ffb7Safresh1 return Math::BigFloat->new($v); 12669f11ffb7Safresh1 } 12679f11ffb7Safresh1 } else { 12689f11ffb7Safresh1 if (length $v > $max_intsize) { 12699f11ffb7Safresh1 if ($allow_bignum) { # from Adam Sussman 1270898184e3Ssthen require Math::BigInt; 1271898184e3Ssthen return Math::BigInt->new($v); 1272898184e3Ssthen } 1273898184e3Ssthen else { 1274898184e3Ssthen return "$v"; 1275898184e3Ssthen } 1276898184e3Ssthen } 1277898184e3Ssthen } 1278898184e3Ssthen 12799f11ffb7Safresh1 return $is_dec ? $v/1.0 : 0+$v; 1280898184e3Ssthen } 1281898184e3Ssthen 1282*e0680481Safresh1 # Compute how many bytes are in the longest legal official Unicode 1283*e0680481Safresh1 # character 1284*e0680481Safresh1 my $max_unicode_length = do { 1285*e0680481Safresh1 no warnings 'utf8'; 1286*e0680481Safresh1 chr 0x10FFFF; 1287*e0680481Safresh1 }; 1288*e0680481Safresh1 utf8::encode($max_unicode_length); 1289*e0680481Safresh1 $max_unicode_length = length $max_unicode_length; 1290898184e3Ssthen 1291898184e3Ssthen sub is_valid_utf8 { 1292898184e3Ssthen 1293*e0680481Safresh1 # Returns undef (setting $utf8_len to 0) unless the next bytes in $text 1294*e0680481Safresh1 # comprise a well-formed UTF-8 encoded character, in which case, 1295*e0680481Safresh1 # return those bytes, setting $utf8_len to their count. 1296898184e3Ssthen 1297*e0680481Safresh1 my $start_point = substr($text, $at - 1); 1298898184e3Ssthen 1299*e0680481Safresh1 # Look no further than the maximum number of bytes in a single 1300*e0680481Safresh1 # character 1301*e0680481Safresh1 my $limit = $max_unicode_length; 1302*e0680481Safresh1 $limit = length($start_point) if $limit > length($start_point); 1303898184e3Ssthen 1304*e0680481Safresh1 # Find the number of bytes comprising the first character in $text 1305*e0680481Safresh1 # (without having to know the details of its internal representation). 1306*e0680481Safresh1 # This loop will iterate just once on well-formed input. 1307*e0680481Safresh1 while ($limit > 0) { # Until we succeed or exhaust the input 1308*e0680481Safresh1 my $copy = substr($start_point, 0, $limit); 1309*e0680481Safresh1 1310*e0680481Safresh1 # decode() will return true if all bytes are valid; false 1311*e0680481Safresh1 # if any aren't. 1312*e0680481Safresh1 if (utf8::decode($copy)) { 1313*e0680481Safresh1 1314*e0680481Safresh1 # Is valid: get the first character, convert back to bytes, 1315*e0680481Safresh1 # and return those bytes. 1316*e0680481Safresh1 $copy = substr($copy, 0, 1); 1317*e0680481Safresh1 utf8::encode($copy); 1318*e0680481Safresh1 $utf8_len = length $copy; 1319*e0680481Safresh1 return substr($start_point, 0, $utf8_len); 1320*e0680481Safresh1 } 1321*e0680481Safresh1 1322*e0680481Safresh1 # If it didn't work, it could be that there is a full legal character 1323*e0680481Safresh1 # followed by a partial or malformed one. Narrow the window and 1324*e0680481Safresh1 # try again. 1325*e0680481Safresh1 $limit--; 1326*e0680481Safresh1 } 1327*e0680481Safresh1 1328*e0680481Safresh1 # Failed to find a legal UTF-8 character. 1329*e0680481Safresh1 $utf8_len = 0; 1330*e0680481Safresh1 return; 1331898184e3Ssthen } 1332898184e3Ssthen 1333898184e3Ssthen 1334898184e3Ssthen sub decode_error { 1335898184e3Ssthen my $error = shift; 1336898184e3Ssthen my $no_rep = shift; 1337898184e3Ssthen my $str = defined $text ? substr($text, $at) : ''; 1338898184e3Ssthen my $mess = ''; 13399f11ffb7Safresh1 my $type = 'U*'; 13409f11ffb7Safresh1 1341898184e3Ssthen for my $c ( unpack( $type, $str ) ) { # emulate pv_uni_display() ? 1342*e0680481Safresh1 my $chr_c = chr($c); 1343*e0680481Safresh1 $mess .= $chr_c eq '\\' ? '\\\\' 1344*e0680481Safresh1 : $chr_c =~ /[[:print:]]/ ? $chr_c 1345*e0680481Safresh1 : $chr_c eq '\a' ? '\a' 1346*e0680481Safresh1 : $chr_c eq '\t' ? '\t' 1347*e0680481Safresh1 : $chr_c eq '\n' ? '\n' 1348*e0680481Safresh1 : $chr_c eq '\r' ? '\r' 1349*e0680481Safresh1 : $chr_c eq '\f' ? '\f' 1350898184e3Ssthen : sprintf('\x{%x}', $c) 1351898184e3Ssthen ; 1352898184e3Ssthen if ( length $mess >= 20 ) { 1353898184e3Ssthen $mess .= '...'; 1354898184e3Ssthen last; 1355898184e3Ssthen } 1356898184e3Ssthen } 1357898184e3Ssthen 1358898184e3Ssthen unless ( length $mess ) { 1359898184e3Ssthen $mess = '(end of string)'; 1360898184e3Ssthen } 1361898184e3Ssthen 1362898184e3Ssthen Carp::croak ( 1363898184e3Ssthen $no_rep ? "$error" : "$error, at character offset $at (before \"$mess\")" 1364898184e3Ssthen ); 1365898184e3Ssthen 1366898184e3Ssthen } 1367898184e3Ssthen 1368898184e3Ssthen 1369898184e3Ssthen sub _json_object_hook { 1370898184e3Ssthen my $o = $_[0]; 1371898184e3Ssthen my @ks = keys %{$o}; 1372898184e3Ssthen 1373898184e3Ssthen if ( $cb_sk_object and @ks == 1 and exists $cb_sk_object->{ $ks[0] } and ref $cb_sk_object->{ $ks[0] } ) { 1374898184e3Ssthen my @val = $cb_sk_object->{ $ks[0] }->( $o->{$ks[0]} ); 1375b46d8ef2Safresh1 if (@val == 0) { 1376b46d8ef2Safresh1 return $o; 1377b46d8ef2Safresh1 } 1378b46d8ef2Safresh1 elsif (@val == 1) { 1379898184e3Ssthen return $val[0]; 1380898184e3Ssthen } 1381b46d8ef2Safresh1 else { 1382b46d8ef2Safresh1 Carp::croak("filter_json_single_key_object callbacks must not return more than one scalar"); 1383b46d8ef2Safresh1 } 1384898184e3Ssthen } 1385898184e3Ssthen 1386898184e3Ssthen my @val = $cb_object->($o) if ($cb_object); 1387b46d8ef2Safresh1 if (@val == 0) { 1388898184e3Ssthen return $o; 1389898184e3Ssthen } 1390b46d8ef2Safresh1 elsif (@val == 1) { 1391898184e3Ssthen return $val[0]; 1392898184e3Ssthen } 1393b46d8ef2Safresh1 else { 1394b46d8ef2Safresh1 Carp::croak("filter_json_object callbacks must not return more than one scalar"); 1395b46d8ef2Safresh1 } 1396898184e3Ssthen } 1397898184e3Ssthen 1398898184e3Ssthen 1399898184e3Ssthen sub PP_decode_box { 1400898184e3Ssthen { 1401898184e3Ssthen text => $text, 1402898184e3Ssthen at => $at, 1403898184e3Ssthen ch => $ch, 1404898184e3Ssthen len => $len, 1405898184e3Ssthen depth => $depth, 1406898184e3Ssthen encoding => $encoding, 1407898184e3Ssthen is_valid_utf8 => $is_valid_utf8, 1408898184e3Ssthen }; 1409898184e3Ssthen } 1410898184e3Ssthen 1411898184e3Ssthen} # PARSE 1412898184e3Ssthen 1413898184e3Ssthen 1414898184e3Ssthensub _decode_surrogates { # from perlunicode 1415898184e3Ssthen my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00); 1416898184e3Ssthen my $un = pack('U*', $uni); 1417898184e3Ssthen utf8::encode( $un ); 1418898184e3Ssthen return $un; 1419898184e3Ssthen} 1420898184e3Ssthen 1421898184e3Ssthen 1422898184e3Ssthensub _decode_unicode { 1423898184e3Ssthen my $un = pack('U', hex shift); 1424898184e3Ssthen utf8::encode( $un ); 1425898184e3Ssthen return $un; 1426898184e3Ssthen} 1427898184e3Ssthen 1428*e0680481Safresh1sub incr_parse { 1429898184e3Ssthen local $Carp::CarpLevel = 1; 1430898184e3Ssthen ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ ); 1431898184e3Ssthen} 1432898184e3Ssthen 1433898184e3Ssthen 1434*e0680481Safresh1sub incr_skip { 1435898184e3Ssthen ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip; 1436898184e3Ssthen} 1437898184e3Ssthen 1438898184e3Ssthen 1439*e0680481Safresh1sub incr_reset { 1440898184e3Ssthen ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset; 1441898184e3Ssthen} 1442898184e3Ssthen 1443*e0680481Safresh1sub incr_text : lvalue { 1444898184e3Ssthen $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new; 1445898184e3Ssthen 14469f11ffb7Safresh1 if ( $_[0]->{_incr_parser}->{incr_pos} ) { 1447898184e3Ssthen Carp::croak("incr_text cannot be called when the incremental parser already started parsing"); 1448898184e3Ssthen } 1449898184e3Ssthen $_[0]->{_incr_parser}->{incr_text}; 1450898184e3Ssthen} 1451898184e3Ssthen 1452898184e3Ssthen 1453898184e3Ssthen############################### 1454898184e3Ssthen# Utilities 1455898184e3Ssthen# 1456898184e3Ssthen 14579f11ffb7Safresh1# shamelessly copied and modified from JSON::XS code. 1458898184e3Ssthen 1459898184e3Ssthen$JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" }; 1460898184e3Ssthen$JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" }; 1461898184e3Ssthen 1462*e0680481Safresh1sub is_bool { 1463*e0680481Safresh1 if (blessed $_[0]) { 1464*e0680481Safresh1 return ( 1465*e0680481Safresh1 $_[0]->isa("JSON::PP::Boolean") 1466*e0680481Safresh1 or $_[0]->isa("Types::Serialiser::BooleanBase") 1467*e0680481Safresh1 or $_[0]->isa("JSON::XS::Boolean") 1468*e0680481Safresh1 ); 1469*e0680481Safresh1 } 1470*e0680481Safresh1 elsif (CORE_BOOL) { 1471*e0680481Safresh1 BEGIN { CORE_BOOL and warnings->unimport('experimental::builtin') } 1472*e0680481Safresh1 return builtin::is_bool($_[0]); 1473*e0680481Safresh1 } 1474*e0680481Safresh1 return !!0; 1475*e0680481Safresh1} 1476898184e3Ssthen 1477898184e3Ssthensub true { $JSON::PP::true } 1478898184e3Ssthensub false { $JSON::PP::false } 1479898184e3Ssthensub null { undef; } 1480898184e3Ssthen 1481898184e3Ssthen############################### 1482898184e3Ssthen 1483898184e3Ssthenpackage JSON::PP::IncrParser; 1484898184e3Ssthen 1485898184e3Ssthenuse strict; 1486898184e3Ssthen 1487898184e3Ssthenuse constant INCR_M_WS => 0; # initial whitespace skipping 1488898184e3Ssthenuse constant INCR_M_STR => 1; # inside string 1489898184e3Ssthenuse constant INCR_M_BS => 2; # inside backslash 1490898184e3Ssthenuse constant INCR_M_JSON => 3; # outside anything, count nesting 1491898184e3Ssthenuse constant INCR_M_C0 => 4; 1492898184e3Ssthenuse constant INCR_M_C1 => 5; 1493b46d8ef2Safresh1use constant INCR_M_TFN => 6; 1494b46d8ef2Safresh1use constant INCR_M_NUM => 7; 1495898184e3Ssthen 1496*e0680481Safresh1our $VERSION = '1.01'; 1497898184e3Ssthen 1498898184e3Ssthensub new { 1499898184e3Ssthen my ( $class ) = @_; 1500898184e3Ssthen 1501898184e3Ssthen bless { 1502898184e3Ssthen incr_nest => 0, 1503898184e3Ssthen incr_text => undef, 15049f11ffb7Safresh1 incr_pos => 0, 15059f11ffb7Safresh1 incr_mode => 0, 1506898184e3Ssthen }, $class; 1507898184e3Ssthen} 1508898184e3Ssthen 1509898184e3Ssthen 1510898184e3Ssthensub incr_parse { 1511898184e3Ssthen my ( $self, $coder, $text ) = @_; 1512898184e3Ssthen 1513898184e3Ssthen $self->{incr_text} = '' unless ( defined $self->{incr_text} ); 1514898184e3Ssthen 1515898184e3Ssthen if ( defined $text ) { 1516898184e3Ssthen $self->{incr_text} .= $text; 1517898184e3Ssthen } 1518898184e3Ssthen 1519898184e3Ssthen if ( defined wantarray ) { 15209f11ffb7Safresh1 my $max_size = $coder->get_max_size; 15219f11ffb7Safresh1 my $p = $self->{incr_pos}; 1522898184e3Ssthen my @ret; 15239f11ffb7Safresh1 { 1524898184e3Ssthen do { 15259f11ffb7Safresh1 unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) { 15269f11ffb7Safresh1 $self->_incr_parse( $coder ); 1527898184e3Ssthen 15289f11ffb7Safresh1 if ( $max_size and $self->{incr_pos} > $max_size ) { 15299f11ffb7Safresh1 Carp::croak("attempted decode of JSON text of $self->{incr_pos} bytes size, but max_size is set to $max_size"); 15309f11ffb7Safresh1 } 15319f11ffb7Safresh1 unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) { 15329f11ffb7Safresh1 # as an optimisation, do not accumulate white space in the incr buffer 15339f11ffb7Safresh1 if ( $self->{incr_mode} == INCR_M_WS and $self->{incr_pos} ) { 15349f11ffb7Safresh1 $self->{incr_pos} = 0; 15359f11ffb7Safresh1 $self->{incr_text} = ''; 15369f11ffb7Safresh1 } 15379f11ffb7Safresh1 last; 15389f11ffb7Safresh1 } 1539898184e3Ssthen } 1540898184e3Ssthen 1541eac174f2Safresh1 unless ( $coder->get_utf8 ) { 1542eac174f2Safresh1 utf8::decode( $self->{incr_text} ); 1543eac174f2Safresh1 } 1544eac174f2Safresh1 15459f11ffb7Safresh1 my ($obj, $offset) = $coder->PP_decode_json( $self->{incr_text}, 0x00000001 ); 15469f11ffb7Safresh1 push @ret, $obj; 15479f11ffb7Safresh1 use bytes; 15489f11ffb7Safresh1 $self->{incr_text} = substr( $self->{incr_text}, $offset || 0 ); 15499f11ffb7Safresh1 $self->{incr_pos} = 0; 15509f11ffb7Safresh1 $self->{incr_nest} = 0; 15519f11ffb7Safresh1 $self->{incr_mode} = 0; 15529f11ffb7Safresh1 last unless wantarray; 15539f11ffb7Safresh1 } while ( wantarray ); 15549f11ffb7Safresh1 } 1555898184e3Ssthen 15569f11ffb7Safresh1 if ( wantarray ) { 1557898184e3Ssthen return @ret; 1558898184e3Ssthen } 1559898184e3Ssthen else { # in scalar context 1560b46d8ef2Safresh1 return defined $ret[0] ? $ret[0] : undef; 1561898184e3Ssthen } 1562898184e3Ssthen } 1563898184e3Ssthen} 1564898184e3Ssthen 1565898184e3Ssthen 1566898184e3Ssthensub _incr_parse { 15679f11ffb7Safresh1 my ($self, $coder) = @_; 15689f11ffb7Safresh1 my $text = $self->{incr_text}; 1569898184e3Ssthen my $len = length $text; 15709f11ffb7Safresh1 my $p = $self->{incr_pos}; 1571898184e3Ssthen 15729f11ffb7Safresh1INCR_PARSE: 1573898184e3Ssthen while ( $len > $p ) { 1574898184e3Ssthen my $s = substr( $text, $p, 1 ); 15759f11ffb7Safresh1 last INCR_PARSE unless defined $s; 15769f11ffb7Safresh1 my $mode = $self->{incr_mode}; 1577898184e3Ssthen 15789f11ffb7Safresh1 if ( $mode == INCR_M_WS ) { 1579898184e3Ssthen while ( $len > $p ) { 15809f11ffb7Safresh1 $s = substr( $text, $p, 1 ); 15819f11ffb7Safresh1 last INCR_PARSE unless defined $s; 1582*e0680481Safresh1 if ( ord($s) > ord " " ) { 15839f11ffb7Safresh1 if ( $s eq '#' ) { 15849f11ffb7Safresh1 $self->{incr_mode} = INCR_M_C0; 15859f11ffb7Safresh1 redo INCR_PARSE; 15869f11ffb7Safresh1 } else { 1587898184e3Ssthen $self->{incr_mode} = INCR_M_JSON; 15889f11ffb7Safresh1 redo INCR_PARSE; 15899f11ffb7Safresh1 } 15909f11ffb7Safresh1 } 15919f11ffb7Safresh1 $p++; 15929f11ffb7Safresh1 } 15939f11ffb7Safresh1 } elsif ( $mode == INCR_M_BS ) { 15949f11ffb7Safresh1 $p++; 15959f11ffb7Safresh1 $self->{incr_mode} = INCR_M_STR; 15969f11ffb7Safresh1 redo INCR_PARSE; 15979f11ffb7Safresh1 } elsif ( $mode == INCR_M_C0 or $mode == INCR_M_C1 ) { 15989f11ffb7Safresh1 while ( $len > $p ) { 15999f11ffb7Safresh1 $s = substr( $text, $p, 1 ); 16009f11ffb7Safresh1 last INCR_PARSE unless defined $s; 16019f11ffb7Safresh1 if ( $s eq "\n" ) { 16029f11ffb7Safresh1 $self->{incr_mode} = $self->{incr_mode} == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; 1603898184e3Ssthen last; 1604898184e3Ssthen } 16059f11ffb7Safresh1 $p++; 16069f11ffb7Safresh1 } 16079f11ffb7Safresh1 next; 1608b46d8ef2Safresh1 } elsif ( $mode == INCR_M_TFN ) { 1609*e0680481Safresh1 last INCR_PARSE if $p >= $len && $self->{incr_nest}; 1610b46d8ef2Safresh1 while ( $len > $p ) { 1611b46d8ef2Safresh1 $s = substr( $text, $p++, 1 ); 1612b46d8ef2Safresh1 next if defined $s and $s =~ /[rueals]/; 1613b46d8ef2Safresh1 last; 1614b46d8ef2Safresh1 } 1615b46d8ef2Safresh1 $p--; 1616b46d8ef2Safresh1 $self->{incr_mode} = INCR_M_JSON; 1617b46d8ef2Safresh1 1618b46d8ef2Safresh1 last INCR_PARSE unless $self->{incr_nest}; 1619b46d8ef2Safresh1 redo INCR_PARSE; 1620b46d8ef2Safresh1 } elsif ( $mode == INCR_M_NUM ) { 1621*e0680481Safresh1 last INCR_PARSE if $p >= $len && $self->{incr_nest}; 1622b46d8ef2Safresh1 while ( $len > $p ) { 1623b46d8ef2Safresh1 $s = substr( $text, $p++, 1 ); 1624b46d8ef2Safresh1 next if defined $s and $s =~ /[0-9eE.+\-]/; 1625b46d8ef2Safresh1 last; 1626b46d8ef2Safresh1 } 1627b46d8ef2Safresh1 $p--; 1628b46d8ef2Safresh1 $self->{incr_mode} = INCR_M_JSON; 1629b46d8ef2Safresh1 1630b46d8ef2Safresh1 last INCR_PARSE unless $self->{incr_nest}; 1631b46d8ef2Safresh1 redo INCR_PARSE; 16329f11ffb7Safresh1 } elsif ( $mode == INCR_M_STR ) { 16339f11ffb7Safresh1 while ( $len > $p ) { 16349f11ffb7Safresh1 $s = substr( $text, $p, 1 ); 16359f11ffb7Safresh1 last INCR_PARSE unless defined $s; 16369f11ffb7Safresh1 if ( $s eq '"' ) { 16379f11ffb7Safresh1 $p++; 16389f11ffb7Safresh1 $self->{incr_mode} = INCR_M_JSON; 16399f11ffb7Safresh1 16409f11ffb7Safresh1 last INCR_PARSE unless $self->{incr_nest}; 16419f11ffb7Safresh1 redo INCR_PARSE; 16429f11ffb7Safresh1 } 16439f11ffb7Safresh1 elsif ( $s eq '\\' ) { 16449f11ffb7Safresh1 $p++; 16459f11ffb7Safresh1 if ( !defined substr($text, $p, 1) ) { 16469f11ffb7Safresh1 $self->{incr_mode} = INCR_M_BS; 16479f11ffb7Safresh1 last INCR_PARSE; 1648898184e3Ssthen } 1649898184e3Ssthen } 16509f11ffb7Safresh1 $p++; 16519f11ffb7Safresh1 } 16529f11ffb7Safresh1 } elsif ( $mode == INCR_M_JSON ) { 16539f11ffb7Safresh1 while ( $len > $p ) { 16549f11ffb7Safresh1 $s = substr( $text, $p++, 1 ); 16559f11ffb7Safresh1 if ( $s eq "\x00" ) { 16569f11ffb7Safresh1 $p--; 16579f11ffb7Safresh1 last INCR_PARSE; 1658*e0680481Safresh1 } elsif ( $s =~ /^[\t\n\r ]$/) { 16599f11ffb7Safresh1 if ( !$self->{incr_nest} ) { 16609f11ffb7Safresh1 $p--; # do not eat the whitespace, let the next round do it 16619f11ffb7Safresh1 last INCR_PARSE; 16629f11ffb7Safresh1 } 16639f11ffb7Safresh1 next; 1664b46d8ef2Safresh1 } elsif ( $s eq 't' or $s eq 'f' or $s eq 'n' ) { 1665b46d8ef2Safresh1 $self->{incr_mode} = INCR_M_TFN; 1666b46d8ef2Safresh1 redo INCR_PARSE; 1667b46d8ef2Safresh1 } elsif ( $s =~ /^[0-9\-]$/ ) { 1668b46d8ef2Safresh1 $self->{incr_mode} = INCR_M_NUM; 1669b46d8ef2Safresh1 redo INCR_PARSE; 16709f11ffb7Safresh1 } elsif ( $s eq '"' ) { 16719f11ffb7Safresh1 $self->{incr_mode} = INCR_M_STR; 16729f11ffb7Safresh1 redo INCR_PARSE; 16739f11ffb7Safresh1 } elsif ( $s eq '[' or $s eq '{' ) { 1674898184e3Ssthen if ( ++$self->{incr_nest} > $coder->get_max_depth ) { 1675898184e3Ssthen Carp::croak('json text or perl structure exceeds maximum nesting level (max_depth set too low?)'); 1676898184e3Ssthen } 16779f11ffb7Safresh1 next; 16789f11ffb7Safresh1 } elsif ( $s eq ']' or $s eq '}' ) { 16799f11ffb7Safresh1 if ( --$self->{incr_nest} <= 0 ) { 16809f11ffb7Safresh1 last INCR_PARSE; 1681898184e3Ssthen } 16829f11ffb7Safresh1 } elsif ( $s eq '#' ) { 16839f11ffb7Safresh1 $self->{incr_mode} = INCR_M_C1; 16849f11ffb7Safresh1 redo INCR_PARSE; 1685898184e3Ssthen } 16869f11ffb7Safresh1 } 1687898184e3Ssthen } 1688898184e3Ssthen } 1689898184e3Ssthen 16909f11ffb7Safresh1 $self->{incr_pos} = $p; 16919f11ffb7Safresh1 $self->{incr_parsing} = $p ? 1 : 0; # for backward compatibility 1692898184e3Ssthen} 1693898184e3Ssthen 1694898184e3Ssthen 1695898184e3Ssthensub incr_text { 16969f11ffb7Safresh1 if ( $_[0]->{incr_pos} ) { 1697898184e3Ssthen Carp::croak("incr_text cannot be called when the incremental parser already started parsing"); 1698898184e3Ssthen } 1699898184e3Ssthen $_[0]->{incr_text}; 1700898184e3Ssthen} 1701898184e3Ssthen 1702898184e3Ssthen 1703898184e3Ssthensub incr_skip { 1704898184e3Ssthen my $self = shift; 17059f11ffb7Safresh1 $self->{incr_text} = substr( $self->{incr_text}, $self->{incr_pos} ); 17069f11ffb7Safresh1 $self->{incr_pos} = 0; 17079f11ffb7Safresh1 $self->{incr_mode} = 0; 17089f11ffb7Safresh1 $self->{incr_nest} = 0; 1709898184e3Ssthen} 1710898184e3Ssthen 1711898184e3Ssthen 1712898184e3Ssthensub incr_reset { 1713898184e3Ssthen my $self = shift; 1714898184e3Ssthen $self->{incr_text} = undef; 17159f11ffb7Safresh1 $self->{incr_pos} = 0; 1716898184e3Ssthen $self->{incr_mode} = 0; 1717898184e3Ssthen $self->{incr_nest} = 0; 1718898184e3Ssthen} 1719898184e3Ssthen 1720898184e3Ssthen############################### 1721898184e3Ssthen 1722898184e3Ssthen 1723898184e3Ssthen1; 1724898184e3Ssthen__END__ 1725898184e3Ssthen=pod 1726898184e3Ssthen 1727898184e3Ssthen=head1 NAME 1728898184e3Ssthen 1729898184e3SsthenJSON::PP - JSON::XS compatible pure-Perl module. 1730898184e3Ssthen 1731898184e3Ssthen=head1 SYNOPSIS 1732898184e3Ssthen 1733898184e3Ssthen use JSON::PP; 1734898184e3Ssthen 1735898184e3Ssthen # exported functions, they croak on error 1736898184e3Ssthen # and expect/generate UTF-8 1737898184e3Ssthen 1738898184e3Ssthen $utf8_encoded_json_text = encode_json $perl_hash_or_arrayref; 1739898184e3Ssthen $perl_hash_or_arrayref = decode_json $utf8_encoded_json_text; 1740898184e3Ssthen 1741898184e3Ssthen # OO-interface 1742898184e3Ssthen 17439f11ffb7Safresh1 $json = JSON::PP->new->ascii->pretty->allow_nonref; 1744898184e3Ssthen 17459f11ffb7Safresh1 $pretty_printed_json_text = $json->encode( $perl_scalar ); 1746898184e3Ssthen $perl_scalar = $json->decode( $json_text ); 1747898184e3Ssthen 1748898184e3Ssthen # Note that JSON version 2.0 and above will automatically use 1749898184e3Ssthen # JSON::XS or JSON::PP, so you should be able to just: 1750898184e3Ssthen 1751898184e3Ssthen use JSON; 1752898184e3Ssthen 1753898184e3Ssthen 1754898184e3Ssthen=head1 DESCRIPTION 1755898184e3Ssthen 1756b46d8ef2Safresh1JSON::PP is a pure perl JSON decoder/encoder, and (almost) compatible to much 17579f11ffb7Safresh1faster L<JSON::XS> written by Marc Lehmann in C. JSON::PP works as 17589f11ffb7Safresh1a fallback module when you use L<JSON> module without having 17599f11ffb7Safresh1installed JSON::XS. 1760898184e3Ssthen 17619f11ffb7Safresh1Because of this fallback feature of JSON.pm, JSON::PP tries not to 17629f11ffb7Safresh1be more JavaScript-friendly than JSON::XS (i.e. not to escape extra 1763b46d8ef2Safresh1characters such as U+2028 and U+2029, etc), 17649f11ffb7Safresh1in order for you not to lose such JavaScript-friendliness silently 17659f11ffb7Safresh1when you use JSON.pm and install JSON::XS for speed or by accident. 17669f11ffb7Safresh1If you need JavaScript-friendly RFC7159-compliant pure perl module, 17679f11ffb7Safresh1try L<JSON::Tiny>, which is derived from L<Mojolicious> web 17689f11ffb7Safresh1framework and is also smaller and faster than JSON::PP. 1769898184e3Ssthen 17709f11ffb7Safresh1JSON::PP has been in the Perl core since Perl 5.14, mainly for 17719f11ffb7Safresh1CPAN toolchain modules to parse META.json. 1772898184e3Ssthen 1773898184e3Ssthen=head1 FUNCTIONAL INTERFACE 1774898184e3Ssthen 17759f11ffb7Safresh1This section is taken from JSON::XS almost verbatim. C<encode_json> 17769f11ffb7Safresh1and C<decode_json> are exported by default. 1777898184e3Ssthen 1778898184e3Ssthen=head2 encode_json 1779898184e3Ssthen 1780898184e3Ssthen $json_text = encode_json $perl_scalar 1781898184e3Ssthen 17829f11ffb7Safresh1Converts the given Perl data structure to a UTF-8 encoded, binary string 17839f11ffb7Safresh1(that is, the string contains octets only). Croaks on error. 1784898184e3Ssthen 1785898184e3SsthenThis function call is functionally identical to: 1786898184e3Ssthen 1787898184e3Ssthen $json_text = JSON::PP->new->utf8->encode($perl_scalar) 1788898184e3Ssthen 17899f11ffb7Safresh1Except being faster. 17909f11ffb7Safresh1 1791898184e3Ssthen=head2 decode_json 1792898184e3Ssthen 1793898184e3Ssthen $perl_scalar = decode_json $json_text 1794898184e3Ssthen 1795898184e3SsthenThe opposite of C<encode_json>: expects an UTF-8 (binary) string and tries 1796898184e3Ssthento parse that as an UTF-8 encoded JSON text, returning the resulting 17979f11ffb7Safresh1reference. Croaks on error. 1798898184e3Ssthen 1799898184e3SsthenThis function call is functionally identical to: 1800898184e3Ssthen 1801898184e3Ssthen $perl_scalar = JSON::PP->new->utf8->decode($json_text) 1802898184e3Ssthen 18039f11ffb7Safresh1Except being faster. 18049f11ffb7Safresh1 1805898184e3Ssthen=head2 JSON::PP::is_bool 1806898184e3Ssthen 1807898184e3Ssthen $is_boolean = JSON::PP::is_bool($scalar) 1808898184e3Ssthen 1809898184e3SsthenReturns true if the passed scalar represents either JSON::PP::true or 1810898184e3SsthenJSON::PP::false, two constants that act like C<1> and C<0> respectively 1811898184e3Ssthenand are also used to represent JSON C<true> and C<false> in Perl strings. 1812898184e3Ssthen 1813*e0680481Safresh1On perl 5.36 and above, will also return true when given one of perl's 1814*e0680481Safresh1standard boolean values, such as the result of a comparison. 1815*e0680481Safresh1 1816898184e3SsthenSee L<MAPPING>, below, for more information on how JSON values are mapped to 1817898184e3SsthenPerl. 1818898184e3Ssthen 18199f11ffb7Safresh1=head1 OBJECT-ORIENTED INTERFACE 1820898184e3Ssthen 18219f11ffb7Safresh1This section is also taken from JSON::XS. 1822898184e3Ssthen 18239f11ffb7Safresh1The object oriented interface lets you configure your own encoding or 18249f11ffb7Safresh1decoding style, within the limits of supported formats. 1825898184e3Ssthen 1826898184e3Ssthen=head2 new 1827898184e3Ssthen 1828898184e3Ssthen $json = JSON::PP->new 1829898184e3Ssthen 18309f11ffb7Safresh1Creates a new JSON::PP object that can be used to de/encode JSON 1831b46d8ef2Safresh1strings. All boolean flags described below are by default I<disabled> 1832b46d8ef2Safresh1(with the exception of C<allow_nonref>, which defaults to I<enabled> since 1833b46d8ef2Safresh1version C<4.0>). 1834898184e3Ssthen 18359f11ffb7Safresh1The mutators for flags all return the JSON::PP object again and thus calls can 1836898184e3Ssthenbe chained: 1837898184e3Ssthen 1838898184e3Ssthen my $json = JSON::PP->new->utf8->space_after->encode({a => [1,2]}) 1839898184e3Ssthen => {"a": [1, 2]} 1840898184e3Ssthen 1841898184e3Ssthen=head2 ascii 1842898184e3Ssthen 1843898184e3Ssthen $json = $json->ascii([$enable]) 1844898184e3Ssthen 1845898184e3Ssthen $enabled = $json->get_ascii 1846898184e3Ssthen 18479f11ffb7Safresh1If C<$enable> is true (or missing), then the C<encode> method will not 18489f11ffb7Safresh1generate characters outside the code range C<0..127> (which is ASCII). Any 18499f11ffb7Safresh1Unicode characters outside that range will be escaped using either a 18509f11ffb7Safresh1single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, 18519f11ffb7Safresh1as per RFC4627. The resulting encoded JSON text can be treated as a native 18529f11ffb7Safresh1Unicode string, an ascii-encoded, latin1-encoded or UTF-8 encoded string, 18539f11ffb7Safresh1or any other superset of ASCII. 1854898184e3Ssthen 18559f11ffb7Safresh1If C<$enable> is false, then the C<encode> method will not escape Unicode 18569f11ffb7Safresh1characters unless required by the JSON syntax or other flags. This results 18579f11ffb7Safresh1in a faster and more compact format. 1858898184e3Ssthen 18599f11ffb7Safresh1See also the section I<ENCODING/CODESET FLAG NOTES> later in this document. 18609f11ffb7Safresh1 18619f11ffb7Safresh1The main use for this flag is to produce JSON texts that can be 18629f11ffb7Safresh1transmitted over a 7-bit channel, as the encoded JSON texts will not 18639f11ffb7Safresh1contain any 8 bit characters. 1864898184e3Ssthen 1865898184e3Ssthen JSON::PP->new->ascii(1)->encode([chr 0x10401]) 1866898184e3Ssthen => ["\ud801\udc01"] 1867898184e3Ssthen 1868898184e3Ssthen=head2 latin1 1869898184e3Ssthen 1870898184e3Ssthen $json = $json->latin1([$enable]) 1871898184e3Ssthen 1872898184e3Ssthen $enabled = $json->get_latin1 1873898184e3Ssthen 18749f11ffb7Safresh1If C<$enable> is true (or missing), then the C<encode> method will encode 18759f11ffb7Safresh1the resulting JSON text as latin1 (or iso-8859-1), escaping any characters 18769f11ffb7Safresh1outside the code range C<0..255>. The resulting string can be treated as a 18779f11ffb7Safresh1latin1-encoded JSON text or a native Unicode string. The C<decode> method 18789f11ffb7Safresh1will not be affected in any way by this flag, as C<decode> by default 18799f11ffb7Safresh1expects Unicode, which is a strict superset of latin1. 1880898184e3Ssthen 18819f11ffb7Safresh1If C<$enable> is false, then the C<encode> method will not escape Unicode 18829f11ffb7Safresh1characters unless required by the JSON syntax or other flags. 1883898184e3Ssthen 18849f11ffb7Safresh1See also the section I<ENCODING/CODESET FLAG NOTES> later in this document. 18859f11ffb7Safresh1 18869f11ffb7Safresh1The main use for this flag is efficiently encoding binary data as JSON 18879f11ffb7Safresh1text, as most octets will not be escaped, resulting in a smaller encoded 18889f11ffb7Safresh1size. The disadvantage is that the resulting JSON text is encoded 18899f11ffb7Safresh1in latin1 (and must correctly be treated as such when storing and 18909f11ffb7Safresh1transferring), a rare encoding for JSON. It is therefore most useful when 18919f11ffb7Safresh1you want to store data structures known to contain binary data efficiently 18929f11ffb7Safresh1in files or databases, not when talking to other JSON encoders/decoders. 18939f11ffb7Safresh1 18949f11ffb7Safresh1 JSON::PP->new->latin1->encode (["\x{89}\x{abc}"] 1895898184e3Ssthen => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) 1896898184e3Ssthen 1897898184e3Ssthen=head2 utf8 1898898184e3Ssthen 1899898184e3Ssthen $json = $json->utf8([$enable]) 1900898184e3Ssthen 1901898184e3Ssthen $enabled = $json->get_utf8 1902898184e3Ssthen 19039f11ffb7Safresh1If C<$enable> is true (or missing), then the C<encode> method will encode 19049f11ffb7Safresh1the JSON result into UTF-8, as required by many protocols, while the 19059f11ffb7Safresh1C<decode> method expects to be handled an UTF-8-encoded string. Please 19069f11ffb7Safresh1note that UTF-8-encoded strings do not contain any characters outside the 19079f11ffb7Safresh1range C<0..255>, they are thus useful for bytewise/binary I/O. In future 19089f11ffb7Safresh1versions, enabling this option might enable autodetection of the UTF-16 19099f11ffb7Safresh1and UTF-32 encoding families, as described in RFC4627. 1910898184e3Ssthen 19119f11ffb7Safresh1If C<$enable> is false, then the C<encode> method will return the JSON 19129f11ffb7Safresh1string as a (non-encoded) Unicode string, while C<decode> expects thus a 19139f11ffb7Safresh1Unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs 19149f11ffb7Safresh1to be done yourself, e.g. using the Encode module. 1915898184e3Ssthen 19169f11ffb7Safresh1See also the section I<ENCODING/CODESET FLAG NOTES> later in this document. 1917898184e3Ssthen 1918898184e3SsthenExample, output UTF-16BE-encoded JSON: 1919898184e3Ssthen 1920898184e3Ssthen use Encode; 1921898184e3Ssthen $jsontext = encode "UTF-16BE", JSON::PP->new->encode ($object); 1922898184e3Ssthen 1923898184e3SsthenExample, decode UTF-32LE-encoded JSON: 1924898184e3Ssthen 1925898184e3Ssthen use Encode; 1926898184e3Ssthen $object = JSON::PP->new->decode (decode "UTF-32LE", $jsontext); 1927898184e3Ssthen 1928898184e3Ssthen=head2 pretty 1929898184e3Ssthen 1930898184e3Ssthen $json = $json->pretty([$enable]) 1931898184e3Ssthen 1932898184e3SsthenThis enables (or disables) all of the C<indent>, C<space_before> and 19339f11ffb7Safresh1C<space_after> (and in the future possibly more) flags in one call to 19349f11ffb7Safresh1generate the most readable (or most compact) form possible. 1935898184e3Ssthen 1936898184e3Ssthen=head2 indent 1937898184e3Ssthen 1938898184e3Ssthen $json = $json->indent([$enable]) 1939898184e3Ssthen 1940898184e3Ssthen $enabled = $json->get_indent 1941898184e3Ssthen 19429f11ffb7Safresh1If C<$enable> is true (or missing), then the C<encode> method will use a multiline 19439f11ffb7Safresh1format as output, putting every array member or object/hash key-value pair 19449f11ffb7Safresh1into its own line, indenting them properly. 19459f11ffb7Safresh1 19469f11ffb7Safresh1If C<$enable> is false, no newlines or indenting will be produced, and the 19479f11ffb7Safresh1resulting JSON text is guaranteed not to contain any C<newlines>. 19489f11ffb7Safresh1 19499f11ffb7Safresh1This setting has no effect when decoding JSON texts. 19509f11ffb7Safresh1 1951898184e3SsthenThe default indent space length is three. 1952898184e3SsthenYou can use C<indent_length> to change the length. 1953898184e3Ssthen 1954898184e3Ssthen=head2 space_before 1955898184e3Ssthen 1956898184e3Ssthen $json = $json->space_before([$enable]) 1957898184e3Ssthen 1958898184e3Ssthen $enabled = $json->get_space_before 1959898184e3Ssthen 1960898184e3SsthenIf C<$enable> is true (or missing), then the C<encode> method will add an extra 1961898184e3Ssthenoptional space before the C<:> separating keys from values in JSON objects. 1962898184e3Ssthen 1963898184e3SsthenIf C<$enable> is false, then the C<encode> method will not add any extra 1964898184e3Ssthenspace at those places. 1965898184e3Ssthen 19669f11ffb7Safresh1This setting has no effect when decoding JSON texts. You will also 19679f11ffb7Safresh1most likely combine this setting with C<space_after>. 1968898184e3Ssthen 1969898184e3SsthenExample, space_before enabled, space_after and indent disabled: 1970898184e3Ssthen 1971898184e3Ssthen {"key" :"value"} 1972898184e3Ssthen 1973898184e3Ssthen=head2 space_after 1974898184e3Ssthen 1975898184e3Ssthen $json = $json->space_after([$enable]) 1976898184e3Ssthen 1977898184e3Ssthen $enabled = $json->get_space_after 1978898184e3Ssthen 1979898184e3SsthenIf C<$enable> is true (or missing), then the C<encode> method will add an extra 1980898184e3Ssthenoptional space after the C<:> separating keys from values in JSON objects 1981898184e3Ssthenand extra whitespace after the C<,> separating key-value pairs and array 1982898184e3Ssthenmembers. 1983898184e3Ssthen 1984898184e3SsthenIf C<$enable> is false, then the C<encode> method will not add any extra 1985898184e3Ssthenspace at those places. 1986898184e3Ssthen 1987898184e3SsthenThis setting has no effect when decoding JSON texts. 1988898184e3Ssthen 1989898184e3SsthenExample, space_before and indent disabled, space_after enabled: 1990898184e3Ssthen 1991898184e3Ssthen {"key": "value"} 1992898184e3Ssthen 1993898184e3Ssthen=head2 relaxed 1994898184e3Ssthen 1995898184e3Ssthen $json = $json->relaxed([$enable]) 1996898184e3Ssthen 1997898184e3Ssthen $enabled = $json->get_relaxed 1998898184e3Ssthen 1999898184e3SsthenIf C<$enable> is true (or missing), then C<decode> will accept some 2000898184e3Ssthenextensions to normal JSON syntax (see below). C<encode> will not be 2001898184e3Ssthenaffected in anyway. I<Be aware that this option makes you accept invalid 2002898184e3SsthenJSON texts as if they were valid!>. I suggest only to use this option to 2003898184e3Ssthenparse application-specific files written by humans (configuration files, 2004898184e3Ssthenresource files etc.) 2005898184e3Ssthen 2006898184e3SsthenIf C<$enable> is false (the default), then C<decode> will only accept 2007898184e3Ssthenvalid JSON texts. 2008898184e3Ssthen 2009898184e3SsthenCurrently accepted extensions are: 2010898184e3Ssthen 2011898184e3Ssthen=over 4 2012898184e3Ssthen 2013898184e3Ssthen=item * list items can have an end-comma 2014898184e3Ssthen 2015898184e3SsthenJSON I<separates> array elements and key-value pairs with commas. This 2016898184e3Ssthencan be annoying if you write JSON texts manually and want to be able to 2017898184e3Ssthenquickly append elements, so this extension accepts comma at the end of 2018898184e3Ssthensuch items not just between them: 2019898184e3Ssthen 2020898184e3Ssthen [ 2021898184e3Ssthen 1, 2022898184e3Ssthen 2, <- this comma not normally allowed 2023898184e3Ssthen ] 2024898184e3Ssthen { 2025898184e3Ssthen "k1": "v1", 2026898184e3Ssthen "k2": "v2", <- this comma not normally allowed 2027898184e3Ssthen } 2028898184e3Ssthen 2029898184e3Ssthen=item * shell-style '#'-comments 2030898184e3Ssthen 2031898184e3SsthenWhenever JSON allows whitespace, shell-style comments are additionally 2032898184e3Ssthenallowed. They are terminated by the first carriage-return or line-feed 2033898184e3Ssthencharacter, after which more white-space and comments are allowed. 2034898184e3Ssthen 2035898184e3Ssthen [ 2036898184e3Ssthen 1, # this comment not allowed in JSON 2037898184e3Ssthen # neither this one... 2038898184e3Ssthen ] 2039898184e3Ssthen 20409f11ffb7Safresh1=item * C-style multiple-line '/* */'-comments (JSON::PP only) 20419f11ffb7Safresh1 20429f11ffb7Safresh1Whenever JSON allows whitespace, C-style multiple-line comments are additionally 20439f11ffb7Safresh1allowed. Everything between C</*> and C<*/> is a comment, after which 20449f11ffb7Safresh1more white-space and comments are allowed. 20459f11ffb7Safresh1 20469f11ffb7Safresh1 [ 20479f11ffb7Safresh1 1, /* this comment not allowed in JSON */ 20489f11ffb7Safresh1 /* neither this one... */ 20499f11ffb7Safresh1 ] 20509f11ffb7Safresh1 20519f11ffb7Safresh1=item * C++-style one-line '//'-comments (JSON::PP only) 20529f11ffb7Safresh1 20539f11ffb7Safresh1Whenever JSON allows whitespace, C++-style one-line comments are additionally 20549f11ffb7Safresh1allowed. They are terminated by the first carriage-return or line-feed 20559f11ffb7Safresh1character, after which more white-space and comments are allowed. 20569f11ffb7Safresh1 20579f11ffb7Safresh1 [ 20589f11ffb7Safresh1 1, // this comment not allowed in JSON 20599f11ffb7Safresh1 // neither this one... 20609f11ffb7Safresh1 ] 20619f11ffb7Safresh1 2062b46d8ef2Safresh1=item * literal ASCII TAB characters in strings 2063b46d8ef2Safresh1 2064b46d8ef2Safresh1Literal ASCII TAB characters are now allowed in strings (and treated as 2065b46d8ef2Safresh1C<\t>). 2066b46d8ef2Safresh1 2067b46d8ef2Safresh1 [ 2068b46d8ef2Safresh1 "Hello\tWorld", 2069b46d8ef2Safresh1 "Hello<TAB>World", # literal <TAB> would not normally be allowed 2070b46d8ef2Safresh1 ] 2071b46d8ef2Safresh1 2072898184e3Ssthen=back 2073898184e3Ssthen 2074898184e3Ssthen=head2 canonical 2075898184e3Ssthen 2076898184e3Ssthen $json = $json->canonical([$enable]) 2077898184e3Ssthen 2078898184e3Ssthen $enabled = $json->get_canonical 2079898184e3Ssthen 2080898184e3SsthenIf C<$enable> is true (or missing), then the C<encode> method will output JSON objects 2081898184e3Ssthenby sorting their keys. This is adding a comparatively high overhead. 2082898184e3Ssthen 2083898184e3SsthenIf C<$enable> is false, then the C<encode> method will output key-value 2084898184e3Ssthenpairs in the order Perl stores them (which will likely change between runs 20859f11ffb7Safresh1of the same script, and can change even within the same run from 5.18 20869f11ffb7Safresh1onwards). 2087898184e3Ssthen 2088898184e3SsthenThis option is useful if you want the same data structure to be encoded as 2089898184e3Ssthenthe same JSON text (given the same overall settings). If it is disabled, 2090898184e3Ssthenthe same hash might be encoded differently even if contains the same data, 2091898184e3Ssthenas key-value pairs have no inherent ordering in Perl. 2092898184e3Ssthen 2093898184e3SsthenThis setting has no effect when decoding JSON texts. 2094898184e3Ssthen 20959f11ffb7Safresh1This setting has currently no effect on tied hashes. 2096898184e3Ssthen 2097898184e3Ssthen=head2 allow_nonref 2098898184e3Ssthen 2099898184e3Ssthen $json = $json->allow_nonref([$enable]) 2100898184e3Ssthen 2101898184e3Ssthen $enabled = $json->get_allow_nonref 2102898184e3Ssthen 2103b46d8ef2Safresh1Unlike other boolean options, this opotion is enabled by default beginning 2104b46d8ef2Safresh1with version C<4.0>. 2105b46d8ef2Safresh1 2106898184e3SsthenIf C<$enable> is true (or missing), then the C<encode> method can convert a 2107898184e3Ssthennon-reference into its corresponding string, number or null JSON value, 2108898184e3Ssthenwhich is an extension to RFC4627. Likewise, C<decode> will accept those JSON 2109898184e3Ssthenvalues instead of croaking. 2110898184e3Ssthen 2111898184e3SsthenIf C<$enable> is false, then the C<encode> method will croak if it isn't 2112898184e3Ssthenpassed an arrayref or hashref, as JSON texts must either be an object 2113898184e3Ssthenor array. Likewise, C<decode> will croak if given something that is not a 2114898184e3SsthenJSON object or array. 2115898184e3Ssthen 2116b46d8ef2Safresh1Example, encode a Perl scalar as JSON value without enabled C<allow_nonref>, 2117b46d8ef2Safresh1resulting in an error: 21189f11ffb7Safresh1 2119b46d8ef2Safresh1 JSON::PP->new->allow_nonref(0)->encode ("Hello, World!") 2120b46d8ef2Safresh1 => hash- or arrayref expected... 2121898184e3Ssthen 2122898184e3Ssthen=head2 allow_unknown 2123898184e3Ssthen 2124898184e3Ssthen $json = $json->allow_unknown([$enable]) 2125898184e3Ssthen 2126898184e3Ssthen $enabled = $json->get_allow_unknown 2127898184e3Ssthen 21289f11ffb7Safresh1If C<$enable> is true (or missing), then C<encode> will I<not> throw an 2129898184e3Ssthenexception when it encounters values it cannot represent in JSON (for 21309f11ffb7Safresh1example, filehandles) but instead will encode a JSON C<null> value. Note 21319f11ffb7Safresh1that blessed objects are not included here and are handled separately by 21329f11ffb7Safresh1c<allow_blessed>. 2133898184e3Ssthen 21349f11ffb7Safresh1If C<$enable> is false (the default), then C<encode> will throw an 2135898184e3Ssthenexception when it encounters anything it cannot encode as JSON. 2136898184e3Ssthen 21379f11ffb7Safresh1This option does not affect C<decode> in any way, and it is recommended to 21389f11ffb7Safresh1leave it off unless you know your communications partner. 2139898184e3Ssthen 2140898184e3Ssthen=head2 allow_blessed 2141898184e3Ssthen 2142898184e3Ssthen $json = $json->allow_blessed([$enable]) 2143898184e3Ssthen 2144898184e3Ssthen $enabled = $json->get_allow_blessed 2145898184e3Ssthen 21469f11ffb7Safresh1See L<OBJECT SERIALISATION> for details. 21479f11ffb7Safresh1 2148898184e3SsthenIf C<$enable> is true (or missing), then the C<encode> method will not 21499f11ffb7Safresh1barf when it encounters a blessed reference that it cannot convert 21509f11ffb7Safresh1otherwise. Instead, a JSON C<null> value is encoded instead of the object. 2151898184e3Ssthen 2152898184e3SsthenIf C<$enable> is false (the default), then C<encode> will throw an 21539f11ffb7Safresh1exception when it encounters a blessed object that it cannot convert 21549f11ffb7Safresh1otherwise. 21559f11ffb7Safresh1 21569f11ffb7Safresh1This setting has no effect on C<decode>. 2157898184e3Ssthen 2158898184e3Ssthen=head2 convert_blessed 2159898184e3Ssthen 2160898184e3Ssthen $json = $json->convert_blessed([$enable]) 2161898184e3Ssthen 2162898184e3Ssthen $enabled = $json->get_convert_blessed 2163898184e3Ssthen 21649f11ffb7Safresh1See L<OBJECT SERIALISATION> for details. 21659f11ffb7Safresh1 2166898184e3SsthenIf C<$enable> is true (or missing), then C<encode>, upon encountering a 2167898184e3Ssthenblessed object, will check for the availability of the C<TO_JSON> method 21689f11ffb7Safresh1on the object's class. If found, it will be called in scalar context and 21699f11ffb7Safresh1the resulting scalar will be encoded instead of the object. 2170898184e3Ssthen 2171898184e3SsthenThe C<TO_JSON> method may safely call die if it wants. If C<TO_JSON> 2172898184e3Ssthenreturns other blessed objects, those will be handled in the same 2173898184e3Ssthenway. C<TO_JSON> must take care of not causing an endless recursion cycle 2174898184e3Ssthen(== crash) in this case. The name of C<TO_JSON> was chosen because other 2175898184e3Ssthenmethods called by the Perl core (== not by the user of the object) are 21769f11ffb7Safresh1usually in upper case letters and to avoid collisions with any C<to_json> 2177898184e3Ssthenfunction or method. 2178898184e3Ssthen 21799f11ffb7Safresh1If C<$enable> is false (the default), then C<encode> will not consider 21809f11ffb7Safresh1this type of conversion. 2181898184e3Ssthen 21829f11ffb7Safresh1This setting has no effect on C<decode>. 2183898184e3Ssthen 2184b46d8ef2Safresh1=head2 allow_tags 2185b46d8ef2Safresh1 2186b46d8ef2Safresh1 $json = $json->allow_tags([$enable]) 2187b46d8ef2Safresh1 2188b46d8ef2Safresh1 $enabled = $json->get_allow_tags 2189b46d8ef2Safresh1 2190b46d8ef2Safresh1See L<OBJECT SERIALISATION> for details. 2191b46d8ef2Safresh1 2192b46d8ef2Safresh1If C<$enable> is true (or missing), then C<encode>, upon encountering a 2193b46d8ef2Safresh1blessed object, will check for the availability of the C<FREEZE> method on 2194b46d8ef2Safresh1the object's class. If found, it will be used to serialise the object into 2195b46d8ef2Safresh1a nonstandard tagged JSON value (that JSON decoders cannot decode). 2196b46d8ef2Safresh1 2197b46d8ef2Safresh1It also causes C<decode> to parse such tagged JSON values and deserialise 2198b46d8ef2Safresh1them via a call to the C<THAW> method. 2199b46d8ef2Safresh1 2200b46d8ef2Safresh1If C<$enable> is false (the default), then C<encode> will not consider 2201b46d8ef2Safresh1this type of conversion, and tagged JSON values will cause a parse error 2202b46d8ef2Safresh1in C<decode>, as if tags were not part of the grammar. 2203b46d8ef2Safresh1 2204b46d8ef2Safresh1=head2 boolean_values 2205b46d8ef2Safresh1 2206b46d8ef2Safresh1 $json->boolean_values([$false, $true]) 2207b46d8ef2Safresh1 2208b46d8ef2Safresh1 ($false, $true) = $json->get_boolean_values 2209b46d8ef2Safresh1 2210b46d8ef2Safresh1By default, JSON booleans will be decoded as overloaded 2211b46d8ef2Safresh1C<$JSON::PP::false> and C<$JSON::PP::true> objects. 2212b46d8ef2Safresh1 2213b46d8ef2Safresh1With this method you can specify your own boolean values for decoding - 2214b46d8ef2Safresh1on decode, JSON C<false> will be decoded as a copy of C<$false>, and JSON 2215b46d8ef2Safresh1C<true> will be decoded as C<$true> ("copy" here is the same thing as 2216b46d8ef2Safresh1assigning a value to another variable, i.e. C<$copy = $false>). 2217b46d8ef2Safresh1 2218b46d8ef2Safresh1This is useful when you want to pass a decoded data structure directly 2219b46d8ef2Safresh1to other serialisers like YAML, Data::MessagePack and so on. 2220b46d8ef2Safresh1 2221b46d8ef2Safresh1Note that this works only when you C<decode>. You can set incompatible 2222b46d8ef2Safresh1boolean objects (like L<boolean>), but when you C<encode> a data structure 2223b46d8ef2Safresh1with such boolean objects, you still need to enable C<convert_blessed> 2224b46d8ef2Safresh1(and add a C<TO_JSON> method if necessary). 2225b46d8ef2Safresh1 2226b46d8ef2Safresh1Calling this method without any arguments will reset the booleans 2227b46d8ef2Safresh1to their default values. 2228b46d8ef2Safresh1 2229b46d8ef2Safresh1C<get_boolean_values> will return both C<$false> and C<$true> values, or 2230b46d8ef2Safresh1the empty list when they are set to the default. 2231b46d8ef2Safresh1 2232*e0680481Safresh1=head2 core_bools 2233*e0680481Safresh1 2234*e0680481Safresh1 $json->core_bools([$enable]); 2235*e0680481Safresh1 2236*e0680481Safresh1If C<$enable> is true (or missing), then C<decode>, will produce standard 2237*e0680481Safresh1perl boolean values. Equivalent to calling: 2238*e0680481Safresh1 2239*e0680481Safresh1 $json->boolean_values(!!1, !!0) 2240*e0680481Safresh1 2241*e0680481Safresh1C<get_core_bools> will return true if this has been set. On perl 5.36, it will 2242*e0680481Safresh1also return true if the boolean values have been set to perl's core booleans 2243*e0680481Safresh1using the C<boolean_values> method. 2244*e0680481Safresh1 2245*e0680481Safresh1The methods C<unblessed_bool> and C<get_unblessed_bool> are provided as aliases 2246*e0680481Safresh1for compatibility with L<Cpanel::JSON::XS>. 2247*e0680481Safresh1 2248898184e3Ssthen=head2 filter_json_object 2249898184e3Ssthen 2250898184e3Ssthen $json = $json->filter_json_object([$coderef]) 2251898184e3Ssthen 2252898184e3SsthenWhen C<$coderef> is specified, it will be called from C<decode> each 2253b46d8ef2Safresh1time it decodes a JSON object. The only argument is a reference to 2254b46d8ef2Safresh1the newly-created hash. If the code references returns a single scalar 2255b46d8ef2Safresh1(which need not be a reference), this value (or rather a copy of it) is 2256b46d8ef2Safresh1inserted into the deserialised data structure. If it returns an empty 2257b46d8ef2Safresh1list (NOTE: I<not> C<undef>, which is a valid scalar), the original 2258b46d8ef2Safresh1deserialised hash will be inserted. This setting can slow down decoding 2259b46d8ef2Safresh1considerably. 2260898184e3Ssthen 2261898184e3SsthenWhen C<$coderef> is omitted or undefined, any existing callback will 2262898184e3Ssthenbe removed and C<decode> will not change the deserialised hash in any 2263898184e3Ssthenway. 2264898184e3Ssthen 2265898184e3SsthenExample, convert all JSON objects into the integer 5: 2266898184e3Ssthen 2267898184e3Ssthen my $js = JSON::PP->new->filter_json_object(sub { 5 }); 2268898184e3Ssthen # returns [5] 2269b46d8ef2Safresh1 $js->decode('[{}]'); 2270b46d8ef2Safresh1 # returns 5 2271898184e3Ssthen $js->decode('{"a":1, "b":2}'); 2272898184e3Ssthen 2273898184e3Ssthen=head2 filter_json_single_key_object 2274898184e3Ssthen 2275898184e3Ssthen $json = $json->filter_json_single_key_object($key [=> $coderef]) 2276898184e3Ssthen 2277898184e3SsthenWorks remotely similar to C<filter_json_object>, but is only called for 2278898184e3SsthenJSON objects having a single key named C<$key>. 2279898184e3Ssthen 2280898184e3SsthenThis C<$coderef> is called before the one specified via 2281898184e3SsthenC<filter_json_object>, if any. It gets passed the single value in the JSON 2282898184e3Ssthenobject. If it returns a single value, it will be inserted into the data 2283898184e3Ssthenstructure. If it returns nothing (not even C<undef> but the empty list), 2284898184e3Ssthenthe callback from C<filter_json_object> will be called next, as if no 2285898184e3Ssthensingle-key callback were specified. 2286898184e3Ssthen 2287898184e3SsthenIf C<$coderef> is omitted or undefined, the corresponding callback will be 2288898184e3Ssthendisabled. There can only ever be one callback for a given key. 2289898184e3Ssthen 2290898184e3SsthenAs this callback gets called less often then the C<filter_json_object> 2291898184e3Ssthenone, decoding speed will not usually suffer as much. Therefore, single-key 2292898184e3Ssthenobjects make excellent targets to serialise Perl objects into, especially 2293898184e3Ssthenas single-key JSON objects are as close to the type-tagged value concept 2294898184e3Ssthenas JSON gets (it's basically an ID/VALUE tuple). Of course, JSON does not 2295898184e3Ssthensupport this in any way, so you need to make sure your data never looks 2296898184e3Ssthenlike a serialised Perl hash. 2297898184e3Ssthen 2298898184e3SsthenTypical names for the single object key are C<__class_whatever__>, or 2299898184e3SsthenC<$__dollars_are_rarely_used__$> or C<}ugly_brace_placement>, or even 2300898184e3Ssthenthings like C<__class_md5sum(classname)__>, to reduce the risk of clashing 2301898184e3Ssthenwith real hashes. 2302898184e3Ssthen 2303898184e3SsthenExample, decode JSON objects of the form C<< { "__widget__" => <id> } >> 2304898184e3Sstheninto the corresponding C<< $WIDGET{<id>} >> object: 2305898184e3Ssthen 2306898184e3Ssthen # return whatever is in $WIDGET{5}: 2307898184e3Ssthen JSON::PP 2308898184e3Ssthen ->new 2309898184e3Ssthen ->filter_json_single_key_object (__widget__ => sub { 2310898184e3Ssthen $WIDGET{ $_[0] } 2311898184e3Ssthen }) 2312898184e3Ssthen ->decode ('{"__widget__": 5') 2313898184e3Ssthen 2314898184e3Ssthen # this can be used with a TO_JSON method in some "widget" class 2315898184e3Ssthen # for serialisation to json: 2316898184e3Ssthen sub WidgetBase::TO_JSON { 2317898184e3Ssthen my ($self) = @_; 2318898184e3Ssthen 2319898184e3Ssthen unless ($self->{id}) { 2320898184e3Ssthen $self->{id} = ..get..some..id..; 2321898184e3Ssthen $WIDGET{$self->{id}} = $self; 2322898184e3Ssthen } 2323898184e3Ssthen 2324898184e3Ssthen { __widget__ => $self->{id} } 2325898184e3Ssthen } 2326898184e3Ssthen 2327898184e3Ssthen=head2 shrink 2328898184e3Ssthen 2329898184e3Ssthen $json = $json->shrink([$enable]) 2330898184e3Ssthen 2331898184e3Ssthen $enabled = $json->get_shrink 2332898184e3Ssthen 23339f11ffb7Safresh1If C<$enable> is true (or missing), the string returned by C<encode> will 23349f11ffb7Safresh1be shrunk (i.e. downgraded if possible). 2335898184e3Ssthen 23369f11ffb7Safresh1The actual definition of what shrink does might change in future versions, 23379f11ffb7Safresh1but it will always try to save space at the expense of time. 2338898184e3Ssthen 23399f11ffb7Safresh1If C<$enable> is false, then JSON::PP does nothing. 2340898184e3Ssthen 2341898184e3Ssthen=head2 max_depth 2342898184e3Ssthen 2343898184e3Ssthen $json = $json->max_depth([$maximum_nesting_depth]) 2344898184e3Ssthen 2345898184e3Ssthen $max_depth = $json->get_max_depth 2346898184e3Ssthen 2347898184e3SsthenSets the maximum nesting level (default C<512>) accepted while encoding 2348898184e3Ssthenor decoding. If a higher nesting level is detected in JSON text or a Perl 2349898184e3Ssthendata structure, then the encoder and decoder will stop and croak at that 2350898184e3Ssthenpoint. 2351898184e3Ssthen 2352898184e3SsthenNesting level is defined by number of hash- or arrayrefs that the encoder 2353898184e3Ssthenneeds to traverse to reach a given point or the number of C<{> or C<[> 2354898184e3Ssthencharacters without their matching closing parenthesis crossed to reach a 2355898184e3Ssthengiven character in a string. 2356898184e3Ssthen 23579f11ffb7Safresh1Setting the maximum depth to one disallows any nesting, so that ensures 23589f11ffb7Safresh1that the object is only a single hash/object or array. 23599f11ffb7Safresh1 2360898184e3SsthenIf no argument is given, the highest possible setting will be used, which 2361898184e3Ssthenis rarely useful. 2362898184e3Ssthen 23639f11ffb7Safresh1See L<JSON::XS/SECURITY CONSIDERATIONS> for more info on why this is useful. 2364898184e3Ssthen 2365898184e3Ssthen=head2 max_size 2366898184e3Ssthen 2367898184e3Ssthen $json = $json->max_size([$maximum_string_size]) 2368898184e3Ssthen 2369898184e3Ssthen $max_size = $json->get_max_size 2370898184e3Ssthen 2371898184e3SsthenSet the maximum length a JSON text may have (in bytes) where decoding is 2372898184e3Ssthenbeing attempted. The default is C<0>, meaning no limit. When C<decode> 2373898184e3Ssthenis called on a string that is longer then this many bytes, it will not 2374898184e3Ssthenattempt to decode the string but throw an exception. This setting has no 2375898184e3Sstheneffect on C<encode> (yet). 2376898184e3Ssthen 2377898184e3SsthenIf no argument is given, the limit check will be deactivated (same as when 2378898184e3SsthenC<0> is specified). 2379898184e3Ssthen 23809f11ffb7Safresh1See L<JSON::XS/SECURITY CONSIDERATIONS> for more info on why this is useful. 2381898184e3Ssthen 2382898184e3Ssthen=head2 encode 2383898184e3Ssthen 2384898184e3Ssthen $json_text = $json->encode($perl_scalar) 2385898184e3Ssthen 23869f11ffb7Safresh1Converts the given Perl value or data structure to its JSON 23879f11ffb7Safresh1representation. Croaks on error. 2388898184e3Ssthen 2389898184e3Ssthen=head2 decode 2390898184e3Ssthen 2391898184e3Ssthen $perl_scalar = $json->decode($json_text) 2392898184e3Ssthen 2393898184e3SsthenThe opposite of C<encode>: expects a JSON text and tries to parse it, 2394898184e3Ssthenreturning the resulting simple scalar or reference. Croaks on error. 2395898184e3Ssthen 2396898184e3Ssthen=head2 decode_prefix 2397898184e3Ssthen 2398898184e3Ssthen ($perl_scalar, $characters) = $json->decode_prefix($json_text) 2399898184e3Ssthen 2400898184e3SsthenThis works like the C<decode> method, but instead of raising an exception 2401898184e3Ssthenwhen there is trailing garbage after the first JSON object, it will 2402898184e3Ssthensilently stop parsing there and return the number of characters consumed 2403898184e3Ssthenso far. 2404898184e3Ssthen 24059f11ffb7Safresh1This is useful if your JSON texts are not delimited by an outer protocol 24069f11ffb7Safresh1and you need to know where the JSON text ends. 24079f11ffb7Safresh1 24089f11ffb7Safresh1 JSON::PP->new->decode_prefix ("[1] the tail") 24099f11ffb7Safresh1 => ([1], 3) 24109f11ffb7Safresh1 24119f11ffb7Safresh1=head1 FLAGS FOR JSON::PP ONLY 24129f11ffb7Safresh1 24139f11ffb7Safresh1The following flags and properties are for JSON::PP only. If you use 24149f11ffb7Safresh1any of these, you can't make your application run faster by replacing 24159f11ffb7Safresh1JSON::PP with JSON::XS. If you need these and also speed boost, 2416b46d8ef2Safresh1you might want to try L<Cpanel::JSON::XS>, a fork of JSON::XS by 2417b46d8ef2Safresh1Reini Urban, which supports some of these (with a different set of 2418b46d8ef2Safresh1incompatibilities). Most of these historical flags are only kept 2419b46d8ef2Safresh1for backward compatibility, and should not be used in a new application. 24209f11ffb7Safresh1 24219f11ffb7Safresh1=head2 allow_singlequote 24229f11ffb7Safresh1 24239f11ffb7Safresh1 $json = $json->allow_singlequote([$enable]) 24249f11ffb7Safresh1 $enabled = $json->get_allow_singlequote 24259f11ffb7Safresh1 24269f11ffb7Safresh1If C<$enable> is true (or missing), then C<decode> will accept 24279f11ffb7Safresh1invalid JSON texts that contain strings that begin and end with 24289f11ffb7Safresh1single quotation marks. C<encode> will not be affected in any way. 24299f11ffb7Safresh1I<Be aware that this option makes you accept invalid JSON texts 24309f11ffb7Safresh1as if they were valid!>. I suggest only to use this option to 24319f11ffb7Safresh1parse application-specific files written by humans (configuration 24329f11ffb7Safresh1files, resource files etc.) 24339f11ffb7Safresh1 24349f11ffb7Safresh1If C<$enable> is false (the default), then C<decode> will only accept 24359f11ffb7Safresh1valid JSON texts. 24369f11ffb7Safresh1 24379f11ffb7Safresh1 $json->allow_singlequote->decode(qq|{"foo":'bar'}|); 24389f11ffb7Safresh1 $json->allow_singlequote->decode(qq|{'foo':"bar"}|); 24399f11ffb7Safresh1 $json->allow_singlequote->decode(qq|{'foo':'bar'}|); 24409f11ffb7Safresh1 24419f11ffb7Safresh1=head2 allow_barekey 24429f11ffb7Safresh1 24439f11ffb7Safresh1 $json = $json->allow_barekey([$enable]) 24449f11ffb7Safresh1 $enabled = $json->get_allow_barekey 24459f11ffb7Safresh1 24469f11ffb7Safresh1If C<$enable> is true (or missing), then C<decode> will accept 24479f11ffb7Safresh1invalid JSON texts that contain JSON objects whose names don't 24489f11ffb7Safresh1begin and end with quotation marks. C<encode> will not be affected 24499f11ffb7Safresh1in any way. I<Be aware that this option makes you accept invalid JSON 24509f11ffb7Safresh1texts as if they were valid!>. I suggest only to use this option to 24519f11ffb7Safresh1parse application-specific files written by humans (configuration 24529f11ffb7Safresh1files, resource files etc.) 24539f11ffb7Safresh1 24549f11ffb7Safresh1If C<$enable> is false (the default), then C<decode> will only accept 24559f11ffb7Safresh1valid JSON texts. 24569f11ffb7Safresh1 24579f11ffb7Safresh1 $json->allow_barekey->decode(qq|{foo:"bar"}|); 24589f11ffb7Safresh1 24599f11ffb7Safresh1=head2 allow_bignum 24609f11ffb7Safresh1 24619f11ffb7Safresh1 $json = $json->allow_bignum([$enable]) 24629f11ffb7Safresh1 $enabled = $json->get_allow_bignum 24639f11ffb7Safresh1 24649f11ffb7Safresh1If C<$enable> is true (or missing), then C<decode> will convert 24659f11ffb7Safresh1big integers Perl cannot handle as integer into L<Math::BigInt> 24669f11ffb7Safresh1objects and convert floating numbers into L<Math::BigFloat> 24679f11ffb7Safresh1objects. C<encode> will convert C<Math::BigInt> and C<Math::BigFloat> 24689f11ffb7Safresh1objects into JSON numbers. 24699f11ffb7Safresh1 24709f11ffb7Safresh1 $json->allow_nonref->allow_bignum; 24719f11ffb7Safresh1 $bigfloat = $json->decode('2.000000000000000000000000001'); 24729f11ffb7Safresh1 print $json->encode($bigfloat); 24739f11ffb7Safresh1 # => 2.000000000000000000000000001 24749f11ffb7Safresh1 24759f11ffb7Safresh1See also L<MAPPING>. 24769f11ffb7Safresh1 24779f11ffb7Safresh1=head2 loose 24789f11ffb7Safresh1 24799f11ffb7Safresh1 $json = $json->loose([$enable]) 24809f11ffb7Safresh1 $enabled = $json->get_loose 24819f11ffb7Safresh1 24829f11ffb7Safresh1If C<$enable> is true (or missing), then C<decode> will accept 24839f11ffb7Safresh1invalid JSON texts that contain unescaped [\x00-\x1f\x22\x5c] 24849f11ffb7Safresh1characters. C<encode> will not be affected in any way. 24859f11ffb7Safresh1I<Be aware that this option makes you accept invalid JSON texts 24869f11ffb7Safresh1as if they were valid!>. I suggest only to use this option to 24879f11ffb7Safresh1parse application-specific files written by humans (configuration 24889f11ffb7Safresh1files, resource files etc.) 24899f11ffb7Safresh1 24909f11ffb7Safresh1If C<$enable> is false (the default), then C<decode> will only accept 24919f11ffb7Safresh1valid JSON texts. 24929f11ffb7Safresh1 24939f11ffb7Safresh1 $json->loose->decode(qq|["abc 24949f11ffb7Safresh1 def"]|); 24959f11ffb7Safresh1 24969f11ffb7Safresh1=head2 escape_slash 24979f11ffb7Safresh1 24989f11ffb7Safresh1 $json = $json->escape_slash([$enable]) 24999f11ffb7Safresh1 $enabled = $json->get_escape_slash 25009f11ffb7Safresh1 25019f11ffb7Safresh1If C<$enable> is true (or missing), then C<encode> will explicitly 25029f11ffb7Safresh1escape I<slash> (solidus; C<U+002F>) characters to reduce the risk of 25039f11ffb7Safresh1XSS (cross site scripting) that may be caused by C<< </script> >> 25049f11ffb7Safresh1in a JSON text, with the cost of bloating the size of JSON texts. 25059f11ffb7Safresh1 25069f11ffb7Safresh1This option may be useful when you embed JSON in HTML, but embedding 25079f11ffb7Safresh1arbitrary JSON in HTML (by some HTML template toolkit or by string 25089f11ffb7Safresh1interpolation) is risky in general. You must escape necessary 25099f11ffb7Safresh1characters in correct order, depending on the context. 25109f11ffb7Safresh1 25119f11ffb7Safresh1C<decode> will not be affected in any way. 25129f11ffb7Safresh1 25139f11ffb7Safresh1=head2 indent_length 25149f11ffb7Safresh1 25159f11ffb7Safresh1 $json = $json->indent_length($number_of_spaces) 25169f11ffb7Safresh1 $length = $json->get_indent_length 25179f11ffb7Safresh1 25189f11ffb7Safresh1This option is only useful when you also enable C<indent> or C<pretty>. 25199f11ffb7Safresh1 25209f11ffb7Safresh1JSON::XS indents with three spaces when you C<encode> (if requested 25219f11ffb7Safresh1by C<indent> or C<pretty>), and the number cannot be changed. 25229f11ffb7Safresh1JSON::PP allows you to change/get the number of indent spaces with these 25239f11ffb7Safresh1mutator/accessor. The default number of spaces is three (the same as 25249f11ffb7Safresh1JSON::XS), and the acceptable range is from C<0> (no indentation; 25259f11ffb7Safresh1it'd be better to disable indentation by C<indent(0)>) to C<15>. 25269f11ffb7Safresh1 25279f11ffb7Safresh1=head2 sort_by 25289f11ffb7Safresh1 25299f11ffb7Safresh1 $json = $json->sort_by($code_ref) 25309f11ffb7Safresh1 $json = $json->sort_by($subroutine_name) 25319f11ffb7Safresh1 25329f11ffb7Safresh1If you just want to sort keys (names) in JSON objects when you 25339f11ffb7Safresh1C<encode>, enable C<canonical> option (see above) that allows you to 25349f11ffb7Safresh1sort object keys alphabetically. 25359f11ffb7Safresh1 25369f11ffb7Safresh1If you do need to sort non-alphabetically for whatever reasons, 25379f11ffb7Safresh1you can give a code reference (or a subroutine name) to C<sort_by>, 25389f11ffb7Safresh1then the argument will be passed to Perl's C<sort> built-in function. 25399f11ffb7Safresh1 25409f11ffb7Safresh1As the sorting is done in the JSON::PP scope, you usually need to 25419f11ffb7Safresh1prepend C<JSON::PP::> to the subroutine name, and the special variables 25429f11ffb7Safresh1C<$a> and C<$b> used in the subrontine used by C<sort> function. 25439f11ffb7Safresh1 25449f11ffb7Safresh1Example: 25459f11ffb7Safresh1 25469f11ffb7Safresh1 my %ORDER = (id => 1, class => 2, name => 3); 25479f11ffb7Safresh1 $json->sort_by(sub { 25489f11ffb7Safresh1 ($ORDER{$JSON::PP::a} // 999) <=> ($ORDER{$JSON::PP::b} // 999) 25499f11ffb7Safresh1 or $JSON::PP::a cmp $JSON::PP::b 25509f11ffb7Safresh1 }); 25519f11ffb7Safresh1 print $json->encode([ 25529f11ffb7Safresh1 {name => 'CPAN', id => 1, href => 'http://cpan.org'} 25539f11ffb7Safresh1 ]); 25549f11ffb7Safresh1 # [{"id":1,"name":"CPAN","href":"http://cpan.org"}] 25559f11ffb7Safresh1 25569f11ffb7Safresh1Note that C<sort_by> affects all the plain hashes in the data structure. 25579f11ffb7Safresh1If you need finer control, C<tie> necessary hashes with a module that 25589f11ffb7Safresh1implements ordered hash (such as L<Hash::Ordered> and L<Tie::IxHash>). 25599f11ffb7Safresh1C<canonical> and C<sort_by> don't affect the key order in C<tie>d 25609f11ffb7Safresh1hashes. 25619f11ffb7Safresh1 25629f11ffb7Safresh1 use Hash::Ordered; 25639f11ffb7Safresh1 tie my %hash, 'Hash::Ordered', 25649f11ffb7Safresh1 (name => 'CPAN', id => 1, href => 'http://cpan.org'); 25659f11ffb7Safresh1 print $json->encode([\%hash]); 25669f11ffb7Safresh1 # [{"name":"CPAN","id":1,"href":"http://cpan.org"}] # order is kept 2567898184e3Ssthen 2568898184e3Ssthen=head1 INCREMENTAL PARSING 2569898184e3Ssthen 25709f11ffb7Safresh1This section is also taken from JSON::XS. 2571898184e3Ssthen 25729f11ffb7Safresh1In some cases, there is the need for incremental parsing of JSON 25739f11ffb7Safresh1texts. While this module always has to keep both JSON text and resulting 25749f11ffb7Safresh1Perl data structure in memory at one time, it does allow you to parse a 25759f11ffb7Safresh1JSON stream incrementally. It does so by accumulating text until it has 25769f11ffb7Safresh1a full JSON object, which it then can decode. This process is similar to 25779f11ffb7Safresh1using C<decode_prefix> to see if a full JSON object is available, but 25789f11ffb7Safresh1is much more efficient (and can be implemented with a minimum of method 25799f11ffb7Safresh1calls). 2580898184e3Ssthen 25819f11ffb7Safresh1JSON::PP will only attempt to parse the JSON text once it is sure it 2582898184e3Ssthenhas enough text to get a decisive result, using a very simple but 2583898184e3Ssthentruly incremental parser. This means that it sometimes won't stop as 25849f11ffb7Safresh1early as the full parser, for example, it doesn't detect mismatched 25859f11ffb7Safresh1parentheses. The only thing it guarantees is that it starts decoding as 2586898184e3Ssthensoon as a syntactically valid JSON text has been seen. This means you need 2587898184e3Ssthento set resource limits (e.g. C<max_size>) to ensure the parser will stop 2588898184e3Ssthenparsing in the presence if syntax errors. 2589898184e3Ssthen 2590898184e3SsthenThe following methods implement this incremental parser. 2591898184e3Ssthen 2592898184e3Ssthen=head2 incr_parse 2593898184e3Ssthen 2594898184e3Ssthen $json->incr_parse( [$string] ) # void context 2595898184e3Ssthen 2596898184e3Ssthen $obj_or_undef = $json->incr_parse( [$string] ) # scalar context 2597898184e3Ssthen 2598898184e3Ssthen @obj_or_empty = $json->incr_parse( [$string] ) # list context 2599898184e3Ssthen 2600898184e3SsthenThis is the central parsing function. It can both append new text and 2601898184e3Ssthenextract objects from the stream accumulated so far (both of these 2602898184e3Ssthenfunctions are optional). 2603898184e3Ssthen 2604898184e3SsthenIf C<$string> is given, then this string is appended to the already 2605898184e3Ssthenexisting JSON fragment stored in the C<$json> object. 2606898184e3Ssthen 2607898184e3SsthenAfter that, if the function is called in void context, it will simply 2608898184e3Ssthenreturn without doing anything further. This can be used to add more text 2609898184e3Ssthenin as many chunks as you want. 2610898184e3Ssthen 2611898184e3SsthenIf the method is called in scalar context, then it will try to extract 2612898184e3Ssthenexactly I<one> JSON object. If that is successful, it will return this 2613898184e3Ssthenobject, otherwise it will return C<undef>. If there is a parse error, 2614898184e3Ssthenthis method will croak just as C<decode> would do (one can then use 26159f11ffb7Safresh1C<incr_skip> to skip the erroneous part). This is the most common way of 2616898184e3Ssthenusing the method. 2617898184e3Ssthen 2618898184e3SsthenAnd finally, in list context, it will try to extract as many objects 2619898184e3Ssthenfrom the stream as it can find and return them, or the empty list 26209f11ffb7Safresh1otherwise. For this to work, there must be no separators (other than 26219f11ffb7Safresh1whitespace) between the JSON objects or arrays, instead they must be 26229f11ffb7Safresh1concatenated back-to-back. If an error occurs, an exception will be 26239f11ffb7Safresh1raised as in the scalar context case. Note that in this case, any 26249f11ffb7Safresh1previously-parsed JSON texts will be lost. 2625898184e3Ssthen 26269f11ffb7Safresh1Example: Parse some JSON arrays/objects in a given string and return 26279f11ffb7Safresh1them. 2628898184e3Ssthen 26299f11ffb7Safresh1 my @objs = JSON::PP->new->incr_parse ("[5][7][1,2]"); 2630898184e3Ssthen 2631898184e3Ssthen=head2 incr_text 2632898184e3Ssthen 2633898184e3Ssthen $lvalue_string = $json->incr_text 2634898184e3Ssthen 2635898184e3SsthenThis method returns the currently stored JSON fragment as an lvalue, that 2636898184e3Ssthenis, you can manipulate it. This I<only> works when a preceding call to 2637898184e3SsthenC<incr_parse> in I<scalar context> successfully returned an object. Under 2638898184e3Ssthenall other circumstances you must not call this function (I mean it. 2639898184e3Ssthenalthough in simple tests it might actually work, it I<will> fail under 2640898184e3Ssthenreal world conditions). As a special exception, you can also call this 2641898184e3Ssthenmethod before having parsed anything. 2642898184e3Ssthen 26439f11ffb7Safresh1That means you can only use this function to look at or manipulate text 26449f11ffb7Safresh1before or after complete JSON objects, not while the parser is in the 26459f11ffb7Safresh1middle of parsing a JSON object. 26469f11ffb7Safresh1 2647898184e3SsthenThis function is useful in two cases: a) finding the trailing text after a 2648898184e3SsthenJSON object or b) parsing multiple JSON objects separated by non-JSON text 2649898184e3Ssthen(such as commas). 2650898184e3Ssthen 2651898184e3Ssthen=head2 incr_skip 2652898184e3Ssthen 2653898184e3Ssthen $json->incr_skip 2654898184e3Ssthen 26559f11ffb7Safresh1This will reset the state of the incremental parser and will remove 26569f11ffb7Safresh1the parsed text from the input buffer so far. This is useful after 26579f11ffb7Safresh1C<incr_parse> died, in which case the input buffer and incremental parser 26589f11ffb7Safresh1state is left unchanged, to skip the text parsed so far and to reset the 26599f11ffb7Safresh1parse state. 26609f11ffb7Safresh1 26619f11ffb7Safresh1The difference to C<incr_reset> is that only text until the parse error 26629f11ffb7Safresh1occurred is removed. 2663898184e3Ssthen 2664898184e3Ssthen=head2 incr_reset 2665898184e3Ssthen 2666898184e3Ssthen $json->incr_reset 2667898184e3Ssthen 2668898184e3SsthenThis completely resets the incremental parser, that is, after this call, 2669898184e3Ssthenit will be as if the parser had never parsed anything. 2670898184e3Ssthen 26719f11ffb7Safresh1This is useful if you want to repeatedly parse JSON objects and want to 2672898184e3Ssthenignore any trailing data, which means you have to reset the parser after 2673898184e3Sstheneach successful decode. 2674898184e3Ssthen 2675898184e3Ssthen=head1 MAPPING 2676898184e3Ssthen 26779f11ffb7Safresh1Most of this section is also taken from JSON::XS. 2678898184e3Ssthen 26799f11ffb7Safresh1This section describes how JSON::PP maps Perl values to JSON values and 26809f11ffb7Safresh1vice versa. These mappings are designed to "do the right thing" in most 26819f11ffb7Safresh1circumstances automatically, preserving round-tripping characteristics 26829f11ffb7Safresh1(what you put in comes out as something equivalent). 26839f11ffb7Safresh1 26849f11ffb7Safresh1For the more enlightened: note that in the following descriptions, 26859f11ffb7Safresh1lowercase I<perl> refers to the Perl interpreter, while uppercase I<Perl> 26869f11ffb7Safresh1refers to the abstract Perl language itself. 2687898184e3Ssthen 2688898184e3Ssthen=head2 JSON -> PERL 2689898184e3Ssthen 2690898184e3Ssthen=over 4 2691898184e3Ssthen 2692898184e3Ssthen=item object 2693898184e3Ssthen 2694898184e3SsthenA JSON object becomes a reference to a hash in Perl. No ordering of object 26959f11ffb7Safresh1keys is preserved (JSON does not preserve object key ordering itself). 2696898184e3Ssthen 2697898184e3Ssthen=item array 2698898184e3Ssthen 2699898184e3SsthenA JSON array becomes a reference to an array in Perl. 2700898184e3Ssthen 2701898184e3Ssthen=item string 2702898184e3Ssthen 2703898184e3SsthenA JSON string becomes a string scalar in Perl - Unicode codepoints in JSON 2704898184e3Ssthenare represented by the same codepoints in the Perl string, so no manual 2705898184e3Ssthendecoding is necessary. 2706898184e3Ssthen 2707898184e3Ssthen=item number 2708898184e3Ssthen 2709898184e3SsthenA JSON number becomes either an integer, numeric (floating point) or 2710898184e3Ssthenstring scalar in perl, depending on its range and any fractional parts. On 2711898184e3Ssthenthe Perl level, there is no difference between those as Perl handles all 2712898184e3Ssthenthe conversion details, but an integer may take slightly less memory and 2713898184e3Ssthenmight represent more values exactly than floating point numbers. 2714898184e3Ssthen 27159f11ffb7Safresh1If the number consists of digits only, JSON::PP will try to represent 2716898184e3Ssthenit as an integer value. If that fails, it will try to represent it as 2717898184e3Ssthena numeric (floating point) value if that is possible without loss of 2718898184e3Ssthenprecision. Otherwise it will preserve the number as a string value (in 2719898184e3Ssthenwhich case you lose roundtripping ability, as the JSON number will be 2720898184e3Ssthenre-encoded to a JSON string). 2721898184e3Ssthen 2722898184e3SsthenNumbers containing a fractional or exponential part will always be 2723898184e3Ssthenrepresented as numeric (floating point) values, possibly at a loss of 2724898184e3Ssthenprecision (in which case you might lose perfect roundtripping ability, but 2725898184e3Ssthenthe JSON number will still be re-encoded as a JSON number). 2726898184e3Ssthen 2727898184e3SsthenNote that precision is not accuracy - binary floating point values cannot 2728898184e3Ssthenrepresent most decimal fractions exactly, and when converting from and to 27299f11ffb7Safresh1floating point, JSON::PP only guarantees precision up to but not including 27309f11ffb7Safresh1the least significant bit. 2731898184e3Ssthen 27329f11ffb7Safresh1When C<allow_bignum> is enabled, big integer values and any numeric 27339f11ffb7Safresh1values will be converted into L<Math::BigInt> and L<Math::BigFloat> 27349f11ffb7Safresh1objects respectively, without becoming string scalars or losing 27359f11ffb7Safresh1precision. 2736898184e3Ssthen 2737898184e3Ssthen=item true, false 2738898184e3Ssthen 2739898184e3SsthenThese JSON atoms become C<JSON::PP::true> and C<JSON::PP::false>, 2740898184e3Ssthenrespectively. They are overloaded to act almost exactly like the numbers 27419f11ffb7Safresh1C<1> and C<0>. You can check whether a scalar is a JSON boolean by using 27429f11ffb7Safresh1the C<JSON::PP::is_bool> function. 2743898184e3Ssthen 2744898184e3Ssthen=item null 2745898184e3Ssthen 2746898184e3SsthenA JSON null atom becomes C<undef> in Perl. 2747898184e3Ssthen 27489f11ffb7Safresh1=item shell-style comments (C<< # I<text> >>) 27499f11ffb7Safresh1 27509f11ffb7Safresh1As a nonstandard extension to the JSON syntax that is enabled by the 27519f11ffb7Safresh1C<relaxed> setting, shell-style comments are allowed. They can start 27529f11ffb7Safresh1anywhere outside strings and go till the end of the line. 2753898184e3Ssthen 2754b46d8ef2Safresh1=item tagged values (C<< (I<tag>)I<value> >>). 2755b46d8ef2Safresh1 2756b46d8ef2Safresh1Another nonstandard extension to the JSON syntax, enabled with the 2757b46d8ef2Safresh1C<allow_tags> setting, are tagged values. In this implementation, the 2758b46d8ef2Safresh1I<tag> must be a perl package/class name encoded as a JSON string, and the 2759b46d8ef2Safresh1I<value> must be a JSON array encoding optional constructor arguments. 2760b46d8ef2Safresh1 2761b46d8ef2Safresh1See L<OBJECT SERIALISATION>, below, for details. 2762b46d8ef2Safresh1 2763898184e3Ssthen=back 2764898184e3Ssthen 2765898184e3Ssthen 2766898184e3Ssthen=head2 PERL -> JSON 2767898184e3Ssthen 2768898184e3SsthenThe mapping from Perl to JSON is slightly more difficult, as Perl is a 2769898184e3Ssthentruly typeless language, so we can only guess which JSON type is meant by 2770898184e3Ssthena Perl value. 2771898184e3Ssthen 2772898184e3Ssthen=over 4 2773898184e3Ssthen 2774898184e3Ssthen=item hash references 2775898184e3Ssthen 27769f11ffb7Safresh1Perl hash references become JSON objects. As there is no inherent 27779f11ffb7Safresh1ordering in hash keys (or JSON objects), they will usually be encoded 27789f11ffb7Safresh1in a pseudo-random order. JSON::PP can optionally sort the hash keys 27799f11ffb7Safresh1(determined by the I<canonical> flag and/or I<sort_by> property), so 27809f11ffb7Safresh1the same data structure will serialise to the same JSON text (given 27819f11ffb7Safresh1same settings and version of JSON::PP), but this incurs a runtime 27829f11ffb7Safresh1overhead and is only rarely useful, e.g. when you want to compare some 27839f11ffb7Safresh1JSON text against another for equality. 2784898184e3Ssthen 2785898184e3Ssthen=item array references 2786898184e3Ssthen 2787898184e3SsthenPerl array references become JSON arrays. 2788898184e3Ssthen 2789898184e3Ssthen=item other references 2790898184e3Ssthen 2791898184e3SsthenOther unblessed references are generally not allowed and will cause an 2792898184e3Ssthenexception to be thrown, except for references to the integers C<0> and 2793898184e3SsthenC<1>, which get turned into C<false> and C<true> atoms in JSON. You can 27949f11ffb7Safresh1also use C<JSON::PP::false> and C<JSON::PP::true> to improve 27959f11ffb7Safresh1readability. 2796898184e3Ssthen 2797898184e3Ssthen to_json [\0, JSON::PP::true] # yields [false,true] 2798898184e3Ssthen 27999f11ffb7Safresh1=item JSON::PP::true, JSON::PP::false 2800898184e3Ssthen 2801898184e3SsthenThese special values become JSON true and JSON false values, 2802898184e3Ssthenrespectively. You can also use C<\1> and C<\0> directly if you want. 2803898184e3Ssthen 28049f11ffb7Safresh1=item JSON::PP::null 28059f11ffb7Safresh1 28069f11ffb7Safresh1This special value becomes JSON null. 2807898184e3Ssthen 2808898184e3Ssthen=item blessed objects 2809898184e3Ssthen 28109f11ffb7Safresh1Blessed objects are not directly representable in JSON, but C<JSON::PP> 28119f11ffb7Safresh1allows various ways of handling objects. See L<OBJECT SERIALISATION>, 28129f11ffb7Safresh1below, for details. 2813898184e3Ssthen 2814898184e3Ssthen=item simple scalars 2815898184e3Ssthen 2816898184e3SsthenSimple Perl scalars (any scalar that is not a reference) are the most 28179f11ffb7Safresh1difficult objects to encode: JSON::PP will encode undefined scalars as 2818898184e3SsthenJSON C<null> values, scalars that have last been used in a string context 2819898184e3Ssthenbefore encoding as JSON strings, and anything else as number value: 2820898184e3Ssthen 2821898184e3Ssthen # dump as number 2822898184e3Ssthen encode_json [2] # yields [2] 2823898184e3Ssthen encode_json [-3.0e17] # yields [-3e+17] 2824898184e3Ssthen my $value = 5; encode_json [$value] # yields [5] 2825898184e3Ssthen 2826898184e3Ssthen # used as string, so dump as string 2827898184e3Ssthen print $value; 2828898184e3Ssthen encode_json [$value] # yields ["5"] 2829898184e3Ssthen 2830898184e3Ssthen # undef becomes null 2831898184e3Ssthen encode_json [undef] # yields [null] 2832898184e3Ssthen 2833b46d8ef2Safresh1You can force the type to be a JSON string by stringifying it: 2834898184e3Ssthen 2835898184e3Ssthen my $x = 3.1; # some variable containing a number 2836898184e3Ssthen "$x"; # stringified 2837898184e3Ssthen $x .= ""; # another, more awkward way to stringify 2838898184e3Ssthen print $x; # perl does it for you, too, quite often 28399f11ffb7Safresh1 # (but for older perls) 2840898184e3Ssthen 2841b46d8ef2Safresh1You can force the type to be a JSON number by numifying it: 2842898184e3Ssthen 2843898184e3Ssthen my $x = "3"; # some variable containing a string 2844898184e3Ssthen $x += 0; # numify it, ensuring it will be dumped as a number 28459f11ffb7Safresh1 $x *= 1; # same thing, the choice is yours. 2846898184e3Ssthen 2847898184e3SsthenYou can not currently force the type in other, less obscure, ways. 2848898184e3Ssthen 2849b46d8ef2Safresh1Since version 2.91_01, JSON::PP uses a different number detection logic 2850b46d8ef2Safresh1that converts a scalar that is possible to turn into a number safely. 2851b46d8ef2Safresh1The new logic is slightly faster, and tends to help people who use older 2852b46d8ef2Safresh1perl or who want to encode complicated data structure. However, this may 2853b46d8ef2Safresh1results in a different JSON text from the one JSON::XS encodes (and 2854b46d8ef2Safresh1thus may break tests that compare entire JSON texts). If you do 2855b46d8ef2Safresh1need the previous behavior for compatibility or for finer control, 2856b46d8ef2Safresh1set PERL_JSON_PP_USE_B environmental variable to true before you 2857b46d8ef2Safresh1C<use> JSON::PP (or JSON.pm). 2858b46d8ef2Safresh1 2859898184e3SsthenNote that numerical precision has the same meaning as under Perl (so 2860898184e3Ssthenbinary to decimal conversion follows the same rules as in Perl, which 2861898184e3Ssthencan differ to other languages). Also, your perl interpreter might expose 2862898184e3Ssthenextensions to the floating point numbers of your platform, such as 2863898184e3Sstheninfinities or NaN's - these cannot be represented in JSON, and it is an 2864898184e3Ssthenerror to pass those in. 2865898184e3Ssthen 28669f11ffb7Safresh1JSON::PP (and JSON::XS) trusts what you pass to C<encode> method 28679f11ffb7Safresh1(or C<encode_json> function) is a clean, validated data structure with 28689f11ffb7Safresh1values that can be represented as valid JSON values only, because it's 28699f11ffb7Safresh1not from an external data source (as opposed to JSON texts you pass to 28709f11ffb7Safresh1C<decode> or C<decode_json>, which JSON::PP considers tainted and 28719f11ffb7Safresh1doesn't trust). As JSON::PP doesn't know exactly what you and consumers 28729f11ffb7Safresh1of your JSON texts want the unexpected values to be (you may want to 28739f11ffb7Safresh1convert them into null, or to stringify them with or without 28749f11ffb7Safresh1normalisation (string representation of infinities/NaN may vary 28759f11ffb7Safresh1depending on platforms), or to croak without conversion), you're advised 28769f11ffb7Safresh1to do what you and your consumers need before you encode, and also not 28779f11ffb7Safresh1to numify values that may start with values that look like a number 28789f11ffb7Safresh1(including infinities/NaN), without validating. 2879898184e3Ssthen 2880898184e3Ssthen=back 2881898184e3Ssthen 28829f11ffb7Safresh1=head2 OBJECT SERIALISATION 2883898184e3Ssthen 2884b46d8ef2Safresh1As JSON cannot directly represent Perl objects, you have to choose between 2885b46d8ef2Safresh1a pure JSON representation (without the ability to deserialise the object 2886b46d8ef2Safresh1automatically again), and a nonstandard extension to the JSON syntax, 2887b46d8ef2Safresh1tagged values. 2888898184e3Ssthen 28899f11ffb7Safresh1=head3 SERIALISATION 2890898184e3Ssthen 28919f11ffb7Safresh1What happens when C<JSON::PP> encounters a Perl object depends on the 2892b46d8ef2Safresh1C<allow_blessed>, C<convert_blessed>, C<allow_tags> and C<allow_bignum> 2893b46d8ef2Safresh1settings, which are used in this order: 2894898184e3Ssthen 28959f11ffb7Safresh1=over 4 2896898184e3Ssthen 2897b46d8ef2Safresh1=item 1. C<allow_tags> is enabled and the object has a C<FREEZE> method. 2898b46d8ef2Safresh1 2899b46d8ef2Safresh1In this case, C<JSON::PP> creates a tagged JSON value, using a nonstandard 2900b46d8ef2Safresh1extension to the JSON syntax. 2901b46d8ef2Safresh1 2902b46d8ef2Safresh1This works by invoking the C<FREEZE> method on the object, with the first 2903b46d8ef2Safresh1argument being the object to serialise, and the second argument being the 2904b46d8ef2Safresh1constant string C<JSON> to distinguish it from other serialisers. 2905b46d8ef2Safresh1 2906b46d8ef2Safresh1The C<FREEZE> method can return any number of values (i.e. zero or 2907b46d8ef2Safresh1more). These values and the paclkage/classname of the object will then be 2908b46d8ef2Safresh1encoded as a tagged JSON value in the following format: 2909b46d8ef2Safresh1 2910b46d8ef2Safresh1 ("classname")[FREEZE return values...] 2911b46d8ef2Safresh1 2912b46d8ef2Safresh1e.g.: 2913b46d8ef2Safresh1 2914b46d8ef2Safresh1 ("URI")["http://www.google.com/"] 2915b46d8ef2Safresh1 ("MyDate")[2013,10,29] 2916b46d8ef2Safresh1 ("ImageData::JPEG")["Z3...VlCg=="] 2917b46d8ef2Safresh1 2918b46d8ef2Safresh1For example, the hypothetical C<My::Object> C<FREEZE> method might use the 2919b46d8ef2Safresh1objects C<type> and C<id> members to encode the object: 2920b46d8ef2Safresh1 2921b46d8ef2Safresh1 sub My::Object::FREEZE { 2922b46d8ef2Safresh1 my ($self, $serialiser) = @_; 2923b46d8ef2Safresh1 2924b46d8ef2Safresh1 ($self->{type}, $self->{id}) 2925b46d8ef2Safresh1 } 2926b46d8ef2Safresh1 2927b46d8ef2Safresh1=item 2. C<convert_blessed> is enabled and the object has a C<TO_JSON> method. 2928898184e3Ssthen 29299f11ffb7Safresh1In this case, the C<TO_JSON> method of the object is invoked in scalar 29309f11ffb7Safresh1context. It must return a single scalar that can be directly encoded into 29319f11ffb7Safresh1JSON. This scalar replaces the object in the JSON text. 2932898184e3Ssthen 29339f11ffb7Safresh1For example, the following C<TO_JSON> method will convert all L<URI> 29349f11ffb7Safresh1objects to JSON strings when serialised. The fact that these values 29359f11ffb7Safresh1originally were L<URI> objects is lost. 2936898184e3Ssthen 29379f11ffb7Safresh1 sub URI::TO_JSON { 29389f11ffb7Safresh1 my ($uri) = @_; 29399f11ffb7Safresh1 $uri->as_string 29409f11ffb7Safresh1 } 2941898184e3Ssthen 2942b46d8ef2Safresh1=item 3. C<allow_bignum> is enabled and the object is a C<Math::BigInt> or C<Math::BigFloat>. 2943898184e3Ssthen 29449f11ffb7Safresh1The object will be serialised as a JSON number value. 2945898184e3Ssthen 2946b46d8ef2Safresh1=item 4. C<allow_blessed> is enabled. 2947898184e3Ssthen 29489f11ffb7Safresh1The object will be serialised as a JSON null value. 2949898184e3Ssthen 2950b46d8ef2Safresh1=item 5. none of the above 2951898184e3Ssthen 29529f11ffb7Safresh1If none of the settings are enabled or the respective methods are missing, 29539f11ffb7Safresh1C<JSON::PP> throws an exception. 2954898184e3Ssthen 2955898184e3Ssthen=back 2956898184e3Ssthen 2957b46d8ef2Safresh1=head3 DESERIALISATION 2958b46d8ef2Safresh1 2959b46d8ef2Safresh1For deserialisation there are only two cases to consider: either 2960b46d8ef2Safresh1nonstandard tagging was used, in which case C<allow_tags> decides, 2961b46d8ef2Safresh1or objects cannot be automatically be deserialised, in which 2962b46d8ef2Safresh1case you can use postprocessing or the C<filter_json_object> or 2963b46d8ef2Safresh1C<filter_json_single_key_object> callbacks to get some real objects our of 2964b46d8ef2Safresh1your JSON. 2965b46d8ef2Safresh1 2966b46d8ef2Safresh1This section only considers the tagged value case: a tagged JSON object 2967b46d8ef2Safresh1is encountered during decoding and C<allow_tags> is disabled, a parse 2968b46d8ef2Safresh1error will result (as if tagged values were not part of the grammar). 2969b46d8ef2Safresh1 2970b46d8ef2Safresh1If C<allow_tags> is enabled, C<JSON::PP> will look up the C<THAW> method 2971b46d8ef2Safresh1of the package/classname used during serialisation (it will not attempt 2972b46d8ef2Safresh1to load the package as a Perl module). If there is no such method, the 2973b46d8ef2Safresh1decoding will fail with an error. 2974b46d8ef2Safresh1 2975b46d8ef2Safresh1Otherwise, the C<THAW> method is invoked with the classname as first 2976b46d8ef2Safresh1argument, the constant string C<JSON> as second argument, and all the 2977b46d8ef2Safresh1values from the JSON array (the values originally returned by the 2978b46d8ef2Safresh1C<FREEZE> method) as remaining arguments. 2979b46d8ef2Safresh1 2980b46d8ef2Safresh1The method must then return the object. While technically you can return 2981b46d8ef2Safresh1any Perl scalar, you might have to enable the C<allow_nonref> setting to 2982b46d8ef2Safresh1make that work in all cases, so better return an actual blessed reference. 2983b46d8ef2Safresh1 2984b46d8ef2Safresh1As an example, let's implement a C<THAW> function that regenerates the 2985b46d8ef2Safresh1C<My::Object> from the C<FREEZE> example earlier: 2986b46d8ef2Safresh1 2987b46d8ef2Safresh1 sub My::Object::THAW { 2988b46d8ef2Safresh1 my ($class, $serialiser, $type, $id) = @_; 2989b46d8ef2Safresh1 2990b46d8ef2Safresh1 $class->new (type => $type, id => $id) 2991b46d8ef2Safresh1 } 2992b46d8ef2Safresh1 2993b46d8ef2Safresh1 29949f11ffb7Safresh1=head1 ENCODING/CODESET FLAG NOTES 29959f11ffb7Safresh1 29969f11ffb7Safresh1This section is taken from JSON::XS. 29979f11ffb7Safresh1 29989f11ffb7Safresh1The interested reader might have seen a number of flags that signify 29999f11ffb7Safresh1encodings or codesets - C<utf8>, C<latin1> and C<ascii>. There seems to be 30009f11ffb7Safresh1some confusion on what these do, so here is a short comparison: 30019f11ffb7Safresh1 30029f11ffb7Safresh1C<utf8> controls whether the JSON text created by C<encode> (and expected 30039f11ffb7Safresh1by C<decode>) is UTF-8 encoded or not, while C<latin1> and C<ascii> only 30049f11ffb7Safresh1control whether C<encode> escapes character values outside their respective 30059f11ffb7Safresh1codeset range. Neither of these flags conflict with each other, although 30069f11ffb7Safresh1some combinations make less sense than others. 30079f11ffb7Safresh1 30089f11ffb7Safresh1Care has been taken to make all flags symmetrical with respect to 30099f11ffb7Safresh1C<encode> and C<decode>, that is, texts encoded with any combination of 30109f11ffb7Safresh1these flag values will be correctly decoded when the same flags are used 30119f11ffb7Safresh1- in general, if you use different flag settings while encoding vs. when 30129f11ffb7Safresh1decoding you likely have a bug somewhere. 30139f11ffb7Safresh1 30149f11ffb7Safresh1Below comes a verbose discussion of these flags. Note that a "codeset" is 30159f11ffb7Safresh1simply an abstract set of character-codepoint pairs, while an encoding 30169f11ffb7Safresh1takes those codepoint numbers and I<encodes> them, in our case into 30179f11ffb7Safresh1octets. Unicode is (among other things) a codeset, UTF-8 is an encoding, 30189f11ffb7Safresh1and ISO-8859-1 (= latin 1) and ASCII are both codesets I<and> encodings at 30199f11ffb7Safresh1the same time, which can be confusing. 30209f11ffb7Safresh1 30219f11ffb7Safresh1=over 4 30229f11ffb7Safresh1 30239f11ffb7Safresh1=item C<utf8> flag disabled 30249f11ffb7Safresh1 30259f11ffb7Safresh1When C<utf8> is disabled (the default), then C<encode>/C<decode> generate 30269f11ffb7Safresh1and expect Unicode strings, that is, characters with high ordinal Unicode 30279f11ffb7Safresh1values (> 255) will be encoded as such characters, and likewise such 30289f11ffb7Safresh1characters are decoded as-is, no changes to them will be done, except 30299f11ffb7Safresh1"(re-)interpreting" them as Unicode codepoints or Unicode characters, 30309f11ffb7Safresh1respectively (to Perl, these are the same thing in strings unless you do 30319f11ffb7Safresh1funny/weird/dumb stuff). 30329f11ffb7Safresh1 30339f11ffb7Safresh1This is useful when you want to do the encoding yourself (e.g. when you 30349f11ffb7Safresh1want to have UTF-16 encoded JSON texts) or when some other layer does 30359f11ffb7Safresh1the encoding for you (for example, when printing to a terminal using a 30369f11ffb7Safresh1filehandle that transparently encodes to UTF-8 you certainly do NOT want 30379f11ffb7Safresh1to UTF-8 encode your data first and have Perl encode it another time). 30389f11ffb7Safresh1 30399f11ffb7Safresh1=item C<utf8> flag enabled 30409f11ffb7Safresh1 30419f11ffb7Safresh1If the C<utf8>-flag is enabled, C<encode>/C<decode> will encode all 30429f11ffb7Safresh1characters using the corresponding UTF-8 multi-byte sequence, and will 30439f11ffb7Safresh1expect your input strings to be encoded as UTF-8, that is, no "character" 30449f11ffb7Safresh1of the input string must have any value > 255, as UTF-8 does not allow 30459f11ffb7Safresh1that. 30469f11ffb7Safresh1 30479f11ffb7Safresh1The C<utf8> flag therefore switches between two modes: disabled means you 30489f11ffb7Safresh1will get a Unicode string in Perl, enabled means you get an UTF-8 encoded 30499f11ffb7Safresh1octet/binary string in Perl. 30509f11ffb7Safresh1 30519f11ffb7Safresh1=item C<latin1> or C<ascii> flags enabled 30529f11ffb7Safresh1 30539f11ffb7Safresh1With C<latin1> (or C<ascii>) enabled, C<encode> will escape characters 30549f11ffb7Safresh1with ordinal values > 255 (> 127 with C<ascii>) and encode the remaining 30559f11ffb7Safresh1characters as specified by the C<utf8> flag. 30569f11ffb7Safresh1 30579f11ffb7Safresh1If C<utf8> is disabled, then the result is also correctly encoded in those 30589f11ffb7Safresh1character sets (as both are proper subsets of Unicode, meaning that a 30599f11ffb7Safresh1Unicode string with all character values < 256 is the same thing as a 30609f11ffb7Safresh1ISO-8859-1 string, and a Unicode string with all character values < 128 is 30619f11ffb7Safresh1the same thing as an ASCII string in Perl). 30629f11ffb7Safresh1 30639f11ffb7Safresh1If C<utf8> is enabled, you still get a correct UTF-8-encoded string, 30649f11ffb7Safresh1regardless of these flags, just some more characters will be escaped using 30659f11ffb7Safresh1C<\uXXXX> then before. 30669f11ffb7Safresh1 30679f11ffb7Safresh1Note that ISO-8859-1-I<encoded> strings are not compatible with UTF-8 30689f11ffb7Safresh1encoding, while ASCII-encoded strings are. That is because the ISO-8859-1 30699f11ffb7Safresh1encoding is NOT a subset of UTF-8 (despite the ISO-8859-1 I<codeset> being 30709f11ffb7Safresh1a subset of Unicode), while ASCII is. 30719f11ffb7Safresh1 30729f11ffb7Safresh1Surprisingly, C<decode> will ignore these flags and so treat all input 30739f11ffb7Safresh1values as governed by the C<utf8> flag. If it is disabled, this allows you 30749f11ffb7Safresh1to decode ISO-8859-1- and ASCII-encoded strings, as both strict subsets of 30759f11ffb7Safresh1Unicode. If it is enabled, you can correctly decode UTF-8 encoded strings. 30769f11ffb7Safresh1 30779f11ffb7Safresh1So neither C<latin1> nor C<ascii> are incompatible with the C<utf8> flag - 30789f11ffb7Safresh1they only govern when the JSON output engine escapes a character or not. 30799f11ffb7Safresh1 30809f11ffb7Safresh1The main use for C<latin1> is to relatively efficiently store binary data 30819f11ffb7Safresh1as JSON, at the expense of breaking compatibility with most JSON decoders. 30829f11ffb7Safresh1 30839f11ffb7Safresh1The main use for C<ascii> is to force the output to not contain characters 30849f11ffb7Safresh1with values > 127, which means you can interpret the resulting string 30859f11ffb7Safresh1as UTF-8, ISO-8859-1, ASCII, KOI8-R or most about any character set and 30869f11ffb7Safresh18-bit-encoding, and still get the same data structure back. This is useful 30879f11ffb7Safresh1when your channel for JSON transfer is not 8-bit clean or the encoding 30889f11ffb7Safresh1might be mangled in between (e.g. in mail), and works because ASCII is a 30899f11ffb7Safresh1proper subset of most 8-bit and multibyte encodings in use in the world. 30909f11ffb7Safresh1 30919f11ffb7Safresh1=back 3092898184e3Ssthen 3093b46d8ef2Safresh1=head1 BUGS 3094b46d8ef2Safresh1 3095b46d8ef2Safresh1Please report bugs on a specific behavior of this module to RT or GitHub 3096b46d8ef2Safresh1issues (preferred): 3097b46d8ef2Safresh1 3098b46d8ef2Safresh1L<https://github.com/makamaka/JSON-PP/issues> 3099b46d8ef2Safresh1 3100b46d8ef2Safresh1L<https://rt.cpan.org/Public/Dist/Display.html?Queue=JSON-PP> 3101b46d8ef2Safresh1 3102b46d8ef2Safresh1As for new features and requests to change common behaviors, please 3103b46d8ef2Safresh1ask the author of JSON::XS (Marc Lehmann, E<lt>schmorp[at]schmorp.deE<gt>) 3104b46d8ef2Safresh1first, by email (important!), to keep compatibility among JSON.pm backends. 3105b46d8ef2Safresh1 3106b46d8ef2Safresh1Generally speaking, if you need something special for you, you are advised 3107b46d8ef2Safresh1to create a new module, maybe based on L<JSON::Tiny>, which is smaller and 3108b46d8ef2Safresh1written in a much cleaner way than this module. 3109b46d8ef2Safresh1 3110898184e3Ssthen=head1 SEE ALSO 3111898184e3Ssthen 31129f11ffb7Safresh1The F<json_pp> command line utility for quick experiments. 3113898184e3Ssthen 31149f11ffb7Safresh1L<JSON::XS>, L<Cpanel::JSON::XS>, and L<JSON::Tiny> for faster alternatives. 31159f11ffb7Safresh1L<JSON> and L<JSON::MaybeXS> for easy migration. 31169f11ffb7Safresh1 31179f11ffb7Safresh1L<JSON::PP::Compat5005> and L<JSON::PP::Compat5006> for older perl users. 3118898184e3Ssthen 3119898184e3SsthenRFC4627 (L<http://www.ietf.org/rfc/rfc4627.txt>) 3120898184e3Ssthen 3121b46d8ef2Safresh1RFC7159 (L<http://www.ietf.org/rfc/rfc7159.txt>) 3122b46d8ef2Safresh1 3123b46d8ef2Safresh1RFC8259 (L<http://www.ietf.org/rfc/rfc8259.txt>) 3124b46d8ef2Safresh1 3125898184e3Ssthen=head1 AUTHOR 3126898184e3Ssthen 3127898184e3SsthenMakamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt> 3128898184e3Ssthen 3129b46d8ef2Safresh1=head1 CURRENT MAINTAINER 3130b46d8ef2Safresh1 3131b46d8ef2Safresh1Kenichi Ishigaki, E<lt>ishigaki[at]cpan.orgE<gt> 3132898184e3Ssthen 3133898184e3Ssthen=head1 COPYRIGHT AND LICENSE 3134898184e3Ssthen 31359f11ffb7Safresh1Copyright 2007-2016 by Makamaka Hannyaharamitu 3136898184e3Ssthen 3137b46d8ef2Safresh1Most of the documentation is taken from JSON::XS by Marc Lehmann 3138b46d8ef2Safresh1 3139898184e3SsthenThis library is free software; you can redistribute it and/or modify 3140898184e3Ssthenit under the same terms as Perl itself. 3141898184e3Ssthen 3142898184e3Ssthen=cut 3143