Line 0
Link Here
|
0 |
- |
1 |
package Koha::Object::JSONFields; |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use JSON; |
21 |
use Try::Tiny; |
22 |
|
23 |
use Koha::Exceptions; |
24 |
use Koha::Exceptions::Object; |
25 |
|
26 |
=head1 NAME |
27 |
|
28 |
Koha::Object::JSONFields - Class that adds JSON field manipulation helper methods |
29 |
for Koha::Object-derived classes |
30 |
|
31 |
=head1 SYNOPSIS |
32 |
|
33 |
use base qw(Koha::Object Koha::Object::JSONFields); |
34 |
my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } ); |
35 |
my $field_name_hashref = $object->decode_json_field({ field => 'field_name' }); |
36 |
$object->encode_json_field({ field => 'field_name', data => $data }); |
37 |
|
38 |
=head1 API |
39 |
|
40 |
=head2 Class methods |
41 |
|
42 |
=head3 decode_json_field |
43 |
|
44 |
my $hashref = $object->decode_json_field({ field => 'field_name' }); |
45 |
|
46 |
Returns a data structure representing the JSON-decoded value of field I<field_name>. |
47 |
|
48 |
=cut |
49 |
|
50 |
sub decode_json_field { |
51 |
my ( $self, $params ) = @_; |
52 |
|
53 |
Koha::Exceptions::MissingParameter->throw( parameter => 'field' ) |
54 |
unless defined $params->{field}; |
55 |
|
56 |
my $field = $params->{field}; |
57 |
|
58 |
return try { |
59 |
$self->$field ? $self->_json->decode( $self->$field ) : undef; |
60 |
} |
61 |
catch { |
62 |
Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); |
63 |
}; |
64 |
} |
65 |
|
66 |
=head3 set_encoded_json_field |
67 |
|
68 |
$object->set_encoded_json_field( |
69 |
{ data => $data, |
70 |
field => 'field_name', |
71 |
} |
72 |
); |
73 |
|
74 |
Sets a JSON string encoded representation of I<$data> into the object's I<field_name> |
75 |
attribute. |
76 |
|
77 |
=cut |
78 |
|
79 |
sub set_encoded_json_field { |
80 |
my ( $self, $params ) = @_; |
81 |
|
82 |
Koha::Exceptions::MissingParameter->throw( parameter => 'field' ) |
83 |
unless defined $params->{field}; |
84 |
|
85 |
Koha::Exceptions::MissingParameter->throw( parameter => 'data' ) |
86 |
unless exists $params->{data}; |
87 |
|
88 |
my $field = $params->{field}; |
89 |
my $data = $params->{data}; |
90 |
|
91 |
return try { |
92 |
$self->$field( $data ? $self->_json->encode($data) : undef ); |
93 |
} |
94 |
catch { |
95 |
Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); |
96 |
}; |
97 |
} |
98 |
|
99 |
=head2 Internal methods |
100 |
|
101 |
=head3 _json |
102 |
|
103 |
my $JSON_object = $self->_json; |
104 |
|
105 |
Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be |
106 |
done later. |
107 |
|
108 |
=cut |
109 |
|
110 |
sub _json { |
111 |
my ($self) = @_; |
112 |
$self->{_json} //= JSON->new->utf8(0); |
113 |
return $self->{_json}; |
114 |
} |
115 |
|
116 |
=head1 AUTHOR |
117 |
|
118 |
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt> |
119 |
|
120 |
=cut |
121 |
|
122 |
1; |