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