Skip Menu |

This queue is for tickets about the JSON CPAN distribution.

Report information
The Basics
Id: 62214
Status: resolved
Priority: 0/
Queue: JSON

People
Owner: Nobody in particular
Requestors: TFM [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: allow decoding to create custom blessed objects
I'm ingesting large JSON feeds containing duplicate keys within objects. Of course such duplicates are lost forever (ohno!) when the later values are assigned during parsing. I have tried to use the filter_json_object functionality to use a Tie::Hash::MultiValue object instead of a plain hashref, but by the time this callback is invoked it is too late and the dupes are gone. It would be useful to be able to specify an "object provider" callback for the JSON parser, something like sub make_multihash { tie my %mhash, 'Tie::Hash::MultiValue'; return \%mhash; } my $parsed = JSON->new ->object_provider(\&make_multihash) ->parse('{"foo":1, "foo":2, "bar":{"a":1, "b":2, "b":3}}'); and then both $parsed and $parsed->{bar} are actually blessed objects and the multiple values are saved (hoorah!). It would work in general for any object_provider callback that returned a tied hashref, e.g. Tie::Hash::Indexed, Tie::Hash::SortedHash, Tie::StoredOrderHash (ahem) What do you think?
Since JSON::PP 2.27104 or JSON::backportPP in JSON 2.50, You can implement that feature. ---- my $json = JSON::PP::Yours->new->object_constructor(sub { Tie::StoredOrderHash->new }); package JSON::PP::Yours; use parent qw( JSON::PP ); # or use base qw( JSON::PP ); use strict; BEGIN { local $^W; my $orig_object = \&JSON::PP::object; my $OBJECT_CONSTRUCTOR; sub object_constructor { $OBJECT_CONSTRUCTOR = $_[1]; $_[0]; } eval q{ sub JSON::PP::object { $orig_object->( $OBJECT_CONSTRUCTOR->() ); } }; }