blib/lib/DBIx/QuickORM/Type/JSON.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 10 / 10 Conditioncond 90.0% 9 / 10 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 97.2% 70 / 72 CC 10 SCAR 9.2
CC
10
Cov
98%
db_type_for
4.0
matches
4.0
is_inflated
2.0
CRAP 2.5
« blib/lib/DBIx/QuickORM/Exception/DB.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm »
1 package DBIx::QuickORM::Type::JSON;
2 84 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 84 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 84 our $VERSION = '0.000033';
6  
7 84 ▶use Cpanel::JSON::XS();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 84 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 84 with 'DBIx::QuickORM::Role::Type';
11  
12 =pod
13  
14 =encoding UTF-8
15  
16 =head1 NAME
17  
18 DBIx::QuickORM::Type::JSON - Perl data structures stored as JSON text.
19  
20 =head1 DESCRIPTION
21  
22 Inflates JSON text into Perl data and deflates references into JSON text.
23 Encoding is canonical (hash keys sorted), so the same data always deflates to
24 the same text. Any JSON value may be stored, including a plain string or
25 number at the top level.
26  
27 A reference is inflated data and is encoded. A plain value is JSON text and is
28 sent as is, even inside C<inflated()>: C<'{"a":1}'> stores that object.
29  
30 So C<'foo'> is not valid JSON, but C<'"foo"'> is: encode a string with a JSON
31 encoder, or write proper JSON. To write back a decoded top-level string, such
32 as C<foo> read from the JSON C<"foo">, encode it or send C<raw_field>'s text.
33  
34 Inflated data can be changed by whoever holds it, so every read decodes the
35 JSON text again and gets data of its own (C<inflate_on_each_read>, see
36 L<DBIx::QuickORM::Role::Type/Cached inflated values>): changing the data read
37 from a row changes neither the row nor the next read. Decoding is faster than
38 deep-copying decoded data at every size measured. Write changes back with
39 C<< $row->update >>.
40  
41 Reading a large document often means decoding it each time, but
42 C<< $row->raw_field >> gives the JSON text without decoding.
43  
44 =head1 SYNOPSIS
45  
46 # json and jsonb fields are typed
47 my $orm = DBIx::QuickORM->new(credentials => \%credentials);
48  
49 my $ann = $orm->insert(
50 users => {
51 name => 'Ann',
52 prefs => {
53 theme => 'dark',
54 tags => ['a', 'b'],
55 },
56 },
57 );
58  
59 my $theme = $ann->field('prefs')->{theme};
60 my $text = $ann->raw_field('prefs'); # the JSON text
61  
62 # a plain string is JSON text
63 $orm->insert(
64 users => {
65 name => 'Bo',
66 prefs => '{"theme":"light"}',
67 },
68 );
69  
70 # Keep a JSON field as text:
71 my $plain = DBIx::QuickORM->new(
72 credentials => \%credentials,
73 types => {users => {prefs => undef}},
74 );
75  
76 my $json = $plain->find(users => $ann->field('id'))->field('prefs');
77  
78 =cut
79  
80 84 my $JSON = Cpanel::JSON::XS->new->canonical(1)->allow_nonref(1);
81 84 my $JSON_BYTES = Cpanel::JSON::XS->new->utf8(1)->allow_nonref(1);
82  
83 =head1 PUBLIC METHODS
84  
85 =over 4
86  
87 =item @types = $type->base_types
88  
89 C<json> and C<jsonb>.
90  
91 =item $bool = $type->is_inflated($value)
92  
93 True for a reference, which is never JSON text. Undef for a plain value: it
94 cannot tell, so the value is sent as given.
95  
96 =item $bool = $type->inflate_on_each_read
97  
98 True: rows decode the JSON text for every read instead of caching decoded
99 data and copying it.
100  
101 =item $db_type = $type->db_type_for($dialect)
102  
103 C<jsonb> on PostgreSQL 9.4 and later, C<json> elsewhere.
104  
105 =back
106  
107 =cut
108  
109 431 ▶sub base_types { qw/json jsonb/ }
Subroutine base_types: called CC=1 SCAR=0.0
Pod documented
110 9 ▶sub db_type_for { $_[1]->name eq 'PostgreSQL' && ($_[1]->server_version // 0) >= 90400 ? 'jsonb' : 'json' }
Subroutine db_type_for: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->name eq 'PostgreSQL' && ($_[1]->server_version // 0) >= 90400 ? : 2 7 9
Condition80%4/5
$_[1]->name eq "PostgreSQL" and ($_[1]->server_version // 0) >= 90400
!ll&&!rl&&r
522
$_[1]->server_version // 0
l!l
40
MC/DC50%1/2
$_[1]->name eq "PostgreSQL" and ($_[1]->server_version // 0) >= 90400
$_[1]->name eq "PostgreSQL" $_[1]->server_version
Truth table66%2/3
A and B
A= $_[1]->name eq "PostgreSQL"B= $_[1]->server_version
ABresult
0X0
100
111
111 10 ▶sub is_inflated { ref($_[1]) ? 1 : undef }
Subroutine is_inflated: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 2 8 10
112 39 ▶sub inflate_on_each_read { 1 }
Subroutine inflate_on_each_read: called CC=1 SCAR=0.0
Pod documented
113  
114 =pod
115  
116 =over 4
117  
118 =item $bool = $type->matches($field_name, \%field, $dialect)
119  
120 True for a field of C<string> affinity named C<json>, C<..._json>,
121 C<json_...>, or C<..._json_...>.
122  
123 This covers MariaDB, whose C<JSON> columns report themselves as C<longtext>.
124  
125 =back
126  
127 =cut
128  
129 2649 ▶sub matches {
Subroutine matches: called CC=4 SCAR=13.9
Pod documented
130 2649 my $class = shift;
131 2649 my ($name, $field) = @_;
132 2649 ▶ return ($field->{affinity} // '') eq 'string' && $name =~ m/(?:^|_)json(?:_|$)/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($field->{'affinity'} // '') eq 'string' && $name =~ /(?:^|_)json(?:_|$)/i ? : 19 2630 2649
Condition100%5/5
($field->{"affinity"} // "") eq "string" and $name =~ /(?:^|_)json(?:_|$)/i
!ll&&!rl&&r
1270136019
$field->{"affinity"} // ""
l!l
26481
MC/DC100%2/2
($field->{"affinity"} // "") eq "string" and $name =~ /(?:^|_)json(?:_|$)/i
$field->{"affinity"} $name =~ /(?:^|_)json(?:_|$)/i
Truth table100%3/3
A and B
A= $field->{"affinity"}B= $name =~ /(?:^|_)json(?:_|$)/i
ABresult
0X0
100
111
133 }
134  
135 =pod
136  
137 =over 4
138  
139 =item $data = $type->inflate($raw, \%field, $conn)
140  
141 Decodes the JSON text.
142  
143 Text without Perl's UTF-8 flag, as the driver returns MySQL's C<JSON>
144 columns, is decoded as UTF-8 bytes.
145  
146 =back
147  
148 =cut
149  
150 49 ▶sub inflate {
Subroutine inflate: called CC=2 SCAR=6.9
Pod documented
151 49 my $class = shift;
152 49 my ($raw) = @_;
153 49 ▶ return utf8::is_utf8($raw) ? $JSON->decode($raw) : $JSON_BYTES->decode($raw);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($raw) ? : 38 11 49
154 }
155  
156 =pod
157  
158 =over 4
159  
160 =item $raw = $type->deflate($value, \%field, $conn)
161  
162 Canonical JSON text for a reference. A plain value is returned as given.
163  
164 =back
165  
166 =cut
167  
168 36 ▶sub deflate {
Subroutine deflate: called CC=2 SCAR=6.9
Pod documented
169 36 my $class = shift;
170 36 my ($value) = @_;
171 36 ▶ return ref($value) ? $JSON->encode($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 35 1 36
172 }
173  
174 84 1;
175  
176 __END__
« blib/lib/DBIx/QuickORM/Exception/DB.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm »