Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session/Serialize/JSON.pm

raw
1 package Lemonldap::NG::Common::Apache::Session::Serialize::JSON;
2
3 use strict;
4 use JSON qw(to_json from_json);
5
6 our $VERSION = '2.19.0';
7
8 sub serialize {
9 my $session = shift;
10
11 $session->{serialized} = to_json( $session->{data}, { allow_nonref => 1 } );
12 }
13
14 sub unserialize {
15 my $session = shift;
16
17 my $data = _unserialize( $session->{serialized} );
18 die "Session could not be unserialized" unless defined $data;
19 $session->{data} = $data;
20 }
21
22 sub unserializeBase64 {
23 my $session = shift;
24
25 my $data = _unserialize( $session->{serialized}, \&decodeThaw64 );
26 die "Session could not be unserialized" unless defined $data;
27 $session->{data} = $data;
28 }
29
30 sub decodeThaw64 {
31 require MIME::Base64;
32 my $s = shift;
33 return Storable::thaw( MIME::Base64::decode_base64($s) );
34 }
35
36 sub _unserialize {
37 my ( $serialized, $next ) = @_;
38 my $tmp;
39 eval { $tmp = from_json( $serialized, { allow_nonref => 1 } ) };
40 if ($@) {
41 eval {
42 require Storable;
43 $next ||= \&Storable::thaw;
44 return &$next($serialized);
45 };
46 if ($@) {
47
48 # Error is catched in unserialize
49 return undef;
50 }
51 }
52 return $tmp;
53 }
54
55 1;
56
57 =pod
58
59 =head1 NAME
60
61 =encoding utf8
62
63 Lemonldap::NG::Common::Apache::Session::Serialize::JSON - Use JSON to zip up data
64
65 =head1 SYNOPSIS
66
67 use Lemonldap::NG::Common::Apache::Session::Serialize::JSON;
68
69 $zipped = Lemonldap::NG::Common::Apache::Session::Serialize::JSON::serialize($ref);
70 $ref = Lemonldap::NG::Common::Apache::Session::Serialize::JSON::unserialize($zipped);
71
72 =head1 DESCRIPTION
73
74 This module fulfills the serialization interface of Apache::Session.
75 It serializes the data in the session object by use of JSON C<to_json>
76 and C<from_json>. The serialized data is UTF-8 text.
77
78
79 =head1 SEE ALSO
80
81 L<JSON>, L<Apache::Session>
82
83 =head1 AUTHORS
84
85 =over
86
87 =item Clement Oudot, E<lt>[email protected]<gt>
88
89 =item François-Xavier Deltombe, E<lt>[email protected]<gt>
90
91 =item Xavier Guimard, E<lt>[email protected]<gt>
92
93 =item Thomas Chemineau, E<lt>[email protected]<gt>
94
95 =back
96
97 =head1 BUG REPORT
98
99 Use OW2 system to report bug or ask for features:
100 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
101
102 =head1 DOWNLOAD
103
104 Lemonldap::NG is available at
105 L<https://lemonldap-ng.org/download>
106
107 =head1 COPYRIGHT AND LICENSE
108
109 =over
110
111 =item Copyright (C) 2015-2016 by Clément Oudot, E<lt>[email protected]<gt>
112
113 =item Copyright (C) 2015-2016 by Xavier Guimard, E<lt>[email protected]<gt>
114
115 =back
116
117 This library is free software; you can redistribute it and/or modify
118 it under the terms of the GNU General Public License as published by
119 the Free Software Foundation; either version 2, or (at your option)
120 any later version.
121
122 This program is distributed in the hope that it will be useful,
123 but WITHOUT ANY WARRANTY; without even the implied warranty of
124 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
125 GNU General Public License for more details.
126
127 You should have received a copy of the GNU General Public License
128 along with this program. If not, see L<http://www.gnu.org/licenses/>.
129
130 =cut