| 1 | package PGObject::Type::JSON; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use PGObject; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | PGObject::Type::JSON - JSON wrappers for PGObject | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Version 2.1.1 | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | our $VERSION = '2.1.1'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | PGOBject::Type::JSON->register(); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Columns of type json will be converted into hashrefs | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $obj = PGOBject::Type::JSON->new($hashref); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $obj will now serialize to the database as json. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | This module allows json types or others (specified by custom register) types to | ||||||||||||||||||||||||||||
| 38 | be converted from JSON into objects according to their values. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | This module assumes that encoding will be in UTF8 across the board and is not | ||||||||||||||||||||||||||||
| 41 | safe to use with other database encodings. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 register(registry => 'default', types => ['json']) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | sub register{ | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=8 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 4 | my $self = shift @_; | |||||||||||||||||||||||||||
| 52 | 4 | ▶ | croak "Can't pass reference to register \n". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | "Hint: use the class instead of the object" if ref $self; | ||||||||||||||||||||||||||||
| 54 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 55 | 4 | my $registry = $args{registry}; | |||||||||||||||||||||||||||
| 56 | 4 | ▶ | $registry ||= 'default'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$registry ||= "default"
MC/DC0%0/1
$registry ||= "default"
$registry
Truth table50%1/2
A
A= $registry
| |||||||||||||||||||||||||||||
| 57 | 4 | my $types = $args{types}; | |||||||||||||||||||||||||||
| 58 | 4 | ▶ | $types = ['json', 'jsonb'] unless defined $types and @$types; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $types and @$types
MC/DC50%1/2
defined $types and @$types
defined $types
@$types
Truth table66%2/3
A and B
A= defined $typesB= @$types
| |||||||||||||||||||||||||||||
| 59 | 4 | for my $type (@$types){ | |||||||||||||||||||||||||||
| 60 | 6 | ▶ | if ($PGObject::VERSION =~ /^1./){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | my $ret = | |||||||||||||||||||||||||||
| 62 | PGObject->register_type(registry => $registry, pg_type => $type, | ||||||||||||||||||||||||||||
| 63 | perl_class => $self); | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return $ret unless $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 6 | PGObject::Type::Registry->register_type( | |||||||||||||||||||||||||||
| 67 | registry => $registry, dbtype => $type, apptype => $self | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 4 | return 1; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 new($ref) | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Stores this as a reference. Nulls are now scoped references to a lexically | ||||||||||||||||||||||||||||
| 78 | scoped variable. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | If values other than scalars, arrayrefs, or hashes are passed in, throws an | ||||||||||||||||||||||||||||
| 81 | error. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 11 | my ($class, $ref) = @_; | |||||||||||||||||||||||||||
| 87 | 11 | ▶ | $ref = null() unless defined $ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 11 | ▶ | if (not ref $ref){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 5 | my $src = $ref; | |||||||||||||||||||||||||||
| 90 | 5 | $ref = \$src; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 11 | bless $ref, $class; | |||||||||||||||||||||||||||
| 93 | 11 | ▶ | croak 'unsupported reftype' unless $ref->reftype =~ /^(SCALAR|ARRAY|HASH)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 10 | return $ref; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 from_db | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | serializes from the db. Note that database nulls are preserved distinct from | ||||||||||||||||||||||||||||
| 100 | json null's. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | my $db_null; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 10 | ▶ | sub null { \$db_null} | ||||||||||||||||||||||||||
|
Subroutine
null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $json_null; | ||||||||||||||||||||||||||||
| 109 | 7 | ▶ | sub json_null { \$json_null } | ||||||||||||||||||||||||||
|
Subroutine
json_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 6 | ▶ | sub is_json_null { json_null eq shift }; | ||||||||||||||||||||||||||
|
Subroutine
is_json_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | sub from_db { | ||||||||||||||||||||||||||
|
Subroutine
from_db: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 6 | my ($class, $var) = @_; | |||||||||||||||||||||||||||
| 114 | 6 | ▶ | return $class->new(undef) unless defined $var; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 5 | ▶ | return $class->new(json_null) if $var eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 4 | my $obj = $class->new(JSON->new->allow_nonref->decode($var)); | |||||||||||||||||||||||||||
| 117 | 4 | ▶ | return $obj->reftype eq 'SCALAR' ? $$obj : $obj ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head2 to_db | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | returns undef if is_null. Otherwise returns the value encoded as JSON | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 null | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Return a null type for storage in the db. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 TO_JSON | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | The handler for setting this to the JSON parser | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 5 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 5 | for ($self->reftype){ | |||||||||||||||||||||||||||
| 142 | 5 | ▶ | if ($_ eq 'SCALAR') { return $$self; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | if ($_ eq 'ARRAY') { return [@$self]; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | if ($_ eq 'HASH') { return { %$self } } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 7 | ▶ | sub to_db { | ||||||||||||||||||||||||||
|
Subroutine
to_db: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 7 | my $self = shift @_; | |||||||||||||||||||||||||||
| 150 | 7 | ▶ | return undef if $self->is_null; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 6 | ▶ | return 'null' if $self->is_json_null; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 5 | return JSON->new->allow_blessed->convert_blessed->encode($self); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 reftype | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Returns the reftype of the object (i.e. HASH, SCALAR, ARRAY) | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 24 | ▶ | sub reftype { | ||||||||||||||||||||||||||
|
Subroutine
reftype: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 24 | my ($self) = @_; | |||||||||||||||||||||||||||
| 163 | 24 | my $reftype = "$self"; | |||||||||||||||||||||||||||
| 164 | 24 | my $pkg = blessed $self; | |||||||||||||||||||||||||||
| 165 | 24 | $reftype =~ s/${pkg}=(\w+)\(.*\)/$1/; | |||||||||||||||||||||||||||
| 166 | 24 | ▶ | $reftype = 'SCALAR' if $reftype eq 'REF'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 24 | return $reftype; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 is_null | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Returns true if is a database null. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 9 | ▶ | sub is_null { | ||||||||||||||||||||||||||
|
Subroutine
is_null: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 9 | my $self = shift @_; | |||||||||||||||||||||||||||
| 178 | 9 | ▶ | return 1 if ref $self && ($self eq null); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self and $self eq null()
MC/DC50%1/2
ref $self and $self eq null()
ref $self
$self eq null()
Truth table66%2/3
A and B
A= ref $selfB= $self eq null()
| |||||||||||||||||||||||||||||
| 179 | 7 | return 0; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 json_null | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Returns a JSON null | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 is_json_null | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Returns true if the value is a JSON null. Else it returns false | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Chris Travers, C<< <chris.travers at gmail.com> >> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 BUGS | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Please report any bugs or feature requests to C<bug-pgobject-type-json at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 197 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Type-JSON>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 198 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | perldoc PGObject::Type::JSON | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | You can also look for information at: | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =over 4 | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Type-JSON> | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | L<http://annocpan.org/dist/PGObject-Type-JSON> | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | L<http://cpanratings.perl.org/d/PGObject-Type-JSON> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<http://search.cpan.org/dist/PGObject-Type-JSON/> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =back | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Copyright 2013-2023 Chris Travers. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | This program is released under the following license: BSD | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 4 | 1; # End of PGObject::Type::JSON | |||||||||||||||||||||||||||