Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to JSON::XS and unicode by kimmel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2014-12-28 13:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (181 votes), past polls