line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PGObject::Type::JSON; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
45412
|
use 5.010; |
|
3
|
|
|
|
|
9
|
|
4
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
55
|
|
5
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
74
|
|
6
|
3
|
|
|
3
|
|
799
|
use PGObject; |
|
3
|
|
|
|
|
7629
|
|
|
3
|
|
|
|
|
15
|
|
7
|
3
|
|
|
3
|
|
1610
|
use JSON; |
|
3
|
|
|
|
|
31420
|
|
|
3
|
|
|
|
|
15
|
|
8
|
3
|
|
|
3
|
|
345
|
use Carp 'croak'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1387
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
PGObject::Type::JSON - JSON wrappers for PGObject |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 2 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = 2.000000; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
PGOBject::Type::JSON->register(); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Columns of type json will be converted into hashrefs |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $obj = PGOBject::Type::JSON->new($hashref); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$obj will now serialize to the database as json. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This module allows json types or others (specified by custom register) types to |
37
|
|
|
|
|
|
|
be converted from JSON into objects according to their values. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
This module assumes that encoding will be in UTF8 across the board and is not |
40
|
|
|
|
|
|
|
safe to use with other database encodings. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 register(registry => 'default', types => ['json']) |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub register{ |
50
|
4
|
|
|
4
|
1
|
813
|
my $self = shift @_; |
51
|
4
|
50
|
|
|
|
12
|
croak "Can't pass reference to register \n". |
52
|
|
|
|
|
|
|
"Hint: use the class instead of the object" if ref $self; |
53
|
4
|
|
|
|
|
13
|
my %args = @_; |
54
|
4
|
|
|
|
|
7
|
my $registry = $args{registry}; |
55
|
4
|
|
100
|
|
|
18
|
$registry ||= 'default'; |
56
|
4
|
|
|
|
|
5
|
my $types = $args{types}; |
57
|
4
|
100
|
66
|
|
|
21
|
$types = ['json', 'jsonb'] unless defined $types and @$types; |
58
|
4
|
|
|
|
|
10
|
for my $type (@$types){ |
59
|
6
|
50
|
|
|
|
21
|
if ($PGObject::VERSION =~ /^1./){ |
60
|
6
|
|
|
|
|
21
|
my $ret = |
61
|
|
|
|
|
|
|
PGObject->register_type(registry => $registry, pg_type => $type, |
62
|
|
|
|
|
|
|
perl_class => $self); |
63
|
6
|
50
|
|
|
|
96
|
return $ret unless $ret; |
64
|
|
|
|
|
|
|
} else { |
65
|
0
|
|
|
|
|
0
|
PGObject::Type::Registry->register_type( |
66
|
|
|
|
|
|
|
registry => $registry, dbtype => $type, apptype => $self |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
4
|
|
|
|
|
18
|
return 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 new($ref) |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Stores this as a reference. Currently database nulls are stored as cyclical |
77
|
|
|
|
|
|
|
references which is probably a bad idea. In the future we should probably |
78
|
|
|
|
|
|
|
have a lexically scoped table for this. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub new { |
83
|
5
|
|
|
5
|
1
|
11
|
my ($class, $ref) = @_; |
84
|
5
|
100
|
|
|
|
16
|
if (!ref $ref) { |
85
|
2
|
|
|
|
|
4
|
my $src = $ref; |
86
|
2
|
|
|
|
|
3
|
$ref = \$src; |
87
|
|
|
|
|
|
|
} |
88
|
5
|
|
|
|
|
32
|
bless $ref, $class; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 from_db |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
serializes from the db. Note that database nulls are preserved distinct from |
94
|
|
|
|
|
|
|
json null's. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub from_db { |
99
|
5
|
|
|
5
|
1
|
28
|
my ($class, $var) = @_; |
100
|
5
|
100
|
|
|
|
16
|
$var = \$var unless defined $var; |
101
|
5
|
100
|
|
|
|
14
|
return "$class"->new($var) if ref $var; |
102
|
4
|
|
|
|
|
43
|
return "$class"->new(JSON->new->allow_nonref->decode($var)); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 to_db |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
returns undef if is_null. Otherwise returns the value encoded as JSON |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub to_db { |
113
|
5
|
|
|
5
|
1
|
11
|
my $self = shift @_; |
114
|
5
|
100
|
|
|
|
11
|
return undef if $self->is_null; |
115
|
4
|
|
|
|
|
5
|
my $copy; |
116
|
4
|
|
|
|
|
9
|
for ($self->reftype){ |
117
|
4
|
50
|
|
|
|
17
|
if ($_ eq 'SCALAR') { $copy = $$self if $_ eq 'SCALAR' } |
|
2
|
100
|
|
|
|
7
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
118
|
1
|
|
|
|
|
2
|
elsif ($_ eq 'ARRAY') { $copy = []; push @$copy, $_ for @$self; } |
|
1
|
|
|
|
|
4
|
|
119
|
1
|
|
|
|
|
7
|
elsif ($_ eq 'HASH') { $copy = {}; |
120
|
1
|
|
|
|
|
7
|
$copy->{$_} = $self->{$_} for keys %$self; } |
121
|
|
|
|
|
|
|
} |
122
|
4
|
|
|
|
|
70
|
return JSON->new->allow_nonref->convert_blessed->encode($copy); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 reftype |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Returns the reftype of the object (i.e. HASH, SCALAR, ARRAY) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub reftype { |
132
|
16
|
|
|
16
|
1
|
32
|
my ($self) = @_; |
133
|
16
|
|
|
|
|
36
|
my $reftype = "$self"; |
134
|
16
|
|
|
|
|
23
|
my $pkg = __PACKAGE__; |
135
|
16
|
|
|
|
|
121
|
$reftype =~ s/${pkg}=(\w+)\(.*\)/$1/; |
136
|
16
|
100
|
|
|
|
51
|
$reftype = 'SCALAR' if $reftype eq 'REF'; |
137
|
16
|
|
|
|
|
58
|
return $reftype; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 is_null |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Returns true if is a database null. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub is_null { |
147
|
7
|
|
|
7
|
1
|
15
|
my $self = shift @_; |
148
|
7
|
100
|
|
|
|
14
|
return 0 if $self->reftype ne 'SCALAR'; |
149
|
5
|
100
|
|
|
|
20
|
return 0 if !defined $$self; |
150
|
3
|
100
|
66
|
|
|
24
|
return 1 if ref $self && ($self eq $$self); |
151
|
1
|
|
|
|
|
5
|
return 0; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 AUTHOR |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Chris Travers, C<< >> |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 BUGS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
161
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
162
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SUPPORT |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
perldoc PGObject::Type::JSON |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
You can also look for information at: |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=over 4 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
L |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
L |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item * CPAN Ratings |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
L |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item * Search CPAN |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
L |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=back |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Copyright 2013 Chris Travers. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
This program is released under the following license: BSD |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
1; # End of PGObject::Type::JSON |