summaryrefslogtreecommitdiffstats
path: root/qooxdoo/source/perl/CGI/Session/Serialize/json.pm
blob: 2f8c8bbb5a2e15dfd8438d6b9ae49cf1f3d54971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package CGI::Session::Serialize::json;

use strict;
use CGI::Session::ErrorHandler;

$CGI::Session::Serialize::json::VERSION = '4.20';
@CGI::Session::Serialize::json::ISA     = ( "CGI::Session::ErrorHandler" );
our $Flavour;

unless($Flavour) {
    my $package = (grep { eval("use $_ (); 1;") } qw(JSON::Syck))[0]
        or die "JSON::Syck is required to use ", __PACKAGE__;
    $Flavour = $package;
}

sub freeze {
    my ($self, $data) = @_;
    return $Flavour->can('Dump')->($data);
}


sub thaw {
    my ($self, $string) = @_;
    return ($Flavour->can('Load')->($string))[0];
}

1;

__END__;

=pod

=head1 NAME

CGI::Session::Serialize::json - serializer for CGI::Session

=head1 DESCRIPTION

This library can be used by CGI::Session to serialize session data. Requires
L<JSON::Syck|JSON::Syck>. JSON is a type of L<YAML|CGI::Session::Serialize::yaml>,
with one extension: serialized JSON strings are actually valid JavaScript
code that a browser can execute. Any langauge that has a YAML parser
(Perl, PHP, Python, Ruby, C, etc) can also read data that has been serialized
with JSON.

=head1 METHODS

=over 4

=item freeze($class, \%hash)

Receives two arguments. First is the class name, the second is the data to be serialized. Should return serialized string on success, undef on failure. Error message should be set using C<set_error()|CGI::Session::ErrorHandler/"set_error()">

=item thaw($class, $string)

Received two arguments. First is the class name, second is the I<JSON> data string. Should return thawed data structure on success, undef on failure. Error message should be set using C<set_error()|CGI::Session::ErrorHandler/"set_error()">

=back

=head1 SEE ALSO

L<CGI::Session>, L<JSON::Syck>.

=cut