GX/Serializer/JSON.pm untested

filter: Statementstmt 0.0% 0 / 26 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 37 CC 3 SCAR 14.6
CC
3
Cov
0%
serialize
6.0
unserialize
6.0
BEGIN
2.0
CRAP 4.3
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Serializer/JSON.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Serializer::JSON;
7  
8 use GX::Exception ();
9  
10 use JSON ();
11  
12  
13 # ----------------------------------------------------------------------------------------------------------------------
14 # Class setup
15 # ----------------------------------------------------------------------------------------------------------------------
16  
17 use GX::Class;
18  
19 extends 'GX::Serializer';
20  
21 build;
22  
23  
24 # ----------------------------------------------------------------------------------------------------------------------
25 # Public methods
26 # ----------------------------------------------------------------------------------------------------------------------
27  
28 sub serialize {
29  
30 my $self = shift;
31 my $data = shift;
32  
33 my $string = eval { JSON::encode_json( $data ) };
34  
35 if ( $@ ) {
36 GX::Exception->complain(
37 message => "Cannot serialize data",
38 subexception => $@
39 );
40 }
41  
42 return $string;
43  
44 }
45  
46 sub unserialize {
47  
48 my $self = shift;
49 my $string = shift;
50  
51 my $data = eval { JSON::decode_json( $string ) };
52  
53 if ( $@ ) {
54 GX::Exception->complain(
55 message => "Cannot unserialize data",
56 subexception => $@
57 );
58 }
59  
60 return $data;
61  
62 }
63  
64  
65 1;
66  
67 __END__