utf8 bug

89 views
Skip to first unread message

xolo

unread,
May 10, 2012, 3:14:37 AM5/10/12
to Mojolicious
hi,

i encountered a problem when serializing utf8 data with Mojo::JSON.
when using JSON::XS everything works fine.

example: <http://pastebin.com/UXQBpUb8>

xolo

bvn13

unread,
May 10, 2012, 3:35:42 AM5/10/12
to mojol...@googlegroups.com
I solve this problem with Mojo::HTML with using:

use encoding 'utf8';

in the header of utf8-encoded source.

Try it.

четверг, 10 мая 2012 г., 11:14:37 UTC+4 пользователь xolo написал:

xolo

unread,
May 10, 2012, 3:48:03 AM5/10/12
to mojol...@googlegroups.com
 
use encoding 'utf8';

in the header of utf8-encoded source.

Try it.

sorry... but this does not work. as far is i understand "use utf8;" does the same.

$self->app->log->info($test1->[0]);

shows the correct output... so it seems to be ok. just Mojo::JSON seems to have a problem.

thanks, anyway!

Sebastian Riedel

unread,
May 10, 2012, 4:30:10 AM5/10/12
to mojol...@googlegroups.com
> just Mojo::JSON seems to have a problem.


Not a bug, templates work with characters and encoded JSON is bytes.

--
Sebastian Riedel
http://twitter.com/kraih
http://mojolicio.us



xolo

unread,
May 10, 2012, 4:45:57 AM5/10/12
to mojol...@googlegroups.com


On Thursday, May 10, 2012 10:30:10 AM UTC+2, sri wrote:
> just Mojo::JSON seems to have a problem.


Not a bug, templates work with characters and encoded JSON is bytes. 

ok, but what do i have to write when i want to embed an utf8 containing json-string in an html template?

changing my example to...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use Mojo::ByteStream qw(b);
...
$self->stash( test => b(Mojo::JSON->new->encode($test)));
...
__DATA__

@@test.html.ep
<%= $test %>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

does not work neither

xolo :-(

sri

unread,
May 10, 2012, 6:36:35 AM5/10/12
to mojol...@googlegroups.com
ok, but what do i have to write when i want to embed an utf8 containing json-string in an html template?

Just turn the bytes back into characters.

--
sebastian

xolo

unread,
May 10, 2012, 7:23:27 AM5/10/12
to mojol...@googlegroups.com
Just turn the bytes back into characters.

???, i think i'll continue to use JSON::XS. this works fine for me.

thanks anyway... 

xolo

sri

unread,
May 10, 2012, 7:33:12 AM5/10/12
to mojol...@googlegroups.com
???, i think i'll continue to use JSON::XS. this works fine for me.


--
sebastian

xolo

unread,
May 10, 2012, 8:48:08 AM5/10/12
to mojol...@googlegroups.com


On Thursday, May 10, 2012 10:30:10 AM UTC+2, sri wrote:
> just Mojo::JSON seems to have a problem.


Not a bug, templates work with characters and encoded JSON is bytes. 

 rfc4627 says "3.  Encoding: JSON text SHALL be encoded in Unicode.  The default encoding is UTF-8"

so the root of my problem seems to be an implementation detail... JSON::XS uses UTF-8 as default encoding while Mojo::JSON delivers bytes.

xolo

ps: so i could write "decode_utf8(Mojo::JSON->new->encode($test))" or just use JSON::XS which default encoding makes my code shorter (and faster?!). ;-)

sri

unread,
May 10, 2012, 12:42:15 PM5/10/12
to mojol...@googlegroups.com
so the root of my problem seems to be an implementation detail... JSON::XS uses UTF-8 as default encoding while Mojo::JSON delivers bytes.

JSON::XS obviously does not generate UTF-8 but Perl characters, something can't be UTF-8 if it isn't bytes.

--
sebastian 
Reply all
Reply to author
Forward
Message has been deleted
0 new messages