t / t0019 / parse_json.perlon commit Merge branch 'jc/post-c89-rules-doc' into maint (f91dbd8)
   1#!/usr/bin/perl
   2use strict;
   3use warnings;
   4use JSON;
   5
   6sub dump_array {
   7    my ($label_in, $ary_ref) = @_;
   8    my @ary = @$ary_ref;
   9
  10    for ( my $i = 0; $i <= $#{ $ary_ref }; $i++ )
  11    {
  12        my $label = "$label_in\[$i\]";
  13        dump_item($label, $ary[$i]);
  14    }
  15}
  16
  17sub dump_hash {
  18    my ($label_in, $obj_ref) = @_;
  19    my %obj = %$obj_ref;
  20
  21    foreach my $k (sort keys %obj) {
  22        my $label = (length($label_in) > 0) ? "$label_in.$k" : "$k";
  23        my $value = $obj{$k};
  24
  25        dump_item($label, $value);
  26    }
  27}
  28
  29sub dump_item {
  30    my ($label_in, $value) = @_;
  31    if (ref($value) eq 'ARRAY') {
  32        print "$label_in array\n";
  33        dump_array($label_in, $value);
  34    } elsif (ref($value) eq 'HASH') {
  35        print "$label_in hash\n";
  36        dump_hash($label_in, $value);
  37    } elsif (ref $value) {
  38        my $bool = $value ? 1 : 0;
  39        print "$label_in $bool\n";
  40    } elsif (defined $value) {
  41        print "$label_in $value\n";
  42    } else {
  43        print "$label_in null\n";
  44    }
  45}
  46
  47my $row = 0;
  48while (<>) {
  49    my $data = decode_json( $_ );
  50    my $label = "row[$row]";
  51
  52    dump_hash($label, $data);
  53    $row++;
  54}
  55