lib/JSON/ize.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 75.0% 18 / 24 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 16 / 16 Podpod 75.0% 9 / 12 totaltotal 83.6% 92 / 110 SCAR 28.3
CC
16
Cov
84%
jsonize
11.4
looks_like_yaml
3.3
__ANON__
3.0
CRAP 17.0
« blib/lib/JSON/ize.pm ↑ Summary Build »
1 package JSON::ize;
2 use base Exporter;
3 use JSON::MaybeXS;
4 use YAML::Any qw/Dump Load LoadFile DumpFile/;
5 use Try::Tiny;
6 use strict;
7 use warnings;
8  
9 our $JOBJ = JSON::MaybeXS->new();
10 our $YOBJ;
11 our $_last_out = "";
12  
13 our @EXPORT = qw/jsonize jsonise J yamlize yamlise Y parsej pretty_json ugly_json/;
14 our $VERSION = "0.201";
15  
16 2 ▶sub jobj { $JOBJ }
Subroutine jobj: called CC=1 SCAR=0.0
Pod undocumented
17  
18 sub jsonize (;$) {
19 2 ▶ my $inp = shift;
Subroutine jsonize: called CC=11 SCAR=24.3
Pod documented
20 2 ▶ if (!defined $inp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inp) 4 20 24
21 2 return $_last_out;
22 }
23 2 ▶ if (ref $inp) { # encode perl object
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inp) { } 5 15 20
24 2 my ($a,$b,$c,$subr,@rest) = caller(1);
25 2 ▶ return $_last_out = ($subr && $subr =~ /Y|yamli[sz]e$/) ? Dump($inp) : jobj()->encode($inp);
Branch: 1 branch
BranchTrueFalseTotal
$subr && $subr =~ /Y|yamli[sz]e$/ ? : 4 1 5
Condition66%2/3
$subr and $subr =~ /Y|yamli[sz]e$/
!ll&&!rl&&r
104
MC/DC50%1/2
$subr and $subr =~ /Y|yamli[sz]e$/
$subr $subr =~ /Y|yamli[sz]e$/
Truth table66%2/3
A and B
A= $subrB= $subr =~ /Y|yamli[sz]e$/
ABresult
0X0
100
111
26 }
27 else { # scalar: decode if looks like json or yaml, or slurp if filename
28 2 ▶ if (looks_like_json($inp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (looks_like_json($inp)) { } 4 11 15
elsif (looks_like_yaml($inp)) { } 0 11 11
29 2 return $_last_out = jobj()->decode($inp);
30 }
31 elsif (looks_like_yaml($inp)) {
32 2 return $_last_out = Load($inp);
33 }
34 else { # try as file
35 2 local $/;
36 2 my ($j,$f);
37 2 ▶ die "'$inp' is not a existing filename, json string, or a reference" unless (-e $inp);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $inp 1 10 11
38 2 ▶ if ( eval "require PerlIO::gzip; 1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require PerlIO::gzip; 1') { } 10 0 10
39 2 ▶ open $f, "<:gzip(autopop)", $inp or die "Problem with file '$inp' : $!";
Branch: 1 branch
BranchTrueFalseTotal
open $f, "<:gzip(autopop)", $inp or die "Problem with file '${inp}' : $!" 0 10 10
40 }
41 else {
42 2 ▶ open $f, "$inp" or die "Problem with file '$inp' : $!";
Branch: 1 branch
BranchTrueFalseTotal
open $f, "$inp" or die "Problem with file '${inp}' : $!" 0 0 0
43 }
44 2 $j = <$f>;
45 try {
46 24 ▶ $_last_out = jobj()->decode($j);
Subroutine __ANON__: called CC=1 SCAR=0.0
47 } catch {
48 24 ▶ /at character offset/ && do { # JSON error
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
/at character offset/ and do { my $jerr = $_; if (looks_like_json($j)) { die "JSON decode barfed.\nJSON err: $jerr"; } ; &try(sub { $_last_out = Load($j); } , &catch(sub { if (looks_like_yaml($j)) { die "YAML decode barfed.\nYAML err: $_"; } ; die "Both JSON and YAML decodes barfed.\nJSON err: $jerr\nYAML err: $_"; } )) } 6 0 6
49 4 my $jerr = $_;
50 20 ▶ if (looks_like_json($j)) { # probably really was JSON
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_json($j)) 2 4 6
51 5 die "JSON decode barfed.\nJSON err: $jerr"
52 }
53 try { # might be YAML
54 15 ▶ $_last_out = Load($j);
Subroutine __ANON__: called CC=1 SCAR=0.0
55 } catch {
56 4 ▶ if (looks_like_yaml($j)) {
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_yaml($j)) 1 1 2
57 0 die "YAML decode barfed.\nYAML err: $_";
58 }
59 11 die "Both JSON and YAML decodes barfed.\nJSON err: $jerr\nYAML err: $_";
60 5 };
61 };
62 2 };
63 15 return $_last_out;
64 }
65 }
66 }
67  
68 11 ▶sub jsonise (;$) { jsonize($_[0]) }
Subroutine jsonise: called CC=1 SCAR=0.0
Pod documented
69 11 ▶sub J (;$) { jsonize($_[0]) }
Subroutine J: called CC=1 SCAR=0.0
Pod documented
70 10 ▶sub yamlize (;$) { jsonize($_[0]) }
Subroutine yamlize: called CC=1 SCAR=0.0
Pod documented
71 10 ▶sub yamlise (;$) { jsonize($_[0]) }
Subroutine yamlise: called CC=1 SCAR=0.0
Pod documented
72 0 ▶sub Y (;$) { jsonize($_[0]) }
Subroutine Y: called CC=1 SCAR=6.9
Pod documented
73  
74  
75 sub parsej () {
76 10 ▶ $_last_out = $JOBJ->incr_parse($_);
Subroutine parsej: called CC=1 SCAR=0.0
Pod documented
77 }
78  
79 10 ▶sub pretty_json { jobj()->pretty; return; }
Subroutine pretty_json: called CC=1 SCAR=0.0
Pod documented
80 10 ▶sub ugly_json { jobj()->pretty(0); return; }
Subroutine ugly_json: called CC=1 SCAR=0.0
Pod documented
81  
82 sub looks_like_json {
83 6 ▶ my $ck = $_[0];
Subroutine looks_like_json: called CC=1 SCAR=0.0
Pod undocumented
84 6 return $ck =~ /^\s*[[{]/;
85 }
86  
87 sub looks_like_yaml {
88 2 ▶ my $ck = $_[0];
Subroutine looks_like_yaml: called CC=3 SCAR=12.0
Pod undocumented
89 4 my @l = $ck =~ /^(?:---|\s+-\s\w+|\s*\w+\s?:\s+\S+)$/gm;
90 4 ▶ return @l && ($l[0] eq '---' || @l > 2 );
Condition50%3/6
@l && ($l[0] eq '---' || @l > 2)
!ll&&!rl&&r
1306
$l[0] eq "---" or @l > 2
l!l&&r!l&&!r
600
MC/DC33%1/3
@l && ($l[0] eq '---' || @l > 2)
@l $l[0] eq "---" @l > 2
Truth table50%2/4
A && B or C
A= @lB= $l[0] eq "---"C= @l > 2
ABCresult
0XX0
1000
11X1
1011
91 }
92  
93  
94 =head1 NAME
95  
96 JSON::ize - Use JSON easily in one-liners - now with YAMLific action
97  
98 =head1 SYNOPSIS
99  
100 $ perl -MJSON::ize -le '$j=jsonize("my.json"); print $j->{thingy};'
101  
102 $ perl -MJSON::ize -le '$j=jsonize("my.yaml"); print $j->{thingy};'
103  
104 # or yamlize, if you prefer
105  
106 $ perl -MJOSN::ize -le '$j=yamlize("my.yaml"); print $j->{thingy};'
107  
108 # plus yamls all the way down...
109  
110 # if you have PerlIO::gzip, this works
111  
112 $ perl -MJSON::ize -le '$j=jsonize("my.json.gz"); print $j->{thingy};'
113  
114 $ perl -MJSON::ize -le 'J("my.json"); print J->{thingy};' # short
115  
116 $ perl -MJSON::ize -le 'print J("my.json")->{thingy};' # shorter
117  
118 $ cat my.json | perl -MJSON::ize -lne 'parsej; END{ print J->{thingy}}' # another way
119  
120 $ perl -MJSON::ize -le '$j="{\"this\":\"also\",\"works\":[1,2,3]}"; print jsonize($j)->{"this"};' # also
121  
122 $ perl -MJSON::ize -e 'pretty_json(); $j=jsonize("ugly.json"); print jsonize($j);' # pretty!
123  
124 $ perl -MJSON::ize -e 'ugly_json; print J(J("indented.json"));' # strip whsp
125  
126 # JSON to YAML
127  
128 $ perl -MJSON::ize -e 'print yamlize jsonize "my.json"'
129  
130 $ perl -MJSON::ize -e 'print Y J "my.json"'
131  
132 # and back
133  
134 $ perl -MJSON::ize -e 'print jsonize yamlize "my.yaml"'
135  
136 $ perl -MJSON::ize -e 'print J Y "my.yaml"'
137  
138 =head1 DESCRIPTION
139  
140 JSON::ize exports a function, C<jsonize()>, and some synonyms (see below), that will do what you mean with the argument.
141  
142 If argument is a filename, it will try to read the file and decode it from JSON or YAML.
143  
144 If argument is a string that looks like JSON or YAML, it will try to encode it:
145  
146 =over
147  
148 =item *
149  
150 If argument is a Perl hashref or arrayref, and you called C<jsonize()>, it will try to encode it as JSON.
151  
152 =item *
153  
154 If argument is a Perl hashref or arrayref, and you called C<yamlize()>, it will try to encode it as YAML.
155  
156 =back
157  
158 The underlying L<JSON> object is
159  
160 $JSON::ize::JOBJ
161  
162 =head1 METHODS
163  
164 =over
165  
166 =item jsonize($j), jsonise($j), J($j)
167  
168 Try to DWYM. In particular, encode to JSON.
169 If called without argument, return the last value returned. Use this to retrieve
170 after L</parsej>.
171  
172 =item yamlize($j), yamlise($j), Y($j)
173  
174 Try to DWYM. In particular, encode to YAML.
175 If called without argument, return the last value returned.
176  
177 =item parsej
178  
179 Parse a piped-in stream of json. Use jsonize() (without arg) to retrieve the object.
180 (Uses L<JSON/incr_parse>.)
181  
182 =item pretty_json()
183  
184 Output pretty (indented) json.
185  
186 =item ugly_json()
187  
188 Output json with no extra whitespace.
189  
190 =back
191  
192 =head1 SEE ALSO
193  
194 L<JSON>, L<JSON::XS>.
195  
196 =head1 AUTHOR
197  
198 Mark A. Jensen
199 CPAN: MAJENSEN
200 mark -dot- jensen -at- nih -dot- gov
201  
202 =head1 LICENSE
203  
204 Copyright (c) 2018, 2019 Mark A. Jensen.
205  
206 Licensed under the Apache License, Version 2.0 (the "License");
207 you may not use this file except in compliance with the License.
208 You may obtain a copy of the License at
209  
210 L<http://www.apache.org/licenses/LICENSE-2.0>
211  
212 Unless required by applicable law or agreed to in writing, software
213 distributed under the License is distributed on an "AS IS" BASIS,
214 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
215 See the License for the specific language governing permissions and
216 limitations under the License.
217  
218  
219 =cut
220  
221 1;
« blib/lib/JSON/ize.pm ↑ Summary Build »