[dancer-users] Returning JSON Data

Russell Jenkins russell.jenkins at strategicdata.com.au
Sun Sep 27 14:41:14 BST 2015

On 27/09/2015 11:54 am, Robert Smith wrote:
> 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')};
> };

Provided you get characters (not octets) back from the database the 
above "just works".
If you are getting octets back they will get encoded again (with their 
default serializer params).
All Dancer2 serializers expect characters and return octets for adding 
to the response body.

Note the #686 is about a different issue; the to_json, to_yaml and 
to_dumper keywords.
Their implementation also returns octets, so when those keywords are 
used as the return
form a route the response is double encoded.

Hope that helps,

More information about the dancer-users mailing list