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

filter: Statementstmt 100% 28 / 28 Branchbran 100% 8 / 8 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 100% 57 / 57 CC 7 SCAR 7.9
CC
7
Cov
100%
matches
4.0
is_inflated
2.0
inflate
2.0
CRAP 2.2
« blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm »
1 package DBIx::QuickORM::Type::JSON;
2 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 50 our $VERSION = '0.000031';
6  
7 50 ▶use Cpanel::JSON::XS();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 50 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 50 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 a row hands every read a
35 deep copy (see L<DBIx::QuickORM::Role::Type/Cached inflated values>):
36 changing the data read from a row changes neither the row nor the next read.
37 Write changes back with C<< $row->update >>.
38  
39 Reading a large document often means copying it each time, but
40 C<< $row->raw_field >> gives the JSON text without copying.
41  
42 =head1 SYNOPSIS
43  
44 # json and jsonb fields are typed
45 my $orm = DBIx::QuickORM->new(credentials => \%credentials);
46  
47 my $ann = $orm->insert(
48 users => {
49 name => 'Ann',
50 prefs => {
51 theme => 'dark',
52 tags => ['a', 'b'],
53 },
54 },
55 );
56  
57 my $theme = $ann->field('prefs')->{theme};
58 my $text = $ann->raw_field('prefs'); # the JSON text
59  
60 # a plain string is JSON text
61 $orm->insert(
62 users => {
63 name => 'Bo',
64 prefs => '{"theme":"light"}',
65 },
66 );
67  
68 # Keep a JSON field as text:
69 my $plain = DBIx::QuickORM->new(
70 credentials => \%credentials,
71 types => {users => {prefs => undef}},
72 );
73  
74 my $json = $plain->find(users => $ann->field('id'))->field('prefs');
75  
76 =cut
77  
78 50 my $JSON = Cpanel::JSON::XS->new->canonical(1)->allow_nonref(1);
79 50 my $JSON_BYTES = Cpanel::JSON::XS->new->utf8(1)->allow_nonref(1);
80  
81 =head1 PUBLIC METHODS
82  
83 =over 4
84  
85 =item @types = $type->base_types
86  
87 C<json> and C<jsonb>.
88  
89 =item $bool = $type->is_inflated($value)
90  
91 True for a reference, which is never JSON text. Undef for a plain value: it
92 cannot tell, so the value is sent as given.
93  
94 =back
95  
96 =cut
97  
98 268 ▶sub base_types { qw/json jsonb/ }
Subroutine base_types: called CC=1 SCAR=0.0
Pod documented
99 7 ▶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 5 7
100  
101 =pod
102  
103 =over 4
104  
105 =item $bool = $type->matches($field_name, \%field, $dialect)
106  
107 True for a field of C<string> affinity named C<json>, C<..._json>,
108 C<json_...>, or C<..._json_...>.
109  
110 This covers MariaDB, whose C<JSON> columns report themselves as C<longtext>.
111  
112 =back
113  
114 =cut
115  
116 1692 ▶sub matches {
Subroutine matches: called CC=4 SCAR=13.9
Pod documented
117 1692 my $class = shift;
118 1692 my ($name, $field) = @_;
119 1692 ▶ return ($field->{affinity} // '') eq 'string' && $name =~ m/(?:^|_)json(?:_|$)/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($field->{'affinity'} // '') eq 'string' && $name =~ /(?:^|_)json(?:_|$)/i ? : 15 1677 1692
Condition100%5/5
($field->{"affinity"} // "") eq "string" and $name =~ /(?:^|_)json(?:_|$)/i
!ll&&!rl&&r
77989815
$field->{"affinity"} // ""
l!l
16911
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
120 }
121  
122 =pod
123  
124 =over 4
125  
126 =item $data = $type->inflate($raw, \%field, $conn)
127  
128 Decodes the JSON text.
129  
130 Text without Perl's UTF-8 flag, as the driver returns MySQL's C<JSON>
131 columns, is decoded as UTF-8 bytes.
132  
133 =back
134  
135 =cut
136  
137 17 ▶sub inflate {
Subroutine inflate: called CC=2 SCAR=6.9
Pod documented
138 17 my $class = shift;
139 17 my ($raw) = @_;
140 17 ▶ return utf8::is_utf8($raw) ? $JSON->decode($raw) : $JSON_BYTES->decode($raw);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($raw) ? : 13 4 17
141 }
142  
143 =pod
144  
145 =over 4
146  
147 =item $raw = $type->deflate($value, \%field, $conn)
148  
149 Canonical JSON text for a reference. A plain value is returned as given.
150  
151 =back
152  
153 =cut
154  
155 18 ▶sub deflate {
Subroutine deflate: called CC=2 SCAR=6.9
Pod documented
156 18 my $class = shift;
157 18 my ($value) = @_;
158 18 ▶ return ref($value) ? $JSON->encode($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 17 1 18
159 }
160  
161 50 1;
162  
163 __END__
« blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm »