/usr/share/perl5/JSON/MultiValueOrdered.pm is in libjson-multivalueordered-perl 0.005-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | use 5.008;
use strict;
use warnings;
use if $] < 5.010, 'UNIVERSAL::DOES';
{
package JSON::MultiValueOrdered;
our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION = '0.005';
use base qw(JSON::Tiny::Subclassable);
use Tie::Hash::MultiValueOrdered ();
sub _new_hash { tie my %h, 'Tie::Hash::MultiValueOrdered'; return \%h }
sub _encode_object {
my $self = shift;
my $object = shift;
my $indent;
if (exists $self->{_indent}) {
$indent = $self->{_indent};
$self->{_indent} .= "\t";
}
my @pairs;
my $space = defined $indent ? q( ) : q();
my $tied = tied(%$object);
if ($tied and $tied->DOES('Tie::Hash::MultiValueOrdered')) {
my @list = $tied->pairs;
for (my $i = 0; $i < @list; $i+=2) {
push @pairs, sprintf(
'%s:%s%s',
$self->_encode_string($list[$i]),
$space,
$self->_encode_values($list[$i + 1]),
);
}
}
else {
while (my ($k, $v) = each %$object) {
push @pairs, sprintf(
'%s:%s%s',
$self->_encode_string($k),
$space,
$self->_encode_values($v),
);
}
}
if (defined $indent)
{
$self->{_indent} =~ s/^.//;
return "{}" unless @pairs;
return "\{\n$indent\t" . join(",\n$indent\t", @pairs) . "\n$indent\}";
}
else
{
return '{' . join(',', @pairs) . '}';
}
}
__PACKAGE__->import('j');
}
1;
__END__
=head1 NAME
JSON::MultiValueOrdered - handle JSON like {"a":1, "a":2}
=head1 SYNOPSIS
use Test::More tests => 4;
use JSON::MultiValueOrdered;
my $j = JSON::MultiValueOrdered->new;
isa_ok $j, 'JSON::Tiny';
my $data = $j->decode(<<'JSON');
{
"a": 1,
"b": 2,
"a": 3,
"b": 4
}
JSON
# As you'd expect, for repeated values, the last value is used
is_deeply(
$data,
{ a => 3, b => 4 },
);
# But hashes within the structure are tied to Tie::Hash::MultiValueOrdered
is_deeply(
[ tied(%$data)->get('b') ],
[ 2, 4 ],
);
# And the extra information from the tied hash is used when re-encoding
is(
$j->encode($data),
q({"a":1,"b":2,"a":3,"b":4}),
);
done_testing;
=head1 DESCRIPTION
The JSON specification allows keys to be repeated within objects. It remains
silent on how repeated keys should be interpreted. Most JSON implementations
end up choosing just one of the values; sometimes the first, sometimes the
last.
JSON::MultiValueOrdered is a subclass of L<JSON::Tiny> which treats objects as
ordered lists of key-value pairs, with duplicate keys allowed. It achieves this
by returning all hashes as tied using L<Tie::Hash::MultiValueOrdered>. While
these hashes behave like standard Perl hashes (albeit while preserving the
original order of the keys), they provide a tied object interface allowing you
to retrieve additional values for each key.
JSON::MultiValueOrdered serialisation also serialises these additional values
and preserves order.
JSON::MultiValueOrdered is a subclass of L<JSON::Tiny::Subclassable> and
L<JSON::Tiny>, which is itself a fork of L<Mojo::JSON>. Except where noted,
the methods listed below behave identically to the methods of the same names
in the superclasses.
=head2 Constructor
=over
=item C<< new(%attributes) >>
=back
=head2 Attributes
=over
=item C<< pretty >>
=item C<< error >>
=back
=head2 Methods
=over
=item C<< decode($bytes) >>
=item C<< encode($ref) >>
=item C<< false >>
=item C<< true >>
=back
=head2 Functions
=over
=item C<< j(\@array) >> / C<< j(\%hash) >> / C<< j($bytes) >>
Encode or decode JSON as applicable.
This function may be exported, but is not exported by default. You may
request to import it with a different name:
use JSON::MultiValueOrdered j => { -as => 'quick_json' };
=back
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=JSON-MultiValueOrdered>.
=head1 SEE ALSO
L<JSON::Tiny::Subclassable>,
L<JSON::Tiny>,
L<Mojo::JSON>.
L<Tie::Hash::MultiValueOrdered>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2012-2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|