mojolicious / mojo

:sparkles: Mojolicious - Perl real-time web framework
https://mojolicious.org
Artistic License 2.0
2.66k stars 577 forks source link

Inconsistent encoding of Mojo::ByteStream with MOJO_NO_JSON_XS #2034

Closed jixam closed 1 year ago

jixam commented 1 year ago

Steps to reproduce the behavior

$ cat json.pl
use v5.36;

use Mojo::JSON;
use Mojo::ByteStream;

say Mojo::JSON::encode_json Mojo::ByteStream->new("\x{2764}");
$ MOJO_NO_JSON_XS=1 perl json.pl
"❤"
$ MOJO_NO_JSON_XS=0 perl json.pl
"❤"

Expected behavior

I expected the output to be unaffected by the MOJO_NO_JSON_XS setting.

Actual behavior

Double UTF-8 encoding of Mojo::ByteStream with MOJO_NO_JSON_XS=0.

jixam commented 1 year ago

Here is a small application that shows how this is a problem for us: JSON built from a template is garbled unless we set MOJO_NO_JSON_XS=1.

use Mojolicious::Lite -signatures;

get '/smile.json' => sub ($c) {
    my $smile = $c->render_to_string('smile', format => 'text');
    $c->render( json => { smile => $smile } );
};

app->start;

__DATA__

@@ smile.text.ep
<%= "\x{1f603}" %>

I have traced the problem into Cpanel::JSON::XS: https://github.com/rurban/Cpanel-JSON-XS/issues/210

It can be worked around in Mojo::ByteStream by adding a TO_JSON method:

sub TO_JSON { shift->to_string }

... but I guess you prefer waiting for an upstream fix?

rurban commented 1 year ago

upstream fix coming today...

rurban commented 1 year ago

Cpanel::JSON::XS Update released. Should work now

jixam commented 1 year ago

Verified working with Cpanel::JSON::XS 4.35.