File Coverage

blib/lib/Apache/Session/Serialize/JSON.pm
Criterion Covered Total %
statement 18 21 85.7
branch 2 4 50.0
condition 0 2 0.0
subroutine 5 5 100.0
pod 0 2 0.0
total 25 34 73.5


line stmt bran cond sub pod time code
1             # Directly copied from Lemonldap::NG project (http://lemonldap-ng.org/)
2             package Apache::Session::Serialize::JSON;
3              
4 5     5   32 use strict;
  5         12  
  5         304  
5 5     5   4143 use JSON qw(to_json from_json);
  5         65799  
  5         35  
6              
7             our $VERSION = '1.2.6';
8              
9             sub serialize {
10 114     114 0 41451 my $session = shift;
11              
12 114         899 $session->{serialized} = to_json( $session->{data}, { allow_nonref => 1 } );
13             }
14              
15             sub unserialize {
16 148     148 0 233 my ( $session, $next ) = @_;
17              
18 148         257 my $data = _unserialize( $session->{serialized}, $next );
19 148 50       261 die "Session could not be unserialized" unless defined $data;
20 148         266 $session->{data} = $data;
21             }
22              
23             sub _unserialize {
24 456     456   1163 my ( $serialized, $next ) = @_;
25 456         737 my $tmp;
26 456         766 eval { $tmp = from_json( $serialized, { allow_nonref => 1 } ) };
  456         4949  
27 456 50       20594 if ($@) {
28 0         0 require Storable;
29 0   0     0 $next ||= \&Storable::thaw;
30 0         0 return &$next($serialized);
31             }
32 456         12982 return $tmp;
33             }
34              
35             1;
36              
37             =pod
38              
39             =head1 NAME
40              
41             =encoding utf8
42              
43             Apache::Session::Serialize::JSON - Use JSON to zip up data
44              
45             =head1 SYNOPSIS
46              
47             use Apache::Session::Serialize::JSON;
48              
49             $zipped = Apache::Session::Serialize::JSON::serialize($ref);
50             $ref = Apache::Session::Serialize::JSON::unserialize($zipped);
51              
52             =head1 DESCRIPTION
53              
54             This module fulfills the serialization interface of Apache::Session.
55             It serializes the data in the session object by use of JSON C
56             and C. The serialized data is UTF-8 text.
57              
58              
59             =head1 SEE ALSO
60              
61             L, L
62              
63             =head1 BUG REPORT
64              
65             Use OW2 system to report bug or ask for features:
66             L
67              
68             =head1 COPYRIGHT AND LICENSE
69              
70             =over
71              
72             =item 2009-2025 by Xavier Guimard
73              
74             =item 2013-2025 by ClĂ©ment Oudot
75              
76             =item 2019-2025 by Maxime Besson
77              
78             =item 2013-2025 by Worteks
79              
80             =item 2023-2025 by Linagora
81              
82             =back
83              
84             This library is free software; you can redistribute it and/or modify
85             it under the terms of the GNU General Public License as published by
86             the Free Software Foundation; either version 2, or (at your option)
87             any later version.
88              
89             This program is distributed in the hope that it will be useful,
90             but WITHOUT ANY WARRANTY; without even the implied warranty of
91             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
92             GNU General Public License for more details.
93              
94             You should have received a copy of the GNU General Public License
95             along with this program. If not, see L.
96              
97             =cut