Hello ,

I'm trying to use JSON::XS. The trouble is that I have French strings
with accented chars. The solutions I tried don't work. Also, I'm
not sure i understand JSON::XS doc on unicode and utf.

I use linux. The shell, terminal, and files all use utf-8.
Locales are installed, and I have LANG=fr_FR.UTF-8 and a bunch of
LC_THING=fr_FR.UTF-8 env variables. Apart from my script, everything
works perfectly well.

As you can see below, with "use utf8;", json is ok and can be
decoded by other progs, but perl strings turn to latin1 encoding.
with "no utf8" or without "use utf8", json strings are
«utf8 encoded twice» but simple perl strings are ok.

Ubuntu linux 12.04 with perl 5.14.2, and JSON::XS 2.320-1build1
if that matters

I must have missed something, hopefully simple. Any Idea ?

#! /usr/bin/perl
use utf8;
use strict;
use warnings;
use JSON::XS;

my %srchash = ( 'éléphant' => 'ça trompe' );
my $json = encode_json \%srchash;
my %dsthash = %{decode_json $json};
while ( my ($key, $value) = each %srchash ) {
print $key, ' => ', $value, "\n";
}
print $json, "\n";
while ( my ($key, $value) = each %dsthash ) {
print $key, ' => ', $value, "\n";
}

The results without "use utf8;" (or with "no utf8;") :
éléphant => ça trompe
{"éléphant":"ça trompe"}
éléphant => ça trompe

with "use utf8", here is what I get
�l�phant => �a trompe
{"éléphant":"ça trompe"}
�l�phant => �a trompe



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to