makamaka / JSON

perl implementation of JSON encoder/decoder
42 stars 40 forks source link

$json->boolean_values documentation issue #45

Closed yahermann closed 3 years ago

yahermann commented 3 years ago

Doc states: $json->boolean_values([ $false, $true ]);

But this is incorrect. Correct call is: $json->boolean_values( $false, $true );

I also suggest including some indication the above call mutates $json. This is to differentiate from other methods that follow $json = $json->some_method( ... );. In fact, $json = $json->boolean_values( ... ); returns the values set, therefore destroying $json.

Test:

use JSON 4.01;

my $json = JSON->new;
$json->boolean_values( 0, 1 );
is( $json->true, 1 );
is( $json->false, 0 );

my $true = $json->decode( '{"value" : true }' );
is( ref($true->{'value'}, '' );
is( $true->{'value'}, 1 );

my $false = $json->decode( '{"value" : false }' );
is( ref($false->{'value'}, '' );
is( $false->{'value'}, 0 );
charsbar commented 3 years ago

You are misunderstanding the point of [ ] in this doc. This indicates that the values inside the brackets can be omitted. Note that the doc says Calling this method without any arguments will reset the booleans to their default values. See also other methods such as $json = $json->ascii ([$enable]) and $json = $json->filter_json_single_key_object ($key [=> $coderef->($value)]) and their actual usage.

The mutation of $json object is a compatibility bug in JSON::PP. Thanks for spotting this. I'll file this issue in its tracker. Closing here.

yahermann commented 3 years ago

Ah. In that case I suggest using something other than [ ] to indicate optional settings. As I'm sure you know, [ $false, $true ] is Perl syntax for an arrayref, very commonly used as a parameter in a function call, therefore easily misunderstood.

In my case, I couldn't get boolean_values to work because I was setting as $json->boolean_values([ 0, 1 ]);. It took digging into the source code to figure out why.