3 use Test::More tests => 49;
5 use OpenSRF::Utils::JSON;
9 # initial state from use
12 # do we have a JSON::XS object?
13 is (ref $OpenSRF::Utils::JSON::parser, 'JSON::XS');
15 # make sure the class and payload keys are as expected
16 is ($OpenSRF::Utils::JSON::JSON_CLASS_KEY, '__c');
17 is ($OpenSRF::Utils::JSON::JSON_PAYLOAD_KEY, '__p');
19 # start with the simplest bits possible
20 is (OpenSRF::Utils::JSON::true, 1);
21 is (OpenSRF::Utils::JSON->true, 1);
22 is (OpenSRF::Utils::JSON->false, 0);
27 my $testmap = { hints => { osrfException =>
28 { hint => 'osrfException',
29 name => 'OpenSRF::DomainObject::oilsException' }
31 classes => { OpenSRF::DomainObject::oilsException =>
32 { hint => 'osrfException',
33 name => 'OpenSRF::DomainObject::oilsException' }
36 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfException',
37 name => 'OpenSRF::DomainObject::oilsException');
38 is_deeply (\%OpenSRF::Utils::JSON::_class_map, $testmap);
43 is (OpenSRF::Utils::JSON->lookup_class('osrfException'), 'OpenSRF::DomainObject::oilsException');
44 is (OpenSRF::Utils::JSON->lookup_class(37), undef, "Argument doesn't exist");
45 is (OpenSRF::Utils::JSON->lookup_class(''), undef, "Null string lookup");
46 is (OpenSRF::Utils::JSON->lookup_class(), undef, "Null request");
51 is (OpenSRF::Utils::JSON->lookup_hint('OpenSRF::DomainObject::oilsException'), 'osrfException');
52 is (OpenSRF::Utils::JSON->lookup_hint(37), undef, "Argument doesn't exist");
53 is (OpenSRF::Utils::JSON->lookup_hint(''), undef, "Null string lookup");
54 is (OpenSRF::Utils::JSON->lookup_hint(), undef, "Null request");
59 my $struct = [ { foo => 'bar' }, 'baz', 'quux', 'x'];
60 is (OpenSRF::Utils::JSON->rawPerl2JSON($struct),
61 '[{"foo":"bar"},"baz","quux","x"]');
62 is (OpenSRF::Utils::JSON->rawPerl2JSON(''), '""', "Null string as argument");
67 is_deeply (OpenSRF::Utils::JSON->rawJSON2perl(OpenSRF::Utils::JSON->rawPerl2JSON($struct)),
68 [ { foo => 'bar' }, 'baz', 'quux', 'x']);
69 is (OpenSRF::Utils::JSON->rawJSON2perl(), undef, "Null argument");
70 is (OpenSRF::Utils::JSON->rawJSON2perl(''), undef, "Null string as argument"); # note inconsistency with above
75 is (OpenSRF::Utils::JSON->perl2JSONObject(), undef, "Returns argument unless it's a ref");
76 is (OpenSRF::Utils::JSON->perl2JSONObject(3), 3, "Returns argument unless it's a ref");
77 is (OpenSRF::Utils::JSON->perl2JSONObject('foo'), 'foo', "Returns argument unless it's a ref");
79 ok (JSON::XS::is_bool(OpenSRF::Utils::JSON->true), 'OpenSRF::Utils::JSON->true is a Boolean according to JSON::XS');
80 is (OpenSRF::Utils::JSON->perl2JSONObject(OpenSRF::Utils::JSON->true), '1', "Returns argument if it's a Boolean according to JSON");
82 my $hashref = { foo => 'bar' };
83 is (UNIVERSAL::isa($hashref,'HASH'), 1);
84 is_deeply (OpenSRF::Utils::JSON->perl2JSONObject($hashref), { foo => 'bar' }, "Passing in unblessed hashref");
86 my $arryref = [ 11, 12 ];
87 is (UNIVERSAL::isa($arryref,'ARRAY'), 1);
88 is_deeply (OpenSRF::Utils::JSON->perl2JSONObject($arryref), [ 11, 12 ], "Passing in unblessed arrayref");
90 my $coderef = sub { return 0 }; # this is almost certainly undesired behavior, but the
91 is (UNIVERSAL::isa($coderef,'CODE'), 1); # code doesn't stop me from doing it
92 is_deeply (OpenSRF::Utils::JSON->perl2JSONObject($coderef),
93 { __c => 'CODE', __p => undef }, "Passing in coderef");
95 my $fakeobj = bless { foo => 'bar' }, 'OpenSRF::DomainObject::oilsException';
96 is (UNIVERSAL::isa($fakeobj,'HASH'), 1);
97 my $jsonobj = OpenSRF::Utils::JSON->perl2JSONObject($fakeobj);
98 is_deeply ($jsonobj, { __c => 'osrfException', __p => { foo => 'bar' } },
99 "Wrap object into an OpenSRF-shaped packet");
104 my $jsonstr = OpenSRF::Utils::JSON->perl2JSON($fakeobj);
105 is ($jsonstr, '{"__c":"osrfException","__p":{"foo":"bar"}}');
110 is (OpenSRF::Utils::JSON->JSONObject2Perl(), undef, "Returns argument unless it's a ref");
111 is (OpenSRF::Utils::JSON->JSONObject2Perl(3), 3, "Returns argument unless it's a ref");
112 is (OpenSRF::Utils::JSON->JSONObject2Perl('foo'), 'foo', "Returns argument unless it's a ref");
113 is (OpenSRF::Utils::JSON->JSONObject2Perl($coderef), $coderef, "Returns argument unless it's a ref");
115 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl([11, 12]), [11, 12], "Arrayrefs get reconstructed as themselves");
116 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl([11, OpenSRF::Utils::JSON->true, 12]), [11, OpenSRF::Utils::JSON->true, 12],
117 "Even when they contain JSON::XS Booleans; those just don't get recursed upon");
118 # note: [11, 1, 12] doesn't work here, even though you can do math on J::X Booleans
120 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl($hashref), { foo => 'bar' }, "Hashrefs without the class flag also get turned into themselves");
121 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl({ foo => OpenSRF::Utils::JSON->true, bar => 'baz' }),
122 { foo => OpenSRF::Utils::JSON->true, bar => 'baz'},
123 "Even when they contain JSON::XS Booleans; those just don't get recursed upon");
125 my $vivobj = OpenSRF::Utils::JSON->JSONObject2Perl($jsonobj);
126 is (ref $vivobj, 'OpenSRF::DomainObject::oilsException');
127 is_deeply ($vivobj, { foo => 'bar' }, "perl2JSONObject-packaged things get blessed to their original contents and class");
129 my $codeobj = OpenSRF::Utils::JSON->perl2JSONObject($coderef);
130 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl($codeobj), undef, "Things with undefined payloads (see above)return undef");
132 $vivobj = OpenSRF::Utils::JSON->JSONObject2Perl({ __c => 'foo', __p => 'bar' });
133 is (ref $vivobj, 'foo');
134 is_deeply ($vivobj, \'bar', "Scalar payload and non-resolvable class hint vivifies to a scalar *ref* and a class of the class flag");
139 my $perlobj = OpenSRF::Utils::JSON->JSON2perl($jsonstr);
140 is (ref $perlobj, 'OpenSRF::DomainObject::oilsException');
141 is_deeply ($perlobj, { foo => 'bar' }, "Successful revivification from JSON in one step");