JSON/Protocol.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 2 / 2 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 91.4% 43 / 47 CC 4 SCAR 5.9
CC
4
Cov
94%
configure
3.0
_init
2.0
BEGIN
1.0
CRAP 1.8
↑ Summary JSON.pm »
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, 2017 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::WebSocket::JSON::Protocol;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base qw( Net::Async::WebSocket::Protocol );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 Net::Async::WebSocket::Protocol->VERSION( '0.11' ); # on_text_frame
12  
13 2 our $VERSION = '0.01';
14  
15 =head1 NAME
16  
17 C<Net::Async::WebSocket::Protocol> - send and receive JSON-encoded data over WebSockets
18  
19 =head1 DESCRIPTION
20  
21 This subclass of L<Net::Async::WebSocket::Protocol> provides some conveniences
22 for sending and receiving JSON-encoded data over WebSockets. Principly, it
23 provides one new method, L<send_json>, for encoding Perl values into JSON and
24 sending them, and one new method, L<on_json>, for decoding received JSON
25 content into Perl values when received.
26  
27 =cut
28  
29 =head1 EVENTS
30  
31 The following events are invoked, either using subclass methods or CODE
32 references in parameters:
33  
34 =head2 on_json
35  
36 $self->on_json( $data )
37 $on_json->( $self, $data )
38  
39 Invoked when a text frame is received and has been decoded from JSON. It is
40 passed the Perl data structure resulting from the decode operation.
41  
42 =cut
43  
44 sub _init
45 2 ▶{
Subroutine _init: called CC=2 SCAR=7.1
46 2 my $self = shift;
47 2 my ( $params ) = @_;
48 2 $self->SUPER::_init( $params );
49  
50 2 ▶ $params->{json} //= do {
Condition33%1/3
$params->{"json"} //= do { require JSON::MaybeXS; "JSON::MaybeXS"->new }
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{"json"} //= do { require JSON::MaybeXS; "JSON::MaybeXS"->new }
$params->{"json"} do { require JSON::MaybeXS; "JSON::MaybeXS"->new }
Truth table33%1/3
A //= B
A= $params->{"json"}B= do { require JSON::MaybeXS; "JSON::MaybeXS"->new }
ABresult
1X1
011
000
51 2 require JSON::MaybeXS;
52 2 JSON::MaybeXS->new;
53 };
54 }
55  
56 =head1 PARAMETERS
57  
58 The following named parameters may be passed to C<new> or C<configure>:
59  
60 =over 8
61  
62 =item json => OBJECT
63  
64 Optional. The JSON codec instance. This must support C<encode> and C<decode>
65 methods compatible with those provided by L<JSON>, L<JSON::XS> or similar.
66  
67 $text = $json->encode( $data )
68 $data = $json->decode( $text )
69  
70 Note in particular that the C<$text> strings are Unicode character strings,
71 not UTF-8 encoded byte strings, and therefore the C<utf8> option must be
72 disabled.
73  
74 If not provided, the L<< JSON::MaybeXS->new >> constructor is used to find a
75 suitable implementation.
76  
77 =item on_json => CODE
78  
79 CODE reference for event handler.
80  
81 =back
82  
83 =cut
84  
85 sub configure
86 7 ▶{
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
87 7 my $self = shift;
88 7 my %params = @_;
89  
90 7 foreach (qw( json on_json )) {
91 14 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 4 10 14
92 }
93  
94 # TODO: forbid on_text_frame
95  
96 7 $self->SUPER::configure( %params );
97 }
98  
99 sub on_text_frame
100 2 ▶{
Subroutine on_text_frame: called CC=1 SCAR=0.0
Pod documented
101 2 my $self = shift;
102 2 my ( $text ) = @_;
103  
104 # TODO: try/catch
105 2 my $data = $self->{json}->decode( $text );
106 2 $self->invoke_event( on_json => $data );
107 }
108  
109 =head1 METHODS
110  
111 The following methods documented with a trailing call to C<< ->get >> return
112 L<Future> instances.
113  
114 =cut
115  
116 =head2 send_json
117  
118 $self->send_json( $data )->get
119  
120 Sends a text frame containing a JSON encoding of the Perl data structure
121 provided.
122  
123 =cut
124  
125 sub send_json
126 2 ▶{
Subroutine send_json: called CC=1 SCAR=0.0
Pod documented
127 2 my $self = shift;
128 2 my ( $data ) = @_;
129  
130 2 $self->send_text_frame( $self->{json}->encode( $data ) );
131 }
132  
133 =head1 AUTHOR
134  
135 Paul Evans <leonerd@leonerd.org.uk>
136  
137 =cut
138  
139 2 0x55AA;
↑ Summary JSON.pm »