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