$ perl -v#!/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 – 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;
In reply to JSON::XS produces valid utf-8, and JSON doesn't - why? by isync
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |