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

filter: Statementstmt 100% 29 / 29 Branchbran 100% 8 / 8 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 100% 60 / 60 CC 7 SCAR 7.5
CC
7
Cov
100%
matches
4.0
is_inflated
2.0
inflate
2.0
CRAP 2.1
« blib/lib/DBIx/QuickORM/Dialect/MariaDB.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Type.pm »
1 package DBIx::QuickORM::Type::JSON;
2 77 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 77 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 77 our $VERSION = '0.000032';
6  
7 77 ▶use Cpanel::JSON::XS();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 77 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 77 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 77 my $JSON = Cpanel::JSON::XS->new->canonical(1)->allow_nonref(1);
81 77 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 =back
102  
103 =cut
104  
105 314 ▶sub base_types { qw/json jsonb/ }
Subroutine base_types: called CC=1 SCAR=0.0
Pod documented
106 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
107 34 ▶sub inflate_on_each_read { 1 }
Subroutine inflate_on_each_read: called CC=1 SCAR=0.0
Pod documented
108  
109 =pod
110  
111 =over 4
112  
113 =item $bool = $type->matches($field_name, \%field, $dialect)
114  
115 True for a field of C<string> affinity named C<json>, C<..._json>,
116 C<json_...>, or C<..._json_...>.
117  
118 This covers MariaDB, whose C<JSON> columns report themselves as C<longtext>.
119  
120 =back
121  
122 =cut
123  
124 2110 ▶sub matches {
Subroutine matches: called CC=4 SCAR=13.9
Pod documented
125 2110 my $class = shift;
126 2110 my ($name, $field) = @_;
127 2110 ▶ return ($field->{affinity} // '') eq 'string' && $name =~ m/(?:^|_)json(?:_|$)/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($field->{'affinity'} // '') eq 'string' && $name =~ /(?:^|_)json(?:_|$)/i ? : 16 2094 2110
Condition100%5/5
($field->{"affinity"} // "") eq "string" and $name =~ /(?:^|_)json(?:_|$)/i
!ll&&!rl&&r
958113616
$field->{"affinity"} // ""
l!l
21091
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
128 }
129  
130 =pod
131  
132 =over 4
133  
134 =item $data = $type->inflate($raw, \%field, $conn)
135  
136 Decodes the JSON text.
137  
138 Text without Perl's UTF-8 flag, as the driver returns MySQL's C<JSON>
139 columns, is decoded as UTF-8 bytes.
140  
141 =back
142  
143 =cut
144  
145 44 ▶sub inflate {
Subroutine inflate: called CC=2 SCAR=6.9
Pod documented
146 44 my $class = shift;
147 44 my ($raw) = @_;
148 44 ▶ return utf8::is_utf8($raw) ? $JSON->decode($raw) : $JSON_BYTES->decode($raw);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($raw) ? : 33 11 44
149 }
150  
151 =pod
152  
153 =over 4
154  
155 =item $raw = $type->deflate($value, \%field, $conn)
156  
157 Canonical JSON text for a reference. A plain value is returned as given.
158  
159 =back
160  
161 =cut
162  
163 33 ▶sub deflate {
Subroutine deflate: called CC=2 SCAR=6.9
Pod documented
164 33 my $class = shift;
165 33 my ($value) = @_;
166 33 ▶ return ref($value) ? $JSON->encode($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 32 1 33
167 }
168  
169 77 1;
170  
171 __END__
« blib/lib/DBIx/QuickORM/Dialect/MariaDB.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Type.pm »