Mojo::JSON;
use Mojo::Util;
use Mojolicious;
has description => 'Show versions of available modules';
has usage => sub { shift->extract_usage };
sub run {
my $self = shift;
my $json = M
ojo::JSON->JSON_XS ? $Cpanel::JSON::XS::VERSION : 'n/a';
my $cryptx = Mojo::Util->CRYPTX ? $CryptX::VERSION : 'n/a';
my $ev = eval { require
, $^O)
Mojolicious ($Mojolicious::VERSION, $Mojolicious::CODENAME)
OPTIONAL
Cpanel::JSON::XS 4.09+ ($json)
CryptX 0.080+ ($cryptx)
EV 4.32+ ($ev)
IO::Socket::So
n->new('/user/:id', id => qr/\d+/);
my $pattern = Mojolicious::Routes::Pattern->new(format => ['json', 'yaml']);
Construct a new L<Mojolicious::Routes::Pattern> object and L</"parse"> pattern if ne
$pattern = $pattern->parse('/user/:id', id=> qr/\d+/);
$pattern = $pattern->parse(format => ['json', 'yaml']);
Parse pattern.
=head2 render
my $path = $pattern->render({id => 24});
my $path
:Command';
use Mojo::Collection qw(c);
use Mojo::DOM;
use Mojo::IOLoop;
use Mojo::JSON qw(to_json j);
use Mojo::JSON::Pointer;
use Mojo::URL;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode ge
d, $url, \%headers, @content));
my $res = $tx->result;
# JSON Pointer
return undef unless defined $selector;
return _json($buffer, $selector) if !length $selector || $selector =~ m!^/!;
}
sub _json {
return unless my $data = j(shift);
return unless defined($data = Mojo::JSON::Pointer->new($data)->get(shift));
_say(ref $data eq 'HASH' || ref $data eq 'ARRAY' ? to_json($data) :
my %RESERVED = map { $_ => 1 } (
qw(action app cb controller data extends format handler inline json layout namespace path status template text),
qw(variant)
);
sub BUILD_DYNAMIC {
my ($class,
r->parse('/user/:id');
$r = $r->parse('/user/:id', id => qr/\d+/);
$r = $r->parse(format => ['json', 'yaml']);
Parse pattern.
=head2 patch
my $route = $r->patch;
my $route = $r->patch('/:fo
my $route = $r->under('/:foo' => (agent => qr/Firefox/));
my $route = $r->under([format => ['json', 'yaml']]);
Generate L<Mojolicious::Routes::Route> object for a nested route with its own inter
addressable with URLs and every resource can have
different representations such as HTML, RSS or JSON. User interface concerns are separated from data storage concerns
and all session state is kept c
Set-Cookie: session=hmac-sha256(base64(json($session)))
In L<Mojolicious> however we are taking this concept one step further by storing everything JSON serialized and Base64
encoded in HMAC-SHA2
;
$c->redirect_to('goodbye');
Just remember that all session data gets serialized with L<Mojo::JSON> and stored in HMAC-SHA256 signed cookies, which
usually have a C<4096> byte (4KiB) limit, depend
o::JSON;
use Mojo::Promise;
use POSIX ();
has deserialize => sub { \&Mojo::JSON::decode_json };
has ioloop => sub { Mojo::IOLoop->singleton }, weak => 1;
has serialize => sub { \&Mojo::JSON::e
ncode_json };
sub exit_code { shift->{exit_code} }
sub pid { shift->{pid} }
sub run {
my ($self, @args) = @_;
$self->ioloop->next_tick(sub { $self->_start(@args) });
return $self;
}
sub run_
e(sub {...});
A callback used to deserialize subprocess return values, defaults to using L<Mojo::JSON>.
$subprocess->deserialize(sub ($bytes) { return [] });
=head2 ioloop
my $loop = $subpr
stallation process. And we do in fact already use several optional CPAN
modules such as L<Cpanel::JSON::XS>, L<CryptX>, L<EV>, L<IO::Socket::Socks>, L<IO::Socket::SSL>, L<Net::DNS::Native>,
L<Plack> a
long. What you can do instead, is to use a L<Mojolicious::Plugin::Config>,
L<Mojolicious::Plugin::JSONConfig> or L<Mojolicious::Plugin::NotYAMLConfig> configuration file.
# myapp.conf
{
hypno
{text => 'Hello.'} -> 200 OK, text/html, 'Hello.'
{json => {x => 3}} -> 200 OK, application/json, '{"x":3}'
{text => 'Oops.', status => '410'} -> 410 Gone, text/ht
);
=head2 Rendering JSON
The C<json> stash value allows you to pass Perl data structures to the renderer which get directly encoded to JSON with
L<Mojo::JSON>.
$c->render(json => {foo => [1, 'tes
plication/json) -> "json"
# /hello (Accept: application/xml) -> "xml"
# /hello.json -> "json"
# /hello.xml -> "xml"
# /hello?_format=json
se Compress::Raw::Zlib qw(Z_SYNC_FLUSH);
use List::Util qw(first);
use Mojo::JSON qw(encode_json j);
use Mojo::Util qw(decode encode trim);
use Mojo::WebSocket qw(WS_BIN
text => encode('UTF-8', $frame)} if ref $frame ne 'HASH';
# JSON
$frame->{text} = encode_json($frame->{json}) if exists $frame->{json};
# Raw text or binary
if (exists $frame->{text}) { $f
urn $self->finish(1009) if length $msg;
$msg = $out;
}
$self->emit(json => j($msg)) if $self->has_subscribers('json');
$op = delete $self->{op};
$self->emit($op == WS_TEXT ? 'text' : 'bin
package Mojolicious::Plugin::JSONConfig;
use Mojo::Base 'Mojolicious::Plugin::Config';
use Mojo::JSON qw(from_json);
use Mojo::Template;
sub parse {
my ($self, $content, $file, $conf, $app) = @_;
guration file "$file" did not return a JSON object} unless ref $config eq 'HASH';
return $config;
}
sub register { shift->SUPER::register(shift, {ext => 'json', %{shift()}}) }
sub render {
my (
}
1;
=encoding utf8
=head1 NAME
Mojolicious::Plugin::JSONConfig - JSON configuration plugin
=head1 SYNOPSIS
# myapp.json (it's just JSON with embedded Perl)
{
%# Just a value
"foo":
se Mojo::Content::MultiPart;
use Mojo::Content::Single;
use Mojo::File qw(path);
use Mojo::JSON qw(encode_json);
use Mojo::Parameters;
use Mojo::Transaction::HTTP;
use Mojo::Transaction::WebSocket;
us
;
has compressed => sub { $ENV{MOJO_GZIP} // 1 };
has generators => sub { {form => \&_form, json => \&_json, multipart => \&_multipart} };
has name => 'Mojolicious (Perl)';
sub add_generator {
ame, $values)};
}
return \@parts;
}
sub _json {
my ($self, $tx, $data) = @_;
_type($tx->req->body(encode_json $data)->headers, 'application/json');
return $tx;
}
sub _multipart {
my ($s
_development('not_found', @_) });
$app->helper('reply.json_exception', => \&_json_exception);
$app->helper('reply.json_not_found', => \&_json_not_found);
$app->helper('reply.txt_exception', =>
ers->reply->txt_exception($e) if $format eq 'txt';
return $c->helpers->reply->json_exception($e) if $format eq 'json';
return $c->helpers->reply->html_exception($e);
}
sub _http_not_found {
my
->helpers->reply->txt_not_found if $format eq 'txt';
return $c->helpers->reply->json_not_found if $format eq 'json';
return $c->helpers->reply->html_not_found;
}
sub _inactivity_timeout {
my (
-> {controller => 'foo', action => 'one', format => 'html'}
# /foo.json -> {controller => 'foo', action => 'one', format => 'json'}
# /bar -> {controller => 'bar', action => 'two', format =>
=> 'two', format => 'html'}
# /bar.json -> {controller => 'bar', action => 'two', format => 'json'}
my $with_format = $r->any('/' => [format => ['html', 'json']])->to(format => undef);
$with_fo
Or just add a C<hypnotoad> section to your L<Mojolicious::Plugin::Config>, L<Mojolicious::Plugin::JSONConfig> or
L<Mojolicious::Plugin::NotYAMLConfig> configuration file.
# myapp.conf
{
hypno
one of the built-in configuration plugins L<Mojolicious::Plugin::Config>,
L<Mojolicious::Plugin::JSONConfig> or L<Mojolicious::Plugin::NotYAMLConfig>.
# myapp.conf
{
plugins => [
{SetU
dule/_search?q=mojolicious' => sub ($ua, $tx) {
$c->render('metacpan', hits => $tx->result->json->{hits}{hits});
});
};
app->start;
__DATA__
@@ metacpan.html.ep
<!DOCTYPE html>
::ByteStream qw(b);
use Mojo::Collection qw(c);
use Mojo::DOM;
use Mojo::File qw(path);
use Mojo::JSON qw(j);
use Mojo::URL;
use Mojo::Util qw(dumper monkey_patch);
# Silent one-liners
$ENV{MOJO_LOG_
unctions, which are automatically exported.
=head2 a
my $app = a('/hello' => sub { $_->render(json => {hello => 'world'}) });
Create a route with L<Mojolicious::Lite/"any"> and return the current
cept => '*/*'} => form => {a => 'b'});
my $res = d('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
Perform C<DELETE> request with L<Mojo::UserAgent/"delete"> and return resulting