| 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
| |||||||||||
| 21 | 2 | return $_last_out; | |||||||||
| 22 | } | ||||||||||
| 23 | 2 | ▶ | if (ref $inp) { # encode perl object | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 34 | 2 | ▶ | if ( eval "require PerlIO::gzip; 1" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | ▶ | open $f, "<:gzip(autopop)", $inp or die "Problem with file '$inp' : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | else { | ||||||||||
| 38 | 2 | ▶ | open $f, "$inp" or die "Problem with file '$inp' : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 45 | 16 | my $jerr = $_; | |||||||||
| 46 | 16 | ▶ | if (looks_like_json($j)) { # probably really was JSON | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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; | ||||||||||