in reply to Re^3: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects (source)
in thread Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects

If I were you, I'd just use something much simpler like JSON::Tiny. And I'd file a patch request to have "my $TRUE" changed to "our $TRUE", etc.

Minimally, I've implemented this in JSON::Tiny version 0.33, uploaded to CPAN just a few minutes ago. my $TRUE... is now our $TRUE, so that users may explicitly override the return value for Booleans. The same goes for my $FALSE.

If there is sufficient call for it, I might also provide additional flexibility in _encode_value() by changing this line: return $value  ? 'true' : 'false' if $ref eq 'JSON::Tiny::_Bool'; to something like return $value  ? 'true' : 'false' if $ref eq $BOOL_CLASS; # A package global. Once we go too far down that road, however, people are going to start asking that $TRUE, $FALSE, and $BOOL_CLASS be implemented as object attributes for the JSON::Tiny object so that they can be manipulated on a per-instance basis... and that leads to becoming less '::Tiny'. ;)

For now just consider the following code:

use JSON::Tiny; $JSON::Tiny::FALSE = 0; $JSON::Tiny::TRUE = 1; my $j = JSON::Tiny->new(); # ... Booleans in JSON being decoded now return 0 and 1 instead of # JSON::Tiny::Bool objects

Update: Version 0.34 adds some tests and documentation on this advanced feature.


Dave

  • Comment on Re^4: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects (source)
  • Select or Download Code

Replies are listed 'Best First'.
Re^5: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects (\$)
by tye (Sage) on Oct 16, 2013 at 05:16 UTC

    Thanks!

    Since, for a boolean type, it is reasonable to implement it as a ref to a scalar (and that is how it has been done over and over in several different JSON modules), I'd just treat a reference to a scalar as a boolean. The only change required is using reftype() or $ref =~ /SCALAR/, as ref() returning exactly "SCALAR" is already handled that way.

    - tye        

      Version 0.35 now treats a reference to a scalar as a Boolean so that users can encode, for example \0 and \1 as false and true. Tests and documentation have also been updated.

      $j->encode( { a => \1, b => \0 } ) # '{"a":true, "b":false}'

      Blessed refs work too.


      Dave

Re^5: Making JSON::{PP,XS} not decode true/false to JSON::{PP,XS}::Boolean objects (source)
by Anonymous Monk on Oct 15, 2013 at 23:10 UTC

    Hmmm, what to do .....

    $JSON::Tiny::FALSE = \'0FALSE'; $JSON::Tiny::TRUE = \'1TRUE'; $JSON::Tiny::FALSE = !!0; $JSON::Tiny::TRUE = !!1; $JSON::Tiny::FALSE = Scalar::Util::dualvar( 0, '0false'); $JSON::Tiny::TRUE = Scalar::Util::dualvar( 1, '1true');