File: legacy.t

package info (click to toggle)
libjson-maybexs-perl 1.004008-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: perl: 299; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,268 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
use strict;
use warnings;

# hide Cpanel::JSON::XS
use lib map {
    my $m = $_;
    sub { return unless $_[1] eq $m; die "Can't locate $m in \@INC (hidden).\n" };
} qw{Cpanel/JSON/XS.pm};

use Test::More 0.88;
use JSON::MaybeXS qw/:legacy/;

my $in = '[1, 2, 3, 4]';

my $arr = from_json($in);
my $j = to_json($arr);
is($j, '[1,2,3,4]');
is(ref($arr), 'ARRAY');

my $json = 'JSON::MaybeXS';
diag "using invocant: $json";
like(
    do { eval { $json->from_json($in) }; $@ },
    qr/from_json should not be called as a method/,
    'blessed invocant detected in from_json',
);

like(
    do { eval { $json->to_json($arr, { blah => 1 } ) }; $@ },
    qr/to_json should not be called as a method/,
    'blessed invocant detected in to_json',
);

done_testing;

__END__

  to_json
       $json_text = to_json($perl_scalar)

    Converts the given Perl data structure to a json string.

    This function call is functionally identical to:

       $json_text = JSON()->new->encode($perl_scalar)

  from_json
       $perl_scalar = from_json($json_text)

    The opposite of "to_json": expects a json string and tries to parse it,
    returning the resulting reference.

    This function call is functionally identical to:

        $perl_scalar = JSON()->decode($json_text)