1#!/usr/bin/perl 2 3package JSON::RPC::Common::Marshal::Text; 4$JSON::RPC::Common::Marshal::Text::VERSION = '0.11'; 5use Moose; 6# ABSTRACT: JSON text marshalling for L<JSON::RPC::Common>. 7 8use Carp qw(croak); 9 10use JSON (); 11use JSON::RPC::Common::Message; 12use JSON::RPC::Common::Procedure::Call; 13use JSON::RPC::Common::Procedure::Return; 14 15use namespace::clean -except => [qw(meta)]; 16 17has json => ( 18 isa => "Object", 19 is => "rw", 20 handles => [qw(encode decode)], 21 lazy_build => 1, 22); 23 24sub _build_json { 25 JSON->new; 26} 27 28has message_class => ( 29 isa => "ClassName", 30 is => "rw", 31 default => "JSON::RPC::Common::Message", 32 handles => { "inflate_message" => "inflate" }, 33); 34 35has call_class => ( 36 isa => "ClassName", 37 is => "rw", 38 default => "JSON::RPC::Common::Procedure::Call", 39 handles => { "inflate_call" => "inflate" }, 40); 41 42has return_class => ( 43 isa => "ClassName", 44 is => "rw", 45 default => "JSON::RPC::Common::Procedure::Return", 46 handles => { "inflate_return" => "inflate" }, 47); 48 49sub deflate_call { 50 my ( $self, $call ) = @_; 51 $call->deflate; 52} 53 54sub deflate_return { 55 my ( $self, $return ) = @_; 56 $return->deflate; 57} 58 59sub message_to_json { 60 my ( $self, $message ) = @_; 61 62 if ( $message->isa("JSON::RPC::Common::Procedure::Call") ) { 63 $self->call_to_json($message); 64 } elsif ( $message->isa("JSON::RPC::Common::Procedure::Return") ) { 65 $self->return_to_json($message); 66 } else { 67 croak "I dunno wtf $message is"; 68 } 69} 70 71sub json_to_message { 72 my ( $self, $json ) = @_; 73 $self->inflate_message( $self->decode($json) ); 74} 75 76sub call_to_json { 77 my ( $self, $call ) = @_; 78 $self->encode( $self->deflate_call($call) ); 79} 80 81sub return_to_json { 82 my ( $self, $ret ) = @_; 83 $self->encode( $self->deflate_return($ret) ); 84} 85 86sub json_to_call { 87 my ( $self, $json ) = @_; 88 $self->inflate_call( $self->decode($json) ); 89} 90 91sub json_to_return { 92 my ( $self, $json ) = @_; 93 $self->inflate_return( $self->decode($json) ); 94} 95 96__PACKAGE__->meta->make_immutable(); 97 98__PACKAGE__ 99 100__END__ 101 102=pod 103 104=head1 NAME 105 106JSON::RPC::Common::Marshal::Text - JSON text marshalling for L<JSON::RPC::Common>. 107 108=head1 VERSION 109 110version 0.11 111 112=head1 SYNOPSIS 113 114 use JSON::RPC::Common::Marshal::Text; 115 116 my $m = JSON::RPC::Common::Marshal::Text->new; 117 118 my $return_obj = $m->json_to_return($json_text); 119 120=head1 DESCRIPTION 121 122This object serializes L<JSON::RPC::Common::Procedure::Call> and 123L<JSON::RPC::Common::Procedure::Return> objects into JSON text using the 124L<JSON> module. 125 126=head1 ATTRIBUTES 127 128=over 4 129 130=item json 131 132The L<JSON> object to use. A default one will be created if not specified. 133 134=item call_class 135 136=item return_class 137 138The classes to call C<inflate> on. 139 140Defaults to L<JSON::RPC::Common::Procedure::Call> and 141L<JSON::RPC::Common::Procedure::Return>. 142 143=back 144 145=head1 METHODS 146 147=over 4 148 149=item call_to_json 150 151=item json_to_call 152 153=item return_to_json 154 155=item json_to_return 156 157These methods do the conversion from objects to json and vice versa. 158 159=back 160 161=head1 AUTHOR 162 163Yuval Kogman <nothingmuch@woobling.org> 164 165=head1 COPYRIGHT AND LICENSE 166 167This software is copyright (c) 2014 by Yuval Kogman and others. 168 169This is free software; you can redistribute it and/or modify it under 170the same terms as the Perl 5 programming language system itself. 171 172=cut 173