Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects

by davido (Archbishop)
on Oct 14, 2013 at 16:54 UTC ( #1058200=note: print w/ replies, xml ) Need Help??


in reply to Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects

You could probably construct something inside of a filter_json_object callback. But off the top of my head it seems like you would need to recurse into whatever $_[0] holds, looking for JSON::Boolean objects and converting them.


Dave


Comment on Re: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects
Download Code
Re^2: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects
by daxim (Chaplain) on Oct 14, 2013 at 17:22 UTC
      Data::Rmap is also an alternative (and is faster in this particular case). But they are both rather slow, so I created Data::Clean::JSON and Data::Clean::FromJSON which generate Perl code to do the cleansing and can be several times faster. A benchmark (from slowest to fastest):
      % perl -MBench -MStorable=dclone -MData::Dump -MJSON -MData::Visitor:: +Callback -E' $data=JSON->new->decode(q([null, true, false, 1, 2, 3, "a", "b", [], { +}])); dd $data; $v=Data::Visitor::Callback->new("JSON::PP::Boolean"=>sub{$_=$_?1:0}, " +JSON::XS::Boolean"=>sub{$_=$_?1:0}); bench sub { $data2=dclone($data); $v->visit($data2) }, -1; dd $data2' [ undef, bless(do{\(my $o = 1)}, "JSON::XS::Boolean"), bless(do{\(my $o = 0)}, "JSON::XS::Boolean"), 1, 2, 3, "a", "b", [], {}, ] 4846 calls (3328/s), 1.456s (0.301ms/call) [undef, 1, 0 .. 3, "a", "b", [], {}] % perl -MBench -MStorable=dclone -MData::Dump -MJSON -MData::Rmap=rmap +_ref -E' $data=JSON->new->decode(q([null, true, false, 1, 2, 3, "a", "b", [], { +}])); dd $data; bench sub { $data2=clone($data); rmap_ref { $_ = ref($_) eq "JSON::PP: +:Boolean" || ref($_) eq "JSON::XS::Boolean" ? ($_?1:0) : $_ } $data } +, -1; dd $data2' [ undef, bless(do{\(my $o = 1)}, "JSON::XS::Boolean"), bless(do{\(my $o = 0)}, "JSON::XS::Boolean"), 1, 2, 3, "a", "b", [], {}, ] 13573 calls (11735/s), 1.157s (0.0852ms/call) [undef, 1, 0 .. 3, "a", "b", [], {}] % perl -MBench -MStorable=dclone -MData::Dump -MJSON -MData::Clean::Fr +omJSON -E' $data=JSON->new->decode(q([null, true, false, 1, 2, 3, "a", "b", [], { +}])); dd $data; $cleanser=Data::Clean::FromJSON->new; bench sub { $data2=dclone($data); $cleanser->clean_in_place($data2) }, + -1; dd $data2' [ undef, bless(do{\(my $o = 1)}, "JSON::XS::Boolean"), bless(do{\(my $o = 0)}, "JSON::XS::Boolean"), 1, 2, 3, "a", "b", [], {}, ] 33781 calls (32790/s), 1.030s (0.0305ms/call) [undef, 1, 0 .. 3, "a", "b", [], {}]

        Man, I wish that I had known about that data-cleaner of yours a few months ago ...!

Re^2: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects
by sedusedan (Monk) on Oct 15, 2013 at 01:46 UTC
    I'm afraid not, filter_json_object will only be called for JSON object (hash). There's also filter_json_single_key_object which is even more selective (called for JSON objects having a single key with a specific name). There's no filter mechanism in JSON that calls a callback for a boolean value.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-12-27 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls