1package JSON::RPC; 2 3use strict; 4 5$JSON::RPC::VERSION = '0.96'; 6 7 81; 9__END__ 10 11=pod 12 13=head1 NAME 14 15JSON::RPC - Perl implementation of JSON-RPC 1.1 protocol 16 17=head1 DESCRIPTION 18 19 JSON-RPC is a stateless and light-weight remote procedure call (RPC) 20 protocol for inter-networking applications over HTTP. It uses JSON 21 as the data format for of all facets of a remote procedure call, 22 including all application data carried in parameters. 23 24quoted from L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>. 25 26 27This module was in JSON package on CPAN before. 28Now its interfaces was completely changed. 29 30The old modules - L<JSONRPC::Transport::HTTP> and L<Apache::JSONRPC> are deprecated. 31Please try to use JSON::RPC::Server and JSON::RPC::Client which support both JSON-RPC 32protocol version 1.1 and 1.0. 33 34 35=head1 EXAMPLES 36 37CGI version. 38 39 #-------------------------- 40 # In your application class 41 package MyApp; 42 43 use base qw(JSON::RPC::Procedure); # Perl 5.6 or more than 44 45 sub echo : Public { # new version style. called by clients 46 # first argument is JSON::RPC::Server object. 47 return $_[1]; 48 } 49 50 51 sub sum : Public(a:num, b:num) { # sets value into object member a, b. 52 my ($s, $obj) = @_; 53 # return a scalar value or a hashref or an arryaref. 54 return $obj->{a} + $obj->{b}; 55 } 56 57 58 sub a_private_method : Private { 59 # ... can't be called by client 60 } 61 62 63 sub sum_old_style { # old version style. taken as Public 64 my ($s, @arg) = @_; 65 return $arg[0] + $arg[1]; 66 } 67 68 69 #-------------------------- 70 # In your triger script. 71 use JSON::RPC::Server::CGI; 72 use MyApp; 73 74 # simple 75 JSON::RPC::Server::CGI->dispatch('MyApp')->handle(); 76 77 # or 78 JSON::RPC::Server::CGI->dispatch([qw/MyApp FooBar/])->handle(); 79 80 # or INFO_PATH version 81 JSON::RPC::Server::CGI->dispatch({'/Test' => 'MyApp'})->handle(); 82 83 #-------------------------- 84 # Client 85 use JSON::RPC::Client; 86 87 my $client = new JSON::RPC::Client; 88 89 my $uri = 'http://www.example.com/jsonrpc/Test'; 90 my $obj = { 91 method => 'sum', # or 'MyApp.sum' 92 params => [10, 20], 93 }; 94 95 my $res = $client->call( $uri, $obj ) 96 97 if($res){ 98 if ($res->is_error) { 99 print "Error : ", $res->error_message; 100 } 101 else { 102 print $res->result; 103 } 104 } 105 else { 106 print $client->status_line; 107 } 108 109 # or 110 111 $client->prepare($uri, ['sum', 'echo']); 112 print $client->sum(10, 23); 113 114 115See to L<JSON::RPC::Server::CGI>, L<JSON::RPC::Server::Daemon>, L<JSON::RPC::Server::Apache> 116L<JSON::RPC::Client> and L<JSON::RPC::Procedure>. 117 118 119=head1 ABOUT NEW VERSION 120 121=over 122 123=item supports JSON-RPC protocol v1.1 124 125 126=back 127 128=head1 TODO 129 130=over 131 132=item Document 133 134=item Examples 135 136=item More Tests 137 138 139=back 140 141 142=head1 AUTHOR 143 144Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt> 145 146 147=head1 COPYRIGHT AND LICENSE 148 149Copyright 2007-2008 by Makamaka Hannyaharamitu 150 151This library is free software; you can redistribute it and/or modify 152it under the same terms as Perl itself. 153 154=cut 155 156 157