rovingeyes has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use JSON module (from CPAN) to encode & decode JSON data in my application. The encoding part is fine but when I'm trying to decode data the boolean values decode fine, albeit in a little weird way. Let me illustrate that with an example. I get this data back from my server as JSON:
{"value":[{"name":"THICNT","value":"1","secure":false,"path":"/","doma +in":"www.google.com","class":"org.openqa.selenium.internal.ReturnedCo +okie"},{"name":"NID","value":"34=pqQMsEqv3oAfy8-qWv-WEYBusldqUSoe_9Tg +iSoaN0itw7","secure":false,"path":"/","domain":".google.com","class": +"org.openqa.selenium.internal.ReturnedCookie"},{"name":"PREF","value" +:"ID=3f5070f587c10823:U=4e8ab4b3b6576160:TM=1271918988:LM=1271918988: +S=NZ9UTF7eCxicziqM","secure":false,"path":"/","domain":".google.com", +"class":"org.openqa.selenium.internal.ReturnedCookie"}]}
and when I decode it by JSON->new->allow_nonref(1)->utf8(1)->decode on the above stream, I get the following output:
$VAR1 = [ { 'domain' => 'www.google.com', 'value' => '1', 'secure' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ) +, 'name' => 'THICNT', 'class' => 'org.openqa.selenium.internal.ReturnedCookie', 'path' => '/' }, { 'domain' => '.google.com', 'value' => '34=pqQMsEqv3oAfy8-qWv WEYBusldqUSoe_9TgiSoaN0i +tw7', 'secure' => $VAR1->[0]{'secure'}, 'name' => 'NID', 'class' => 'org.openqa.selenium.internal.ReturnedCookie', 'path' => '/' }, { 'domain' => '.google.com', 'value' => 'ID=3f5070f587c10823:U=4e8ab4b3b6576160:TM=1271 +918988:LM=1271918988:S=NZ9UTF7eCxicziqM', 'secure' => $VAR1->[0]{'secure'}, 'name' => 'PREF', 'class' => 'org.openqa.selenium.internal.ReturnedCookie', 'path' => '/' } ];

As you can see it is an array of all the cookies, but that is irrelevant. What is relevant is that the 'secure' key's value is boolean. And the first occurrence of secure's value is an actual blessed reference & the remaining such occurrences are references to that first blessed reference.

So my question is simple - how can I invoke decode so that each boolean value is its own blessed reference rather than a reference to an already existing value? Or is that just by design & that is the only way JSON.pm decodes data?

Replies are listed 'Best First'.
Re: JSON module & decoding boolean data
by ikegami (Patriarch) on Apr 22, 2010 at 07:04 UTC

    Are you planning on changing the value of false? It's an immutable object. There's no reason to make copies of it.

      Actually I don't care for an object there at all. According to the module POD, upon decode true should become 1 & false should be represented as 0, which is what I'm looking for. So I guess what I want is instead of JSON::false, can I get 0 back upon decode.
        If you use these values (e.g. in print, if or mathematical calculation), these will work as 0 or 1

        UPDATE:

        # perl -MJSON::XS -e '$x = q/{"bool":true}/;$y=decode_json($x);print " +$y->{bool}\n"' 1