File Coverage

blib/lib/PGObject/Type/JSON.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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