1 package OpenSRF::Utils::JSON;
4 my $parser = JSON::XS->new;
5 $parser->ascii(1); # output \u escaped strings for any char with a value over 127
6 $parser->allow_nonref(1); # allows non-reference values to equate to themselves (see perldoc)
9 my $JSON_CLASS_KEY = '__c';
10 my $JSON_PAYLOAD_KEY = '__p';
15 OpenSRF::Utils::JSON - Bucket-o-Routines for JSON
19 C<O::U::JSON> is a functional-style package which exports nothing. All
20 calls to routines must use the fully-qualified name, and expect an
23 OpenSRF::Utils::JSON->JSON2perl($string);
25 Most routines are straightforward data<->JSON transformation wrappers
26 around L<JSON::XS>, but some (like L</register_class_hint>) provide
27 OpenSRF functionality.
31 =head2 register_class_hint
35 sub register_class_hint {
36 my ($pkg, %args) = @_;
37 $_class_map{hints}{$args{hint}} = \%args;
38 $_class_map{classes}{$args{name}} = \%args;
46 my ($pkg, $hint) = @_;
47 return $_class_map{hints}{$hint}{name}
55 my ($pkg, $class) = @_;
56 return $_class_map{classes}{$class}{hint}
64 my( $pkg, $string ) = @_;
65 my $perl = $class->rawJSON2perl($string);
66 return $class->JSONObject2Perl($perl);
74 my( $pkg, $obj ) = @_;
75 my $json = $pkg->perl2JSONObject($obj);
76 return $pkg->rawPerl2JSON($json);
81 Internal routine used by L</JSON2Perl>. Wrapper around
87 my ($class, $json) = @_;
88 return undef unless defined $json and $json !~ /^\s*$/o;
89 return $parser->decode($json);
94 Internal routine used by L</Perl2JSON>. Wrapper around
100 my ($class, $perl) = @_;
101 return $parser->encode($perl);
104 =head2 JSONObject2Perl
108 sub JSONObject2Perl {
109 my ($pkg, $obj) = @_;
111 if( $ref eq 'HASH' ) {
112 if( defined($obj->{$JSON_CLASS_KEY})) {
113 my $class = $obj->{$JSON_CLASS_KEY};
116 if( $obj = $pkg->JSONObject2Perl($obj->{$JSON_PAYLOAD_KEY}) ) {
117 $class = $pkg->lookup_class($class) || $class;
118 return bless(\$obj, $class) unless ref($obj);
119 return bless($obj, $class);
123 for my $k (keys %$obj) {
124 $obj->{$k} = $pkg->JSONObject2Perl($obj->{$k})
125 unless ref($obj->{$k}) eq 'JSON::XS::Boolean';
127 } elsif( $ref eq 'ARRAY' ) {
128 for my $i (0..scalar(@$obj) - 1) {
129 $obj->[$i] = $pkg->JSONObject2Perl($obj->[$i])
130 unless ref($obj->[$i]) eq 'JSON::XS::Boolean';
136 =head2 perl2JSONObject
140 sub perl2JSONObject {
145 return $obj unless $ref;
147 return $obj if $ref eq 'JSON::XS::Boolean';
150 if(UNIVERSAL::isa($obj, 'HASH')) {
152 $newobj->{$_} = $class->perl2JSONObject($obj->{$_}) for (keys %$obj);
153 } elsif(UNIVERSAL::isa($obj, 'ARRAY')) {
155 $newobj->[$_] = $class->perl2JSONObject($obj->[$_]) for(0..scalar(@$obj) - 1);
158 if($ref ne 'HASH' and $ref ne 'ARRAY') {
159 $ref = $class->lookup_hint($ref) || $ref;
160 $newobj = {$JSON_CLASS_KEY => $ref, $JSON_PAYLOAD_KEY => $newobj};
168 Wrapper for JSON::XS::true. J::X::true and J::X::false, according to
169 its documentation, "are JSON atoms become JSON::XS::true and
170 JSON::XS::false, respectively. They are overloaded to act almost
171 exactly like the numbers 1 and 0"
175 sub true { return $parser->true }
183 sub false { return $parser->false }