1use strict;
2use Test::More tests => 9;
3use Cpanel::JSON::XS;
4
5my $json = Cpanel::JSON::XS->new;
6
7# disallow dupkeys
8ok (!eval { $json->decode ('{"a":"b","a":"c"}') }); # y_object_duplicated_key.json
9ok (!eval { $json->decode ('{"a":"b","a":"b"}') }); # y_object_duplicated_key_and_value.json
10
11# relaxed allows dupkeys
12$json->relaxed;
13# y_object_duplicated_key.json
14is (encode_json ($json->decode ('{"a":"b","a":"c"}')), '{"a":"c"}', 'relaxed');
15# y_object_duplicated_key_and_value.json
16is (encode_json ($json->decode ('{"a":"b","a":"b"}')), '{"a":"b"}', 'relaxed');
17
18# turning off relaxed disallows dupkeys
19$json->relaxed(0);
20$json->allow_dupkeys; # but turn it on
21is (encode_json ($json->decode ('{"a":"b","a":"c"}')), '{"a":"c"}', 'allow_dupkeys');
22is (encode_json ($json->decode ('{"a":"b","a":"b"}')), '{"a":"b"}', 'allow_dupkeys');
23
24# disallow dupkeys explicitly
25$json->allow_dupkeys(0);
26eval { $json->decode ('{"a":"b","a":"c"}') };
27like ($@, qr/^Duplicate keys not allowed/, 'allow_dupkeys(0)');
28
29# disallow dupkeys explicitly with relaxed
30$json->relaxed;
31$json->allow_dupkeys(0);
32eval { $json->decode ('{"a":"b","a":"c"}') }; # the XS slow path
33like ($@, qr/^Duplicate keys not allowed/, 'relaxed and allow_dupkeys(0)');
34
35$json->allow_dupkeys;
36$json->relaxed(0); # tuning off relaxed needs to turn off dupkeys
37eval { $json->decode ('{"a":"b","a":"c"}') };
38like ($@, qr/^Duplicate keys not allowed/, 'relaxed(0)');
39