summaryrefslogtreecommitdiff
path: root/cpan/JSON-PP/t/004_dwiw_encode.t
blob: a3329470fcb09716d3c779f9c5d7ce83e2320d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# copied over from JSON::XS and modified to use JSON::PP

# copied over from JSON::DWIW and modified to use JSON::PP

# Creation date: 2007-02-20 19:51:06
# Authors: don

use strict;
use Test;

# main
{
    BEGIN { plan tests => 5 }

    BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }

use JSON::PP;

    my $data;

    #    my $expected_str = '{"var1":"val1","var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}],"var3":"val3"}';

    my $expected_str1 = '{"var1":"val1","var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}]}';
    my $expected_str2 = '{"var2":["first_element",{"sub_element":"sub_val","sub_element2":"sub_val2"}],"var1":"val1"}';
    my $expected_str3 = '{"var2":["first_element",{"sub_element2":"sub_val2","sub_element":"sub_val"}],"var1":"val1"}';
    my $expected_str4 = '{"var1":"val1","var2":["first_element",{"sub_element2":"sub_val2","sub_element":"sub_val"}]}';

    my $json_obj = JSON::PP->new->allow_nonref (1);
    my $json_str;
    # print STDERR "\n" . $json_str . "\n\n";

    my $expected_str;

    $data = 'stuff';
    $json_str = $json_obj->encode($data);
    ok($json_str eq '"stuff"');

    $data = "stu\nff";
    $json_str = $json_obj->encode($data);
    ok($json_str eq '"stu\nff"');

    $data = [ 1, 2, 3 ];
    $expected_str = '[1,2,3]';
    $json_str = $json_obj->encode($data);

    ok($json_str eq $expected_str);

    $data = { var1 => 'val1', var2 => 'val2' };
    $json_str = $json_obj->encode($data);

    ok($json_str eq '{"var1":"val1","var2":"val2"}'
       or $json_str eq '{"var2":"val2","var1":"val1"}');
    
    $data = { var1 => 'val1',
              var2 => [ 'first_element',
                        { sub_element => 'sub_val', sub_element2 => 'sub_val2' },
                      ],
              #                 var3 => 'val3',
            };

    $json_str = $json_obj->encode($data);

    ok($json_str eq $expected_str1 or $json_str eq $expected_str2
       or $json_str eq $expected_str3 or $json_str eq $expected_str4);
}

exit 0;

###############################################################################
# Subroutines