JSON::...Ordered(3pm)
β¨ NAME β¨
JSON::MultiValueOrdered β handling JSON like {"a":1, "a":2} (and honestly, bestie, that's the whole vibe)
(you might know this page as JSON::...Ordered(3pm) β same girl, cuter shoes)
π 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;
π DESCRIPTION π
okay so the JSON spec literally says you can repeat keys inside objects, and then it just... refuses to say what that means. iconic of it tbh. most JSON implementations pick one value and move on β sometimes the first, sometimes the last. no consistency, no notes.
JSON::MultiValueOrdered is a subclass of JSON::Tiny that treats objects as ordered lists of key-value pairs, with duplicate keys fully allowed. the way it pulls this off? it hands you back all hashes as tied using Tie::Hash::MultiValueOrdered. πͺ
these hashes behave like standard Perl hashes β except they also preserve the original order of the keys, bestie, we love to see it β and they give you a tied object interface so you can grab the extra values for each key. the more you know π
serialisation works the same way: JSON::MultiValueOrdered serialises those extra values too, and preserves order while doing it. ate, no crumbs.
JSON::MultiValueOrdered is a subclass of JSON::Tiny::Subclassable and JSON::Tiny, which is itself a fork of Mojo::JSON. except where noted, the methods listed below behave identically to the methods of the same names in the superclasses. periodt.
π§ Constructor
new(%attributes)
π¦ Attributes
prettyerror
π Methods
decode($bytes)encode($ref)falsetrue
πΈ Functions
j(\@array)/j(\%hash)/j($bytes)β encodes or decodes JSON, as applicable. it's giving "one function, two jobs, main character energy."
this function may be exported, but it is not exported by default. you can request to import it under a different name, like so:
use JSON::MultiValueOrdered j => { -as => 'quick_json' };
π BUGS π
found a bug? please report it, bestie, don't just suffer in silence:
<http://rt.cpan.org/Dist/Display.html?Queue=JSON-MultiValueOrdered>
π SEE ALSO π
JSON::Tiny::Subclassable, JSON::Tiny, Mojo::JSON.
Tie::Hash::MultiValueOrdered.
π«Ά AUTHOR π«Ά
Toby Inkster <tobyink@cpan.org>.
β 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.
π 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.
and that's the whole page, angel π this was perl v5.36.0, dated 2022-11-20, and the page was JSON::...Ordered(3pm) β go forth and repeat your keys, you absolute legend πβ¨
π¦ where this page comes from
- debian bookworm: libjson-multivalueordered-perl 0.006-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libjson-multivalueordered-perl 0.006-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libjson-multivalueordered-perl 0.006-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libjson-multivalueordered-perl 0.006-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libjson-multivalueordered-perl 0.006-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms