khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
61 stars 30 forks source link

utf8-encoding json-output twice? #6

Closed chrisv closed 10 years ago

chrisv commented 10 years ago

Hi, I get the feeling that when serving json-data, Raisin encodes the outgoing data twice: once when serializing it via encode_json (Plugin/Format/JSON.pm) which as you know encodes it already into utf8 (see JSON docs), and again in Response.pm where you do $self->body(encode 'UTF-8', $body); (line 61).

This results in any non-latin1 characters like the letter ë (e-with-umlaut) to be output by Raisin as ë which is typical of a double-encoding of the letter as you can see from the UTF-8 encoding debugging chart here:

http://www.i18nqa.com/debug/utf8-debug.html

I tried commenting out line 61 in Response.pm but that leaves the response body with no content.

Am I doing something wrong or is Raisin, in fact, wrongly encoding twice into utf8?

Many thanks in advance! Chris.

khrt commented 10 years ago

Yes, it is true. I encode to the UTF-8 twice. Will fix it soon.

chrisv commented 10 years ago

Thanks! Very much appreciated.

chrisv commented 10 years ago

FYI: I changed line 61 in Raisin::Response from:

(61) $self->body(encode 'UTF-8', $body);

to:

(61) $self->body($body);

And this fixed it for me as I'm using JSON output only. It appears that non-JSON format-plugins don't do any utf8-encoding themselves, so it may be cleaner to leave Raisin::Response as is and fix Raisin::Plugin::Format::JSON instead, but I leave that up to you.

Best, Chris.