blib/lib/JSON/DWIW.pm

filter: Statementstmt 63.6% 203 / 319 Branchbran 44.9% 62 / 138 Conditioncond 44.2% 23 / 52 MC/DCmcdc 17.6% 6 / 34 Subroutinesub 75.0% 27 / 36 Podpod 73.3% 11 / 15 totaltotal 55.8% 332 / 594 CC 92 SCAR 39.5
CC
92
Cov
57%
to_json_file
306.0
_jsonml_xml
42.0
from_json_file
21.6
CRAP 52.1
↑ Summary blib/lib/JSON/DWIW/Boolean.pm »
1 # Creation date: 2007-02-19 16:54:44
2 # Authors: don
3 #
4 # Copyright (c) 2007-2010 Don Owens <don@regexguy.com>. All rights reserved.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the Perl Artistic license. You should have received a copy of the
8 # Artistic license with this distribution, in the file named
9 # "Artistic". You may also obtain a copy from
10 # http://regexguy.com/license/Artistic
11 #
12 # This program is distributed in the hope that it will be
13 # useful, but WITHOUT ANY WARRANTY; without even the implied
14 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 # PURPOSE.
16  
17 # $Revision: 1737 $
18  
19 # TODO
20 # * support surrogate pairs as described in http://www.ietf.org/rfc/rfc4627.txt
21 # * check for first surrogate: 0xD800 => 0xDBFF
22 # * check for second surrogate: 0xDC00 => 0xDFFF
23 # * take code point - 0x10000, add lower 10 bits to second surrogate, add upper 10 bits to first
24  
25 =pod
26  
27 =head1 NAME
28  
29 JSON::DWIW - JSON converter that Does What I Want
30  
31 =head1 SYNOPSIS
32  
33 use JSON::DWIW;
34 my $json_obj = JSON::DWIW->new;
35 my $data = $json_obj->from_json($json_str);
36 my $str = $json_obj->to_json($data);
37
38 my ($data, $error_string) = $json_obj->from_json($json_str);
39
40 my $data = JSON::DWIW::deserialize($json_str);
41 my $error_str = JSON::DWIW::get_error_string();
42
43 use JSON::DWIW qw/deserialize_json from_json/
44 my $data = deserialize_json($json_str);
45 my $error_str = JSON::DWIW::get_error_string();
46
47 my $error_string = $json_obj->get_error_string;
48 my $error_data = $json_obj->get_error_data;
49 my $stats = $json_obj->get_stats;
50
51 my $data = $json_obj->from_json_file($file)
52 my $ok = $json_obj->to_json_file($data, $file);
53
54 my $data = JSON::DWIW->from_json($json_str);
55 my $str = JSON::DWIW->to_json($data);
56
57 my $data = JSON::DWIW->from_json($json_str, \%options);
58 my $str = JSON::DWIW->to_json($data, \%options);
59
60 my $true_value = JSON::DWIW->true;
61 my $false_value = JSON::DWIW->false;
62 my $data = { var1 => "stuff", var2 => $true_value,
63 var3 => $false_value, };
64 my $str = JSON::DWIW->to_json($data);
65  
66 my $data = JSON::DWIW::deserialize($str, { start_depth => 1,
67 start_depth_handler => $handler });
68  
69  
70 =head1 DESCRIPTION
71  
72 Other JSON modules require setting several parameters before
73 calling the conversion methods to do what I want. This module
74 does things by default that I think should be done when working
75 with JSON in Perl. This module also encodes and decodes faster
76 than L<JSON>.pm and L<JSON::Syck> in my benchmarks.
77  
78 This means that any piece of data in Perl (assuming it's valid
79 unicode) will get converted to something in JSON instead of
80 throwing an exception. It also means that output will be strict
81 JSON, while accepted input will be flexible, without having to
82 set any options.
83  
84 For a list of changes in recent versions, see the documentation
85 for L<JSON::DWIW::Changes>.
86  
87 This module can be downloaded from L<http://www.cpan.org/authors/id/D/DO/DOWENS/>.
88  
89 =head2 Encoding
90  
91 Perl objects get encoded as their underlying data structure, with
92 the exception of L<Math::BigInt> and L<Math::BigFloat>, which will be
93 output as numbers, and L<JSON::DWIW::Boolean>, which will get output
94 as a true or false value (see the true() and false() methods).
95 For example, a blessed hash ref will be represented as an object
96 in JSON, a blessed array will be represented as an array. etc. A
97 reference to a scalar is dereferenced and represented as the
98 scalar itself. Globs, Code refs, etc., get stringified, and
99 undef becomes null.
100  
101 Scalars that have been used as both a string and a number will be
102 output as a string. A reference to a reference is currently
103 output as an empty string, but this may change.
104  
105 You may notice there is a deserialize function, but not a
106 serialize one. The deserialize function was written as a full
107 rewrite (the parsing is in a separate, event-based library now)
108 of from_json (now from_json calls deserialize). In the future,
109 there will be a serialize function that is a rewrite of to_json.
110  
111 =head2 Decoding
112  
113 Input is expected to utf-8. When decoding, null, true, and false
114 become undef, 1, and 0, repectively. Numbers that appear to be
115 too long to be supported natively are converted to L<Math::BigInt>
116 or L<Math::BigFloat> objects, if you have them installed.
117 Otherwise, long numbers are turned into strings to prevent data
118 loss.
119  
120 The parser is flexible in what it accepts and handles some
121 things not in the JSON spec:
122  
123 =over 4
124  
125 =item quotes
126  
127 Both single and double quotes are allowed for quoting a string, e.g.,
128  
129 =for pod2rst next-code-block: javascript
130  
131 [ "string1", 'string2' ]
132  
133 =item bare keys
134  
135 Object/hash keys can be bare if they look like an identifier, e.g.,
136  
137 =for pod2rst next-code-block: javascript
138  
139 { var1: "myval1", var2: "myval2" }
140  
141 =item extra commas
142  
143 Extra commas in objects/hashes and arrays are ignored, e.g.,
144  
145 =for pod2rst next-code-block: javascript
146  
147 [1,2,3,,,4,]
148  
149 becomes a 4 element array containing 1, 2, 3, and 4.
150  
151 =item escape sequences
152  
153 Latin1 hexadecimal escape sequences (\xHH) are accepted, as in
154 Javascript. Also, the vertical tab escape \v is recognized (\u000b).
155  
156 =item comments
157  
158 C, C++, and shell-style comments are accepted. That is
159  
160 =for pod2rst next-code-block: c++
161  
162 /* this is a comment */
163 // this is a comment
164  
165 # this is also a comment
166  
167 =back
168  
169 =cut
170  
171 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
173  
174 36 ▶use 5.006_00;
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 36 ▶use JSON::DWIW::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 package JSON::DWIW;
179  
180 36 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 # work around utf-8 weirdness in Perl < 5.8
183 36 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 36 require Exporter;
186 36 require DynaLoader;
187 36 @ISA = qw(DynaLoader);
188  
189 36 @EXPORT = ( );
190 36 @EXPORT_OK = ();
191 36 %EXPORT_TAGS = (all => [ 'to_json', 'from_json', 'deserialize_json' ]);
192  
193 36 Exporter::export_ok_tags('all');
194  
195 # change in POD as well!
196 36 our $VERSION = '0.47';
197  
198 36 JSON::DWIW->bootstrap($VERSION);
199  
200  
201 {
202 package JSON::DWIW::Exporter;
203 36 ▶ use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
204 36 @ISA = qw(Exporter);
205  
206 36 *EXPORT = \@JSON::DWIW::EXPORT;
207 36 *EXPORT_OK = \@JSON::DWIW::EXPORT_OK;
208 36 *EXPORT_TAGS = \%JSON::DWIW::EXPORT_TAGS;
209  
210 36 *deserialize_json = \&JSON::DWIW::deserialize_json;
211  
212 36 ▶ sub import {
Subroutine import: called CC=1 SCAR=0.0
213 36 JSON::DWIW::Exporter->export_to_level(2, @_);
214 }
215  
216 0 ▶ sub to_json {
Subroutine to_json: not called CC=1 SCAR=6.9
217 0 return JSON::DWIW->to_json(@_);
218 }
219  
220 1 ▶ sub from_json {
Subroutine from_json: called CC=1 SCAR=0.0
221 # return JSON::DWIW->from_json(@_);
222 1 return JSON::DWIW::deserialize(@_);
223 }
224 }
225  
226 36 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
227 36 JSON::DWIW::Exporter::import(@_);
228 }
229  
230 {
231 # workaround for weird importing bug on some installations
232 36 local($SIG{__DIE__});
233 36 ▶ eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
234 use Math::BigInt;
235 use Math::BigFloat;
236 };
237 }
238  
239  
240 =pod
241  
242 =head1 METHODS
243  
244 =head2 C<new(\%options)>
245  
246 Create a new L<JSON::DWIW> object.
247  
248 C<%options> is an optional hash of parameters that will change the
249 bahavior of this module when encoding to JSON. You may also
250 pass these options as the second argument to C<to_json()> and
251 C<from_json()>. The following options are supported:
252  
253 =head3 I<bare_keys>
254  
255 If set to a true value, keys in hashes will not be quoted when
256 converted to JSON if they look like identifiers. This is valid
257 Javascript in current browsers, but not in JSON.
258  
259 =head3 I<use_exceptions>
260  
261 If set to a true value, errors found when converting to or from
262 JSON will result in C<die()> being called with the error message.
263 The default is to not use exceptions.
264  
265 =head3 I<bad_char_policy>
266  
267 This options indicates what should be done if bad characters are
268 found, e.g., bad utf-8 sequence. The default is to return an
269 error and drop all the output.
270  
271 The following values for bad_char_policy are supported:
272  
273 =head4 I<error>
274  
275 default action, i.e., drop any output built up and return an error
276  
277 =head4 I<convert>
278  
279 Convert to a utf-8 char using the value of the byte as a code
280 point. This is basically the same as assuming the bad character
281 is in latin-1 and converting it to utf-8.
282  
283 =head4 I<pass_through>
284  
285 Ignore the error and pass through the raw bytes (invalid JSON)
286  
287 =head3 I<escape_multi_byte>
288  
289 If set to a true value, escape all multi-byte characters (e.g.,
290 \u00e9) when converting to JSON.
291  
292 =head3 I<ascii>
293  
294 Synonym for escape_multi_byte
295  
296 =head3 I<pretty>
297  
298 Add white space to the output when calling to_json() to make the
299 output easier for humans to read.
300  
301 =head3 I<convert_bool>
302  
303 When converting from JSON, return objects for booleans so that
304 "true" and "false" can be maintained when encoding and decoding.
305 If this flag is set, then "true" becomes a L<JSON::DWIW::Boolean>
306 object that evaluates to true in a boolean context, and "false"
307 becomes an object that evaluates to false in a boolean context.
308 These objects are recognized by the to_json() method, so they
309 will be output as "true" or "false" instead of "1" or "0".
310  
311 =head3 I<bare_solidus>
312  
313 Don't escape solidus characters ("/") in strings. The output is
314 still legal JSON with this option turned on.
315  
316 =head3 I<minimal_escaping>
317  
318 Only do required escaping in strings (solidus and quote). Tabs,
319 newlines, backspaces, etc., will not be escaped with this
320 optioned turned on (but the output will still be valid JSON).
321  
322 =head3 I<sort_keys>
323  
324 Set to a true value to sort hash keys (alphabetically) when converting to JSON.
325  
326 =head3 I<parse_number>
327  
328 A subroutine reference to call when parsing a number. The
329 subroutine will be provided one string that is the number being
330 parsed. The return value from the subroutine will be used to
331 populate the return data instead of converting to a number.
332  
333 E.g.,
334  
335 my $json = '{ "a": 6.3e-10 }';
336 my $cb = sub { my ($val) = @_; return "I got the number '$val'"; };
337
338 my $data = JSON::DWIW::deserialize($json, { parse_number => $cb });
339  
340 =head3 I<parse_constant>
341  
342 A subroutine reference to call when parsing a constant (true,
343 false, or null). The subroutine will be provided one string that
344 is the constant being parsed. The return value from the
345 subroutine will be used to populate the return data instead of
346 converting to a boolean or undef. See the "parse_number" option.
347  
348 =head3 I<start_depth>
349  
350 Depth at which C<start_depth_handler> should be called. See L</start_depth_handler>.
351  
352 =head3 I<start_depth_handler>
353  
354 A reference to a subroutine to called when parsing and at level
355 I<start_depth> in the data structure. When specified along with I<start_depth>, the
356 parser does not return the entire data structure. Instead, it
357 calls I<start_depth_handler> for each element in the array when
358 the parser is at level I<start_depth>. This is useful for
359 parsing a very large array without loading all the data into
360 memory (especially when using C<deserialize_file>).
361  
362 E.g., with I<start_depth> set to 1 and I<start_depth_handler> set to C<$handler>:
363  
364 my $str = '[ { "foo": "bar", "cat": 1 }, { "concat": 1, "lambda" : [ "one", 2, 3 ] } ]';
365
366 my $foo = { foo => [ ] };
367 my $handler = sub { push @{$foo->{foo}}, $_[0]; return 1; };
368
369 my $data = JSON::DWIW::deserialize($str, { start_depth => 1,
370 start_depth_handler => $handler });
371 print STDERR Data::Dumper->Dump([ $foo ], [ 'foo' ]) . "\n";
372 print STDERR Data::Dumper->Dump([ $data ], [ 'leftover_data' ]) . "\n";
373  
374 # Output
375 $foo = {
376 'foo' => [
377 {
378 'cat' => 1,
379 'foo' => 'bar'
380 },
381 {
382 'lambda' => [
383 'one',
384 2,
385 3
386 ],
387 'concat' => 1
388 }
389 ]
390 };
391  
392  
393 $leftover_data = [];
394  
395 =cut
396  
397 100 ▶sub new {
Subroutine new: called CC=7 SCAR=20.2
Pod documented
398 100 my $proto = shift;
399  
400 100 ▶ my $self = bless {}, ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
3970
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
401 100 my $params = shift;
402
403 100 ▶ return $self unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 81 19 100
404  
405 19 ▶ unless (defined($params) and UNIVERSAL::isa($params, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params and UNIVERSAL::isa($params, "HASH")) 0 19 19
Condition33%1/3
defined $params and UNIVERSAL::isa($params, "HASH")
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $params and UNIVERSAL::isa($params, "HASH")
defined $params UNIVERSAL::isa($params, "HASH")
Truth table33%1/3
A and B
A= defined $paramsB= UNIVERSAL::isa($params, "HASH")
ABresult
0X0
100
111
406 0 return $self;
407 }
408  
409 19 foreach my $field (qw/bare_keys use_exceptions bad_char_policy dump_vars pretty
410 escape_multi_byte convert_bool detect_circular_refs
411 ascii bare_solidus minimal_escaping
412 parse_number parse_constant sort_keys start_depth start_depth_handler/) {
413 304 ▶ if (exists($params->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{$field}) 21 283 304
414 21 $self->{$field} = $params->{$field};
415 }
416 }
417  
418 19 return $self;
419 }
420  
421 =pod
422  
423 =head2 C<to_json>
424  
425 Returns the JSON representation of $data (arbitrary
426 datastructure). See http://www.json.org/ for details.
427  
428 Called in list context, this method returns a list whose first
429 element is the encoded JSON string and the second element is an
430 error message, if any. If $error_msg is defined, there was a
431 problem converting to JSON. You may also pass a second argument
432 to to_json() that is a reference to a hash of options -- see
433 new().
434  
435 my $json_str = JSON::DWIW->to_json($data);
436
437 my ($json_str, $error_msg) = JSON::DWIW->to_json($data);
438
439 my $json_str = JSON::DWIW->to_json($data, { use_exceptions => 1 });
440  
441 Aliases: toJson, toJSON, objToJson
442  
443 =cut
444  
445 50 ▶sub to_json {
Subroutine to_json: called CC=11 SCAR=24.0
Pod documented
446 50 my $proto = shift;
447 50 my $data;
448
449 my $self;
450 50 ▶ if (UNIVERSAL::isa($proto, 'JSON::DWIW')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($proto, 'JSON::DWIW')) { } 49 1 50
451 49 $data = shift;
452 49 my $options = shift;
453 49 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 10 39 49
454 10 ▶ if (ref($proto) and $proto->isa('HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto and $proto->isa("HASH")) 3 7 10
Condition66%2/3
ref $proto and $proto->isa("HASH")
!ll&&!rl&&r
703
MC/DC50%1/2
ref $proto and $proto->isa("HASH")
ref $proto $proto->isa("HASH")
Truth table66%2/3
A and B
A= ref $protoB= $proto->isa("HASH")
ABresult
0X0
100
111
455 3 ▶ if (UNIVERSAL::isa($options, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options, "HASH")) 3 0 3
456 3 $options = { %$proto, %$options };
457 }
458 }
459  
460 10 $self = $proto->new($options, @_);
461 }
462 else {
463 39 ▶ $self = ref($proto) ? $proto : $proto->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 25 14 39
464 }
465 }
466 else {
467 1 $data = $proto;
468 1 $self = JSON::DWIW->new(@_);
469 }
470  
471 50 my $error_msg;
472 my $error_data;
473 50 my $stats_data = { };
474 50 my $str = _xs_to_json($self, $data, \$error_msg, \$error_data, $stats_data);
475  
476 50 ▶ if ($stats_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats_data) 50 0 50
477 50 $JSON::DWIW::Last_Stats = $stats_data;
478 50 $self->{last_stats} = $stats_data;
479 }
480  
481 50 $JSON::DWIW::LastError = $error_msg;
482 50 $self->{last_error} = $error_msg;
483  
484 50 $JSON::DWIW::LastErrorData = $error_data;
485 50 $self->{last_error_data} = $error_data;
486  
487 50 ▶ if (defined($error_msg) and $self->{use_exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error_msg and $self->{"use_exceptions"}) 1 49 50
Condition66%2/3
defined $error_msg and $self->{"use_exceptions"}
!ll&&!rl&&r
4901
MC/DC50%1/2
defined $error_msg and $self->{"use_exceptions"}
defined $error_msg $self->{"use_exceptions"}
Truth table66%2/3
A and B
A= defined $error_msgB= $self->{"use_exceptions"}
ABresult
0X0
100
111
488 1 die $error_msg;
489 }
490 49 ▶ return wantarray ? ($str, $error_msg) : $str;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 48 49
491 }
492 {
493 36 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
494
495 36 *toJson = \&to_json;
496 36 *toJSON = \&to_json;
497 36 *objToJson = \&to_json;
498 }
499  
500 1 ▶sub serialize {
Subroutine serialize: called CC=3 SCAR=11.1
Pod undocumented
501 1 my $data = shift;
502 1 ▶ my $options = shift || { };
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
503  
504 1 my $error_msg;
505 my $error_data;
506 1 my $stats_data = { };
507 1 my $str = _xs_to_json($options, $data, \$error_msg, \$error_data, $stats_data);
508  
509 1 ▶ if ($stats_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats_data) 1 0 1
510 1 $JSON::DWIW::Last_Stats = $stats_data;
511 }
512  
513 1 $JSON::DWIW::LastError = $error_msg;
514  
515 1 $JSON::DWIW::LastErrorData = $error_data;
516  
517 1 return $str;
518 }
519  
520 # total process size in pages
521 0 ▶sub get_proc_size {
Subroutine get_proc_size: not called CC=4 SCAR=30.0
Pod undocumented
522 0 ▶ if ($^O eq 'linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux") 0 0 0
523 0 my $statm_path = "/proc/$$/statm";
524 0 ▶ if (-e $statm_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $statm_path) 0 0 0
525 0 ▶ open(my $in_fh, '<', $statm_path) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<", $statm_path or return undef 0 0 0
526 0 my $statm = <$in_fh>;
527 0 close $in_fh;
528
529 0 my @fields = split /\s+/, $statm;
530
531 0 return $fields[0];
532 }
533 }
534  
535 0 return undef;
536 }
537  
538 =pod
539  
540 =head2 C<deserialize($json_str, \%options)>
541  
542 Returns the Perl data structure for the given JSON string. The
543 value for true becomes 1, false becomes 0, and null gets
544 converted to undef.
545  
546 This function should not be called as a method (for performance
547 reasons). Unlike C<from_json()>, it returns a single value, the
548 data structure resulting from the conversion. If the return
549 value is undef, check the result of the C<get_error_string()>
550 function/method to see if an error is defined.
551  
552 =head2 C<deserialize_file($file, \%options)>
553  
554 Same as deserialize, except that it takes a file as an argument.
555 On Unix, this mmap's the file, so it does not load a big file
556 into memory all at once, and does less buffer copying.
557  
558 =cut
559  
560 =pod
561  
562 =head2 C<from_json>
563  
564 Similar to C<deserialize()>, but expects to be called as a method.
565  
566 Called in list context, this method returns a list whose first
567 element is the data and the second element is the error message,
568 if any. If C<$error_msg> is defined, there was a problem parsing
569 the JSON string, and C<$data> will be undef. You may also pass a
570 second argument to C<from_json()> that is a reference to a hash of
571 options -- see C<new()>.
572  
573 my $data = from_json($json_str)
574
575 my ($data, $error_msg) = from_json($json_str)
576  
577  
578 Aliases: fromJson, fromJSON, jsonToObj
579  
580 =cut
581  
582 60 ▶sub from_json {
Subroutine from_json: called CC=11 SCAR=25.1
Pod documented
583 60 my $proto = shift;
584 60 my $json;
585 my $self;
586  
587 60 ▶ if (UNIVERSAL::isa($proto, 'JSON::DWIW')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($proto, 'JSON::DWIW')) { } 58 2 60
588 58 $json = shift;
589 58 my $options = shift;
590 58 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 4 54 58
591 4 ▶ if (ref($proto) and $proto->isa('HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto and $proto->isa("HASH")) 0 4 4
Condition33%1/3
ref $proto and $proto->isa("HASH")
!ll&&!rl&&r
400
MC/DC0%0/2
ref $proto and $proto->isa("HASH")
ref $proto $proto->isa("HASH")
Truth table33%1/3
A and B
A= ref $protoB= $proto->isa("HASH")
ABresult
0X0
100
111
592 0 ▶ if (UNIVERSAL::isa($options, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options, "HASH")) 0 0 0
593 0 $options = { %$proto, %$options };
594 }
595 }
596  
597 4 $self = $proto->new($options, @_);
598 }
599 else {
600 54 ▶ $self = ref($proto) ? $proto : $proto->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 17 37 54
601 }
602 }
603 else {
604 2 $json = $proto;
605 2 $self = JSON::DWIW->new(@_);
606 }
607  
608 60 my $data;
609 60 ▶ if (%$self) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$self) { } 18 42 60
610 18 $data = JSON::DWIW::deserialize($json, $self);
611 }
612 else {
613 42 $data = JSON::DWIW::deserialize($json);
614 }
615  
616 56 $self->{last_error} = $JSON::DWIW::LastError;
617 56 $self->{last_error_data} = $JSON::DWIW::LastErrorData;
618 56 $self->{last_stats} = $JSON::DWIW::Last_Stats;
619  
620 56 ▶ if (defined($JSON::DWIW::LastError) and $self->{use_exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $JSON::DWIW::LastError and $self->{"use_exceptions"}) 0 56 56
Condition66%2/3
defined $JSON::DWIW::LastError and $self->{"use_exceptions"}
!ll&&!rl&&r
4790
MC/DC0%0/2
defined $JSON::DWIW::LastError and $self->{"use_exceptions"}
defined $JSON::DWIW::LastError $self->{"use_exceptions"}
Truth table66%2/3
A and B
A= defined $JSON::DWIW::LastErrorB= $self->{"use_exceptions"}
ABresult
0X0
100
111
621 0 die $JSON::DWIW::LastError;
622 }
623  
624 56 ▶ return wantarray ? ($data, $JSON::DWIW::LastError) : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 51 56
625 }
626  
627 {
628 36 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
629 36 *jsonToObj = \&from_json;
630 36 *fromJson = \&from_json;
631 36 *fromJSON = \&from_json;
632 }
633  
634 =pod
635  
636 =head2 C<from_json_file>
637  
638 Similar to C<deserialize_file()>, except that it expects to be
639 called a a method, and it also returns the error, if any, when called
640 in list context.
641  
642 my ($data, $error_msg) = $json->from_json_file($file, \%options)
643  
644 =cut
645 22 ▶sub from_json_file {
Subroutine from_json_file: called CC=11 SCAR=30.7
Pod documented
646 22 my $proto = shift;
647 22 my $file;
648 my $self;
649
650 22 ▶ if (UNIVERSAL::isa($proto, 'JSON::DWIW')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($proto, 'JSON::DWIW')) { } 22 0 22
651 22 $file = shift;
652 22 my $options = shift;
653 22 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 0 22 22
654 0 ▶ if (ref($proto) and $proto->isa('HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto and $proto->isa("HASH")) 0 0 0
Condition0%0/3
ref $proto and $proto->isa("HASH")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $proto and $proto->isa("HASH")
ref $proto $proto->isa("HASH")
Truth table0%0/3
A and B
A= ref $protoB= $proto->isa("HASH")
ABresult
0X0
100
111
655 0 ▶ if (UNIVERSAL::isa($options, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options, "HASH")) 0 0 0
656 0 $options = { %$proto, %$options };
657 }
658 }
659  
660 0 $self = $proto->new($options, @_);
661 }
662 else {
663 22 ▶ $self = ref($proto) ? $proto : $proto->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 1 21 22
664 }
665 }
666 else {
667 0 $file = $proto;
668 0 $self = JSON::DWIW->new(@_);
669 }
670  
671 22 my $data;
672 22 ▶ if (%$self) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$self) { } 0 22 22
673 0 $data = JSON::DWIW::deserialize_file($file, $self);
674 }
675 else {
676 22 $data = JSON::DWIW::deserialize_file($file);
677 }
678  
679 22 $self->{last_error} = $JSON::DWIW::LastError;
680 22 $self->{last_error_data} = $JSON::DWIW::LastErrorData;
681 22 $self->{last_stats} = $JSON::DWIW::Last_Stats;
682  
683 22 ▶ if (defined($JSON::DWIW::LastError) and $self->{use_exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $JSON::DWIW::LastError and $self->{"use_exceptions"}) 0 22 22
Condition66%2/3
defined $JSON::DWIW::LastError and $self->{"use_exceptions"}
!ll&&!rl&&r
6160
MC/DC0%0/2
defined $JSON::DWIW::LastError and $self->{"use_exceptions"}
defined $JSON::DWIW::LastError $self->{"use_exceptions"}
Truth table66%2/3
A and B
A= defined $JSON::DWIW::LastErrorB= $self->{"use_exceptions"}
ABresult
0X0
100
111
684 0 die $JSON::DWIW::LastError;
685 }
686  
687 22 ▶ return wantarray ? ($data, $JSON::DWIW::LastError) : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 20 2 22
688 }
689  
690 =pod
691  
692 =head2 C<to_json_file>
693  
694 Converts C<$data> to JSON and writes the result to the file C<$file>.
695 Currently, this is simply a convenience routine that converts
696 the data to a JSON string and then writes it to the file.
697  
698 my ($ok, $error) = $json->to_json_file($data, $file, \%options);
699  
700 =cut
701 0 ▶sub to_json_file {
Subroutine to_json_file: not called CC=17 SCAR=57.2
Pod documented
702 0 my $proto = shift;
703 0 my $file;
704 my $data;
705 0 my $self;
706
707 0 ▶ if (UNIVERSAL::isa($proto, 'JSON::DWIW')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($proto, 'JSON::DWIW')) { } 0 0 0
708 0 $data = shift;
709 0 $file = shift;
710 0 my $options = shift;
711 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 0 0 0
712 0 ▶ if (ref($proto) and $proto->isa('HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto and $proto->isa("HASH")) 0 0 0
Condition0%0/3
ref $proto and $proto->isa("HASH")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $proto and $proto->isa("HASH")
ref $proto $proto->isa("HASH")
Truth table0%0/3
A and B
A= ref $protoB= $proto->isa("HASH")
ABresult
0X0
100
111
713 0 ▶ if (UNIVERSAL::isa($options, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options, "HASH")) 0 0 0
714 0 $options = { %$proto, %$options };
715 }
716 }
717  
718 0 $self = $proto->new($options, @_);
719 }
720 else {
721 0 ▶ $self = ref($proto) ? $proto : $proto->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 0 0 0
722 }
723 }
724 else {
725 0 $data = $proto;
726 0 $file = shift;
727 0 $self = JSON::DWIW->new(@_);
728 }
729  
730 0 my $out_fh;
731 0 ▶ unless (open($out_fh, '>', $file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $out_fh, ">", $file) 0 0 0
732 0 my $msg = "JSON::DWIW v$VERSION - couldn't open output file $file";
733 0 ▶ if ($self->{use_exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_exceptions'}) { } 0 0 0
734 0 die $msg;
735 } else {
736 0 ▶ return wantarray ? ( undef, $msg ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
737 }
738 }
739  
740 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
741 0 binmode($out_fh, 'utf8');
742 }
743  
744 0 my $error_msg;
745 my $error_data;
746 0 my $stats_data = { };
747 0 my $str = _xs_to_json($self, $data, \$error_msg, \$error_data, $stats_data);
748  
749 0 ▶ if ($stats_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats_data) 0 0 0
750 0 $JSON::DWIW::Last_Stats = $stats_data;
751 0 $self->{last_stats} = $stats_data;
752 }
753  
754 0 $JSON::DWIW::LastError = $error_msg;
755 0 $self->{last_error} = $error_msg;
756  
757 0 $JSON::DWIW::LastErrorData = $error_data;
758 0 $self->{last_error_data} = $error_data;
759  
760  
761 0 ▶ if (defined($error_msg) and $self->{use_exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error_msg and $self->{"use_exceptions"}) 0 0 0
Condition0%0/3
defined $error_msg and $self->{"use_exceptions"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $error_msg and $self->{"use_exceptions"}
defined $error_msg $self->{"use_exceptions"}
Truth table0%0/3
A and B
A= defined $error_msgB= $self->{"use_exceptions"}
ABresult
0X0
100
111
762 0 die $error_msg;
763 }
764  
765 0 ▶ if ($error_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_msg) 0 0 0
766 0 ▶ return wantarray ? (undef, $error_msg) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
767 }
768  
769 0 print $out_fh $str;
770 0 close $out_fh;
771  
772 0 ▶ return wantarray ? (1, $error_msg) : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
773 }
774  
775 0 ▶sub parse_mmap_file {
Subroutine parse_mmap_file: not called CC=3 SCAR=24.8
Pod undocumented
776 0 my $proto = shift;
777 0 my $file = shift;
778  
779 0 my $error_msg;
780 0 my $self = $proto->new;
781  
782 0 my $data = _parse_mmap_file($self, $file, \$error_msg);
783 0 ▶ if ($error_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_msg) 0 0 0
784 0 ▶ return wantarray ? (undef, $error_msg) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
785 }
786 }
787  
788 =pod
789  
790 =head2 C<get_error_string>
791  
792 Returns the error message from the last call, if there was one, e.g.,
793  
794 my $data = JSON::DWIW->from_json($json_str)
795 or die "JSON error: " . JSON::DWIW->get_error_string;
796
797 my $data = $json_obj->from_json($json_str)
798 or die "JSON error: " . $json_obj->get_error_string;
799  
800  
801 Aliases: get_err_str(), errstr()
802  
803 =cut
804 53 ▶sub get_error_string {
Subroutine get_error_string: called CC=2 SCAR=6.9
Pod documented
805 53 my $self = shift;
806  
807 53 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 2 51 53
808 2 return $self->{last_error};
809 }
810
811 51 return $JSON::DWIW::LastError;
812 }
813 36 *get_err_str = \&get_error_string;
814 36 *errstr = \&get_error_string;
815  
816 =pod
817  
818 =head2 C<get_error_data>
819  
820 Returns the error details from the last call, in a hash ref, e.g.,
821  
822 $error_data = {
823 'byte' => 23,
824 'byte_col' => 23,
825 'col' => 22,
826 'char' => 22,
827 'version' => '0.15a',
828 'line' => 1
829 };
830  
831 This is really only useful when decoding JSON.
832  
833 Aliases: get_error(), error()
834  
835 =cut
836 0 ▶sub get_error_data {
Subroutine get_error_data: not called CC=2 SCAR=17.9
Pod documented
837 0 my $self = shift;
838  
839 0 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
840 0 return $self->{last_error_data};
841 }
842  
843 0 return $JSON::DWIW::LastErrorData;
844 }
845 36 *get_error = \&get_error_data;
846 36 *error = \&get_error_data;
847  
848 =pod
849  
850 =head2 C<get_stats>
851  
852 Returns statistics from the last method called to encode or
853 decode. E.g., for an encoding (C<to_json()> or C<to_json_file()>),
854  
855 $stats = {
856 'bytes' => 78,
857 'nulls' => 1,
858 'max_string_bytes' => 5,
859 'max_depth' => 2,
860 'arrays' => 1,
861 'numbers' => 6,
862 'lines' => 1,
863 'max_string_chars' => 5,
864 'strings' => 6,
865 'bools' => 1,
866 'chars' => 78,
867 'hashes' => 1
868 };
869  
870 =cut
871 11 ▶sub get_stats {
Subroutine get_stats: called CC=2 SCAR=7.6
Pod documented
872 11 my $self = shift;
873 11 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 11 11
874 0 return $self->{last_stats};
875 }
876  
877 11 return $JSON::DWIW::Last_Stats;
878 }
879 36 *stats = \&get_stats;
880  
881  
882 =pod
883  
884 =head2 C<true>
885  
886 Returns an object that will get output as a true value when encoding to JSON.
887  
888 =cut
889  
890 1 ▶sub true {
Subroutine true: called CC=1 SCAR=0.0
Pod documented
891 1 return JSON::DWIW::Boolean->true;
892 }
893  
894 =pod
895  
896 =head2 C<false>
897  
898 Returns an object that will get output as a false value when encoding to JSON.
899  
900 =cut
901  
902 1 ▶sub false {
Subroutine false: called CC=1 SCAR=0.0
Pod documented
903 1 return JSON::DWIW::Boolean->false;
904 }
905  
906 52 ▶sub _escape_xml_body {
Subroutine _escape_xml_body: called CC=2 SCAR=7.0
907 52 my ($text) = @_;
908 52 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 52 52
909  
910 # FIXME: benchmark this and test fully
911 # $text =~ s/([&<>"'])/$Escape_Map->{$1}/eg;
912 # return $text;
913
914 52 $text =~ s/\&/\&amp;/g;
915 52 $text =~ s/</\&lt;/g;
916 52 $text =~ s/>/\&gt;/g;
917  
918 52 return $text;
919 }
920  
921 0 ▶sub _escape_xml_attr {
Subroutine _escape_xml_attr: not called CC=2 SCAR=17.9
922 0 my ($text) = @_;
923 0 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
924  
925 # FIXME: benchmark this and test fully
926 # $text =~ s/([&<>"'])/$Escape_Map->{$1}/eg;
927 # return $text;
928
929 0 $text =~ s/\&/\&amp;/g;
930 0 $text =~ s/</\&lt;/g;
931 0 $text =~ s/>/\&gt;/g;
932 0 $text =~ s/\"/\&quot;/g;
933  
934 0 return $text;
935 }
936  
937 78 ▶sub _to_xml {
Subroutine _to_xml: called CC=19 SCAR=30.6
938 78 my ($data, $level, $params, $parent_tag) = @_;
939  
940 78 ▶ return '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 78 78
941
942 78 ▶ $params = { } unless $params and UNIVERSAL::isa($params, 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless $params and UNIVERSAL::isa($params, "HASH") 5 73 78
Condition66%2/3
$params and UNIVERSAL::isa($params, "HASH")
!ll&&!rl&&r
5073
MC/DC50%1/2
$params and UNIVERSAL::isa($params, "HASH")
$params UNIVERSAL::isa($params, "HASH")
Truth table66%2/3
A and B
A= $paramsB= UNIVERSAL::isa($params, "HASH")
ABresult
0X0
100
111
943 78 ▶ $level ||= 0;
Condition100%2/2
$level ||= 0
l!l
6810
MC/DC100%1/1
$level ||= 0
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
944
945 78 my $xml = '';
946  
947 78 my $ref = ref($data);
948  
949 78 ▶ unless ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 52 26 78
950 # string
951 52 return _escape_xml_body($data);
952 }
953  
954 26 ▶ my $indent = $params->{pretty} ? (' ' x $level) : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'pretty'} ? : 0 26 26
955 26 ▶ my $nl = $params->{pretty} ? "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'pretty'} ? : 0 26 26
956 26 ▶ my $start = ($params->{pretty} and $level) ? "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'pretty'} && $level ? : 0 26 26
Condition33%1/3
$params->{"pretty"} and $level
!ll&&!rl&&r
2600
MC/DC0%0/2
$params->{"pretty"} and $level
$params->{"pretty"} $level
Truth table33%1/3
A and B
A= $params->{"pretty"}B= $level
ABresult
0X0
100
111
957 26 ▶ my $end = ($params->{pretty} and $level >= 2) ? (' ' x ($level - 1)) : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'pretty'} && $level >= 2 ? : 0 26 26
Condition33%1/3
$params->{"pretty"} and $level >= 2
!ll&&!rl&&r
2600
MC/DC0%0/2
$params->{"pretty"} and $level >= 2
$params->{"pretty"} $level >= 2
Truth table33%1/3
A and B
A= $params->{"pretty"}B= $level >= 2
ABresult
0X0
100
111
958 26 my $first = 1;
959  
960 26 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 10 16 26
elsif ($ref eq 'HASH') { } 16 0 16
961 10 foreach my $e (@$data) {
962 31 $xml .= "$start$indent<$parent_tag>";
963 31 $xml .= _to_xml($e, $level + 1, $params, $parent_tag);
964 31 $xml .= "</$parent_tag>$nl$end";
965 }
966 continue {
967 31 $first = 0;
968 }
969  
970 10 return $xml;
971 }
972 elsif ($ref eq 'HASH') {
973 16 foreach my $k (sort keys %$data) {
974 42 ▶ $start = '' unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 26 16 42
975 42 (my $tag = $k) =~ s/[^\w-]/_/g;
976 42 my $this_ref = ref($data->{$k});
977 42 ▶ if ($this_ref and $this_ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_ref and $this_ref eq "ARRAY") 10 32 42
Condition100%3/3
$this_ref and $this_ref eq "ARRAY"
!ll&&!rl&&r
221010
MC/DC100%2/2
$this_ref and $this_ref eq "ARRAY"
$this_ref $this_ref eq "ARRAY"
Truth table100%3/3
A and B
A= $this_refB= $this_ref eq "ARRAY"
ABresult
0X0
100
111
978 10 $xml .= _to_xml($data->{$k}, $level, $params, $tag);
979 10 next;
980 }
981
982 32 $xml .= "$start$indent<$tag>";
983 32 $xml .= _to_xml($data->{$k}, $level + 1, $params, $tag);
984 32 $xml .= "</$tag>$nl$end";
985 }
986 continue {
987 42 $first = 0;
988 }
989  
990 16 return $xml;
991 }
992 else {
993 # make sure objects are stringified, e.g., Math::BigInt
994 0 return _escape_xml_body($data . '');
995 }
996  
997 0 return $xml;
998 }
999  
1000 5 ▶sub _data_to_xml {
Subroutine _data_to_xml: called CC=1 SCAR=0.0
1001 5 my ($data, $params) = @_;
1002
1003 5 return _to_xml($data, 0, $params);
1004 }
1005  
1006  
1007 =pod
1008  
1009 =head2 C<json_to_xml($json, \%params)>
1010  
1011 This function (not a method) converts the given JSON to XML.
1012 Hash/object keys become tag names. Arrays that are hash values
1013 are output as multiple tags with the hash key as the tag name.
1014  
1015 Any characters in hash keys not in C<[\w-]> (i.e., letters, numbers,
1016 underscore, or dash), get converted to underscore ("_") when
1017 output as XML tags.
1018  
1019 Valid parameters in C<\%params> are the same as for passing
1020 to C<deserialize()> or C<from_json()>, plus the C<pretty> option, which
1021 will add newlines and indentation to the XML to make it more
1022 human-readable.
1023  
1024 =cut
1025 5 ▶sub json_to_xml {
Subroutine json_to_xml: called CC=4 SCAR=16.3
Pod documented
1026 5 my ($json, $params) = @_;
1027  
1028 5 my $data;
1029 5 ▶ if ($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) { } 0 5 5
1030 0 $data = JSON::DWIW::deserialize($json, $params);
1031 }
1032 else {
1033 5 $data = JSON::DWIW::deserialize($json);
1034 }
1035  
1036 5 my $ref = ref($data);
1037 5 ▶ if ($ref and $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and $ref eq "ARRAY") 0 5 5
Condition33%1/3
$ref and $ref eq "ARRAY"
!ll&&!rl&&r
050
MC/DC0%0/2
$ref and $ref eq "ARRAY"
$ref $ref eq "ARRAY"
Truth table33%1/3
A and B
A= $refB= $ref eq "ARRAY"
ABresult
0X0
100
111
1038 0 warn "top level of data must be an object/hash ref in json_to_xml() call";
1039 0 return undef;
1040 }
1041  
1042 5 return _data_to_xml($data, $params);
1043 }
1044  
1045 0 ▶sub jsonml_to_xml {
Subroutine jsonml_to_xml: not called CC=2 SCAR=17.9
Pod undocumented
1046 0 my ($jsonml) = @_;
1047  
1048 0 my $elements = JSON::DWIW::deserialize($jsonml);
1049 0 ▶ return undef unless defined $elements;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elements 0 0 0
1050  
1051 0 return _jsonml_xml($elements);
1052 }
1053  
1054 0 ▶sub _jsonml_xml {
Subroutine _jsonml_xml: not called CC=6 SCAR=37.4
1055 0 my ($elements) = @_;
1056  
1057 0 ▶ unless (ref($elements)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $elements) 0 0 0
1058 # string
1059 0 return _escape_xml_body($elements);
1060 }
1061  
1062 0 my $name = $elements->[0];
1063 0 my $attrs = $elements->[1];
1064 0 my $attr_str = '';
1065  
1066 0 my @rest;
1067 0 ▶ if (defined $attrs and UNIVERSAL::isa($attrs, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs and UNIVERSAL::isa($attrs, 'HASH')) { } 0 0 0
Condition0%0/3
defined $attrs and UNIVERSAL::isa($attrs, "HASH")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attrs and UNIVERSAL::isa($attrs, "HASH")
defined $attrs UNIVERSAL::isa($attrs, "HASH")
Truth table0%0/3
A and B
A= defined $attrsB= UNIVERSAL::isa($attrs, "HASH")
ABresult
0X0
100
111
1068 0 my @keys = sort keys %$attrs;
1069 0 my @pairs = map { qq{$_="} . _escape_xml_attr($attrs->{$_}) . qq{"} } @keys;
1070 0 $attr_str = ' ' . join(' ', @pairs);
1071 0 @rest = @$elements[2 .. $#$elements];
1072 }
1073 else {
1074 0 $attrs = undef;
1075 0 @rest = @$elements[1 .. $#$elements];
1076 }
1077
1078 0 my $xml = "<$name$attr_str";
1079  
1080 0 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) { } 0 0 0
1081 0 $xml .= '>';
1082 0 foreach my $e (@rest) {
1083 0 $xml .= _jsonml_xml($e);
1084 }
1085 0 $xml .= "</$name>";
1086 }
1087 else {
1088 0 $xml .= '/>';
1089 }
1090  
1091 0 return $xml;
1092 }
1093  
1094 # used from XS code to sort keys in Perl < 5.8.0 where we don't have
1095 # access to sortsv() from XS
1096 0 ▶sub _sort_keys {
Subroutine _sort_keys: not called CC=1 SCAR=6.9
1097 0 return [ sort keys %{ $_[0] } ]
1098 }
1099  
1100  
1101 =pod
1102  
1103 =head1 Utilities
1104  
1105 Following are some methods I use for debugging and testing.
1106  
1107 =head2 C<flagged_as_utf8($str)>
1108  
1109 Returns true if the given string is flagged as utf-8.
1110  
1111 =head2 C<flag_as_utf8($str)>
1112  
1113 Flags the given string as utf-8.
1114  
1115 =head2 C<unflag_as_utf8($str)>
1116  
1117 Clears the flag that tells Perl the string is utf-8.
1118  
1119 =head2 C<is_valid_utf8($str)>
1120  
1121 Returns true if the given string is valid utf-8 (regardless of the flag).
1122  
1123 =head2 C<upgrade_to_utf8($str)>
1124  
1125 Converts the string to utf-8, assuming it is latin1. This effects $str itself in place, but also returns $str.
1126  
1127 =head2 C<code_point_to_utf8_str($cp)>
1128  
1129 Returns a utf8 string containing the byte sequence for the given code point.
1130  
1131 =head2 C<code_point_to_hex_bytes($cp)>
1132  
1133 Returns a string representing the byte sequence for $cp encoding in utf-8. E.g.,
1134  
1135 my $hex_bytes = JSON::DWIW->code_point_to_hex_bytes(0xe9);
1136 print "$hex_bytes\n"; # \xc3\xa9
1137  
1138 =head2 C<bytes_to_code_points($str)>
1139  
1140 Returns a reference to an array of code points from the given string, assuming the string is encoded in utf-8.
1141  
1142 =head2 C<peek_scalar($scalar)>
1143  
1144 Dumps the internal structure of the given scalar.
1145  
1146 =head1 BENCHMARKS
1147  
1148 Need new benchmarks here.
1149  
1150 =head1 DEPENDENCIES
1151  
1152 Perl 5.6 or later
1153  
1154 =head1 BUGS/LIMITATIONS
1155  
1156 If you find a bug, please file a tracker request at
1157 L<http://rt.cpan.org/Public/Dist/Display.html?Name=JSON-DWIW>.
1158  
1159 When decoding a JSON string, it is a assumed to be utf-8 encoded.
1160 The module should detect whether the input is utf-8, utf-16, or
1161 utf-32.
1162  
1163 =head1 AUTHOR
1164  
1165 Don Owens <don@regexguy.com>
1166  
1167 =head1 ACKNOWLEDGEMENTS
1168  
1169 Thanks to Asher Blum for help with testing.
1170  
1171 Thanks to Nigel Bowden for helping with compilation on Windows.
1172  
1173 Thanks to Robert Peters for discovering and tracking down the source of a number parsing bug.
1174  
1175 Thanks to Mark Phillips for helping with a bug under Solaris on Sparc.
1176  
1177 Thanks to Josh for helping debug [rt.cpan.org #47344].
1178  
1179 =head1 LICENSE AND COPYRIGHT
1180  
1181 Copyright (c) 2007-2010 Don Owens <don@regexguy.com>. All rights reserved.
1182  
1183 This is free software; you can redistribute it and/or modify it
1184 under the same terms as Perl itself. See perlartistic.
1185  
1186 This program is distributed in the hope that it will be
1187 useful, but WITHOUT ANY WARRANTY; without even the implied
1188 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
1189 PURPOSE.
1190  
1191 =head1 SEE ALSO
1192  
1193 =over 4
1194  
1195 =item The JSON home page: L<http://json.org/>
1196  
1197 =item The JSON spec: L<http://www.ietf.org/rfc/rfc4627.txt>
1198  
1199 =item The JSON-RPC spec: L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>
1200  
1201 =item L<JSON>
1202  
1203 =item L<JSON::Syck> (included in L<YAML::Syck>)
1204  
1205 =back
1206  
1207 =cut
1208  
1209 36 1;
1210  
1211 # Local Variables: #
1212 # mode: perl #
1213 # tab-width: 4 #
1214 # indent-tabs-mode: nil #
1215 # cperl-indent-level: 4 #
1216 # perl-indent-level: 4 #
1217 # End: #
1218 # vim:set ai si et sta ts=4 sw=4 sts=4:
↑ Summary blib/lib/JSON/DWIW/Boolean.pm »