"Fossies" - the Fresh Open Source Software Archive

Member "perl-5.32.1/cpan/JSON-PP/t/004_dwiw_encode.t" (18 Dec 2020, 2134 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 19:51:06
    6 # Authors: don
    7 
    8 use strict;
    9 use Test;
   10 
   11 # main
   12 {
   13     BEGIN { plan tests => 5 }
   14 
   15     BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
   16 
   17 use JSON::PP;
   18 
   19     my $data;
   20 
   21     #    my $expected_str = '{"var1":"val1","var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}],"var3":"val3"}';
   22 
   23     my $expected_str1 = '{"var1":"val1","var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}]}';
   24     my $expected_str2 = '{"var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}],"var1":"val1"}';
   25     my $expected_str3 = '{"var2":["first_element",{"sub_element2":"sub_val2","sub_element":"sub_val"}],"var1":"val1"}';
   26     my $expected_str4 = '{"var1":"val1","var2":["first_element",{"sub_element2":"sub_val2","sub_element":"sub_val"}]}';
   27 
   28     my $json_obj = JSON::PP->new->allow_nonref (1);
   29     my $json_str;
   30     # print STDERR "\n" . $json_str . "\n\n";
   31 
   32     my $expected_str;
   33 
   34     $data = 'stuff';
   35     $json_str = $json_obj->encode($data);
   36     ok($json_str eq '"stuff"');
   37 
   38     $data = "stu\nff";
   39     $json_str = $json_obj->encode($data);
   40     ok($json_str eq '"stu\nff"');
   41 
   42     $data = [ 1, 2, 3 ];
   43     $expected_str = '[1,2,3]';
   44     $json_str = $json_obj->encode($data);
   45 
   46     ok($json_str eq $expected_str);
   47 
   48     $data = { var1 => 'val1', var2 => 'val2' };
   49     $json_str = $json_obj->encode($data);
   50 
   51     ok($json_str eq '{"var1":"val1","var2":"val2"}'
   52        or $json_str eq '{"var2":"val2","var1":"val1"}');
   53     
   54     $data = { var1 => 'val1',
   55               var2 => [ 'first_element',
   56                         { sub_element => 'sub_val', sub_element2 => 'sub_val2' },
   57                       ],
   58               #                 var3 => 'val3',
   59             };
   60 
   61     $json_str = $json_obj->encode($data);
   62 
   63     ok($json_str eq $expected_str1 or $json_str eq $expected_str2
   64        or $json_str eq $expected_str3 or $json_str eq $expected_str4);
   65 }
   66 
   67 exit 0;
   68 
   69 ###############################################################################
   70 # Subroutines
   71