| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2014 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Async::JSONStream; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use base qw( IO::Async::Stream ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | IO::Async::Stream->VERSION( '0.57' ); # ->read future | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use JSON qw( encode_json decode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | C<IO::Async::JSONStream> - send or receive lines of JSON data in C<IO::Async> | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use IO::Async::JSONStream; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 29 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $jsonstream = IO::Async::JSONStream->new; | ||||||||||||||||||||||||||||
| 32 | $loop->add( $jsonstream ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $jsonstream->connect( | ||||||||||||||||||||||||||||
| 35 | host => "my.server", | ||||||||||||||||||||||||||||
| 36 | service => 12345, | ||||||||||||||||||||||||||||
| 37 | )->then( sub { | ||||||||||||||||||||||||||||
| 38 | $jsonstream->write_json( [ data => { goes => "here" } ] ); | ||||||||||||||||||||||||||||
| 39 | $jsonstream->read_json | ||||||||||||||||||||||||||||
| 40 | })->on_done( sub { | ||||||||||||||||||||||||||||
| 41 | my ( $data ) = @_; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | print "Received the data $data\n"; | ||||||||||||||||||||||||||||
| 44 | })->get; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | This subclass of L<IO::Async::Stream> implements a simple JSON-encoded data | ||||||||||||||||||||||||||||
| 49 | stream, sending and receiving Perl data structures by JSON-encoded lines of | ||||||||||||||||||||||||||||
| 50 | text. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | The following events are invoked, either using subclass methods or CODE | ||||||||||||||||||||||||||||
| 57 | references in parameters: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 on_json $data | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Invoked when a line of JSON-encoded data is received. It is passed the decoded | ||||||||||||||||||||||||||||
| 62 | data as a regular Perl data structure. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head2 on_json_error $error | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Invoked when a line is received but JSON decoding fails. It is passed the | ||||||||||||||||||||||||||||
| 67 | failure exception from the JSON decoder. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =over 8 | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =item on_json => CODE | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item on_json_error => CODE | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | CODE references for event handlers. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item eol => STRING | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Optional. Sets the string used for the line ending on the stream when writing | ||||||||||||||||||||||||||||
| 86 | JSON. Defaults to C<\n> if not given. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =back | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub _init | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | 2 | $self->SUPER::_init( @_ ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | $self->{eol} = "\n"; | |||||||||||||||||||||||||||
| 98 | 2 | $self->{json_read_f} = []; | |||||||||||||||||||||||||||
| 99 | 2 | $self->{json} = JSON->new; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub configure | ||||||||||||||||||||||||||||
| 103 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 105 | 8 | my %args = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 8 | foreach (qw( on_json on_json_error eol )) { | |||||||||||||||||||||||||||
| 108 | 24 | ▶ | $self->{$_} = delete $args{$_} if exists $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 8 | ▶ | if( $self->read_handle ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | $self->can_event( $_ ) or croak "Expected either an $_ callback or to be able to ->$_" | ||||||||||||||||||||||||||||
| 113 | 5 | ▶ | for qw( on_json on_json_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | $self->SUPER::configure( %args ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub on_read | ||||||||||||||||||||||||||||
| 120 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
on_read: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | 8 | my ( $buffref, $eof ) = @_; | |||||||||||||||||||||||||||
| 123 | 8 | ▶ | return if $eof; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 8 | my $json = $self->{json}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 8 | $json->incr_parse( $$buffref ); | |||||||||||||||||||||||||||
| 128 | 8 | $$buffref = ''; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | PARSE_ONE: { | ||||||||||||||||||||||||||||
| 131 | 8 | my $data; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 26 | my $fail = not eval { | |||||||||||||||||||||||||||
| 134 | 26 | $data = $json->incr_parse; | |||||||||||||||||||||||||||
| 135 | 14 | 1 | |||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||
| 137 | 26 | chomp( my $e = $@ ); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 26 | my $f; | |||||||||||||||||||||||||||
| 140 | 26 | ▶ | 1 while $f = shift @{ $self->{json_read_f} } and $f->is_cancelled; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$f = shift @{$$self{"json_read_f"};} and $f->is_cancelled
MC/DC0%0/2
$f = shift @{$$self{"json_read_f"};} and $f->is_cancelled
$f = shift @{$$self{"json_read_f"};}
$f->is_cancelled
Truth table66%2/3
A and B
A= $f = shift @{$$self{"json_read_f"};}B= $f->is_cancelled
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 26 | ▶ | if( $data ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | 6 | ▶ | $f ? $f->done( $data ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | : $self->invoke_event( on_json => $data ); | ||||||||||||||||||||||||||||
| 145 | 6 | redo PARSE_ONE; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | elsif( $fail ) { | ||||||||||||||||||||||||||||
| 148 | 12 | ▶ | $f ? $f->fail( $e, json => ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | : $self->invoke_event( on_json_error => $e ); | ||||||||||||||||||||||||||||
| 150 | 12 | $json->incr_skip; | |||||||||||||||||||||||||||
| 151 | 12 | redo PARSE_ONE; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | # else last | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 8 | return 0; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 METHODS | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 $jsonstream->write_json( $data, %args ) | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Writes a new line of JSON-encoded data from the given Perl data structure. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Other arguments are passed to the C<write> method. Returns a C<Future> which | ||||||||||||||||||||||||||||
| 168 | will complete when the line is flushed. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | sub write_json | ||||||||||||||||||||||||||||
| 173 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
write_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 2 | my ( $data, @args ) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | $self->write( encode_json( $data ) . $self->{eol}, @args ); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 $jsonstream->read_json ==> $data | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Returns a L<Future> that will yield the next line of JSON-encoded data to be | ||||||||||||||||||||||||||||
| 183 | read from the stream. This takes place instead of the C<on_json> event. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | If a JSON decoding error occurs it will result in a failed Future with the | ||||||||||||||||||||||||||||
| 186 | operation name C<json> and the line on which decoding failed as its argument. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub read_json | ||||||||||||||||||||||||||||
| 191 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 3 | push @{ $self->{json_read_f} }, my $f = $self->loop->new_future; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 3 | return $f; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Incremental parsing support added by Frew Schmidt | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 3 | 0x55AA; | |||||||||||||||||||||||||||