Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Dancer2: config UTF8 for JSON serializer from code

by TheTailgunner (Initiate)
on Sep 15, 2017 at 09:25 UTC ( [id://1199452]=perlquestion: print w/replies, xml ) Need Help??

TheTailgunner has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I have a simple web-app with Dancer2, separated for two apps:

MyApp->to_app; builder { enable 'Deflater'; mount '/' => MyApp->to_app; mount '/json' => MyApp::Controller->to_app; }
one for serving content (pages etc) and one for handling JSON API;

When im trying to set up serializer engine from config.yml, like this:

serializer: "JSON" engines: serializer: JSON: pretty: 1 allow_blessed: 1 canonical: 0 utf8: 1
app serving pages ceases to work (probably, because it tries to use JSON-serializer).

All works with

set serializer => 'JSON';
in my controller app, but this way i lost ability to customize serialization settings (at least because i dont understand how). With your permission, I will not show examples of attempts to you - they are uncomplicated and uneffective. How can i customize config setting from code (or not from code) for my second app?

With best regards, Vlad.

Replies are listed 'Best First'.
Re: Dancer2: config UTF8 for JSON serializer from code
by 1nickt (Canon) on Sep 15, 2017 at 12:15 UTC

    Hi Vlad, you can customize the serializer in the application package by setting the engine config first, then the serializer option:

    # in MyApp::Controller use Dancer2; set engines => { serializer => { JSON => { pretty => 1, allow_blessed => 1, canonical => 0, utf8 => 1, }, }, }; set serializer => 'JSON'; get '/config' => sub { return config; };
    Output in browser when requesting /config is something like:
    { "apphandler" : "PSGI", # bunch of stuff snipped ... "engines" : { "serializer" : { "JSON" : { "utf8" : 1, "canonical" : 0, "allow_blessed" : 1, "pretty" : 1 } } }, "serializer" : null, "static_handler" : 1, "host" : "0.0.0.0" }
    Note that, oddly, the config shows the value of serializer as null, but as the format of the output shows, the settings are being implemented.

    Hope this helps!


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1199452]
Approved by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found