Commit | Line | Data |
ebf1e433 |
1 | use strict; |
4e1fb2f8 |
2 | use warnings; |
ebf1e433 |
3 | |
b4d74169 |
4 | # hide Cpanel::JSON::XS |
5 | use lib map { |
7c484eb9 |
6 | my $m = $_; |
7 | sub { return unless $_[1] eq $m; die "Can't locate $m in \@INC (hidden).\n" }; |
b4d74169 |
8 | } qw{Cpanel/JSON/XS.pm}; |
9 | |
2c67ae64 |
10 | use Test::More 0.88; |
ebf1e433 |
11 | use JSON::MaybeXS qw/:legacy/; |
12 | |
ebf1e433 |
13 | my $in = '[1, 2, 3, 4]'; |
14 | |
ebf1e433 |
15 | my $arr = from_json($in); |
16 | my $j = to_json($arr); |
17 | is($j, '[1,2,3,4]'); |
18 | is(ref($arr), 'ARRAY'); |
19 | |
c397f194 |
20 | my $json = 'JSON::MaybeXS'; |
21 | diag "using invocant: $json"; |
22 | like( |
23 | do { eval { $json->from_json($in) }; $@ }, |
24 | qr/from_json should not be called as a method/, |
25 | 'blessed invocant detected in from_json', |
26 | ); |
27 | |
28 | like( |
29 | do { eval { $json->to_json($arr, { blah => 1 } ) }; $@ }, |
30 | qr/to_json should not be called as a method/, |
31 | 'blessed invocant detected in to_json', |
32 | ); |
33 | |
ebf1e433 |
34 | done_testing; |
35 | |
36 | __END__ |
37 | |
38 | to_json |
39 | $json_text = to_json($perl_scalar) |
40 | |
41 | Converts the given Perl data structure to a json string. |
42 | |
43 | This function call is functionally identical to: |
44 | |
20f884e2 |
45 | $json_text = JSON()->new->encode($perl_scalar) |
ebf1e433 |
46 | |
47 | from_json |
48 | $perl_scalar = from_json($json_text) |
49 | |
50 | The opposite of "to_json": expects a json string and tries to parse it, |
51 | returning the resulting reference. |
52 | |
53 | This function call is functionally identical to: |
54 | |
20f884e2 |
55 | $perl_scalar = JSON()->decode($json_text) |