Skip to content

Instantly share code, notes, and snippets.

@fgabolde
Created November 7, 2012 10:50
Show Gist options
  • Save fgabolde/4030830 to your computer and use it in GitHub Desktop.
Save fgabolde/4030830 to your computer and use it in GitHub Desktop.
Solving JSON encoding issues when handling numbers
#!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