ron.savage has asked for the wisdom of the Perl Monks concerning the following question:
1) Versions: Postgres V 8.4.12. Perl V 5.14.2. DBI V 1.622. DBD::Pg V 2.19.2. DBIx::Class V 0.08196. JSON::XS V 2.32. JSON::Syck not tried. Plack V 0.9988. YUI V 3.5.1.
2) Postgres database creation command line:
psql=# create database novels owner ron encoding 'UTF8';
3) CSV file (606 lines, 1 sample):
"author","category","title","rating","comment","isbn","publisher","p +ublication_date","review_date" "Colm Tóibín","Novel","The South","***","-","-","Picador","-","2012- +06-12"
4) Importing from that CSV file and exporting to Postgres:
Note: Encode qw/decode encode/ not used.use feature 'unicode_strings'; use open qw/:std :utf8/; ... # DBIx::Class: my($rs) = $schema -> resultset('Author'); my($result); for (sort keys %$data) { $result = $rs -> create({name => $_, upper_name => uc $_}); }
5) Postgres search command line:
So far, so good.novels=# select * from authors where name like 'Colm%'; id | name | upper_name -----+-------------+------------- 100 | Colm Tóibín | COLM TóIBíN (1 row)
6) Perl command line test script to read db:
use feature qw/say unicode_strings/; use open qw/:std :utf8/; use Encode qw/decode encode/; ... my($row) = $sth -> fetchall_hashref('id'); my($name) = $$row{100}{name}; my($decode) = decode('utf8', $name); my($json) = JSON::XS -> new -> utf8(0) -> encode({name => $decode} +); say "name: $name."; say "decode: $decode."; say "json: $json.";
7) Output of (6):
So far, so good.ron@zigzag:~/perl.modules/Local-Novels$ perl scripts/test.utf8.pl name: Colm TóibÃn. decode: Colm Tóibín. json: {"name":"Colm Tóibín"}.
8) Conclusion (after trying many combinations :-(): I need JSON's utf8(0) and encode() acting on the decoded database field to get the expected JSON output: json: {"name":"Colm Tóibín"}.
9) But, if I use decode('utf8', ...) and utf8(0) for an AJAX call under Plack here's what happens. The HTML page contains:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" / +>
Now for the Perl:
use feature 'unicode_strings'; use Encode 'decode'; ... while (my $item = $rs -> next) { push @$result, { author_name => decode('utf8', $item -> author -> name), ... } } ... $output = {results => $result}; return JSON::XS -> new -> utf8(0) -> encode($output);
Plack reports: Body must be bytes and should not contain wide characters (UTF-8 strings) at...
If I keep Plack happy with:
The displayed value for author's name is: Colm TóibÃnreturn JSON::XS -> new -> utf8(1) -> encode($output);
What to do?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: utf8/yui/json/ajax/plack troubles
by Corion (Patriarch) on Jun 19, 2012 at 06:39 UTC | |
by ron.savage (Acolyte) on Jun 20, 2012 at 02:13 UTC | |
Re: utf8/yui/json/ajax/plack troubles
by Anonymous Monk on Jun 19, 2012 at 07:13 UTC | |
Re: utf8/yui/json/ajax/plack troubles
by Your Mother (Archbishop) on Jun 20, 2012 at 02:39 UTC | |
by ron.savage (Acolyte) on Jun 20, 2012 at 04:05 UTC |