package Message::Passing::Filter::Encoder::JSON;
use Moo;
use MooX::Types::MooseLike::Base qw/ Bool /;
use JSON qw/ to_json /;
use Scalar::Util qw/ blessed /;
use Try::Tiny;
use Message::Passing::Exce
an('to_hash')) {
$message = $message->to_hash;
}
}
to_json( $message, { utf8 => 1, $self->pretty ? (pretty => 1) : () } )
}
catch {
$self->
:Role::Filter::Encoder::JSON - Encodes data structures as JSON for output
=head1 DESCRIPTION
This filter takes a hash ref or an object for a message, and serializes it to JSON.
Plain refs work as e
package Message::Passing::Filter::Decoder::JSON;
use Moo;
use JSON qw/ from_json /;
use Try::Tiny;
use Message::Passing::Exception::Decoding;
use namespace::clean -except => 'meta';
with qw/
Mess
/;
sub filter {
my ($self, $message) = @_;
try {
ref($message) ? $message : from_json( $message, { utf8 => 1 } )
}
catch {
$self->error->consume(Message::Passing::Exc
Passing::Role::Filter::Decoder::JSON
=head1 DESCRIPTION
Decodes string messages from JSON into data structures.
=head1 ATTRIBUTES
=head1 METHODS
=head2 filter
JSON decodes a message supplied as
efault => 'Null' ),
CLIComponent( name => 'decoder', default => 'JSON' ),
CLIComponent( name => 'encoder', default => 'JSON' ),
CLIComponent( name => 'error', default => 'STDERR' ),
CL
IComponent( name => 'error_encoder', default => 'Message::Passing::Filter::Encoder::JSON' ),
'Message::Passing::Role::Script';
option configfile => (
is => 'ro',
format => 's',
);
sub ge
=head2 BASIC PREMISE
You have data for discrete events, represented by a hash (and
serialized as JSON).
This could be a text log line, an audit record of an API
event, a metric emitted from your app
ead2 filename
An attribute for the file name to write to.
=head2 consume
Consumes a message by JSON encoding it and printing it, followed by \n
=head1 SEE ALSO
L<Message::Passing>
=head1 SPONSOR
er within a chain.
message_chain {
encoder fooenc => ( output_to => 'out', class => 'JSON' );
....
};
Class names will be assumed to prefixed with 'Message::Passing::Filter::
in a chain.
message_chain {
decoder zmq_decode => ( output_to => 'filter', class => 'JSON' );
....
};
Class names will be assumed to prefixed with 'Message::Passing::Filter::
og::Dispatch;
use Log::Dispatch::Message::Passing;
use Message::Passing::Filter::Encoder::JSON;
use Message::Passing::Output::ZeroMQ;
my $log = Log::Dispatch->new;
$log->add(Log:
egate_log',
min_level => 'debug',
output => Message::Passing::Filter::Encoder::JSON->new(
output_to => Message::Passing::Output::ZeroMQ->new(
connect =>
package Message::Passing::Role::Output;
use Moo::Role;
use JSON qw/ to_json /;
use Scalar::Util qw/ blessed /;
#use namespace::clean -except => 'meta';
requires 'consume';
no Moo::Role;
1;
=head1
g::Output::STDERR';
require_module 'Message::Passing::Filter::Encoder::JSON';
Message::Passing::Filter::Encoder::JSON->new(
output_to => Message::Passing::Output::STDERR->new,
);
}
he error chain.
By default, this is a chain of:
=over
=item Message::Passing::Filter::Encoder::JSON
=item Message::Passing::Output::STDOUT
=back
=head1 WARNINGS
=head2 ERROR CHAINS CAN LOOP
If
s around before after with) ];
use MooX::Options;
use MooX::Types::MooseLike::Base qw/ Str /;
use JSON ();
use Try::Tiny qw/ try /;
sub make_variant {
my ($class, $target_package, %arguments) = @
my $str = shift;
if (! ref $str) {
try {
$str = JSON->new->relaxed->decode($str)
};
}
$str;
},
tional attribute called C<< "${name}_options" >> will also be added,
which coerces a hashref from JSON.
=head2 default
A default value for the main attribute. If this is not supplied, than the attri
package Message::Passing::Types;
use MooseX::Types ();
use Moose::Util::TypeConstraints;
use JSON ();
use MooseX::Types::Moose qw/ Str HashRef ArrayRef /;
use MooseX::Types::Common::String qw/ NonEmpt
-declare => [qw{
Output_Type
Input_Type
Filter_Type
Codec_Type
Hash_from_JSON
JSON_from_Hash
ArrayOfStr
}];
role_type Input_Type, { role => 'Message::Passing::Role::Input
m_JSON,
as HashRef;
coerce Hash_from_JSON,
from NonEmptySimpleStr,
via { try { JSON->new->relaxed->decode($_) } };
MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
Hash_from_JSON,