| 1 | package JSON::JQ; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 7 | # internal flags | ||||||||||||||||||||||||||||
| 8 | 5 | our $DEBUG = 0; | |||||||||||||||||||||||||||
| 9 | 5 | our $DUMP_DISASM = 0; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use FindBin (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | FindBin::again(); | |||||||||||||||||||||||||||
| 13 | 5 | ▶ | use POSIX qw/isatty/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Path::Tiny qw/path/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use JSON qw/from_json/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # jv_print_flags in jv.h | ||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use enum qw/BITMASK:JV_PRINT_ PRETTY ASCII COLOR SORTED INVALID REFCOUNT TAB ISATTY SPACE0 SPACE1 SPACE2/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | # jq.h | ||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use enum qw/:JQ_DEBUG_=1 TRACE TRACE_DETAIL TRACE_ALL/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use XSLoader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | XSLoader::load('JSON::JQ', $VERSION); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||||||||||
| 26 | 50 | ▶ | my ( $pkg, $param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 50 | ▶ | croak "script or script_file parameter required" unless exists $param->{script} or exists $param->{script_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $param->{"script"} or exists $param->{"script_file"}
MC/DC0%0/2
exists $param->{"script"} or exists $param->{"script_file"}
exists $param->{"script"}
exists $param->{"script_file"}
Truth table33%1/3
A or B
A= exists $param->{"script"}B= exists $param->{"script_file"}
| |||||||||||||||||||||||||||||
| 29 | 50 | my $self = {}; | |||||||||||||||||||||||||||
| 30 | # script string or script file | ||||||||||||||||||||||||||||
| 31 | 50 | ▶ | $self->{script} = $param->{script} if exists $param->{script}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 50 | ▶ | $self->{script_file} = $param->{script_file} if exists $param->{script_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | # script initial arguments | ||||||||||||||||||||||||||||
| 34 | 50 | ▶ | $self->{variable} = exists $param->{variable} ? $param->{variable} : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | # internal attributes | ||||||||||||||||||||||||||||
| 36 | 50 | ▶ | $self->{_attribute}->{JQ_ORIGIN} = path($FindBin::Bin)->realpath->stringify if $FindBin::Bin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | $self->{_attribute}->{JQ_LIBRARY_PATH} = exists $param->{library_paths} ? $param->{library_paths} : | ||||||||||||||||||||||||||||
| 38 | 50 | ▶ | [ '~/.jq', '$ORIGIN/../lib/jq', '$ORIGIN/lib' ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 50 | ▶ | $self->{_attribute}->{PROGRAM_ORIGIN} = exists $param->{script_file} ? path($param->{script_file})->parent->stringify : '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | # error callback will push error messages into this array | ||||||||||||||||||||||||||||
| 41 | 50 | $self->{_errors} = []; | |||||||||||||||||||||||||||
| 42 | # debug callback print flags | ||||||||||||||||||||||||||||
| 43 | 50 | my $dump_opts = JV_PRINT_INDENT_FLAGS(2); | |||||||||||||||||||||||||||
| 44 | 50 | $dump_opts |= JV_PRINT_SORTED; | |||||||||||||||||||||||||||
| 45 | 50 | ▶ | $dump_opts |= JV_PRINT_COLOR | JV_PRINT_ISATTY if isatty(*STDERR); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 50 | $self->{_dumpopts} = $dump_opts; | |||||||||||||||||||||||||||
| 47 | # jq debug flags | ||||||||||||||||||||||||||||
| 48 | 50 | ▶ | $self->{jq_flags} = exists $param->{debug_flag} ? $param->{debug_flag} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 50 | bless $self, $pkg; | |||||||||||||||||||||||||||
| 50 | 50 | ▶ | unless ($self->_init()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | croak "jq_compile_args() failed with errors:\n ". join("\n ", @{ $self->{_errors} }); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 49 | return $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub process { | ||||||||||||||||||||||||||||
| 57 | 56 | ▶ | my ( $self, $param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=10 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 56 | my $input; | |||||||||||||||||||||||||||
| 60 | 56 | ▶ | if (exists $param->{data}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 61 | 55 | $input = $param->{data}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | elsif (exists $param->{json}) { | ||||||||||||||||||||||||||||
| 64 | 1 | $input = from_json($param->{json}, { utf8 => 1 }); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | elsif (exists $param->{json_file}) { | ||||||||||||||||||||||||||||
| 67 | 0 | my $file = path($param->{json_file}); | |||||||||||||||||||||||||||
| 68 | 0 | $input = from_json($file->slurp, { utf8 => 1 }); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 0 | croak "JSON::JQ::process(): required parameter not found, check method documentation"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 56 | my $output = []; | |||||||||||||||||||||||||||
| 74 | 56 | my $rc = $self->_process($input, $output); | |||||||||||||||||||||||||||
| 75 | # treat it as option EXIT_STATUS is on | ||||||||||||||||||||||||||||
| 76 | 56 | ▶ | $rc -= 10 if $rc >= 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 56 | ▶ | if ($rc == 1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 78 | # NOTE: treat this case as successful run | ||||||||||||||||||||||||||||
| 79 | 4 | warn "JSON::JQ::process(): returned null/false (undef output), perhaps the input is undef.\n"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | elsif ($rc == 4 and @{ $self->{_errors} } == 0) { | ||||||||||||||||||||||||||
|
Condition66%2/3
$rc == 4 and @{$self->{"_errors"};} == 0
MC/DC50%1/2
$rc == 4 and @{$self->{"_errors"};} == 0
$rc == 4
@{$self->{"_errors"};} == 0
Truth table66%2/3
A and B
A= $rc == 4B= @{$self->{"_errors"};} == 0
| |||||||||||||||||||||||||||||
| 82 | # treat it as succeeded | ||||||||||||||||||||||||||||
| 83 | 2 | push @$output, undef; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | elsif ($rc != 0) { | ||||||||||||||||||||||||||||
| 86 | 0 | croak "JSON::JQ::process(): failed with return code = $rc and errors:\n ". join("\n ", @{ $self->{_errors} }); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 56 | ▶ | return wantarray ? @$output : $output; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | JSON::JQ - jq (https://stedolan.github.io/jq/) library binding | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | use JSON::JQ; | ||||||||||||||||||||||||||||
| 98 | my $jq = JSON::JQ->new({ script => '.' }); | ||||||||||||||||||||||||||||
| 99 | # 1. process perl data | ||||||||||||||||||||||||||||
| 100 | my $results = $jq->process({ data => { foo => 'bar' }}); | ||||||||||||||||||||||||||||
| 101 | # 2. process json string | ||||||||||||||||||||||||||||
| 102 | my $results = $jq->process({ json => '{ "foo": "bar" }'}); | ||||||||||||||||||||||||||||
| 103 | # 3. process json file | ||||||||||||||||||||||||||||
| 104 | my $results = $jq->process({ json_file => 'foo.json' }); | ||||||||||||||||||||||||||||
| 105 | # check items in @$results | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | This is a L<jq|https://stedolan.github.io/jq/> library binding, making it possible to process | ||||||||||||||||||||||||||||
| 111 | data using jq script/filter/module. Check the jq homepage for a detailed explanation and documentation. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 METHODS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 new({ parameter => value, ... }) | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Construct a jq engine instance and return it, jq script must be provided by either I<script> or | ||||||||||||||||||||||||||||
| 119 | I<script_file> parameter. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =over 4 | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item * script | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | A string of jq script. The simplest one is '.', which does data 'echo'. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item * script_file | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | A path to the file which contains the jq script. Shell-bang on first line will be ignored safely. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item * variable | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | A hash reference with pre-defined variables and their values, they can be used by the jq script | ||||||||||||||||||||||||||||
| 134 | later. Complex data structures like nested arrays and/or hashes are acceptable. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Check the jq official documentation on how to reference variables inside script. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item * library_paths | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | An array reference with one or more directory paths inside. They will be used to search jq | ||||||||||||||||||||||||||||
| 141 | library/module when needed. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | The default search paths are I<'~/.jq'> I<'$ORIGIN/../lib/jq'> I<'$ORIGIN/lib'>, which | ||||||||||||||||||||||||||||
| 144 | confirm with jq executable. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Check the jq official documentation on how to use this functionality in script. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 process({ parameter => value, ... }) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Process given input and return results as array reference. The input data must be provided | ||||||||||||||||||||||||||||
| 153 | via one of the parameters below. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =over 4 | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =item * data | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | A Perl variable representing JSON formed data. The straight way to understand its equivalent | ||||||||||||||||||||||||||||
| 160 | is the return of C<JSON::from_json> call. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Any other type of data which the jq engine will accept can do. Such as I<undef>, which says Null | ||||||||||||||||||||||||||||
| 163 | input. It is useful when the output data is created solely by a script itself. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Bear in mind that the jq engine cannot understand (blessed) perl objects, with one exception - objects | ||||||||||||||||||||||||||||
| 166 | returned via C<JSON::true()> or C<JSON::false()>. They will be handled by underlying XS code | ||||||||||||||||||||||||||||
| 167 | properly before passing them to the jq engine. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Check I<SPECIAL DATA MAPPING> section below. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item * json | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | A json encoded string. It will be decoded using C<JSON::from_json> before handling to jq engine. | ||||||||||||||||||||||||||||
| 174 | Which also means, it must fully conform with the JSON specification. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item * json_file | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Similar to I<json> parameter above, instead read the JSON string from given file. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =back | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SPECIAL DATA MAPPING | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | The following JSON values are mapped to corresponding Perl values: | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =over 4 | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item * true: C<JSON::true> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item * false: C<JSON::false> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item * null: C<undef> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =back | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 CALLBACKS | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | The following jq engine callbacks are implemented: | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =over 4 | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item error callback | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Any error message raised by jq engine during its initialization and execution will be | ||||||||||||||||||||||||||||
| 205 | pushed into perl instance's private I<_error> attribute. It is transparent to user, | ||||||||||||||||||||||||||||
| 206 | each method will croak on critical errors and show them. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =item debug callback | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This is a builtin debug feature of the engine. It prints out debug messages when triggered. | ||||||||||||||||||||||||||||
| 211 | Check the jq official documentation for more details. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =back | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 DEBUG | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Limited debug functionality is implemented via the following module variables: | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =over 4 | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =item C<$JSON::JQ::DUMP_DISASM> [default: off] | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | When on, print out the jq script disassembly code using C<jq_dump_disassembly>. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =item C<$JSON::JQ::DEBUG> [default: off] | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | B<Internal use only>. When on, print out debug messages from XS code. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =back | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 BUGS | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Please report bugs to L<https://github.com/dxma/perl5-json-jq/issues>. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Dongxu Ma | ||||||||||||||||||||||||||||
| 238 | CPAN ID: DONGXU | ||||||||||||||||||||||||||||
| 239 | dongxu _dot_ ma _at_ gmail.com | ||||||||||||||||||||||||||||
| 240 | https://github.com/dxma | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | This program is free software licensed under the... | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | The MIT License | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 249 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =over | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =item L<jq official wiki|https://github.com/stedolan/jq/wiki> | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =back | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =cut | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 5 | 1; | |||||||||||||||||||||||||||