Created
April 10, 2014 11:09
-
-
Save memememomo/10369209 to your computer and use it in GitHub Desktop.
MojoliciousでJSON::XSを使う ref: http://qiita.com/uchiko/items/6dceff5360b31d9baea2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ cpanm JSON::XS Mojo::JSON::XS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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