Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Converting boolean values in XMLout

by Anonymous Monk
on Jan 26, 2012 at 11:57 UTC ( #950077=note: print w/ replies, xml ) Need Help??


in reply to Re: Converting boolean values in XMLout
in thread Converting boolean values in XMLout

In case it's useful to someone out there, I found that Rmap as used in pKai's sample code, didn't recurse as I wanted it to, for some Json strings anyway.

For instance, if you have a nested json object where some of the values in an object are the same (e.g. a list of keys with boolean values), decode_json uses JSON::true or JSON::false for the first instance it comes across, but then adds hash references to an existing true or false for all subsequent instances. This is presumably for performance? Whatever the reason, it caused me a bit of chin-scratching (not hard!).

Consider the following string of Json, (which might have been received as a post parameter, say):

{ "a": "x", "b": { "c":true, "d":false, "e":true, "f":false } }

If I tried this in pKai's code above, you don't get the required result - XML::Simple still complains:

$VAR1 = { 'a' => 'x', 'b' => { 'e' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' + ), 'c' => $VAR1->{'b'}{'e'}, 'd' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' + ), 'f' => $VAR1->{'b'}{'d'} } }; $VAR1 = { 'a' => 'x', 'b' => { 'e' => 'true', 'c' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' + ), 'd' => 'false', 'f' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' + ) } }; Can't encode a value of type: JSON::XS::Boolean at test2.pl line 13

The Rmap docs state that if there are multiple routes to the same node, only the first will be followed... so I'm pretty sure this is working as designed, but it's just not what I needed...

One way round it was just to repeat the same rmap_ref call against the object until there weren't any blessed objects left, but that seems dumb - it can't be very efficient? But it does work:

use strict; use warnings; use JSON; use XML::Simple; use Data::Rmap qw(rmap_ref); use Data::Dumper; my $text = '{"a":"x","b":{"c":true,"d":false,"e":true,"f":false}}'; my $result = decode_json($text); print Dumper($result); rmap_ref { $_ = "$_" if JSON::is_bool($_) } $result; print Dumper($result); rmap_ref { $_ = "$_" if JSON::is_bool($_) } $result; print Dumper($result); my $rec = XMLout( $result, RootName => 'root', SuppressEmpty => 1); print Dumper($rec);
$VAR1 = { 'a' => 'x', 'b' => { 'e' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' + ), 'c' => $VAR1->{'b'}{'e'}, 'd' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' + ), 'f' => $VAR1->{'b'}{'d'} } }; $VAR1 = { 'a' => 'x', 'b' => { 'e' => 'true', 'c' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' + ), 'd' => 'false', 'f' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' + ) } }; $VAR1 = { 'a' => 'x', 'b' => { 'e' => 'true', 'c' => 'true', 'd' => 'false', 'f' => 'false' } }; $VAR1 = '<root a="x"> <b c="true" d="false" e="true" f="false" /> </root> ';

In the end, I used something similar to ikegami's code above to make sure I walk a nested structure fully, but instead of using 1 or 0 to substitute the blessed JSON booleans, I used references, so that I could 'round trip' the resulting object using encode_json and get proper json booleans at the client (rather than the integers 1 or 0).

elsif (JSON::XS::is_bool($node)) { #$node += 0; # this results in 1 or 0 #$node = "$node"; # this results in "true" or "false" $node = ($node) ? \1 : \0; # this results in \1 or \0 (whic +h are mapped to Json true and false by encode_json...) }

There's probably a neat way to use Rmap to do the walking, but it was beyond my abilities I'm afraid. :(


Comment on Re^2: Converting boolean values in XMLout
Select or Download Code
Re^3: Converting boolean values in XMLout
by ikegami (Pope) on Jan 31, 2012 at 09:50 UTC

    decode_json uses JSON::true or JSON::false for the first instance it comes across, but then adds hash references to an existing true or false for all subsequent instances.

    No, not quite.

    When JSON::XS is loaded, it creates two objects

    $JSON::XS::true = do { bless \(my $dummy = 1), "JSON::XS::Boolean" }; $JSON::XS::false = do { bless \(my $dummy = 0), "JSON::XS::Boolean" };

    No other instances are ever created. No references to those are created. Those two objects are used everywhere.

    There's probably a neat way to use Rmap to do the walking, but it was beyond my abilities I'm afraid. :(

    It's refusing to visit the same reference twice. You could probably mess with its seen method (which appears to be a way at peeking at the module's internals).

      The exact code for perl to take into consideration multiple routes to the same node so that rmap_ref need not be called multiple times would be

      rmap_ref { $_[0]{seen} = {}; $_ = "$_" if JSON::is_bool($_) } $result;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2014-09-20 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls