-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow decoding of JSON messages in non-standard character sets #2093
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ use Mojo::JSON qw(j); | |
use Mojo::JSON::Pointer; | ||
use Mojo::Parameters; | ||
use Mojo::Upload; | ||
use Mojo::Util qw(decode); | ||
use Mojo::Util qw(encode decode); | ||
|
||
has content => sub { Mojo::Content::Single->new }; | ||
has default_charset => 'UTF-8'; | ||
|
@@ -132,7 +132,8 @@ sub is_limit_exceeded { !!shift->{limit} } | |
sub json { | ||
my ($self, $pointer) = @_; | ||
return undef if $self->content->is_multipart; | ||
my $data = $self->{json} //= j($self->body); | ||
my $charset = $self->body_params->charset || $self->default_charset; | ||
my $data = $self->{json} //= j($charset eq 'UTF-8' ? $self->body : encode('UTF-8', decode($charset, $self->body))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Tried this before: my $data = $charset eq 'UTF-8' ? j($self->body) : from_json(decode($charset,$self->body)); which seems more natural but breaks the last test in t/mojo/response.t subtest "Parse response and extract JSON data"
i.e. this one: is_deeply $res->json('/baz'), [1, 2, 3], 'right result';
$res->json->{baz}[1] = 4;
is_deeply $res->json('/baz'), [1, 4, 3], 'right result'; # <-- This one here
I might be missing something, so all suggestions are welcome. |
||
return $pointer ? Mojo::JSON::Pointer->new($data)->get($pointer) : $data; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use Mojo::Base -strict; | ||
|
||
BEGIN {$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll'} | ||
|
||
use Test::Mojo; | ||
use Test::More; | ||
use Mojo::ByteStream qw(b); | ||
use Mojolicious::Lite; | ||
|
||
my $ascii = 'abc'; | ||
my $yatta = 'やった'; | ||
my $yatta_sjis = b($yatta)->encode('shift_jis')->to_string; | ||
my $miyagawa = '宮川'; | ||
my $miyagawa_jp = b($miyagawa)->encode('euc-jp')->to_string; | ||
my $hola = "áèñ"; | ||
my $hola_latin1 = "\x{e1}\x{e8}\x{f1}"; | ||
|
||
get '/' => [ format => [ $yatta ] ] => { format => undef } => 'index'; | ||
|
||
post '/' => sub { | ||
my $c = shift; | ||
$c->render(text => "foo: " . $c->param('foo')); | ||
}; | ||
|
||
get '/ascii' => sub { | ||
my $c = shift; | ||
$c->render(json => { test => $ascii }) | ||
}; | ||
|
||
get '/unicode' => sub { | ||
my $c = shift; | ||
$c->render(json => { test => $yatta }) | ||
}; | ||
|
||
get '/shift_jis' => sub { | ||
my $c = shift; | ||
$c->res->headers->content_type("application/json;charset=Shift_JIS"); | ||
$c->render(data => qq({"test":"$yatta_sjis"})); | ||
}; | ||
|
||
get '/euc_jp' => sub { | ||
my $c = shift; | ||
$c->res->headers->content_type("application/json;charset=euc-jp"); | ||
$c->render(data => qq({"test":"$miyagawa_jp"})); | ||
}; | ||
|
||
get '/latin1' => sub { | ||
my $c = shift; | ||
$c->res->headers->content_type("application/json;charset=ISO-8859-1"); | ||
$c->stash(data => qq({"test":"$hola_latin1"})); | ||
}; | ||
|
||
my $t = Test::Mojo->new; | ||
|
||
$t->get_ok('/ascii')->status_is(200)->content_is('{"test":"abc"}'); | ||
$t->get_ok('/ascii')->status_is(200)->json_is('/test' => $ascii); | ||
$t->get_ok('/unicode')->status_is(200)->content_type_is('application/json;charset=UTF-8')->json_is('/test' => $yatta); | ||
$t->get_ok('/shift_jis')->status_is(200)->content_type_is('application/json;charset=Shift_JIS')->json_is('/test' => $yatta); | ||
$t->get_ok('/euc_jp')->status_is(200)->content_type_is('application/json;charset=euc-jp')->json_is('/test' => $miyagawa); | ||
$t->get_ok('/latin1')->status_is(200)->content_type_is('application/json;charset=ISO-8859-1')->json_is('/test' => $hola); | ||
|
||
|
||
done_testing(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what the performance cost of this is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right,
$self->content->charset
is the way to go.