xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/052_object.t (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1# copied over from JSON::XS and modified to use JSON::PP
2
3use strict;
4use Test::More;
5BEGIN { plan tests => 20 };
6BEGIN { $^W = 0 } # hate
7
8BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
9
10use JSON::PP;
11
12my $json = JSON::PP->new->convert_blessed->allow_tags->allow_nonref;
13
14ok (1);
15
16sub JSON::PP::tojson::TO_JSON {
17   ok (@_ == 1);
18   ok (JSON::PP::tojson:: eq ref $_[0]);
19   ok ($_[0]{k} == 1);
20   7
21}
22
23my $obj = bless { k => 1 }, JSON::PP::tojson::;
24
25ok (1);
26
27my $enc = $json->encode ($obj);
28ok ($enc eq 7);
29
30ok (1);
31
32sub JSON::PP::freeze::FREEZE {
33   ok (@_ == 2);
34   ok ($_[1] eq "JSON");
35   ok (JSON::PP::freeze:: eq ref $_[0]);
36   ok ($_[0]{k} == 1);
37   (3, 1, 2)
38}
39
40sub JSON::PP::freeze::THAW {
41   ok (@_ == 5);
42   ok (JSON::PP::freeze:: eq $_[0]);
43   ok ($_[1] eq "JSON");
44   ok ($_[2] == 3);
45   ok ($_[3] == 1);
46   ok ($_[4] == 2);
47   777
48}
49
50my $obj = bless { k => 1 }, JSON::PP::freeze::;
51my $enc = $json->encode ($obj);
52ok ($enc eq '("JSON::PP::freeze")[3,1,2]');
53
54my $dec = $json->decode ($enc);
55ok ($dec eq 777);
56
57ok (1);
58
59