<?xml version="1.0" encoding="windows-1252"?>
<node id="992521" title="JSON::XS and unicode" created="2012-09-08 15:16:38" updated="2012-09-08 15:16:38">
<type id="115">
perlquestion</type>
<author id="910459">
kimmel</author>
<data>
<field name="doctext">
&lt;p&gt;
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:
&lt;/p&gt;
&lt;code&gt;
#!/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",
"¥": "£",
"&amp;#8353;": "&amp;#1074;&amp;#1086;&amp;#1083;&amp;#1085;"
}';

my $pattern_list = decode_json( $wl );
print Dumper $pattern_list;
&lt;/code&gt;
&lt;p&gt;Generates the following message: 'Wide character in subroutine entry at ./micro_test.pl line 22.' Now I tried switching the decode_json call to &lt;code&gt;my $pattern_list = JSON::XS-&gt;new-&gt;utf8-&gt;decode( $wl );&lt;/code&gt; 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.&lt;/p&gt;

&lt;code&gt;
#!/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",
"¥": "£",
"&amp;#8353;": "&amp;#1074;&amp;#1086;&amp;#1083;&amp;#1085;"
}';

open my $fh, '&gt;', 'test_file2';
say {$fh} $wl;
close $fh;

my $pattern_list = decode_json( read_file('test_file2') );
print Dumper $pattern_list;
&lt;/code&gt;

&lt;p&gt;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 &lt;a href="https://metacpan.org/module/JSON::XS"&gt;JSON::XS&lt;/a&gt; states that decode_json expects UTF-8. I also tried JSON::PP and it gave me different errors.&lt;/p&gt;

&lt;code&gt;
#!/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",
"¥": "£",
"&amp;#8353;": "&amp;#1074;&amp;#1086;&amp;#1083;&amp;#1085;"
}';

open my $fh, '&gt;', 't2';
say {$fh} $wl;
close $fh;

my $pattern_list = decode_json( read_file('t2') );
print Dumper $pattern_list;
&lt;/code&gt;

&lt;p&gt;, or } expected while parsing object/hash, at character offset 21 (before "\n") at ./micro_test.pl line 28&lt;/p&gt;</field>
</data>
</node>
