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:

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 $_}); }
Note: Encode qw/decode encode/ not used.

5) Postgres search command line:

novels=# select * from authors where name like 'Colm%'; id | name | upper_name -----+-------------+------------- 100 | Colm Tóibín | COLM TóIBíN (1 row)
So far, so good.

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):

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"}.
So far, so good.

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:

return JSON::XS -> new -> utf8(1) -> encode($output);
The displayed value for author's name is: Colm Tóibín

What to do?


In reply to utf8/yui/json/ajax/plack troubles by ron.savage

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.