Dear Russel, please see additional information below. I really appreciate your help.


On Sep 27, 2015, at 10:41 PM, Russell Jenkins <russell.jenkins@strategicdata.com.au> wrote:

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.


It doesn’t "just work” however it seems. If this was true, shouldn’t the YAML serializer be having the same issue? Whatever my database is returning works perfectly fine with the YAML serializer, but not the JSON serializer.

Here is my config.yml


appname: "BeaconTrackerAPI"
layout: "main"
charset: "UTF-8"
serializer: "JSON"
engines:
    serializer:
        JSON:
            pretty: 1
            allow_blessed: 1
            canonical: 0
#            ascii: 1
#utf8: 1

template: "simple"
plugins:
    Database:
        driver: 'Pg'
        database: ‘***************'
        host: 'localhost'
        port: 5432
        username: ‘*******'
        password: '********'
        connection_check_threshold: 10
        on_connect_do: ["SET NAMES 'utf8'"]


Here is the method used in both the JSON and YAML tests:

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 });
        #the following is broken in Dancer2 serializer
        return \%{$sth->fetchall_hashref('id')};
};


Here is the output to the browser (broken):

{
   "1" : {
      "client_id" : "1123",
      "name" : "WANSecurity, K.K.",
      "address" : "東京都渋谷区上原2−48−12 とよ代々木上原コーポ#201",
      "id" : "1"
   }
}



Here is output from my terminal via psql (Looks good):

beacon_tracker=> select * from establishments;
 id |       name        |                          address                          | client_id
----+-------------------+-----------------------------------------------------------+-----------
  1 | WANSecurity, K.K. | 東京都渋谷区上原2−48 | 1123



Is this the expected behavior?


Here is the output from YAML when using the YAML serializer (Looks good):

---
1:
  address: 東京都渋谷区上原2−48
  client_id: 1123
  id: 1
  name: 'WANSecurity, K.K.'


YAML looks exactly like what I would expect to see, right out of the box. Something strange is happening with the JSON serializer it seems.


Am I missing anything?




-Robert





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,
 Russell.

_______________________________________________
dancer-users mailing list
dancer-users@dancer.pm
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users