KiokuDB/Backend/Serialize/JSON.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 50.0% 2 / 4 totaltotal 89.2% 50 / 56 CC 6 SCAR 9.0
CC
6
Cov
91%
deserialize_from_stream
4.2
_build_json
3.0
BEGIN
1.0
CRAP 2.4
« KiokuDB/Role/Scan.pm ↑ Summary KiokuDB/TypeMap/Composite.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Backend::Serialize::JSON;
4 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 with qw(
11 KiokuDB::Backend::Serialize
12 KiokuDB::Backend::Role::UnicodeSafe
13 KiokuDB::Backend::Serialize::JSPON
14 );
15  
16 6 has pretty => (
17 isa => "Bool",
18 is => "rw",
19 default => 0,
20 );
21  
22 6 has [qw(utf8 canonical)] => (
23 isa => "Bool",
24 is => "rw",
25 default => 1,
26 );
27  
28 6 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 ▶ my $self = shift;
Subroutine _build_json: called CC=3 SCAR=11.0
37  
38 17 my $json = JSON->new;
39  
40 17 foreach my $flag (qw(utf8 pretty canonical)) {
41 51 ▶ $json->$flag if $self->$flag;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$flag 34 17 51
42 }
43  
44 17 return $json;
45 }
46  
47 sub serialize {
48 740 ▶ my ( $self, @args ) = @_;
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
49 740 $self->encode( $self->collapse_jspon(@args) );
50 }
51  
52 sub deserialize {
53 1747 ▶ my ( $self, $json, @args ) = @_;
Subroutine deserialize: called CC=1 SCAR=0.0
Pod documented
54 1747 $self->expand_jspon( $self->decode($json), @args );
55 }
56  
57 sub serialize_to_stream {
58 3 ▶ my ( $self, $fh, $entry ) = @_;
Subroutine serialize_to_stream: called CC=1 SCAR=0.0
Pod undocumented
59 3 $fh->print( $self->serialize($entry) );
60 }
61  
62 sub deserialize_from_stream {
63 2 ▶ my ( $self, $fh ) = @_;
Subroutine deserialize_from_stream: called CC=4 SCAR=14.4
Pod undocumented
64  
65 2 local $_;
66 2 local $/ = \4096;
67  
68 2 my $json = $self->json;
69  
70 2 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1 1 2
71 1 ▶ if ( my @docs = $json->incr_parse($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@docs) = $json->incr_parse($_)) { } 1 0 1
72 1 my @entries = map { $self->expand_jspon($_) } @docs;
73 1 return @entries;
74 } else {
75 0 ▶ return if $fh->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 0 0
76 }
77 }
78  
79 1 return;
80 }
81  
82 6 __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  
« KiokuDB/Role/Scan.pm ↑ Summary KiokuDB/TypeMap/Composite.pm »