makamaka / JSON

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

JSON::PP::_looks_like_number is fail. #35

Closed sugitach closed 6 years ago

sugitach commented 6 years ago

The following code will fail.

perl -MJSON -e '$a={a=>10, b=>20}; $a->{a} eq "" ; print encode_json($a), "\n";'

->

{"a":"10","b":20}

Data::Dumper is not fail.

perl -MData::Dumper -e '$a={a=>10, b=>20}; $a->{a} eq "" ; print Dumper($a), "\n";'
$VAR1 = {
      'a' => 10,
      'b' => 20
    };

Even if it is not "eq", if you execute the string operation in general, the output changes without assigning it.

sugitach commented 6 years ago

JSON::XS is the same, too.

charsbar commented 6 years ago

@sugitach

  1. If you have installed JSON::XS, then JSON usually uses it instead of JSON::PP. If you think this is a JSON::PP issue as you've said in the issue title, use it explicitly for clarity. ( perl -MJSON::PP .... )

  2. And you must have been using an old version of JSON::PP (like 2.27xxx) if you see the same result. Upgrade it to the latest version (2.97001) if you can. You'll see a different result.

  3. If you stick to JSON::XS (and older versions of JSON::PP), then this issue is your problem, not ours: read the JSON::XS documentation. It clearly explains how to specify the type. ( https://metacpan.org/pod/JSON::XS#PERL-%3E-JSON ) Because you've done explicit stringification in your code, it's natural (at least to the JSON::XS author) that JSON(::XS) returns a stringified value. This might be hard to understand at first, but it certainly is a consistent way when you need to handle more subtle values.

  4. You migth want to use JSON::Types ( https://metacpan.org/pod/JSON::Types ) or JSON::Typist ( https://metacpan.org/pod/JSON::Typist ) to mitigate this issue.

  5. You might also want to use Cpanel::JSON::XS, or JSON::Tiny/Mojo::JSON, all of which don't have this issue (though they might have their own issues; that's another story).

sugitach commented 6 years ago

@charsbar Thank you for your reply.

The following three points were confirmed.

  1. JSON::PP 2.97001 has no probrem.
  2. JSON::XS has this issue, but it is a just specification.
  3. Cpanel::JSON::XS has no probrem.

Then I selected Cpanel::JSON::XS. Thanks again.