Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created April 10, 2014 11:09
Show Gist options
  • Select an option

  • Save memememomo/10369209 to your computer and use it in GitHub Desktop.

Select an option

Save memememomo/10369209 to your computer and use it in GitHub Desktop.
MojoliciousでJSON::XSを使う ref: http://qiita.com/uchiko/items/6dceff5360b31d9baea2
$ cpanm JSON::XS Mojo::JSON::XS
use Mojolicious::Lite;
use Mojo::JSON::XS;
use Mojo::ByteStream 'b';
app->renderer->add_handler(json => sub {
my ($self, $c, $output, $options) = @_;
$$output = Mojo::JSON::XS->new->encode($options->{json});
});
{
no strict 'refs';
*{"Mojo::ByteStream::TO_JSON"} = sub {
my $self = shift;
$self->to_string; # 自身を文字列に変換する
};
}
get '/' => sub {
my $self = shift;
$self->render(json => {name => b('太郎')});
};
app->start;
use Mojolicious::Lite;
use Mojo::JSON::XS;
...
app->renderer->add_handler(json => sub {
my ($self, $c, $output, $options) = @_;
$$output = Mojo::JSON::XS->new->encode($options->{json});
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment