| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package DBIx::Class::InflateColumn::Serializer::JSON; |
|
2
|
|
|
|
|
|
|
$DBIx::Class::InflateColumn::Serializer::JSON::VERSION = '0.09'; |
|
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 will be passed to the L constructor, |
|
36
|
|
|
|
|
|
|
to be used by the JSON backend for both serializing and deserializing. |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
|
39
|
|
|
|
|
|
|
|
|
40
|
3
|
|
|
3
|
|
11
|
use strict; |
|
|
3
|
|
|
|
|
3
|
|
|
|
3
|
|
|
|
|
68
|
|
|
41
|
3
|
|
|
3
|
|
9
|
use warnings; |
|
|
3
|
|
|
|
|
3
|
|
|
|
3
|
|
|
|
|
64
|
|
|
42
|
3
|
|
|
3
|
|
1162
|
use JSON::MaybeXS; |
|
|
3
|
|
|
|
|
14866
|
|
|
|
3
|
|
|
|
|
184
|
|
|
43
|
3
|
|
|
3
|
|
23
|
use Carp; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
140
|
|
|
44
|
3
|
|
|
3
|
|
13
|
use namespace::clean; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
22
|
|
|
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
|
|
|
9
|
1
|
19
|
my ($class, $column, $info, $args) = @_; |
|
57
|
|
|
|
|
|
|
|
|
58
|
9
|
|
|
|
|
14
|
my $opts = $info->{serializer_options}; |
|
59
|
|
|
|
|
|
|
|
|
60
|
9
|
100
|
66
|
|
|
58
|
my $serializer = JSON::MaybeXS->new($opts && %$opts ? %$opts: ()); |
|
61
|
|
|
|
|
|
|
|
|
62
|
9
|
100
|
|
|
|
135
|
if (defined $info->{'size'}){ |
|
63
|
3
|
|
|
|
|
4
|
my $size = $info->{'size'}; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
return sub { |
|
66
|
2
|
|
|
2
|
|
133340
|
my $s = $serializer->encode(shift); |
|
67
|
2
|
100
|
|
|
|
39
|
croak "serialization too big" if (length($s) > $size); |
|
68
|
1
|
|
|
|
|
16
|
return $s; |
|
69
|
3
|
|
|
|
|
23
|
}; |
|
70
|
|
|
|
|
|
|
} else { |
|
71
|
|
|
|
|
|
|
return sub { |
|
72
|
2
|
|
|
2
|
|
1232
|
return $serializer->encode(shift); |
|
73
|
6
|
|
|
|
|
33
|
}; |
|
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
|
|
|
9
|
1
|
15
|
my ($class, $column, $info, $args) = @_; |
|
88
|
|
|
|
|
|
|
|
|
89
|
9
|
|
|
|
|
12
|
my $opts = $info->{serializer_options}; |
|
90
|
|
|
|
|
|
|
return sub { |
|
91
|
4
|
100
|
66
|
4
|
|
19786
|
JSON::MaybeXS->new($opts && %$opts ? %$opts : ())->decode(shift); |
|
92
|
9
|
|
|
|
|
38
|
}; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |