Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created June 24, 2016 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/3f3e47c1fb9dddd840f3f7e9a17cc53b to your computer and use it in GitHub Desktop.
Save brianmed/3f3e47c1fb9dddd840f3f7e9a17cc53b to your computer and use it in GitHub Desktop.
Attempt at pretty printing JSON in a Mojolicious::Lite app
use Mojolicious::Lite;
use Tie::IxHash;
use JSON::XS;
use strict;
use warnings;
helper pretty_json => sub {
my ($self, $ref) = @_;
my $json = JSON::XS->new;
$json->pretty(1);
return $json->encode( $ref );
};
get '/' => sub {
my $c = shift;
tie(my %j, 'Tie::IxHash');
$j{result} = { counter => "123" };
$j{number} = { num => '55' };
my $pretty_json = $c->pretty_json(\%j);
$c->render(text => $pretty_json);
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment