lib/JSON/ize.pm

filter: Statementstmt 94.1% 32 / 34 Branchbran 66.6% 12 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 75.0% 6 / 8 totaltotal 86.1% 62 / 72 SCAR 23.4
CC
10
Cov
85%
jsonize
8.3
__ANON__
3.1
__ANON__
2.0
CRAP 10.4
« 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 parsej pretty_json ugly_json/;
14 our $VERSION = "0.200";
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=8 SCAR=21.2
Pod documented
20 2 ▶ if (!defined $inp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inp) 4 12 16
21 2 return $_last_out;
22 }
23 2 ▶ if (ref $inp) { # encode perl object
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inp) { } 0 12 12
24 2 return $_last_out = jobj()->encode($inp);
25 }
26 else { # scalar: decode if looks like json, or slurp if filename
27 2 ▶ if (looks_like_json($inp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_json($inp)) { } 4 8 12
28 2 return $_last_out = jobj()->decode($inp);
29 }
30 else { # try as file
31 2 local $/;
32 2 my ($j,$f);
33 2 ▶ die "'$inp' is not a existing filename, json string, or a reference" unless (-e $inp);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $inp 1 7 8
34 2 ▶ if ( eval "require PerlIO::gzip; 1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require PerlIO::gzip; 1') { } 7 0 7
35 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 7 7
36 }
37 else {
38 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
39 }
40 2 $j = <$f>;
41 try {
42 2 ▶ $_last_out = jobj()->decode($j);
Subroutine __ANON__: called CC=1 SCAR=0.0
43 } catch {
44 11 ▶ /at character offset/ && do { # JSON error
Subroutine __ANON__: called CC=3 SCAR=11.2
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 { die "Both JSON and YAML decodes barfed.\nJSON err: $jerr\nYAML err: $_"; } )) } 4 0 4
45 16 my $jerr = $_;
46 16 ▶ if (looks_like_json($j)) { # probably really was JSON
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_json($j)) 2 2 4
47 4 die "JSON decode barfed.\nJSON err: $jerr"
48 }
49 try { # might be YAML
50 0 ▶ $_last_out = Load($j);
Subroutine __ANON__: called CC=1 SCAR=6.9
51 } catch {
52 12 ▶ die "Both JSON and YAML decodes barfed.\nJSON err: $jerr\nYAML err: $_";
Subroutine __ANON__: called CC=1 SCAR=0.0
53 12 };
54 };
55 2 };
56 2 return $_last_out;
57 }
58 }
59 }
60  
61 4 ▶sub jsonise (;$) { jsonize($_[0]) }
Subroutine jsonise: called CC=1 SCAR=0.0
Pod documented
62 8 ▶sub J (;$) { jsonize($_[0]) }
Subroutine J: called CC=1 SCAR=0.0
Pod documented
63  
64  
65 sub parsej () {
66 8 ▶ $_last_out = $JOBJ->incr_parse($_);
Subroutine parsej: called CC=1 SCAR=0.0
Pod documented
67 }
68  
69 8 ▶sub pretty_json { jobj()->pretty; return; }
Subroutine pretty_json: called CC=1 SCAR=0.0
Pod documented
70 7 ▶sub ugly_json { jobj()->pretty(0); return; }
Subroutine ugly_json: called CC=1 SCAR=1.2
Pod documented
71  
72 sub looks_like_json {
73 7 ▶ my $ck = $_[0];
Subroutine looks_like_json: called CC=1 SCAR=0.0
Pod undocumented
74 7 return $ck =~ /^\s*[[{]/;
75 }
76  
77 =head1 NAME
78  
79 JSON::ize - Use JSON easily in one-liners - now with YAMLific action
80  
81 =head1 SYNOPSIS
82  
83 $ perl -MJSON::ize -le '$j=jsonize("my.json"); print $j->{thingy};'
84  
85 $ perl -MJSON::ize -le '$j=jsonize("my.yaml"); print $j->{thingy};'
86  
87 # plus yamls all the way down...
88  
89 # if you have PerlIO::gzip, this works
90  
91 $ perl -MJSON::ize -le '$j=jsonize("my.json.gz"); print $j->{thingy};'
92  
93 $ perl -MJSON::ize -le 'J("my.json"); print J->{thingy};' # short
94  
95 $ perl -MJSON::ize -le 'print J("my.json")->{thingy};' # shorter
96  
97 $ cat my.json | perl -MJSON::ize -lne 'parsej; END{ print J->{thingy}}' # another way
98  
99 $ perl -MJSON::ize -le '$j="{\"this\":\"also\",\"works\":[1,2,3]}"; print jsonize($j)->{"this"};' # also
100  
101 $ perl -MJSON::ize -e 'pretty_json(); $j=jsonize("ugly.json"); print jsonize($j);' # pretty!
102  
103 $ perl -MJSON::ize -e 'ugly_json; print J(J("indented.json"));' # strip whsp
104  
105  
106 =head1 DESCRIPTION
107  
108 JSON::ize exports a function, C<jsonize()>, that will do what you mean with the argument.
109 If argument is a filename, it will try to read the file and decode it as JSON or YAML.
110 If argument is a string that looks like JSON, it will try to encode it.
111 If argument is a Perl hashref or arrayref, it will try to encode it as JSON.
112  
113 The underlying L<JSON> object is
114  
115 $JSON::ize::JOBJ
116  
117 =head1 METHODS
118  
119 =over
120  
121 =item jsonize($j), jsonise($j), J($j)
122  
123 Try to DWYM.
124 If called without argument, return the last value returned. Use this to retrieve
125 after L</parsej>.
126  
127 =item parsej
128  
129 Parse a piped-in stream of json. Use jsonize() (without arg) to retrieve the object.
130 (Uses L<JSON/incr_parse>.)
131  
132 =item pretty_json()
133  
134 Output pretty (indented) json.
135  
136 =item ugly_json()
137  
138 Output json with no extra whitespace.
139  
140 =back
141  
142 =head1 SEE ALSO
143  
144 L<JSON>, L<JSON::XS>.
145  
146 =head1 AUTHOR
147  
148 Mark A. Jensen
149 CPAN: MAJENSEN
150 mark -dot- jensen -at- nih -dot- gov
151  
152 =head1 LICENSE
153  
154 Copyright (c) 2018, 2019 Mark A. Jensen.
155  
156 Licensed under the Apache License, Version 2.0 (the "License");
157 you may not use this file except in compliance with the License.
158 You may obtain a copy of the License at
159  
160 L<http://www.apache.org/licenses/LICENSE-2.0>
161  
162 Unless required by applicable law or agreed to in writing, software
163 distributed under the License is distributed on an "AS IS" BASIS,
164 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
165 See the License for the specific language governing permissions and
166 limitations under the License.
167  
168  
169 =cut
170  
171 1;
« blib/lib/JSON/ize.pm ↑ Summary Build »