Consider this :)(error message, ignore the choice of bytes)

$ perl -MEncode -MJSON -e " print JSON->new->utf8(0)->pretty(1)->encod +e([ decode 'utf8', qq{\xFA\xFG} ]); " Wide character in print at -e line 1. [ "�\u000fG" ] $ perl -MEncode -MJSON -e " print JSON->new->ascii(1)->pretty(1)->enco +de([ decode 'utf8', qq{\xFA\xFG} ]); " [ "\ufffd\u000fG" ]

The error Body must be bytes and should not contain wide characters (UTF-8 strings) comes from Plack::Middleware::Lint, and I remember a similar message from LWP/HTTP::Message. I've learned from perlunitut: Unicode in Perl#I/O flow (the actual 5 minute tutorial) that this means you have to write

return encode 'UTF-8', JSON->...

I write JSON because JSON loads JSON::XS if its available, but the code doesn't break of JSON::XS isn't available for some reason :)

This is confirmed by PSGI::FAQ#I want to send Unicode content in the HTTP response. How can I do so?

I'm wouldn't be surprised if there exists a Plack::Middleware::Encoding or some such which automatically encodes your UTF-8 strings and adds a charset header, but I haven't seen one


In reply to Re: utf8/yui/json/ajax/plack troubles by Anonymous Monk
in thread 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.