logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

JSON::MultiValueOrdered - handle JSON like {"a":1, "a":2}

Author

       Toby Inkster <tobyink@cpan.org>.

Bugs

       Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=JSON-MultiValueOrdered>.

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

   Constructor
       "new(%attributes)"

   Attributes
       "pretty"
       "error"

   Methods
       "decode($bytes)"
       "encode($ref)"
       "false"
       "true"

   Functions
       "j(\@array)" / "j(\%hash)" / "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' };

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.

perl v5.36.0                                       2022-11-20                       JSON::MultiValueOrdered(3pm)

Name

       JSON::MultiValueOrdered - handle JSON like {"a":1, "a":2}

See Also

       JSON::Tiny::Subclassable, JSON::Tiny, Mojo::JSON.

       Tie::Hash::MultiValueOrdered.

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;

See Also