Serializer/JSON.pm

filter: Statementstmt 100% 32 / 32 Branchbran 100% 8 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 93.5% 58 / 62 CC 7 SCAR 8.1
CC
7
Cov
96%
get_freezer
4.0
__ANON__
3.0
__ANON__
2.0
CRAP 2.3
« Serializer.pm ↑ Summary Serializer/Storable.pm »
1 package DBIx::Class::InflateColumn::Serializer::JSON;
2 3 $DBIx::Class::InflateColumn::Serializer::JSON::VERSION = '0.08';
3 =head1 NAME
4  
5 DBIx::Class::InflateColumn::Serializer::JSON - JSON Inflator
6  
7 =head1 SYNOPSIS
8  
9 package MySchema::Table;
10 use base 'DBIx::Class';
11  
12 __PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
13 __PACKAGE__->add_columns(
14 'data_column' => {
15 'data_type' => 'VARCHAR',
16 'size' => 255,
17 'serializer_class' => 'JSON',
18 'serializer_options' => { allow_blessed => 1, convert_blessed => 1, pretty => 1 }, # optional
19 }
20 );
21  
22 Then in your code...
23  
24 my $struct = { 'I' => { 'am' => 'a struct' };
25 $obj->data_column($struct);
26 $obj->update;
27  
28 And you can recover your data structure with:
29  
30 my $obj = ...->find(...);
31 my $struct = $obj->data_column;
32  
33 The data structures you assign to "data_column" will be saved in the database in JSON format.
34  
35 Any arguments included in C<serializer_options> will be passed to the L<JSON::MaybeXS> constructor,
36 to be used by the JSON backend for both serializing and deserializing.
37  
38 =cut
39  
40 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 =over 4
47  
48 =item get_freezer
49  
50 Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes
51 the data passed to it. Returns a coderef.
52  
53 =cut
54  
55 sub get_freezer{
56 9 ▶ my ($class, $column, $info, $args) = @_;
Subroutine get_freezer: called CC=4 SCAR=13.9
Pod documented
57  
58 9 my $opts = $info->{serializer_options};
59  
60 9 ▶ my $serializer = JSON::MaybeXS->new($opts && %$opts ? %$opts: ());
Branch: 1 branch
BranchTrueFalseTotal
$opts && %$opts ? : 3 6 9
Condition66%2/3
$opts and %$opts
!ll&&!rl&&r
603
MC/DC50%1/2
$opts and %$opts
$opts %$opts
Truth table66%2/3
A and B
A= $optsB= %$opts
ABresult
0X0
100
111
61  
62 9 ▶ if (defined $info->{'size'}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{'size'}) { } 3 6 9
63 3 my $size = $info->{'size'};
64  
65 return sub {
66 2 ▶ my $s = $serializer->encode(shift);
Subroutine __ANON__: called CC=2 SCAR=6.9
67 2 ▶ croak "serialization too big" if (length($s) > $size);
Branch: 1 branch
BranchTrueFalseTotal
if length $s > $size 1 1 2
68 1 return $s;
69 3 };
70 } else {
71 return sub {
72 2 ▶ return $serializer->encode(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
73 6 };
74 }
75 }
76  
77 =item get_unfreezer
78  
79 Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes
80 the data stored in the column. Returns a coderef.
81  
82 =back
83  
84 =cut
85  
86 sub get_unfreezer {
87 9 ▶ my ($class, $column, $info, $args) = @_;
Subroutine get_unfreezer: called CC=1 SCAR=0.0
Pod documented
88  
89 9 my $opts = $info->{serializer_options};
90 return sub {
91 4 ▶ JSON::MaybeXS->new($opts && %$opts ? %$opts : ())->decode(shift);
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$opts && %$opts ? : 1 3 4
Condition66%2/3
$opts and %$opts
!ll&&!rl&&r
301
MC/DC50%1/2
$opts and %$opts
$opts %$opts
Truth table66%2/3
A and B
A= $optsB= %$opts
ABresult
0X0
100
111
92 9 };
93 }
94  
95  
96 3 1;
« Serializer.pm ↑ Summary Serializer/Storable.pm »