IO::Async::JSONStream - send or receive lines of JSON data in IO::Async
IO::Async::JSONStream
IO::Async
use IO::Async::JSONStream; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $jsonstream = IO::Async::JSONStream->new; $loop->add( $jsonstream ); $jsonstream->connect( host => "my.server", service => 12345, )->then( sub { $jsonstream->write_json( [ data => { goes => "here" } ] ); $jsonstream->read_json })->on_done( sub { my ( $data ) = @_; print "Received the data $data\n"; })->get;
This subclass of IO::Async::Stream implements a simple JSON-encoded data stream, sending and receiving Perl data structures by JSON-encoded lines of text.
The following events are invoked, either using subclass methods or CODE references in parameters:
Invoked when a line of JSON-encoded data is received. It is passed the decoded data as a regular Perl data structure.
Invoked when a line is received but JSON decoding fails. It is passed the failure exception from the JSON decoder and the line on which decoding failed.
The following named parameters may be passed to new or configure:
new
configure
CODE references for event handlers.
Optional. Sets the string used for the line ending on the stream. Defaults to \n if not given.
\n
Writes a new line of JSON-encoded data from the given Perl data structure.
Other arguments are passed to the write method. Returns a Future which will complete when the line is flushed.
write
Future
Returns a Future that will yield the next line of JSON-encoded data to be read from the stream. This takes place instead of the on_json event.
on_json
If a JSON decoding error occurs it will result in a failed Future with the operation name json and the line on which decoding failed as its argument.
json
Consider a true streaming mode, using JSON's incremental parsing ability.
JSON
Paul Evans <leonerd@leonerd.org.uk>
To install IO::Async::JSONStream, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::Async::JSONStream
CPAN shell
perl -MCPAN -e shell install IO::Async::JSONStream
For more information on module installation, please visit the detailed CPAN module installation guide.