Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Last active Aug 29, 2015
Embed
What would you like to do?
JSON::PP with utf8 in perl 5.8.x
> PLENV_VERSION=5.8.4 perl -I$HOME/dev/cpanminus/fatlib ~/tmp/json-pp-utf8.t
1..3
# perl 5.008004
ok 1 - parse OK {"a": "å"}
ok 2 - parse OK {"a": 1, "b" : "å"}
not ok 3 - parse OK {"a": 0, "b" : "å"}
# Failed test (/Users/miyagawa/tmp/json-pp-utf8.t at line 8)
# , or } expected while parsing object/hash, at character offset 7 (before ", "b" : "\x{c3}\x{a5}...") at /Users/miyagawa/tmp/json-pp-utf8.t line 7
# Looks like you failed 1 tests of 3.
> PLENV_VERSION=5.8.5 perl -I$HOME/dev/cpanminus/fatlib ~/tmp/json-pp-utf8.t
1..3
# perl 5.008005
ok 1 - parse OK {"a": "å"}
ok 2 - parse OK {"a": 1, "b" : "å"}
not ok 3 - parse OK {"a": 0, "b" : "å"}
# Failed test (/Users/miyagawa/tmp/json-pp-utf8.t at line 8)
# , or } expected while parsing object/hash, at character offset 7 (before ", "b" : "\x{c3}\x{a5}...") at /Users/miyagawa/tmp/json-pp-utf8.t line 7
# Looks like you failed 1 tests of 3.
> PLENV_VERSION=5.8.8 perl -I$HOME/dev/cpanminus/fatlib ~/tmp/json-pp-utf8.t
1..3
# perl 5.008008
ok 1 - parse OK {"a": "å"}
ok 2 - parse OK {"a": 1, "b" : "å"}
ok 3 - parse OK {"a": 0, "b" : "å"}
use JSON::PP;
use Test::More tests => 3;
diag "perl $]";
sub parse_ok {
eval { JSON::PP->new->decode($_[0]) };
ok !$@, "parse OK $_[0]";
diag $@ if $@;
}
my $u = "\xc3\xa5"; # U+00e5 in utf-8
parse_ok qq({"a": "$u"});
parse_ok qq({"a": 1, "b" : "$u"});
parse_ok qq({"a": 0, "b" : "$u"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment