I think I found an inconsistency between JSON::XS' and JSON's to_json routine: JSON::XS produces utf-8 which can be utf8::decode()ed, while JSON does not.
Now I'm puzzled, as I don't see what I'm doing wrong. (Is JSON::XS fixing something automagically, or getting corner-cases right, and JSON is not?). Demo script follows:
#!/usr/bin/env perl # http://www.perl.com/pub/2012/04/perlunicook-standard-preamble.html use utf8; # so literals and identifiers can be in UTF-8 use v5.12; # or later to get "unicode_strings" feature use strict; # quote strings, declare variables use warnings; # on by default use warnings qw(FATAL utf8); # fatalize encoding glitches use open qw(:std :utf8); # undeclared streams in UTF-8 use charnames qw(:full :short); # unneeded in v5.16 use Dancer; use Encode (); use HTML::Entities (); use JSON::XS (); use Devel::Peek; use utf8; our $string = "Hello &ndash; World"; get '/' => sub { return '<a href="/ok">ok</a> <a href="/crash">crash</a>'; }; get '/ok' => sub { my $perl_internal = Encode::decode('utf-8', $string); # "use ut +f8;" above made $string be in utf-8, decode to Perl-internal (= Unico +de, stored internally as utf-8, right?) HTML::Entities::decode_entities($perl_internal); # in void cont +ext, expand entities in-place to Unicode my $json = JSON::XS::encode_json([$perl_internal]); # pass a Pe +rl data structure, and expect output to be a utf-8 encoded, binary st +ring Dump( $perl_internal ); Dump( $json ); my $ok = utf8::decode($json); # fairly good way to +validate utf-8 return "JSON:$json <br>". ($ok ? "ok" : "not ok"); }; get '/crash' => sub { my $perl_internal = Encode::decode('utf-8', $string); HTML::Entities::decode_entities($perl_internal); my $json = to_json([$perl_internal],{pretty => 0}); # JSON's to +_json, in contrary to JSON::XS's to_json seems to do something wrong, Dump( $perl_internal ); Dump( $json ); # or is less clever in automagically h +andling what I'm doing wrong while preparing the scalar my $ok = utf8::decode($json); return "JSON:$json <br>". ($ok ? "ok" : "not ok"); }; dance;
[download]
$ perl -v
This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi

JSON::XS: 2.34
JSON: 2.61, now updated to 2.90 - no change
... both a little outdated

In reply to JSON::XS produces valid utf-8, and JSON doesn't - why? by isync

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.