isync has asked for the wisdom of the Perl Monks concerning the following question:
$ 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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: JSON::XS produces valid utf-8, and JSON doesn't - why?
by Anonymous Monk on Nov 12, 2015 at 23:54 UTC | |
|
Re: Is JSON::XS doing more things right here,
by Laurent_R (Canon) on Nov 12, 2015 at 18:49 UTC | |
by isync (Hermit) on Nov 12, 2015 at 19:23 UTC | |
by tye (Sage) on Nov 12, 2015 at 19:56 UTC | |
by isync (Hermit) on Nov 12, 2015 at 20:46 UTC | |
by tye (Sage) on Nov 12, 2015 at 23:10 UTC | |
|
Re: JSON::XS produces valid utf-8, and JSON doesn't - why?
by isync (Hermit) on Nov 13, 2015 at 01:45 UTC |