mojolicious / mojo

:sparkles: Mojolicious - Perl real-time web framework

Home Page:https://mojolicious.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent encoding of Mojo::ByteStream with MOJO_NO_JSON_XS

jixam opened this issue · comments

commented
  • Mojolicious version: 9.31
  • Perl version: 5.36
  • Operating system: Ubuntu 22.04 LTS

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.

commented

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: rurban/Cpanel-JSON-XS#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?

upstream fix coming today...

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

commented

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