| 1 | package Message::Passing::Filter::Encoder::JSON; | ||||||||||||||
| 2 | 6 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 6 | ▶ | use MooX::Types::MooseLike::Base qw( Bool HasMethods ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | ▶ | use JSON::MaybeXS qw (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 6 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 6 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use Message::Passing::Exception::Encoding; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 6 | with qw/ | |||||||||||||
| 11 | Message::Passing::Role::Filter | ||||||||||||||
| 12 | Message::Passing::Role::HasErrorChain | ||||||||||||||
| 13 | /; | ||||||||||||||
| 14 | |||||||||||||||
| 15 | has pretty => ( | ||||||||||||||
| 16 | isa => Bool, | ||||||||||||||
| 17 | 8 | ▶ | default => sub { 0 }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 6 | is => 'ro', | |||||||||||||
| 19 | ); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | has _json => ( | ||||||||||||||
| 22 | is => 'lazy', | ||||||||||||||
| 23 | isa => HasMethods [qw( encode )], | ||||||||||||||
| 24 | default => sub { | ||||||||||||||
| 25 | 4 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 4 | return JSON::MaybeXS->new( utf8 => 1, pretty => $self->pretty ); | |||||||||||||
| 27 | }, | ||||||||||||||
| 28 | 6 | ); | |||||||||||||
| 29 | |||||||||||||||
| 30 | sub filter { | ||||||||||||||
| 31 | 7 | ▶ | my ($self, $message) = @_; | ||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 32 | try { | ||||||||||||||
| 33 | 7 | ▶ | return $message unless ref($message); | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||||||
| 34 | 6 | ▶ | if (blessed $message) { # FIXME - This should be moved out of here! | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 4 | ▶ | if ($message->can('pack')) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 36 | 3 | $message = $message->pack; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | elsif ($message->can('to_hash')) { | ||||||||||||||
| 39 | 1 | $message = $message->to_hash; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | 6 | $self->_json->encode( $message ); | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | catch { | ||||||||||||||
| 45 | 1 | ▶ | $self->error->consume(Message::Passing::Exception::Encoding->new( | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | exception => $_, | ||||||||||||||
| 47 | stringified_data => $message, | ||||||||||||||
| 48 | )); | ||||||||||||||
| 49 | 1 | return; # Explicitly drop the message from normal processing | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | 7 | } | |||||||||||||
| 52 | |||||||||||||||
| 53 | 6 | 1; | |||||||||||||
| 54 | |||||||||||||||
| 55 | =head1 NAME | ||||||||||||||
| 56 | |||||||||||||||
| 57 | Message::Passing::Role::Filter::Encoder::JSON - Encodes data structures as JSON for output | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =head1 DESCRIPTION | ||||||||||||||
| 60 | |||||||||||||||
| 61 | This filter takes a hash ref or an object for a message, and serializes it to JSON. | ||||||||||||||
| 62 | |||||||||||||||
| 63 | Plain refs work as expected, and classes generated by either: | ||||||||||||||
| 64 | |||||||||||||||
| 65 | =over | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =item Log::Message::Structures | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =item MooseX::Storage | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =back | ||||||||||||||
| 72 | |||||||||||||||
| 73 | should be correctly serialized. | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =head1 METHODS | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =head2 filter | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Performs the JSON encoding. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head2 pretty | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Attribute controlling if JSON is pretty printed. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head1 SEE ALSO | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =over | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =item L<Message::Passing> | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =item L<Message::Passing::Manual::Concepts> | ||||||||||||||
| 92 | |||||||||||||||
| 93 | =back | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 SPONSORSHIP | ||||||||||||||
| 96 | |||||||||||||||
| 97 | This module exists due to the wonderful people at Suretec Systems Ltd. | ||||||||||||||
| 98 | <http://www.suretecsystems.com/> who sponsored its development for its | ||||||||||||||
| 99 | VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with | ||||||||||||||
| 100 | the SureVoIP API - | ||||||||||||||
| 101 | <http://www.surevoip.co.uk/support/wiki/api_documentation> | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =head1 AUTHOR, COPYRIGHT AND LICENSE | ||||||||||||||
| 104 | |||||||||||||||
| 105 | See L<Message::Passing>. | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =cut | ||||||||||||||
| 108 | |||||||||||||||