CC: | "Daniel Coupal -X (dcoupal - TechOne at Cisco)" <dcoupal [...] cisco.com> |
Subject: | JSON 2.53 bug report - JSON should give priority to 'int' type over 'string' type |
Date: | Fri, 14 Dec 2012 23:21:18 +0000 |
To: | "bug-JSON [...] rt.cpan.org" <bug-JSON [...] rt.cpan.org> |
From: | "Daniel Coupal -X (dcoupal - TechOne at Cisco)" <dcoupal [...] cisco.com> |
The issue is reproducible with JSON 2.53
Basically, if you have a variable in the Object that you want to serialize to JSON that has at the same time a representation as a numeral (int for example) and as a string (which will be done if you print the value with %s), the JSON serializer will not consider the value as a numeral, but as a string.
It should the other way around. If it is a numeral, then you use this type.
See the example below where you serialize a hash, then print a member of the hash and serialize again. You get 2 different serialization results.
Thanks,
Daniel Coupal
=====================================
# Test to show issue in JSON.pm
# If a value is known as a 'int' and 'string' from Perl,
# JSON encoding should prefer the 'int' value.
use JSON 2.53;
use strict;
use warnings;
my $message = {
'key1' => 1
};
my $json_message1 = JSON::encode_json($message);
print("Known as int: $json_message1\n");
# Have Perl compute a string representation of the value
# Note that we are not changing the value
# At this point Perl has both an 'int' and 'string' representation
# of the value if you look at the SV falgs
my $fmt_message = sprintf("%s", $message->{'key1'});
my $json_message2 = JSON::encode_json($message);
print("Known as string and int: $json_message2\n");