JSON::RPC::Common::Marshal::Text - JSON text marshalling for L<JSON::RPC::Common>.


JSON-RPC-Common documentation Contained in the JSON-RPC-Common distribution.

Index


Code Index:

NAME

Top

JSON::RPC::Common::Marshal::Text - JSON text marshalling for JSON::RPC::Common.

VERSION

Top

version 0.10

SYNOPSIS

Top

	use JSON::RPC::Common::Marshal::Text;

	my $m = JSON::RPC::Common::Marshal::Text->new;

	my $return_obj = $m->json_to_return($json_text);

DESCRIPTION

Top

This object serializes JSON::RPC::Common::Procedure::Call and JSON::RPC::Common::Procedure::Return objects into JSON text using the JSON module.

ATTRIBUTES

Top

json

The JSON object to use. A default one will be created if not specified.

call_class
return_class

The classes to call inflate on.

Defaults to JSON::RPC::Common::Procedure::Call and JSON::RPC::Common::Procedure::Return.

METHODS

Top

call_to_json
json_to_call
return_to_json
json_to_return

These methods do the conversion from objects to json and vice versa.

AUTHOR

Top

Yuval Kogman <nothingmuch@woobling.org>

COPYRIGHT AND LICENSE

Top


JSON-RPC-Common documentation Contained in the JSON-RPC-Common distribution.

#!/usr/bin/perl

package JSON::RPC::Common::Marshal::Text;
BEGIN {
  $JSON::RPC::Common::Marshal::Text::VERSION = '0.10';
}
use Moose;
# ABSTRACT: JSON text marshalling for L<JSON::RPC::Common>.

use Carp qw(croak);

use JSON ();
use JSON::RPC::Common::Message;
use JSON::RPC::Common::Procedure::Call;
use JSON::RPC::Common::Procedure::Return;

use namespace::clean -except => [qw(meta)];

has json => (
	isa => "Object",
	is  => "rw",
	handles => [qw(encode decode)],
	lazy_build => 1,
);

sub _build_json {
	JSON->new;
}

has message_class => (
	isa => "ClassName",
	is  => "rw",
	default => "JSON::RPC::Common::Message",
	handles => { "inflate_message" => "inflate" },
);

has call_class => (
	isa => "ClassName",
	is  => "rw",
	default => "JSON::RPC::Common::Procedure::Call",
	handles => { "inflate_call" => "inflate" },
);

has return_class => (
	isa => "ClassName",
	is  => "rw",
	default => "JSON::RPC::Common::Procedure::Return",
	handles => { "inflate_return" => "inflate" },
);

sub deflate_call {
	my ( $self, $call ) = @_;
	$call->deflate;
}

sub deflate_return {
	my ( $self, $return ) = @_;
	$return->deflate;
}

sub message_to_json {
	my ( $self, $message ) = @_;

	if ( $message->isa("JSON::RPC::Common::Procedure::Call") ) {
		$self->call_to_json($message);
	} elsif ( $message->isa("JSON::RPC::Common::Procedure::Return") ) {
		$self->return_to_json($message);
	} else {
		croak "I dunno wtf $message is";
	}
}

sub json_to_message {
	my ( $self, $json ) = @_;
	$self->inflate_message( $self->decode($json) );
}

sub call_to_json {
	my ( $self, $call ) = @_;
	$self->encode( $self->deflate_call($call) );
}

sub return_to_json {
	my ( $self, $ret ) = @_;
	$self->encode( $self->deflate_return($ret) );
}

sub json_to_call {
	my ( $self, $json ) = @_;
	$self->inflate_call( $self->decode($json) );
}

sub json_to_return {
	my ( $self, $json ) = @_;
	$self->inflate_return( $self->decode($json) );
}

__PACKAGE__->meta->make_immutable();

__PACKAGE__



__END__