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