1 package OpenSRF::Utils::JSON;
7 our $parser = JSON::XS->new;
8 $parser->ascii(1); # output \u escaped strings for any char with a value over 127
9 $parser->allow_nonref(1); # allows non-reference values to equate to themselves (see perldoc)
12 our $JSON_CLASS_KEY = '__c'; # points to the classname of encoded objects
13 our $JSON_PAYLOAD_KEY = '__p'; # same, for payload
18 OpenSRF::Utils::JSON - Serialize/Vivify objects
22 C<O::U::JSON> is a functional-style package which exports nothing. All
23 calls to routines must use the fully-qualified name, and expect an
26 OpenSRF::Utils::JSON->JSON2perl($string);
28 The routines which are called by existing external code all deal with
29 the serialization/stringification of objects and their revivification.
33 =head2 register_class_hint
35 This routine is used by objects which wish to serialize themselves
36 with the L</perl2JSON> routine. It has two required arguments, C<name>
39 OpenSRF::Util::JSON->register_class_hint( hint => 'osrfException',
40 name => 'OpenSRF::DomainObject::oilsException');
42 Where C<hint> can be any unique string (but canonically is the name
43 from the IDL which matches the object being operated on), and C<name>
44 is the language-specific classname which objects will be revivified
49 sub register_class_hint {
50 # FIXME hint can't be a dupe
51 # FIXME fail unless we have hint and name
52 my ($pkg, %args) = @_;
53 $_class_map{hints}{$args{hint}} = \%args;
54 $_class_map{classes}{$args{name}} = \%args;
63 my( $pkg, $string ) = @_;
64 my $perl = $pkg->rawJSON2perl($string);
65 return $pkg->JSONObject2Perl($perl);
74 my( $pkg, $obj ) = @_;
75 my $json = $pkg->perl2JSONObject($obj);
76 return $pkg->rawPerl2JSON($json);
80 =head2 JSONObject2Perl
87 if( $ref eq 'HASH' ) {
88 if( defined($obj->{$JSON_CLASS_KEY})) {
89 my $class = $obj->{$JSON_CLASS_KEY};
92 if( $obj = $pkg->JSONObject2Perl($obj->{$JSON_PAYLOAD_KEY}) ) {
93 $class = $pkg->lookup_class($class) || $class;
94 return bless(\$obj, $class) unless ref($obj);
95 return bless($obj, $class);
99 for my $k (keys %$obj) {
100 $obj->{$k} = $pkg->JSONObject2Perl($obj->{$k})
101 unless ref($obj->{$k}) eq 'JSON::XS::Boolean';
103 } elsif( $ref eq 'ARRAY' ) {
104 for my $i (0..scalar(@$obj) - 1) {
105 $obj->[$i] = $pkg->JSONObject2Perl($obj->[$i])
106 unless ref($obj->[$i]) eq 'JSON::XS::Boolean';
113 =head2 perl2JSONObject
117 sub perl2JSONObject {
118 my ($pkg, $obj) = @_;
121 return $obj unless $ref;
123 return $obj if $ref eq 'JSON::XS::Boolean';
126 if(UNIVERSAL::isa($obj, 'HASH')) {
128 $newobj->{$_} = $pkg->perl2JSONObject($obj->{$_}) for (keys %$obj);
129 } elsif(UNIVERSAL::isa($obj, 'ARRAY')) {
131 $newobj->[$_] = $pkg->perl2JSONObject($obj->[$_]) for(0..scalar(@$obj) - 1);
134 if($ref ne 'HASH' and $ref ne 'ARRAY') {
135 $ref = $pkg->lookup_hint($ref) || $ref;
136 $newobj = {$JSON_CLASS_KEY => $ref, $JSON_PAYLOAD_KEY => $newobj};
145 Internal routine used by L</JSON2Perl>. Wrapper around
151 my ($pkg, $json) = @_;
152 return undef unless defined $json and $json !~ /^\s*$/o;
153 return $parser->decode($json);
159 Internal routine used by L</Perl2JSON>. Wrapper around
165 my ($pkg, $perl) = @_;
166 return $parser->encode($perl);
175 # FIXME when there are tests, see if these two routines can be
176 # rewritten as one, or at least made to do lookup in the structure
177 # they're named after. best case: flatten _class_map, since hints
178 # and classes are identical
179 my ($pkg, $hint) = @_;
180 return $_class_map{hints}{$hint}{name}
189 my ($pkg, $class) = @_;
190 return $_class_map{classes}{$class}{hint}
195 Wrapper for JSON::XS::true. J::X::true and J::X::false, according to
196 its documentation, "are JSON atoms become JSON::XS::true and
197 JSON::XS::false, respectively. They are overloaded to act almost
198 exactly like the numbers 1 and 0"
202 sub true { return $parser->true }
210 sub false { return $parser->false }