[dancer-users] Returning JSON Data
Robert Smith
spamfree at wansecurity.com
Sun Sep 27 02:54:03 BST 2015
JSON is enabled in config.yml
serializer: "JSON"
engines:
serializer:
JSON:
pretty: 1
allow_blessed: 1
canonical: 0
utf8: 1
YAML is working fine as a serializer using the the following method:
JSON prints mojibake when using the serializer using the following method:
get '/establishments' => sub {
my $sql = 'select * from establishments';
my $sth = database->prepare($sql) or die database->errstr;
$sth->execute or die $sth->errstr;
#the following is broken in Dancer2
return \%{$sth->fetchall_hashref('id')};
};
JSON is disabled in config.yml
#serializer: "JSON"
engines:
serializer:
JSON:
pretty: 1
allow_blessed: 1
canonical: 0
utf8: 1
returns fine when disabling the serializer JSON in the config.yml file.
get '/establishments' => sub {
my $sql = 'select * from establishments';
my $sth = database->prepare($sql) or die database->errstr;
$sth->execute or die $sth->errstr;
return to_json(\%{$sth->fetchall_hashref('id')}, { utf8 => 0 });
};
My question is, is this still the expected behavior due to issue #686 still being open?
Can I contribute monetarily to help get this issue fixed ASAP?
-Robert
> On Sep 27, 2015, at 5:05 AM, Robert Smith <spamfree at wansecurity.com> wrote:
>
> It seems that there is still a problem with Dancer2 serializing JSON output and double encoding UTF-8 data.
>
> I’ve thoroughly read:
>
> https://github.com/PerlDancer/Dancer2/issues/686
>
>
> https://github.com/PerlDancer/Dancer2/pull/520
>
> Setting the options in 520 results in: "Body must be bytes and should not contain wide characters (UTF-8 strings)” error
>
> https://github.com/PerlDancer/Dancer2/pull/647
>
>
> I guess that since #686 is still open that there is no work around for those of us who need to use serializer: JSON and have UTF-8 encoded data coming from our database?
>
> -Robert
>
>
>> On Sep 14, 2015, at 4:19 PM, Kadir Beyazlı <kadirbeyazli at gmail.com> wrote:
>>
>> Hi Russel,
>>
>>
>>
>> On Mon, Sep 14, 2015 at 2:19 AM, Russell Jenkins
>> <russell.jenkins at strategicdata.com.au> wrote:
>>> On 14/09/2015 3:56 am, Kadir Beyazlı wrote:
>>>
>>> Now my only problem is encoding JSON data.
>>> My data is Turkish, I was setting charset as follow to display correcty
>>>
>>> print $q->header(-content_type => "application/json; charset='utf8');
>>>
>>> How will I do it at Dancer2?
>>>
>>>
>>> Hey Kadir.
>>>
>>> You do no need to specify your JSON content as being UTF-8.
>>>
>>> RFC4627 states:
>>> "JSON text SHALL be encoded in Unicode. The default encoding is UTF-8"
>>> and goes on to explain how to determine the encoding of the octet stream.
>> [KB] I did not specify UTF-8, but my display problem for Turkish
>> characters is still going on.
>> I am returning JSON data from file myproject.pm and using JSON data at
>> file report.js.
>> My files (myproject.pm and report.js) character sets are also UTF-8.
>> At old method, when I set content to UTF-8, it was solved
>>
>>
>>> Furthermore, the IANA assignment for 'application/json' includes the
>>> following note:
>>> "No "charset" parameter is defined for this registration. Adding one really
>>> has no effect on compliant recipients."
>>>
>>> Hope that helps,
>>> Russell.
>>>
>>>
>>> _______________________________________________
>>> dancer-users mailing list
>>> dancer-users at dancer.pm
>>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>>>
>>
>>
>>
>> --
>> Kadir Beyazlı
>> Computer Engineer
>> GSM : +90 535 821 50 00
>> _______________________________________________
>> dancer-users mailing list
>> dancer-users at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
More information about the dancer-users
mailing list