File Coverage

blib/lib/KiokuDB/Backend/Serialize/JSON.pm
Criterion Covered Total %
statement 30 31 96.7
branch 3 6 50.0
condition n/a
subroutine 8 8 100.0
pod 2 4 50.0
total 43 49 87.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::Backend::Serialize::JSON;
4 6     6   3967 use Moose::Role;
  6         10  
  6         46  
5              
6 6     6   29087 use IO::Handle;
  6         11129  
  6         335  
7              
8 6     6   30 use namespace::clean -except => 'meta';
  6         7  
  6         44  
9              
10             with qw(
11             KiokuDB::Backend::Serialize
12             KiokuDB::Backend::Role::UnicodeSafe
13             KiokuDB::Backend::Serialize::JSPON
14             );
15              
16             has pretty => (
17             isa => "Bool",
18             is => "rw",
19             default => 0,
20             );
21              
22             has [qw(utf8 canonical)] => (
23             isa => "Bool",
24             is => "rw",
25             default => 1,
26             );
27              
28             has json => (
29             isa => "Object",
30             is => "rw",
31             lazy_build => 1,
32             handles => [qw(encode decode)],
33             );
34              
35             sub _build_json {
36 17     17   31 my $self = shift;
37              
38 17         235 my $json = JSON->new;
39              
40 17         37 foreach my $flag (qw(utf8 pretty canonical)) {
41 51 100       1407 $json->$flag if $self->$flag;
42             }
43              
44 17         448 return $json;
45             }
46              
47             sub serialize {
48 740     740 1 41017 my ( $self, @args ) = @_;
49 740         2409 $self->encode( $self->collapse_jspon(@args) );
50             }
51              
52             sub deserialize {
53 1747     1747 1 22711 my ( $self, $json, @args ) = @_;
54 1747         5746 $self->expand_jspon( $self->decode($json), @args );
55             }
56              
57             sub serialize_to_stream {
58 3     3 0 1780 my ( $self, $fh, $entry ) = @_;
59 3         8 $fh->print( $self->serialize($entry) );
60             }
61              
62             sub deserialize_from_stream {
63 2     2 0 423 my ( $self, $fh ) = @_;
64              
65 2         3 local $_;
66 2         6 local $/ = \4096;
67              
68 2         67 my $json = $self->json;
69              
70 2         179 while ( <$fh> ) {
71 1 50       26 if ( my @docs = $json->incr_parse($_) ) {
72 1         2 my @entries = map { $self->expand_jspon($_) } @docs;
  3         18  
73 1         11 return @entries;
74             } else {
75 0 0       0 return if $fh->eof;
76             }
77             }
78              
79 1         3 return;
80             }
81              
82             __PACKAGE__
83              
84             __END__
85              
86             =pod
87              
88             =head1 NAME
89              
90             KiokuDB::Backend::Serialize::JSON - Role to serialize entries to JSON strings
91             with L<KiokuDB::Backend::Serialize::JSPON> semantics
92              
93             =head1 SYNOPSIS
94              
95             with qw(KiokuDB::Backend::Serialize::JSON);
96              
97             sub foo {
98             my ( $self, $entry ) = @_;
99              
100             my $json_string = $self->serialize($entry);
101             }
102              
103             =head1 DESCRIPTION
104              
105             This role provides additional convenience attributes and methods for backends
106             that encode entries to JSON strings, on top of
107             L<KiokuDB::Backend::Serialize::JSPON> which only restructures the data.
108              
109             L<KiokuDB::Backend::Serialize::Delegate> is preferred to using this directly.
110              
111             =head1 METHODS
112              
113             =over 4
114              
115             =item serialize $entry
116              
117             Returns a JSON string
118              
119             =item deserialize $json_str
120              
121             Returns a L<KiokuDB::Entry>
122              
123             =back
124              
125             =head1 ATTRIBUTES
126              
127             =over 4
128              
129             =item json
130              
131             The L<JSON> instance used to encode/decode the JSON.
132              
133             =item pretty
134              
135             Whether or not to pass the C<pretty> flag to the L<JSON> object after
136             construction.
137              
138             =back
139              
140             =cut
141              
142