in reply to Need same JSON response from Mojo & CGI::App

Please try to pass the same data structure to both, if you want the same result.

The first one is a string, the second a (malformed) hash.

I'd say { Hello => 1} does the same.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Need same JSON response from Mojo & CGI::App
by Anonymous Monk on Jan 04, 2017 at 11:18 UTC
    With that I get hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) returned
      You haven't told us which result you need on the receiving side.

      Only "hello" ?

      Since JSON is an (JS) Object Notation I'm not sure if blank strings (i.e. nonref) are legal.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      my mistake, I get the following {"Hello":1}

        It seems that CGI::Application itself doesn't declare a ->json_body method, but judging from the error message you get, you need to configure the JSON generator of that module to allow non-reference things to be output as JSON (strings).

        The error message most likely comes from JSON::XS, so $whatever->json_generator->allow_nonref() maybe is the correct method call.

        Looking at JSON CGI::Application, I see CGI::Application::Plugin::JSON, which uses JSON::Any, which doesn't really have a way to specify configuration to whatever module it instantiates. There is a hint of setting $ENV{JSON_ANY_CONFIG} in the code, which might work for you, because the plugin itself does not provide any way to give it a preconfigured JSON encoder. Bad plugin.

        How about
        $c->render( json => { 'Hello' => undef } );