File: json.t

package info (click to toggle)
liblist-objects-withutils-perl 2.028003-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,276 kB
  • sloc: perl: 1,957; makefile: 17; sh: 6
file content (56 lines) | stat: -rw-r--r-- 1,329 bytes parent folder | download | duplicates (4)
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

BEGIN {
  unless (eval {; require JSON::PP; 1 } && !$@ ) {
    require Test::More;
    Test::More::plan(skip_all => 
      'these tests require JSON::PP'
    );
  }
}

use Test::More;
use strict; use warnings FATAL => 'all';

use JSON::PP;
use List::Objects::WithUtils;

my $js = JSON::PP->new;
$js->convert_blessed(1);

{ my $obj = array(1,2,3);
  ok my $res = $js->encode($obj), 'encoded array';
  my $arr = $js->decode($res);
  is_deeply $arr, [ 1, 2, 3 ], 'round-tripped array';
}

{ my $obj = immarray(1,2,3);
  ok my $res = $js->encode($obj), 'encoded immarray';
  my $arr = $js->decode($res);
  is_deeply $arr, [ 1, 2, 3 ], 'round-tripped immarray';
}

{ my $obj = array( 1, 2, array(3, 4) );
  ok my $res = $js->encode($obj), 'encoded (deep) array';
  my $arr = $js->decode($res);
  is_deeply $arr, [ 1, 2, [ 3, 4 ] ], 'round-tripped (deep) array';
}

{ my $obj = hash(foo => 'bar');
  ok my $res = $js->encode($obj), 'encoded hash';
  my $hash = $js->decode($res);
  is_deeply $hash, +{ foo => 'bar' }, 'round-tripped hash';
}

{ my $obj = hash( foo => hash(bar => 1), bar => array(1,2) );
  ok my $res = $js->encode($obj), 'encoded (deep) hash';
  my $hash = $js->decode($res);
  is_deeply 
    $hash, 
    +{
      foo => +{ bar => 1 },
      bar => [ 1, 2 ],
    },
    'round-tripped (deep) hash';
}

done_testing;