1 package Tak::JSONChannel;
3 use JSON::PP qw(encode_json decode_json);
5 use Scalar::Util qw(weaken);
8 has read_fh => (is => 'ro', required => 1);
9 has write_fh => (is => 'ro', required => 1);
11 sub BUILD { shift->write_fh->autoflush(1); }
15 while (my $line = readline($self->read_fh)) {
16 if (my $unpacked = $self->_unpack_line($line)) {
23 my ($self, $line) = @_;
24 my $data = eval { decode_json($line) };
26 $self->send(MISTAKE => invalid_json => $@||'No data and no exception');
29 unless (ref($data) eq 'ARRAY') {
30 $self->send(MISTAKE => message_format => "Not an ARRAY");
34 $self->send(MISTAKE => message_format => "Empty request array");
41 my ($self, @msg) = @_;
42 my $json = eval { encode_json(\@msg) };
46 [ FAILURE => invalid_message => $@||'No data and no exception' ]
51 $self->_raw_send($json);
55 my ($self, $raw) = @_;
56 print { $self->write_fh } $raw."\n";