Lite/Serializer/JSON.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 130 CC 18 SCAR 37.4
CC
18
Cov
0%
Serialize
90.0
Deserialize
56.0
HandleJSONsNotStringCrap
20.0
CRAP 42.1
1 package RPC::Lite::Serializer::JSON;
2  
3 use strict;
4 use base qw( RPC::Lite::Serializer );
5  
6 use RPC::Lite::Request;
7 use RPC::Lite::Response;
8 use RPC::Lite::Notification;
9 use RPC::Lite::Error;
10  
11 use JSON;
12  
13 use Data::Dumper;
14  
15 our $DEBUG = $ENV{DEBUG_SERIALIZER};
16  
17 sub VersionSupported
18 {
19 my $self = shift;
20 my $version = shift;
21  
22 # FIXME make sure we support this serializer version
23 return 1;
24 }
25  
26 sub GetVersion
27 {
28 my $self = shift;
29 return $JSON::VERSION;
30 }
31  
32 sub Serialize
33 {
34 my $self = shift;
35 my $object = shift;
36
37 my $type = ref($object);
38
39 # plain perl data structures...
40 if(!defined($type))
41 {
42 # no-op
43 }
44 elsif($type eq 'HASH')
45 {
46 # no-op
47 }
48 elsif($type eq 'ARRAY')
49 {
50 # no-op
51 }
52 elsif($type eq 'RPC::Lite::Request')
53 {
54 # effectively 'unbless' this thing
55 $object =
56 {
57 class => $type,
58 method => $object->{method},
59 params => $object->{params}, # assume simple types, could recurse to attempt to throw real objects over the wire
60 id => $object->{id},
61 };
62 }
63 elsif($type eq 'RPC::Lite::Response')
64 {
65 $object =
66 {
67 class => $type,
68 result => $object->{result}, # assume simple types
69 error => $object->{error},
70 id => $object->{id},
71 };
72 }
73 elsif($type eq 'RPC::Lite::Notification')
74 {
75 $object =
76 {
77 class => $type,
78 params => $object->{params}, # assume simple types
79 method => $object->{method},
80 id => $object->{id}, # will be undef
81 };
82 }
83 elsif($type eq 'RPC::Lite::Error')
84 {
85 $object =
86 {
87 class => $type,
88 result => $object->{result}, # undef
89 error => $object->{error},
90 id => $object->{id},
91 };
92 }
93 else # try our best
94 {
95 # JSON should unbless this for us...
96 }
97  
98 my $data = to_json( $object, { convert_blessed => 1 } );
99  
100 $self->_Debug('Serializing', Dumper($object), $data) if($DEBUG);
101  
102 return $data;
103 }
104  
105 sub Deserialize
106 {
107 my $self = shift;
108 my $data = shift;
109  
110 length($data) or return undef;
111
112 my $object = from_json( $data, { convert_blessed => 1 } );
113 $self->HandleJSONsNotStringCrap(\$object);
114  
115 my $result = $object;
116  
117 if(defined($object->{class}))
118 {
119 my $type = delete $object->{class};
120
121 if($type eq 'RPC::Lite::Request')
122 {
123 $result = $type->new($object->{method}, $object->{params});
124 $result->Id($object->{id});
125 }
126 elsif($type eq 'RPC::Lite::Response')
127 {
128 $result = $type->new($object->{result});
129 $result->Id($object->{id});
130 }
131 elsif($type eq 'RPC::Lite::Notification')
132 {
133 $result = $type->new($object->{method}, $object->{params});
134 }
135 elsif($type eq 'RPC::Lite::Error')
136 {
137 $result = $type->new($object->{error});
138 $result->Id($object->{id});
139 }
140 }
141  
142 $self->_Debug('Deserializing', $data, Dumper($result)) if($DEBUG);
143
144 return $result;
145 }
146  
147 sub HandleJSONsNotStringCrap
148 {
149 my $self = shift;
150 my $ref = shift;
151
152 my $type = ref($$ref);
153
154 if($type eq 'ARRAY')
155 {
156 foreach my $element (@$$ref)
157 {
158 $self->HandleJSONsNotStringCrap(\$element);
159 }
160 }
161 elsif($type eq 'HASH')
162 {
163 foreach my $key (keys(%$$ref))
164 {
165 $self->HandleJSONsNotStringCrap(\$$ref->{$key});
166 }
167 }
168 elsif($type eq 'JSON::NotString')
169 {
170 $$ref = $$ref->{value};
171 }
172 }
173  
174 sub _Debug
175 {
176 my $self = shift;
177 my $action = shift;
178 my $input = shift;
179 my $output = shift;
180  
181 print qq{
182 $action
183 ===================================================================
184  
185 input:
186
187 $input
188
189 output:
190
191 $output
192
193 ===================================================================
194
195 };
196 }
197  
198 1;