kimmel has asked for the wisdom of the Perl Monks concerning the following question:
I have run into what I believe is a bug when passing unicode text to decode_json. I am using JSON::XS 2.33 which is the latest version. I also checked the below programs and the source files I was using before with File and isutf8 (from moreutils) both of which report everything is proper UTF-8 unicode text. Here is the broken code:
#!/usr/bin/perl use v5.14; use warnings; use utf8::all; use JSON::XS qw( decode_json ); use Data::Dumper; my $wl = '{"creche": "crèche", "¥": "£", "₡": "волн" }'; my $pattern_list = decode_json( $wl ); print Dumper $pattern_list;
Generates the following message: 'Wide character in subroutine entry at ./micro_test.pl line 22.' Now I tried switching the decode_json call to my $pattern_list = JSON::XS->new->utf8->decode( $wl ); but I got the same error message. Now if I save that data to a file and then load the file I get no error message.
#!/usr/bin/perl use v5.14; use warnings; use utf8::all; use JSON::XS qw( decode_json ); use File::Slurp qw( read_file ); use Data::Dumper; my $wl = '{"creche": "crèche", "¥": "£", "₡": "волн" }'; open my $fh, '>', 'test_file2'; say {$fh} $wl; close $fh; my $pattern_list = decode_json( read_file('test_file2') ); print Dumper $pattern_list;
Is there a step missing from the first program that I am unaware of? I expected the first program to just work since the POD for JSON::XS states that decode_json expects UTF-8. I also tried JSON::PP and it gave me different errors.
#!/usr/bin/perl use v5.14; use warnings; use utf8::all; use JSON::PP qw( decode_json ); use File::Slurp qw( read_file ); use Data::Dumper; my $wl = '{"creche": "crèche", "¥": "£", "₡": "волн" }'; open my $fh, '>', 't2'; say {$fh} $wl; close $fh; my $pattern_list = decode_json( read_file('t2') ); print Dumper $pattern_list;
, or } expected while parsing object/hash, at character offset 21 (before "\n") at ./micro_test.pl line 28
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: JSON::XS and unicode
by chip (Curate) on Sep 08, 2012 at 20:57 UTC | |
by kimmel (Scribe) on Sep 08, 2012 at 23:01 UTC | |
by remiah (Hermit) on Sep 08, 2012 at 22:45 UTC | |
|
Re: JSON::XS and unicode
by philiprbrenan (Monk) on Sep 08, 2012 at 20:02 UTC | |
by kimmel (Scribe) on Sep 08, 2012 at 20:11 UTC | |
by chip (Curate) on Sep 08, 2012 at 21:01 UTC | |
by philiprbrenan (Monk) on Sep 09, 2012 at 11:04 UTC | |
|
Re: JSON::XS and unicode
by fluffyvoidwarrior (Monk) on Sep 10, 2012 at 10:52 UTC | |
by Corion (Patriarch) on Sep 10, 2012 at 10:59 UTC | |
by fluffyvoidwarrior (Monk) on Sep 10, 2012 at 11:24 UTC |