json-test.pl to HTML.

index -|- end

Generated: Mon Sep 12 17:54:28 2016 from json-test.pl 2016/09/02 3.2 KB. text copy

#!/usr/bin/perl -w
#< some json tests
# 20160902 - Use 'eval` block on bad json

use strict;
use warnings;
use JSON;
use Time::Local;
use Date::Calc qw(Week_of_Year);
use Time::gmtime;
use Data::Dumper;

my %hash = ();
my %h = ();
my @pilots = qw( AAA BBB CCC );
my @air = qw( ac1 ac2 ac3 );
my $add_time_tests = 0;

sub prt($) { print shift; }

sub get_scalar($) {
    my $rh = shift;

    my ($pilot,$cnt,$ra,$air,$cnt2);
    $cnt = 0;
    $cnt2 = 0;
    foreach $pilot (@pilots) {
        $cnt++;
        ${$rh}{$pilot}{'total_time'} = $cnt;
        ${$rh}{$pilot}{'aircrafts'} = [];
        $ra = ${$rh}{$pilot}{'aircrafts'};
        foreach $air (@air) {
            $cnt2++;
            my %h2 = ();
            $h2{'aircraft'} = $air;
            $h2{'time'} = $cnt2;
            #push(@{$ra}, [ $air, $cnt2 ]);
            push(@{$ra}, \%h2);
        }
    }
}

my $rh = \%hash;
get_scalar($rh);
$h{'pilots'} = $rh;
$rh = \%h;

my $json_text = encode_json $rh;
prt("\nThe simple encoded JSON text from the ref hash...\n");
prt("$json_text\n");

my $json = JSON::XS->new->pretty(1)->encode($rh);
prt("\nThe 'pretty' encoded JSON text...\n");
prt("$json\n");

prt("\nDecode tests...\n");
my $jsn = JSON->new->allow_nonref;
$json .= "Add err";
eval {
    my $rh1 = $jsn->decode( $json );
    prt(Dumper($rh1)."\n");
    if (defined ${$rh1}{pilots}) {
        my $rh2 = ${$rh1}{pilots};
        prt(Dumper($rh2)."\n");
    }
    1;
} or do {
  my $e = $@;
  prt("Error: $e\n");
};

# alternative
prt("\nUsing alternative 'Try::Tiny'...\n");
use Try::Tiny;
try {
  my $rh3 = decode_json($json);
  prt(Dumper($rh3)."\n");
} catch {
  print "Error: $_\n";
};


#############################################################
### Nothing to do with JSON
#############################################################
if (!$add_time_tests) {
    exit 0;
}

my $str = '2007/07/17 13:21';
my $str2 = '24.06.2012 12:48:28';
my $str3 = '26.05.2012 18:39:08';

my @t   = $str =~ m!(\d{4})/(\d{2})/(\d{2})\s(\d{2}):(\d{2})!;
$t[1]--;
my $timestamp = timelocal 0,@t[4,3,2,1,0];
# verify...
print $str."\n";
print scalar localtime $timestamp;
print "\n";

@t   = $str2 =~ m!(\d{2})\.(\d{2})\.(\d{4})\s(\d{2}):(\d{2})!;
$t[1]--;
my $tmp = $t[0];
$t[0] = $t[2];
$t[2] = $tmp;
$timestamp = timelocal 0,@t[4,3,2,1,0];
print $str2."\n";
print scalar localtime $timestamp;
print "\n";

my $tm = gmtime($timestamp);
#my $m = sprintf( "%04d/%02d/%02d", $tm->year() + 1900, $tm->mon() + 1, $tm->mday());
my $year = $tm->year() + 1900;
my $month = $tm->mon() + 1;
my $day = $tm->mday();
my ($week,$ayear) = Week_of_Year($year,$month,$day); 
prt("Is week $week of $ayear\n");

@t   = $str3 =~ m!(\d{2})\.(\d{2})\.(\d{4})\s(\d{2}):(\d{2})!;
$t[1]--;
$tmp = $t[0];
$t[0] = $t[2];
$t[2] = $tmp;
$timestamp = timelocal 0,@t[4,3,2,1,0];
print $str3."\n";
print scalar localtime $timestamp;
print "\n";

$tm = gmtime($timestamp);
#my $m = sprintf( "%04d/%02d/%02d", $tm->year() + 1900, $tm->mon() + 1, $tm->mday());
$year = $tm->year() + 1900;
$month = $tm->mon() + 1;
$day = $tm->mday();
($week,$ayear) = Week_of_Year($year,$month,$day); 
prt("Is week $week of $ayear\n");

exit 0;

# eof

index -|- top

checked by tidy  Valid HTML 4.01 Transitional