Created
November 7, 2012 10:50
-
-
Save fgabolde/4030830 to your computer and use it in GitHub Desktop.
Solving JSON encoding issues when handling numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use strict; | |
use warnings; | |
use 5.010; | |
use JSON; | |
my $number = 3.5; | |
my $string = 3.5; | |
# whoops: stringified a number | |
say "logging the string: $string"; | |
# outputs {"number":3.5,"string":"3.5"} | |
say encode_json({ number => $number, string => $string }); | |
# fix: numify the string back into a number | |
$string *= 1; | |
# outputs {"number":3.5,"string":3.5} | |
say encode_json({ number => $number, string => $string }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment