| 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 | use strict; | ||
| 9 | |||
| 10 | package Web::DataService::Plugin::JSON; | ||
| 11 | |||
| 12 | use JSON; | ||
| 13 | use Encode; | ||
| 14 | use Scalar::Util qw(reftype); | ||
| 15 | use Carp qw(croak); | ||
| 16 | |||
| 17 | use parent 'Exporter'; | ||
| 18 | |||
| 19 | 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 | my ($class, $request, $field_list) = @_; | ||
| 29 | |||
| 30 | my $output = '{' . "\n"; | ||
| 31 | |||
| 32 | # Check if we have been asked to report the data source and parameters. | ||
| 33 | |||
| 34 | if ( $request->display_datainfo ) | ||
| 35 | { | ||
| 36 | my $info = $request->data_info; | ||
| 37 | |||
| 38 | foreach my $key ( $request->data_info_keys ) | ||
| 39 | { | ||
| 40 | next unless $info->{$key}; | ||
| 41 | my $value = json_clean($info->{$key}); | ||
| 42 | |||
| 43 | $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 | $output .= '"parameters":{' . "\n"; | ||
| 55 | |||
| 56 | my @display = $request->params_for_display; | ||
| 57 | my $sep = ''; | ||
| 58 | |||
| 59 | while ( @display ) | ||
| 60 | { | ||
| 61 | my $param = shift @display; | ||
| 62 | my $value = shift @display; | ||
| 63 | |||
| 64 | next unless defined $param && $param ne ''; | ||
| 65 | $value //= ''; | ||
| 66 | |||
| 67 | $output .= $sep; $sep = ",\n"; | ||
| 68 | |||
| 69 | if ( ref $value eq 'ARRAY' ) | ||
| 70 | { | ||
| 71 | $output .= json_list_value($param, @$value); | ||
| 72 | } | ||
| 73 | |||
| 74 | else | ||
| 75 | { | ||
| 76 | $output .= qq<"$param":"$value">; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | $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 | $output .= '"elapsed_time":' . sprintf("%.3g", $request->{elapsed}) . ",\n"; | ||
| 87 | |||
| 88 | if ( $request->display_counts ) | ||
| 89 | { | ||
| 90 | my $counts = $request->result_counts; | ||
| 91 | |||
| 92 | $output .= '"records_found":' . json_clean($counts->{found} || '0') . ",\n"; | ||
| 93 | $output .= '"records_returned":' . json_clean($counts->{returned} || '0') . ",\n"; | ||
| 94 | $output .= '"record_offset":' . json_clean($counts->{offset}) . ",\n" | ||
| 95 | if defined $counts->{offset} && $counts->{offset} > 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | # Check if we have any warning messages to convey | ||
| 99 | |||
| 100 | if ( my @msgs = $request->warnings ) | ||
| 101 | { | ||
| 102 | $output .= qq<"warnings":[\n>; | ||
| 103 | my $sep = ''; | ||
| 104 | foreach my $m (@msgs) | ||
| 105 | { | ||
| 106 | $output .= $sep; $sep = ",\n"; | ||
| 107 | $output .= json_clean($m); | ||
| 108 | } | ||
| 109 | $output .= qq<\n],\n>; | ||
| 110 | } | ||
| 111 | |||
| 112 | # Check if we have summary data to output. | ||
| 113 | |||
| 114 | if ( $request->{summary_data} && $request->{summary_field_list} ) | ||
| 115 | { | ||
| 116 | $output .= qq<"summary": >; | ||
| 117 | $output .= $class->emit_object($request, $request->{summary_data}, $request->{summary_field_list}) || '""'; | ||
| 118 | $output .= ",\n"; | ||
| 119 | } | ||
| 120 | |||
| 121 | # The actual data will go into an array, in a field called "records". | ||
| 122 | |||
| 123 | $output .= qq<"records": [\n>; | ||
| 124 | 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 | return ",\n"; | ||
| 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 | my ($class, $request) = @_; | ||
| 146 | |||
| 147 | return ''; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | # emit_footer ( ) | ||
| 152 | # | ||
| 153 | # Return the final text for a JSON result. | ||
| 154 | |||
| 155 | sub emit_footer { | ||
| 156 | |||
| 157 | my ($class, $request) = @_; | ||
| 158 | |||
| 159 | 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 | my ($class, $code, $errors, $warnings, $cautions) = @_; | ||
| 170 | |||
| 171 | unless ( ref $errors eq 'ARRAY' ) | ||
| 172 | { | ||
| 173 | $errors = [ "bad call to 'emit_error'" ]; | ||
| 174 | } | ||
| 175 | |||
| 176 | if ( defined $warnings && ! ref $warnings eq 'ARRAY' ) | ||
| 177 | { | ||
| 178 | $warnings = [ "bad call to 'emit_error'" ]; | ||
| 179 | } | ||
| 180 | |||
| 181 | my $error = '"status_code": ' . $code; | ||
| 182 | $error .= ",\n" . json_list_value("errors", @$errors) if ref $errors eq 'ARRAY' && @$errors; | ||
| 183 | $error .= ",\n" . json_list_value("cautions", @$cautions) if ref $cautions eq 'ARRAY' && @$cautions; | ||
| 184 | $error .= ",\n" . json_list_value("warnings", @$warnings) if ref $warnings eq 'ARRAY' && @$warnings; | ||
| 185 | |||
| 186 | 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 | my ($class, $request, $record, $field_list) = @_; | ||
| 198 | |||
| 199 | 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 | my ($class, $request, $record, $field_list) = @_; | ||
| 211 | |||
| 212 | # Start with an empty string. | ||
| 213 | |||
| 214 | my $outrec = '{'; | ||
| 215 | 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 | foreach my $f (reftype $field_list && reftype $field_list eq 'ARRAY' ? @$field_list : $field_list) | ||
| 221 | { | ||
| 222 | # Skip any field that is empty, unless 'always' or 'value' is set. | ||
| 223 | |||
| 224 | my $field = $f->{field}; | ||
| 225 | my $data_type = $f->{data_type}; | ||
| 226 | |||
| 227 | next unless $f->{always} or defined $f->{value} or | ||
| 228 | defined $record->{$field} and $record->{$field} ne ''; | ||
| 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 | and $record->{$field} eq $record->{$f->{dedup}}; | ||
| 235 | |||
| 236 | # Skip any field with a 'if_field' attribute if the corresponding | ||
| 237 | # field does not have a true value. | ||
| 238 | |||
| 239 | next if $f->{if_field} and not $record->{$f->{if_field}}; | ||
| 240 | |||
| 241 | # Skip any field with a 'not_field' attribute if the corresponding | ||
| 242 | # field has a true value. | ||
| 243 | |||
| 244 | next if $f->{not_field} and $record->{$f->{not_field}}; | ||
| 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 | : defined $record->{$field} ? $record->{$field} | ||
| 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 | if ( defined $f->{sub_record} ) | ||
| 261 | { | ||
| 262 | my $ds = $request->ds; | ||
| 263 | $ds->configure_block($request, $f->{sub_record}); | ||
| 264 | |||
| 265 | my $output_list = $request->{block_field_list}{$f->{sub_record}}; | ||
| 266 | my $proc_list = $request->{block_proc_list}{$f->{sub_record}}; | ||
| 267 | |||
| 268 | if ( ref $value && reftype $value eq 'HASH' ) | ||
| 269 | { | ||
| 270 | $request->_process_record($value, $proc_list) if $proc_list && @$proc_list; | ||
| 271 | |||
| 272 | if ( $output_list && @$output_list ) | ||
| 273 | { | ||
| 274 | $value = $class->emit_object($request, $value, $output_list); | ||
| 275 | } | ||
| 276 | else | ||
| 277 | { | ||
| 278 | $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' ) | ||
| 286 | { | ||
| 287 | if ( $proc_list && @$proc_list ) | ||
| 288 | { | ||
| 289 | foreach my $v ( @$value ) | ||
| 290 | { | ||
| 291 | $request->_process_record($v, $proc_list) if $proc_list; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | if ( $output_list && @$output_list ) | ||
| 296 | { | ||
| 297 | $value = $class->emit_array($request, $value, $output_list); | ||
| 298 | } | ||
| 299 | else | ||
| 300 | { | ||
| 301 | $value = json_clean($value, $data_type); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | else | ||
| 306 | { | ||
| 307 | $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 | $value = $class->emit_array($request, $value); | ||
| 318 | } | ||
| 319 | |||
| 320 | elsif ( $f->{show_as_list} ) | ||
| 321 | { | ||
| 322 | $value = $class->emit_array($request, [ $value ]); | ||
| 323 | } | ||
| 324 | |||
| 325 | # Otherwise just use the value. | ||
| 326 | |||
| 327 | else | ||
| 328 | { | ||
| 329 | $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 | my $outkey = $f->{name}; | ||
| 336 | |||
| 337 | $outrec .= qq<$sep"$outkey":$value>; | ||
| 338 | $sep = q<,>; | ||
| 339 | } | ||
| 340 | |||
| 341 | # If this record has hierarchical children, process them now. (Do we | ||
| 342 | # still need this?) | ||
| 343 | |||
| 344 | if ( exists $record->{hier_child} ) | ||
| 345 | { | ||
| 346 | my $children = $class->emit_array($record->{hier_child}, $field_list); | ||
| 347 | $outrec .= qq<,"children":$children>; | ||
| 348 | } | ||
| 349 | |||
| 350 | # Now finish the output string and return it. | ||
| 351 | |||
| 352 | $outrec .= '}'; | ||
| 353 | |||
| 354 | 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 | my ($class, $request, $arrayref, $field_list) = @_; | ||
| 366 | |||
| 367 | my $f = $field_list if reftype $field_list && reftype $field_list ne 'ARRAY'; | ||
| 368 | |||
| 369 | # Start with an empty string. | ||
| 370 | |||
| 371 | my $outrec = '['; | ||
| 372 | my $sep = ''; | ||
| 373 | |||
| 374 | # Go through the elements of the specified arrayref, applying the | ||
| 375 | # specified rule to each one. | ||
| 376 | |||
| 377 | my $value = ''; | ||
| 378 | |||
| 379 | foreach my $elt ( @$arrayref ) | ||
| 380 | { | ||
| 381 | if ( reftype $elt && reftype $elt eq 'ARRAY' ) | ||
| 382 | { | ||
| 383 | $value = $class->emit_array($request, $elt, $field_list); | ||
| 384 | } | ||
| 385 | |||
| 386 | elsif ( reftype $elt && reftype $elt eq 'HASH' ) | ||
| 387 | { | ||
| 388 | next unless $field_list; | ||
| 389 | $value = $class->emit_object($request, $elt, $field_list); | ||
| 390 | } | ||
| 391 | |||
| 392 | elsif ( ref $elt ) | ||
| 393 | { | ||
| 394 | next; | ||
| 395 | } | ||
| 396 | |||
| 397 | else | ||
| 398 | { | ||
| 399 | $value = json_clean($elt); | ||
| 400 | } | ||
| 401 | |||
| 402 | if ( defined $value and $value ne '' ) | ||
| 403 | { | ||
| 404 | $outrec .= "$sep$value"; | ||
| 405 | $sep = ','; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | $outrec .= ']'; | ||
| 410 | |||
| 411 | 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 | my ($key, @values) = @_; | ||
| 423 | |||
| 424 | my $output = qq<"$key": [>; | ||
| 425 | my $sep = ''; | ||
| 426 | |||
| 427 | foreach my $m (@values) | ||
| 428 | { | ||
| 429 | $output .= $sep; $sep = ', '; | ||
| 430 | $output .= json_clean($m); | ||
| 431 | } | ||
| 432 | |||
| 433 | $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 | my (%ESCAPE) = ( '\\' => '\\\\', '"' => '\\"', "\t" => '\\t', "\n" => '\\n', | ||
| 443 | "\r" => '\\r' ); #' | ||
| 444 | |||
| 445 | sub json_clean { | ||
| 446 | |||
| 447 | my ($string, $data_type) = @_; | ||
| 448 | |||
| 449 | # Return an empty string unless the value is defined. | ||
| 450 | |||
| 451 | return '""' unless defined $string and $string ne ''; | ||
| 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 | return $string if $string =~ qr{ ^ -? (?: [1-9][0-9]* | 0 ) (?: \. [0-9]+ )? (?: [Ee] -? [0-9]+ )? $ }x | ||
| 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 | return '"' . $string . '"' unless $string =~ /[^a-zA-Z0-9 _.,;:<>-]/; | ||
| 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 | $string =~ s/&\#(\d)+;/decode_utf8(pack("U", $1))/eg; | ||
| 473 | |||
| 474 | # Next, escape all backslashes, double-quotes and whitespace control characters | ||
| 475 | |||
| 476 | $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 | $string =~ s/[\0-\037\177]//g; | ||
| 483 | |||
| 484 | return '"' . $string . '"'; | ||
| 485 | } | ||
| 486 | |||
| 487 | |||
| 488 | 1; |