JSON::...Ordered(3pm)

πŸ“¦
version 0.006 Β· 2 versions
  • 0.006 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.006 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ 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

  • pretty
  • error

πŸ’œ Methods

  • decode($bytes)
  • encode($ref)
  • false
  • true

🌸 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>.


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