lib/Web/DataService/Plugin/JSON.pm

filter: Statementstmt 13.6% 21 / 154 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 95 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 37.5% 6 / 16 Podpod 0.0% 0 / 10 totaltotal 6.5% 27 / 414 CC 81 SCAR 64.1
CC
81
Cov
6%
emit_object
1332.0
emit_header
380.0
emit_array
156.0
CRAP 610.0
« blib/lib/Web/DataService.pm ↑ Summary lib/Web/DataService/Vocabulary.pm »
1 #
2 # Web::DataService::JSON
3 #
4 # This module is responsible for putting data responses into JSON format.
5 #
6 # Author: Michael McClennen
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Web::DataService::Plugin::JSON;
11  
12 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our @EXPORT_OK = qw(json_list_value json_clean);
20  
21  
22 # emit_header ( request, field_list )
23 #
24 # Return the initial text of a JSON result.
25  
26 sub emit_header {
27  
28 0 ▶ my ($class, $request, $field_list) = @_;
Subroutine emit_header: not called CC=19 SCAR=59.4
Pod undocumented
29
30 0 my $output = '{' . "\n";
31
32 # Check if we have been asked to report the data source and parameters.
33
34 0 ▶ if ( $request->display_datainfo )
Branch: 1 branch
BranchTrueFalseTotal
if ($request->display_datainfo) 0 0 0
35 {
36 0 my $info = $request->data_info;
37
38 0 foreach my $key ( $request->data_info_keys )
39 {
40 0 ▶ next unless $info->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{$key} 0 0 0
41 0 my $value = json_clean($info->{$key});
42
43 0 $output .= qq{"$key":$value,\n};
44 }
45
46 #$output .= '"data_provider":' . json_clean($info->{data_provider}) . ",\n";
47 #$output .= '"data_source":' . json_clean($info->{data_source}) . ",\n";
48 #$output .= '"data_source_url":' . json_clean($base . '/') . ",\n";
49 #$output .= '"data_license":' . json_clean($info->{license}) . ",\n";
50 #$output .= '"data_license_url":' . json_clean($info->{license_url}) . ",\n";
51 #$output .= '"documentation_url":' . json_clean($doc_url) . ",\n";
52 #$output .= '"data_url":' . json_clean($data_url) . ",\n";
53 #$output .= '"access_time":' . json_clean($info->{access_time}) . ",\n";
54 0 $output .= '"parameters":{' . "\n";
55
56 0 my @display = $request->params_for_display;
57 0 my $sep = '';
58
59 0 ▶ while ( @display )
Branch: 1 branch
BranchTrueFalseTotal
if (@display) 0 0 0
60 {
61 0 my $param = shift @display;
62 0 my $value = shift @display;
63
64 0 ▶ next unless defined $param && $param ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param ne "" 0 0 0
Condition0%0/3
defined $param and $param ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $param and $param ne ""
defined $param $param ne ""
Truth table0%0/3
A and B
A= defined $paramB= $param ne ""
ABresult
0X0
100
111
65 0 ▶ $value //= '';
Condition0%0/2
$value //= ""
l!l
00
MC/DC0%0/1
$value //= ""
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
66
67 0 $output .= $sep; $sep = ",\n";
68
69 0 ▶ if ( ref $value eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
70 {
71 0 $output .= json_list_value($param, @$value);
72 }
73
74 else
75 {
76 0 $output .= qq<"$param":"$value">;
77 }
78 }
79
80 0 $output .= "\n},\n";
81 }
82
83 # Check if we have been asked to report the result count, and if it is
84 # available.
85
86 0 $output .= '"elapsed_time":' . sprintf("%.3g", $request->{elapsed}) . ",\n";
87
88 0 ▶ if ( $request->display_counts )
Branch: 1 branch
BranchTrueFalseTotal
if ($request->display_counts) 0 0 0
89 {
90 0 my $counts = $request->result_counts;
91
92 0 ▶ $output .= '"records_found":' . json_clean($counts->{found} || '0') . ",\n";
Condition0%0/2
$counts->{'found'} || '0'
l!l
00
MC/DC0%0/1
$counts->{'found'} || '0'
$counts->{'found'}
Truth table0%0/2
A
A= $counts->{'found'}
Aresult
11
00
93 0 ▶ $output .= '"records_returned":' . json_clean($counts->{returned} || '0') . ",\n";
Condition0%0/2
$counts->{'returned'} || '0'
l!l
00
MC/DC0%0/1
$counts->{'returned'} || '0'
$counts->{'returned'}
Truth table0%0/2
A
A= $counts->{'returned'}
Aresult
11
00
94 $output .= '"record_offset":' . json_clean($counts->{offset}) . ",\n"
95 0 ▶ if defined $counts->{offset} && $counts->{offset} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $counts->{"offset"} and $counts->{"offset"} > 0 0 0 0
Condition0%0/3
defined $counts->{"offset"} and $counts->{"offset"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $counts->{"offset"} and $counts->{"offset"} > 0
defined $counts->{"offset"} $counts->{"offset"} > 0
Truth table0%0/3
A and B
A= defined $counts->{"offset"}B= $counts->{"offset"} > 0
ABresult
0X0
100
111
96 }
97
98 # Check if we have any warning messages to convey
99
100 0 ▶ if ( my @msgs = $request->warnings )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@msgs) = $request->warnings) 0 0 0
101 {
102 0 $output .= qq<"warnings":[\n>;
103 0 my $sep = '';
104 0 foreach my $m (@msgs)
105 {
106 0 $output .= $sep; $sep = ",\n";
107 0 $output .= json_clean($m);
108 }
109 0 $output .= qq<\n],\n>;
110 }
111
112 # Check if we have summary data to output.
113
114 0 ▶ if ( $request->{summary_data} && $request->{summary_field_list} )
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"summary_data"} and $request->{"summary_field_list"}) 0 0 0
Condition0%0/3
$request->{"summary_data"} and $request->{"summary_field_list"}
!ll&&!rl&&r
000
MC/DC0%0/2
$request->{"summary_data"} and $request->{"summary_field_list"}
$request->{"summary_data"} $request->{"summary_field_list"}
Truth table0%0/3
A and B
A= $request->{"summary_data"}B= $request->{"summary_field_list"}
ABresult
0X0
100
111
115 {
116 0 $output .= qq<"summary": >;
117 0 ▶ $output .= $class->emit_object($request, $request->{summary_data}, $request->{summary_field_list}) || '""';
Condition0%0/2
$class->emit_object($request, $request->{'summary_data'}, $request->{'summary_field_list'}) || '""'
l!l
00
MC/DC0%0/1
$class->emit_object($request, $request->{'summary_data'}, $request->{'summary_field_list'}) || '""'
$class->emit_object($request, $request->{'summary_data'}, $request->{'summary_field_list'})
Truth table0%0/2
A
A= $class->emit_object($request, $request->{'summary_data'}, $request->{'summary_field_list'})
Aresult
11
00
118 0 $output .= ",\n";
119 }
120
121 # The actual data will go into an array, in a field called "records".
122
123 0 $output .= qq<"records": [\n>;
124 0 return $output;
125 }
126  
127  
128 # emit_separator ( )
129 #
130 # Return the record separator string. This will be output between each
131 # record, but not before the first one.
132  
133 sub emit_separator {
134
135 0 ▶ return ",\n";
Subroutine emit_separator: not called CC=1 SCAR=6.9
Pod undocumented
136 }
137  
138  
139 # emit_empty ( )
140 #
141 # Return the string (if any) to output in lieu of an empty result set.
142  
143 sub emit_empty {
144
145 0 ▶ my ($class, $request) = @_;
Subroutine emit_empty: not called CC=1 SCAR=6.9
Pod undocumented
146
147 0 return '';
148 }
149  
150  
151 # emit_footer ( )
152 #
153 # Return the final text for a JSON result.
154  
155 sub emit_footer {
156
157 0 ▶ my ($class, $request) = @_;
Subroutine emit_footer: not called CC=1 SCAR=6.9
Pod undocumented
158
159 0 return qq<\n]\n}\n>;
160 }
161  
162  
163 # emit_error ( code, errors, warnings )
164 #
165 # Return the formatted output for an error message body in JSON.
166  
167 sub emit_error {
168
169 0 ▶ my ($class, $code, $errors, $warnings, $cautions) = @_;
Subroutine emit_error: not called CC=10 SCAR=47.0
Pod undocumented
170
171 0 ▶ unless ( ref $errors eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $errors eq "ARRAY") 0 0 0
172 {
173 0 $errors = [ "bad call to 'emit_error'" ];
174 }
175
176 0 ▶ if ( defined $warnings && ! ref $warnings eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $warnings and !ref($warnings) eq "ARRAY") 0 0 0
Condition0%0/3
defined $warnings and !ref($warnings) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $warnings and !ref($warnings) eq "ARRAY"
defined $warnings !ref($warnings) eq "ARRAY"
Truth table0%0/3
A and not(B)
A= defined $warningsB= !ref($warnings) eq "ARRAY"
ABresult
0X0
100
111
177 {
178 0 $warnings = [ "bad call to 'emit_error'" ];
179 }
180
181 0 my $error = '"status_code": ' . $code;
182 0 ▶ $error .= ",\n" . json_list_value("errors", @$errors) if ref $errors eq 'ARRAY' && @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if ref $errors eq "ARRAY" and @$errors 0 0 0
Condition0%0/3
ref $errors eq "ARRAY" and @$errors
!ll&&!rl&&r
000
MC/DC0%0/2
ref $errors eq "ARRAY" and @$errors
ref $errors eq "ARRAY" @$errors
Truth table0%0/3
A and B
A= ref $errors eq "ARRAY"B= @$errors
ABresult
0X0
100
111
183 0 ▶ $error .= ",\n" . json_list_value("cautions", @$cautions) if ref $cautions eq 'ARRAY' && @$cautions;
Branch: 1 branch
BranchTrueFalseTotal
if ref $cautions eq "ARRAY" and @$cautions 0 0 0
Condition0%0/3
ref $cautions eq "ARRAY" and @$cautions
!ll&&!rl&&r
000
MC/DC0%0/2
ref $cautions eq "ARRAY" and @$cautions
ref $cautions eq "ARRAY" @$cautions
Truth table0%0/3
A and B
A= ref $cautions eq "ARRAY"B= @$cautions
ABresult
0X0
100
111
184 0 ▶ $error .= ",\n" . json_list_value("warnings", @$warnings) if ref $warnings eq 'ARRAY' && @$warnings;
Branch: 1 branch
BranchTrueFalseTotal
if ref $warnings eq "ARRAY" and @$warnings 0 0 0
Condition0%0/3
ref $warnings eq "ARRAY" and @$warnings
!ll&&!rl&&r
000
MC/DC0%0/2
ref $warnings eq "ARRAY" and @$warnings
ref $warnings eq "ARRAY" @$warnings
Truth table0%0/3
A and B
A= ref $warnings eq "ARRAY"B= @$warnings
ABresult
0X0
100
111
185
186 0 return "{ $error }\n";
187 }
188  
189  
190 # emit_record ( request, record, field_list )
191 #
192 # Return the formatted output for a single record in JSON according to the
193 # specified field list.
194  
195 sub emit_record {
196
197 0 ▶ my ($class, $request, $record, $field_list) = @_;
Subroutine emit_record: not called CC=1 SCAR=6.9
Pod undocumented
198
199 0 return $class->emit_object($request, $record, $field_list);
200 }
201  
202  
203 # emit_object ( request, record, field_list )
204 #
205 # Generate text that expresses the given record in JSON according to the given
206 # list of output field specifications.
207  
208 sub emit_object {
209  
210 0 ▶ my ($class, $request, $record, $field_list) = @_;
Subroutine emit_object: not called CC=36 SCAR=71.9
Pod undocumented
211
212 # Start with an empty string.
213
214 0 my $outrec = '{';
215 0 my $sep = '';
216
217 # Go through the rule list, generating the fields one by one. $field_list
218 # may be either an array of rule records or a single one.
219
220 0 ▶ foreach my $f (reftype $field_list && reftype $field_list eq 'ARRAY' ? @$field_list : $field_list)
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($field_list) && builtin::reftype($field_list) eq 'ARRAY' ? : 0 0 0
Condition0%0/3
builtin::reftype($field_list) and builtin::reftype($field_list) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($field_list) and builtin::reftype($field_list) eq "ARRAY"
builtin::reftype($field_list) builtin::reftype($field_list) eq "ARRAY"
Truth table0%0/3
A and B
A= builtin::reftype($field_list)B= builtin::reftype($field_list) eq "ARRAY"
ABresult
0X0
100
111
221 {
222 # Skip any field that is empty, unless 'always' or 'value' is set.
223
224 0 my $field = $f->{field};
225 0 my $data_type = $f->{data_type};
226
227 next unless $f->{always} or defined $f->{value} or
228 0 ▶ defined $record->{$field} and $record->{$field} ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $f->{"always"} or defined $f->{"value"} or defined $record->{$field} and $record->{$field} ne "" 0 0 0
Condition0%0/9
$f->{"always"} or defined $f->{"value"} or defined $record->{$field} and $record->{$field} ne ""
l!l&&r!l&&!r
000
$f->{"always"} or defined $f->{"value"}
l!l&&r!l&&!r
000
defined $record->{$field} and $record->{$field} ne ""
!ll&&!rl&&r
000
MC/DC0%0/4
$f->{"always"} or defined $f->{"value"} or defined $record->{$field} and $record->{$field} ne ""
$f->{"always"} defined $f->{"value"} defined $record->{$field} $record->{$field} ne ""
Truth table0%0/5
A or B or C and D
A= $f->{"always"}B= defined $f->{"value"}C= defined $record->{$field}D= $record->{$field} ne ""
ABCDresult
1XXX1
01XX1
00111
000X0
00100
229
230 # Skip any field with a 'dedup' attribute if its value is the same as
231 # the value of the field indicated by the attribute.
232
233 next if $f->{dedup} and defined $record->{$field} and defined $record->{$f->{dedup}}
234 0 ▶ and $record->{$field} eq $record->{$f->{dedup}};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"dedup"} and defined $record->{$field} and defined $$record{$f->{"dedup"}} and $record->{$field} eq $$record{$f->{"dedup"}} 0 0 0
Condition0%0/9
$f->{"dedup"} and defined $record->{$field} and defined $$record{$f->{"dedup"}} and $record->{$field} eq $$record{$f->{"dedup"}}
!ll&&!rl&&r
000
$f->{"dedup"} and defined $record->{$field} and defined $$record{$f->{"dedup"}}
!ll&&!rl&&r
000
$f->{"dedup"} and defined $record->{$field}
!ll&&!rl&&r
000
MC/DC0%0/4
$f->{"dedup"} and defined $record->{$field} and defined $$record{$f->{"dedup"}} and $record->{$field} eq $$record{$f->{"dedup"}}
$f->{"dedup"} defined $record->{$field} defined $$record{$f->{"dedup"}} $record->{$field} eq $$record{$f->{"dedup"}}
Truth table0%0/5
A and B and C and D
A= $f->{"dedup"}B= defined $record->{$field}C= defined $$record{$f->{"dedup"}}D= $record->{$field} eq $$record{$f->{"dedup"}}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
235
236 # Skip any field with a 'if_field' attribute if the corresponding
237 # field does not have a true value.
238
239 0 ▶ next if $f->{if_field} and not $record->{$f->{if_field}};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"if_field"} and not $$record{$f->{"if_field"}} 0 0 0
Condition0%0/3
$f->{"if_field"} and not $$record{$f->{"if_field"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$f->{"if_field"} and not $$record{$f->{"if_field"}}
$f->{"if_field"} not $$record{$f->{"if_field"}}
Truth table0%0/3
A and not(B)
A= $f->{"if_field"}B= not $$record{$f->{"if_field"}}
ABresult
0X0
100
111
240
241 # Skip any field with a 'not_field' attribute if the corresponding
242 # field has a true value.
243
244 0 ▶ next if $f->{not_field} and $record->{$f->{not_field}};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"not_field"} and $$record{$f->{"not_field"}} 0 0 0
Condition0%0/3
$f->{"not_field"} and $$record{$f->{"not_field"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$f->{"not_field"} and $$record{$f->{"not_field"}}
$f->{"not_field"} $$record{$f->{"not_field"}}
Truth table0%0/3
A and B
A= $f->{"not_field"}B= $$record{$f->{"not_field"}}
ABresult
0X0
100
111
245
246 # Start with the initial value for this field. If it contains a
247 # 'value' attribute, use that. Otherwise, use the indicated field
248 # value from the current record. If that is not defined, use the
249 # empty string.
250
251 my $value = defined $f->{value} ? $f->{value}
252 0 ▶ : defined $record->{$field} ? $record->{$field}
Branch: 2 branches
BranchTrueFalseTotal
defined $f->{'value'} ? : 0 0 0
defined $record->{$field} ? : 0 0 0
253 : '';
254
255 # If the field has a 'sub_record' attribute and the value is a hashref then
256 # generate output to represent a sub-object by applying the named
257 # output section to the value. If the value is a scalar then this
258 # field is silently ignored.
259
260 0 ▶ if ( defined $f->{sub_record} )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $f->{'sub_record'}) { } 0 0 0
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif ($f->{'show_as_list'}) { } 0 0 0
261 {
262 0 my $ds = $request->ds;
263 0 $ds->configure_block($request, $f->{sub_record});
264
265 0 my $output_list = $request->{block_field_list}{$f->{sub_record}};
266 0 my $proc_list = $request->{block_proc_list}{$f->{sub_record}};
267
268 0 ▶ if ( ref $value && reftype $value eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value and builtin::reftype($value) eq 'HASH') { } 0 0 0
elsif (ref $value and builtin::reftype($value) eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $value and builtin::reftype($value) eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and builtin::reftype($value) eq "HASH"
ref $value builtin::reftype($value) eq "HASH"
Truth table0%0/3
A and B
A= ref $valueB= builtin::reftype($value) eq "HASH"
ABresult
0X0
100
111
269 {
270 0 ▶ $request->_process_record($value, $proc_list) if $proc_list && @$proc_list;
Branch: 1 branch
BranchTrueFalseTotal
if $proc_list and @$proc_list 0 0 0
Condition0%0/3
$proc_list and @$proc_list
!ll&&!rl&&r
000
MC/DC0%0/2
$proc_list and @$proc_list
$proc_list @$proc_list
Truth table0%0/3
A and B
A= $proc_listB= @$proc_list
ABresult
0X0
100
111
271
272 0 ▶ if ( $output_list && @$output_list )
Branch: 1 branch
BranchTrueFalseTotal
if ($output_list and @$output_list) { } 0 0 0
Condition0%0/3
$output_list and @$output_list
!ll&&!rl&&r
000
MC/DC0%0/2
$output_list and @$output_list
$output_list @$output_list
Truth table0%0/3
A and B
A= $output_listB= @$output_list
ABresult
0X0
100
111
273 {
274 0 $value = $class->emit_object($request, $value, $output_list);
275 }
276 else
277 {
278 0 $value = json_clean($value, $data_type);
279 }
280 }
281
282 # If instead the value is an arrayref then apply the rule to each item
283 # in the list.
284
285 ▶ elsif ( ref $value && reftype $value eq 'ARRAY' )
Condition0%0/3
ref $value and builtin::reftype($value) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and builtin::reftype($value) eq "ARRAY"
ref $value builtin::reftype($value) eq "ARRAY"
Truth table0%0/3
A and B
A= ref $valueB= builtin::reftype($value) eq "ARRAY"
ABresult
0X0
100
111
286 {
287 0 ▶ if ( $proc_list && @$proc_list )
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_list and @$proc_list) 0 0 0
Condition0%0/3
$proc_list and @$proc_list
!ll&&!rl&&r
000
MC/DC0%0/2
$proc_list and @$proc_list
$proc_list @$proc_list
Truth table0%0/3
A and B
A= $proc_listB= @$proc_list
ABresult
0X0
100
111
288 {
289 0 foreach my $v ( @$value )
290 {
291 0 ▶ $request->_process_record($v, $proc_list) if $proc_list;
Branch: 1 branch
BranchTrueFalseTotal
if $proc_list 0 0 0
292 }
293 }
294
295 0 ▶ if ( $output_list && @$output_list )
Branch: 1 branch
BranchTrueFalseTotal
if ($output_list and @$output_list) { } 0 0 0
Condition0%0/3
$output_list and @$output_list
!ll&&!rl&&r
000
MC/DC0%0/2
$output_list and @$output_list
$output_list @$output_list
Truth table0%0/3
A and B
A= $output_listB= @$output_list
ABresult
0X0
100
111
296 {
297 0 $value = $class->emit_array($request, $value, $output_list);
298 }
299 else
300 {
301 0 $value = json_clean($value, $data_type);
302 }
303 }
304
305 else
306 {
307 0 $value = json_clean($value, $data_type);
308 }
309 }
310
311 # Otherwise, if the value is an arrayref then we generate output for
312 # an array. If the field is marked "show_as_list", then do this even
313 # if there is only one value.
314
315 elsif ( ref $value eq 'ARRAY' )
316 {
317 0 $value = $class->emit_array($request, $value);
318 }
319
320 elsif ( $f->{show_as_list} )
321 {
322 0 $value = $class->emit_array($request, [ $value ]);
323 }
324
325 # Otherwise just use the value.
326
327 else
328 {
329 0 $value = json_clean($value, $data_type);
330 }
331
332 # Now, add the value to the growing output. Add a comma before each
333 # record except the first.
334
335 0 my $outkey = $f->{name};
336
337 0 $outrec .= qq<$sep"$outkey":$value>;
338 0 $sep = q<,>;
339 }
340
341 # If this record has hierarchical children, process them now. (Do we
342 # still need this?)
343
344 0 ▶ if ( exists $record->{hier_child} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $record->{"hier_child"}) 0 0 0
345 {
346 0 my $children = $class->emit_array($record->{hier_child}, $field_list);
347 0 $outrec .= qq<,"children":$children>;
348 }
349
350 # Now finish the output string and return it.
351
352 0 $outrec .= '}';
353
354 0 return $outrec;
355 }
356  
357  
358 # emit_array ( request, arrayref, field_list )
359 #
360 # Generate text that expresses the given array of values in JSON according to
361 # the given list of field specifications.
362  
363 sub emit_array {
364  
365 0 ▶ my ($class, $request, $arrayref, $field_list) = @_;
Subroutine emit_array: not called CC=12 SCAR=50.5
Pod undocumented
366
367 0 ▶ my $f = $field_list if reftype $field_list && reftype $field_list ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($field_list) and builtin::reftype($field_list) ne "ARRAY" 0 0 0
Condition0%0/3
builtin::reftype($field_list) and builtin::reftype($field_list) ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($field_list) and builtin::reftype($field_list) ne "ARRAY"
builtin::reftype($field_list) builtin::reftype($field_list) ne "ARRAY"
Truth table0%0/3
A and B
A= builtin::reftype($field_list)B= builtin::reftype($field_list) ne "ARRAY"
ABresult
0X0
100
111
368
369 # Start with an empty string.
370
371 0 my $outrec = '[';
372 0 my $sep = '';
373
374 # Go through the elements of the specified arrayref, applying the
375 # specified rule to each one.
376
377 0 my $value = '';
378
379 0 foreach my $elt ( @$arrayref )
380 {
381 0 ▶ if ( reftype $elt && reftype $elt eq 'ARRAY' )
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::reftype($elt) and builtin::reftype($elt) eq 'ARRAY') { } 0 0 0
elsif (builtin::reftype($elt) and builtin::reftype($elt) eq 'HASH') { } 0 0 0
elsif (ref $elt) { } 0 0 0
Condition0%0/3
builtin::reftype($elt) and builtin::reftype($elt) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($elt) and builtin::reftype($elt) eq "ARRAY"
builtin::reftype($elt) builtin::reftype($elt) eq "ARRAY"
Truth table0%0/3
A and B
A= builtin::reftype($elt)B= builtin::reftype($elt) eq "ARRAY"
ABresult
0X0
100
111
382 {
383 0 $value = $class->emit_array($request, $elt, $field_list);
384 }
385
386 ▶ elsif ( reftype $elt && reftype $elt eq 'HASH' )
Condition0%0/3
builtin::reftype($elt) and builtin::reftype($elt) eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($elt) and builtin::reftype($elt) eq "HASH"
builtin::reftype($elt) builtin::reftype($elt) eq "HASH"
Truth table0%0/3
A and B
A= builtin::reftype($elt)B= builtin::reftype($elt) eq "HASH"
ABresult
0X0
100
111
387 {
388 0 ▶ next unless $field_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_list 0 0 0
389 0 $value = $class->emit_object($request, $elt, $field_list);
390 }
391
392 elsif ( ref $elt )
393 {
394 0 next;
395 }
396
397 else
398 {
399 0 $value = json_clean($elt);
400 }
401
402 0 ▶ if ( defined $value and $value ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "") 0 0 0
Condition0%0/3
defined $value and $value ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table0%0/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
403 {
404 0 $outrec .= "$sep$value";
405 0 $sep = ',';
406 }
407 }
408
409 0 $outrec .= ']';
410
411 0 return $outrec;
412 }
413  
414  
415 # json_list_value ( key, @values )
416 #
417 # Return a string representing a JSON key with a list of values. This is used
418 # for generating error and warning keys.
419  
420 sub json_list_value {
421
422 0 ▶ my ($key, @values) = @_;
Subroutine json_list_value: not called CC=2 SCAR=17.9
Pod undocumented
423
424 0 my $output = qq<"$key": [>;
425 0 my $sep = '';
426
427 0 foreach my $m (@values)
428 {
429 0 $output .= $sep; $sep = ', ';
430 0 $output .= json_clean($m);
431 }
432
433 0 $output .= qq<]>;
434 }
435  
436  
437 # json_clean ( string )
438 #
439 # Given a string value, return an equivalent string value that will be valid
440 # as part of a JSON result.
441  
442 1 my (%ESCAPE) = ( '\\' => '\\\\', '"' => '\\"', "\t" => '\\t', "\n" => '\\n',
443 "\r" => '\\r' ); #'
444  
445 sub json_clean {
446
447 0 ▶ my ($string, $data_type) = @_;
Subroutine json_clean: not called CC=7 SCAR=40.3
Pod undocumented
448
449 # Return an empty string unless the value is defined.
450
451 0 ▶ return '""' unless defined $string and $string ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and $string ne "" 0 0 0
Condition0%0/3
defined $string and $string ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $string and $string ne ""
defined $string $string ne ""
Truth table0%0/3
A and B
A= defined $stringB= $string ne ""
ABresult
0X0
100
111
452
453 # Do a quick check for numbers. If it matches, return the value as-is
454 # unless the data_type is 'str'. In that case, the field value is
455 # intended to be a string so we should quote it even if it looks like a number.
456
457 0 ▶ return $string if $string =~ qr{ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ }x
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ qr/ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ /x and not defined $data_type && $data_type eq "str" 0 0 0
Condition0%0/6
$string =~ qr/ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ /x and not defined $data_type && $data_type eq "str"
!ll&&!rl&&r
000
defined $data_type && $data_type eq 'str'
!ll&&!rl&&r
000
MC/DC0%0/3
$string =~ qr/ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ /x and not defined $data_type && $data_type eq "str"
$string =~ qr/ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ /x defined $data_type $data_type eq 'str'
Truth table0%0/4
A and not(B && C)
A= $string =~ qr/ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ /xB= defined $data_typeC= $data_type eq 'str'
ABCresult
0XX0
1110
10X1
1101
458 and not (defined $data_type && $data_type eq 'str');
459
460 # Do another quick check for okay characters. If there's nothing exotic,
461 # just return the quoted value.
462
463 0 ▶ return '"' . $string . '"' unless $string =~ /[^a-zA-Z0-9 _.,;:<>-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /[^a-zA-Z0-9 _.,;:<>-]/ 0 0 0
464
465 # Otherwise, we need to do some longer processing.
466
467 # Turn any numeric character references into actual Unicode characters.
468 # The database does contain some of these.
469
470 # WARNING: this decoding needs to be checked. $$$
471
472 0 $string =~ s/&\#(\d)+;/decode_utf8(pack("U", $1))/eg;
473
474 # Next, escape all backslashes, double-quotes and whitespace control characters
475
476 0 $string =~ s/(\\|\"|\n|\t|\r)/$ESCAPE{$1}/ge;
477
478 # Finally, delete all other control characters (they shouldn't be in the
479 # database in the first place, but unfortunately some rows do contain
480 # them).
481
482 0 $string =~ s/[\0-\037\177]//g;
483
484 0 return '"' . $string . '"';
485 }
486  
487  
488 1 1;
« blib/lib/Web/DataService.pm ↑ Summary lib/Web/DataService/Vocabulary.pm »