• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

lib/JSON/H03-May-2022-2,9191,084

t/H03-May-2022-560460

Build.PLH A D07-Oct-20141.8 KiB7356

ChangesH A D07-Oct-20143.5 KiB10373

LICENSEH A D07-Oct-201417.9 KiB379292

MANIFESTH A D07-Oct-2014575 2929

META.jsonH A D07-Oct-20144.2 KiB154153

META.ymlH A D07-Oct-20142.6 KiB10099

MakefileH A D07-Oct-201427.4 KiB902585

README.mdH A D07-Oct-20147.3 KiB239166

cpanfileH A D07-Oct-2014386 1816

README.md

1# NAME
2
3JSON::RPC - JSON RPC 2.0 Server Implementation
4
5# SYNOPSIS
6
7    # app.psgi
8    use strict;
9    use JSON::RPC::Dispatch;
10
11    my $dispatch = JSON::RPC::Dispatch->new(
12        prefix => "MyApp::JSONRPC::Handler",
13        router => Router::Simple->new( ... )
14    );
15
16    sub {
17        my $env = shift;
18        $dispatch->handle_psgi($env);
19    };
20
21# DESCRIPTION
22
23JSON::RPC is a set of modules that implement JSON RPC 2.0 protocol.
24
25    If you are using old JSON::RPC code (up to 0.96), DO NOT EXPECT
26    YOUR CODE TO WORK WITH THIS VERSION. THIS VERSION IS
27    ****BACKWARDS INCOMPATIBLE****
28
29# BASIC USAGE
30
31The JSON::RPC::Dispatch object is responsible for marshalling the request.
32
33    my $dispatch = JSON::RPC::Dispatch->new(
34        router => ...,
35    );
36
37The routing between the JSON RPC methods and their implementors are handled by
38Router::Simple. For example, if you want to map method "foo" to a "MyApp::JSONRPC::Handler" object instance's "handle\_foo" method, you specify something like the following in your router instance:
39
40    use Router::Simple::Declare;
41    my $router = router {
42        connect "foo" => {
43            handler => "+MyApp::JSONRPC::Handler",
44            action  => "handle_foo"
45        };
46    };
47
48    my $dispatch = JSON::RPC::Dispatch->new(
49        router => $router,
50    );
51
52The "+" prefix in the handler classname denotes that it is already a fully qualified classname. Without the prefix, the value of "prefix" in the dispatch object will be used to qualify the classname. If you specify it in your Dispatch instance, you may omit the prefix part to save you some typing:
53
54    use JSON::RPC::Dispatch;
55    use Router::Simple::Declare;
56
57    my $router = router {
58        connect "foo" => {
59            handler => "Foo",
60            action  => "process",
61        };
62        connect "bar" => {
63            handler => "Bar",
64            action => "process"
65        }
66    };
67    my $dispatch = JSON::RPC::Dispatch->new(
68        prefix => "MyApp::JSONRPC::Handler",
69        router => $router,
70    );
71
72    # The above will roughly translate to the following:
73    #
74    # for method "foo"
75    #    my $handler = MyApp::JSONRPC::Handler::Foo->new;
76    #    $handler->process( ... );
77    #
78    # for method "bar"
79    #    my $handler = MyApp::JSONRPC::Handler::Bar->new;
80    #    $handler->process( ... );
81
82The implementors are called handlers. Handlers are simple objects, and will be instantiated automatically for you. Their return values are converted to JSON objects automatically.
83
84You may also choose to pass objects in the handler argument to connect in  your router. This will save you the cost of instantiating the handler object, and you also don't have to rely on us instantiating your handler object.
85
86    use Router::Simple::Declare;
87    use MyApp::JSONRPC::Handler;
88
89    my $handler = MyApp::JSONRPC::Handler->new;
90    my $router = router {
91        connect "foo" => {
92            handler => $handler,
93            action  => "handle_foo"
94        };
95    };
96
97# HANDLERS
98
99Your handlers are objects responsible for returning some sort of reference structure that can be properly encoded via JSON/JSON::XS. The handler only needs to implement the methods that you specified in your router.
100
101The handler methods will receive the following parameters:
102
103    sub your_handler_method {
104        my ($self, $params, $procedure, @extra_args) = @_;
105
106        return $some_structure;
107    }
108
109In most cases you will only need the parameters. The exact format of the $params is dependent on the caller -- you will be passed whatever JSON structure that caller used to call your handler.
110
111$procedure is an instance of JSON::RPC::Procedure. Use it if you need to figure out more about the procedure.
112
113@extra\_args is optional, and will be filled with whatever extra arguments you passed to handle\_psgi(). For example,
114
115    # app.psgi
116    sub {
117        $dispatch->handle_psgi($env, "arg1", "arg2", "arg3");
118    }
119
120will cause your handlers to receive the following arguments:
121
122    sub your_handler_method {
123        my ($self, $params, $procedure, $arg1, $arg2, $arg3) = @_;
124
125    }
126
127This is convenient if you have application-specific data that needs to be passed to your handlers.
128
129# EMBED IT IN YOUR WEBAPP
130
131If you already have a web app (and whatever framework you might already have), you may choose to embed JSON::RPC in your webapp instead of directly calling it in your PSGI application.
132
133For example, if you would like to your webapp's "rpc" handler to marshall the JSON RPC request, you can do something like the following:
134
135    package MyApp;
136    use My::Favorite::WebApp;
137
138    sub rpc {
139        my ($self, $context) = @_;
140
141        my $dispatch =  ...; # grab it from somewhere
142        $dispatch->handle_psgi( $context->env );
143    }
144
145# ERRORS
146
147When your handler dies, it is automatically included in the response hash, unless no response was requested (see ["NOTIFICATIONS"](#notifications)).
148
149For example, something like below
150
151    sub rpc {
152        ...
153        if ($bad_thing_happend) {
154            die "Argh! I failed!";
155        }
156    }
157
158Would result in a response like
159
160    {
161        error => {
162            code => -32603,
163            message => "Argh! I failed! at ...",
164        }
165    }
166
167However, you can include custom data by die()'ing with a hash:
168
169    sub rpc {
170        ...
171        if ($bad_thing_happend) {
172            die { message => "Argh! I failed!", data => time() };
173        }
174    }
175
176This would result in:
177
178    {
179        error => {
180            code => -32603,
181            message => "Argh! I failed! at ...",
182            data => 1339817722,
183        }
184    }
185
186# NOTIFICATIONS
187
188Notifications are defined as procedures without an id.
189Notification handling does not produce a response. When all procedures are notifications no content is returned (if the request is valid).
190To maintain some basic compatibility with relaxed client implementations, JSON::RPC::Dispatch includes responses when procedures do not have a "jsonrpc" field set to "2.0".
191
192Note that no error is returned in response to a notification when the handler dies or when the requested method is not available.
193
194For example, a request structure like this:
195
196    [
197        {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
198        {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
199        {"jsonrpc": "2.0", "method": "keep_alive"},
200        {"jsonrpc": "2.0", "method": "get_data", "id": "9"}
201    ]
202
203Would result in a response like
204
205    [
206        {"jsonrpc": "2.0", "result": 7, "id": "1"},
207        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
208    ]
209
210# BACKWARDS COMPATIBILITY
211
212Eh, not compatible at all. JSON RPC 0.xx was fine, but it predates PSGI, and things are just... different before and after PSGI.
213
214Code at version 0.96 has been moved to JSON::RPC::Legacy namespace, so change your application to use JSON::RPC::Legacy if you were using the old version.
215
216# AUTHORS
217
218Daisuke Maki
219
220Shinichiro Aska
221
222Yoshimitsu Torii
223
224# AUTHOR EMERITUS
225
226Makamaka Hannyaharamitu, <makamaka\[at\]cpan.org> - JSON::RPC modules up to 0.96
227
228# COPYRIGHT AND LICENSE
229
230The JSON::RPC module is
231
232Copyright (C) 2011 by Daisuke Maki
233
234This library is free software; you can redistribute it and/or modify
235it under the same terms as Perl itself, either Perl version 5.8.0 or,
236at your option, any later version of Perl 5 you may have available.
237
238See JSON::RPC::Legacy for copyrights and license for previous versions.
239