"Fossies" - the Fresh Open Source Software Archive

Member "perl-5.32.1/cpan/JSON-PP/t/005_dwiw_decode.t" (18 Dec 2020, 2154 Bytes) of package /linux/misc/perl-5.32.1.tar.xz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # copied over from JSON::XS and modified to use JSON::PP
    2 
    3 # copied over from JSON::DWIW and modified to use JSON::PP
    4 
    5 # Creation date: 2007-02-20 21:54:09
    6 # Authors: don
    7 
    8 use strict;
    9 use warnings;
   10 use Test;
   11 
   12 # main
   13 {
   14     BEGIN { plan tests => 7 }
   15 
   16     BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
   17 
   18 use JSON::PP;
   19 
   20     my $json_str = '{"var1":"val1","var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}],"var3":"val3"}';
   21 
   22     my $json_obj = JSON::PP->new->allow_nonref(1);
   23     my $data = $json_obj->decode($json_str);
   24 
   25     my $pass = 1;
   26     if ($data->{var1} eq 'val1' and $data->{var3} eq 'val3') {
   27         if ($data->{var2}) {
   28             my $array = $data->{var2};
   29             if (ref($array) eq 'ARRAY') {
   30                 if ($array->[0] eq 'first_element') {
   31                     my $hash = $array->[1];
   32                     if (ref($hash) eq 'HASH') {
   33                         unless ($hash->{sub_element} eq 'sub_val'
   34                                 and $hash->{sub_element2} eq 'sub_val2') {
   35                             $pass = 0;
   36                         }
   37                     }
   38                     else {
   39                         $pass = 0;
   40                     }
   41                 }
   42                 else {
   43                     $pass = 0;
   44                 }
   45             }
   46             else {
   47                 $pass = 0;
   48             }
   49         }
   50         else {
   51             $pass = 0;
   52         }
   53     }
   54     
   55     ok($pass);
   56 
   57     $json_str = '"val1"';
   58     $data = $json_obj->decode($json_str);
   59     ok($data eq 'val1');
   60 
   61     $json_str = '567';
   62     $data = $json_obj->decode($json_str);
   63     ok($data == 567);
   64 
   65     $json_str = "5e1";
   66     $data = $json_obj->decode($json_str);
   67     ok($data == 50);
   68 
   69     $json_str = "5e3";
   70     $data = $json_obj->decode($json_str);
   71     ok($data == 5000);
   72 
   73     $json_str = "5e+1";
   74     $data = $json_obj->decode($json_str);
   75     ok($data == 50);
   76 
   77     $json_str = "5e-1";
   78     $data = $json_obj->decode($json_str);
   79     ok($data == 0.5);
   80 
   81 
   82 
   83     
   84 #     use Data::Dumper;
   85 #     print STDERR Dumper($test_data) . "\n\n";
   86 
   87 }
   88 
   89 exit 0;
   90 
   91 ###############################################################################
   92 # Subroutines
   93