Serialize/Hstore.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 63 CC 6 SCAR 21.7
CC
6
Cov
0%
serialize
12.0
_unserialize
12.0
unserialize
6.0
CRAP 8.8
1 package Apache::Session::Serialize::Hstore;
2  
3 use strict;
4 use JSON qw(to_json from_json);
5  
6 our $VERSION = '1.2.5';
7  
8 sub serialize {
9 my ($session) = @_;
10 $session->{serialized} = {};
11 my $data = $session->{data};
12 my $res = '';
13 if ( ref $data and %$data ) {
14 foreach ( keys %$data ) {
15 my $v;
16 if ( ref $data->{$_} ) {
17 $v = '_json://' . to_json( $data->{$_} );
18 }
19 else {
20 $v = $data->{$_};
21 }
22 $v =~ s/"/#%22/g;
23 $res .= qq'"$_" => "$v",';
24 }
25 }
26 $res =~ s/,$//;
27 $session->{serialized} = $res;
28 }
29  
30 sub unserialize {
31 my ($session) = @_;
32  
33 my $data = _unserialize( $session->{serialized} );
34 die "Session could not be unserialized" unless defined $data;
35 $session->{data} = $data;
36 }
37  
38 sub _unserialize {
39 my ( $serialized, $next ) = @_;
40 my $res = {};
41 while ( $serialized =~ s/\s*"([^"]*)"\s*=>\s*"([^"]*)"\s*,?// ) {
42 my ( $k, $v ) = ( $1, $2 );
43 $v =~ s/#%22/"/g;
44 if ( $v =~ s#^_json://## ) {
45 my $tmp;
46 eval { $tmp = from_json($v) };
47 if ($@) {
48 print STDERR "JSON error: $@\n";
49 return undef;
50 }
51 $v = $tmp;
52 }
53 $res->{$k} = $v;
54 }
55 return $res;
56 }
57  
58 1;
59  
60 =pod
61  
62 =head1 NAME
63  
64 =encoding utf8
65  
66 Apache::Session::Serialize::Hstore - Serialize/unserialize datas for PostgreSQL
67 "hstore" storage.
68  
69 =head1 SYNOPSIS
70  
71 use Apache::Session::Serialize::Hstore;
72  
73 $zipped = Apache::Session::Serialize::Hstore::serialize($ref);
74 $ref = Apache::Session::Serialize::Hstore::unserialize($zipped);
75  
76 =head1 DESCRIPTION
77  
78 This module fulfills the serialization interface of Apache::Session.
79 It serializes only ref data value for PostgreSQL "hstore" fields.
80  
81 =head1 SEE ALSO
82  
83 L<JSON>, L<Apache::Session>
84  
85 =head1 BUG REPORT
86  
87 Use OW2 system to report bug or ask for features:
88 L<http://jira.ow2.org>
89  
90 =head1 COPYRIGHT AND LICENSE
91  
92 =over
93  
94 =item 2009-2025 by Xavier Guimard
95  
96 =item 2013-2025 by Clément Oudot
97  
98 =item 2019-2025 by Maxime Besson
99  
100 =item 2013-2025 by Worteks
101  
102 =item 2023-2025 by Linagora
103  
104 =back
105  
106 This library is free software; you can redistribute it and/or modify
107 it under the terms of the GNU General Public License as published by
108 the Free Software Foundation; either version 2, or (at your option)
109 any later version.
110  
111 This program is distributed in the hope that it will be useful,
112 but WITHOUT ANY WARRANTY; without even the implied warranty of
113 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
114 GNU General Public License for more details.
115  
116 You should have received a copy of the GNU General Public License
117 along with this program. If not, see L<http://www.gnu.org/licenses/>.
118  
119 =cut