]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/JSON.pm
removed number overloads
[OpenSRF.git] / src / perlmods / JSON.pm
1 package JSON::number;
2 sub new {
3         my $class = shift;
4         my $x = shift || $class;
5         return bless \$x => __PACKAGE__;
6 }
7
8 use overload ( '""' => \&toString );
9
10 sub toString { defined($_[1]) ? ${$_[1]} : ${$_[0]} }
11
12 package JSON::bool::true;
13 sub new { return bless {} => __PACKAGE__ }
14 use overload ( '""' => \&toString );
15 use overload ( 'bool' => sub { 1 } );
16 use overload ( '0+' => sub { 1 } );
17
18 sub toString { 'true' }
19
20 package JSON::bool::false;
21 sub new { return bless {} => __PACKAGE__ }
22 use overload ( '""' => \&toString );
23 use overload ( 'bool' => sub { 0 } );
24 use overload ( '0+' => sub { 0 } );
25
26 sub toString { 'false' }
27
28 package JSON;
29 use vars qw/%_class_map/;
30
31 sub register_class_hint {
32         my $class = shift;
33         my %args = @_;
34
35         $_class_map{hints}{$args{hint}} = \%args;
36         $_class_map{classes}{$args{name}} = \%args;
37 }
38
39 sub _JSON_regex {
40         my $string = shift;
41
42         $string =~ s/^\s* ( 
43                            {                            | # start object
44                            \[                           | # start array
45                            -?\d+\.?\d*                  | # number literal
46                            "(?:(?:\\[\"])|[^\"])*"      | # string literal
47                            (?:\/\*.+?\*\/)              | # C comment
48                            true                         | # bool true
49                            false                        | # bool false
50                            null                         | # undef()
51                            :                            | # object key-value sep
52                            ,                            | # list sep
53                            \]                           | # array end
54                            }                              # object end
55                         )
56                  \s*//sox;
57         return ($string,$1);
58 }
59
60 sub _json_hint_to_class {
61         my $type = shift;
62         my $hint = shift;
63
64         return $_class_map{hints}{$hint}{name} if (exists $_class_map{hints}{$hint});
65         
66         $type = 'hash' if ($type eq '}');
67         $type = 'array' if ($type eq ']');
68
69         JSON->register_class_hint(name => $hint, hint => $hint, type => $type);
70
71         return $hint;
72 }
73
74 sub JSON2perl {
75         my $class = shift;
76         local $_ = shift;
77
78         s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
79         s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
80         s/(?<!\\)\%/\\\%/gmo; # fixup % for later
81
82         # Convert JSON Unicode...
83         s/\\u(\d{4})/chr(hex($1))/esog;
84
85         # handle class blessings
86         s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
87         s/(\]|\}|")\s*\/\*--\s*E\w*?\s+(\S+)\s*--\*\//$1 => _json_hint_to_class("$1", "$2")) /sog;
88
89         my $re = qr/((?<!\\)"(?>(?<=\\)"|[^"])*(?<!\\)")/;
90         # Grab strings...
91         my @strings = /$re/sog;
92
93         # Replace with code...
94         #s/"(?:(?:\\[\"])|[^\"])*"/ do{ \$t = '"'.shift(\@strings).'"'; eval \$t;} /sog;
95         s/$re/ eval shift(\@strings) /sog;
96
97         # Perlify hash notation
98         s/:/ => /sog;
99
100         # Do numbers...
101 #       s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
102
103         # Change javascript stuff to perl...
104         s/null/ undef /sog;
105         s/true/ bless( {}, "JSON::bool::true") /sog;
106         s/false/ bless( {}, "JSON::bool::false") /sog;
107
108         my $ret;
109         return eval '$ret = '.$_;
110 }
111
112 sub old_JSON2perl {
113         my ($class, $json) = @_;
114
115         if (!defined($json)) {
116                 return undef;
117         }
118
119         $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
120         $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
121         $json =~ s/(?<!\\)\%/\\\%/gmo; # fixup % for later
122
123         my @casts;
124         my $casting_depth = 0;
125         my $current_cast;
126         my $element;
127         my $output = '';
128         while (($json,$element) = _JSON_regex($json)) {
129
130                 last unless ($element);
131
132                 if ($element eq 'null') {
133                         $output .= ' undef() ';
134                         next;
135                 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
136                         my $hint = $1;
137                         if (exists $_class_map{hints}{$hint}) {
138                                 $casts[$casting_depth] = $hint;
139                                 $output .= ' bless(';
140                         }
141                         next;
142                 } elsif ($element =~ /^\/\*/) {
143                         next;
144                 } elsif ($element =~ /^\d/) {
145                         $output .= "do { JSON::number::new($element) }";
146                         next;
147                 } elsif ($element eq '{' or $element eq '[') {
148                         $casting_depth++;
149                 } elsif ($element eq '}' or $element eq ']') {
150                         $casting_depth--;
151                         my $hint = $casts[$casting_depth];
152                         $casts[$casting_depth] = undef;
153                         if (defined $hint and exists $_class_map{hints}{$hint}) {
154                                 $output .= $element . ',"'. $_class_map{hints}{$hint}{name} . '")';
155                                 next;
156                         }
157                 } elsif ($element eq ':') {
158                         $output .= ' => ';
159                         next;
160                 } elsif ($element eq 'true') {
161                         $output .= 'bless( {}, "JSON::bool::true")';
162                         next;
163                 } elsif ($element eq 'false') {
164                         $output .= 'bless( {}, "JSON::bool::false")';
165                         next;
166                 }
167                 
168                 $output .= $element;
169         }
170
171         return eval $output;
172 }
173
174 sub perl2JSON {
175         my ($class, $perl) = @_;
176
177         my $output = '';
178         if (!defined($perl)) {
179                 $output = 'null';
180         } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
181                 $output .= $perl;
182         } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
183                 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
184                 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
185                         my %hash =  %$perl;
186                         $output .= perl2JSON(undef,\%hash);
187                 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
188                         my @array =  @$perl;
189                         $output .= perl2JSON(undef,\@array);
190                 }
191                 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
192         } elsif (ref($perl) and ref($perl) =~ /HASH/) {
193                 $output .= '{';
194                 my $c = 0;
195                 for my $key (sort keys %$perl) {
196                         $output .= ',' if ($c); 
197                         
198                         $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
199                         $c++;
200                 }
201                 $output .= '}';
202         } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
203                 $output .= '[';
204                 my $c = 0;
205                 for my $part (@$perl) {
206                         $output .= ',' if ($c); 
207                         
208                         $output .= perl2JSON(undef,$part);
209                         $c++;
210                 }
211                 $output .= ']';
212         } elsif (ref($perl) and ("$perl" =~ /^([^=]+)=(\w+)/o)) {
213                 my $type = $2;
214                 my $name = $1;
215                 JSON->register_class_hint(name => $name, hint => $name, type => lc($type));
216                 $output .= perl2JSON(undef,$perl);
217         } else {
218                 $perl =~ s{\\}{\\\\}sgo;
219                 $perl =~ s/"/\\"/sgo;
220                 $perl =~ s/\t/\\t/sgo;
221                 $perl =~ s/\f/\\f/sgo;
222                 $perl =~ s/\r/\\r/sgo;
223                 $perl =~ s/\n/\\n/sgo;
224                 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
225                 $output = '"'.$perl.'"';
226         }
227
228         return $output;
229 }
230
231 my $depth = 0;
232 sub perl2prettyJSON {
233         my ($class, $perl, $nospace) = @_;
234         $perl ||= $class;
235
236         my $output = '';
237         if (!defined($perl)) {
238                 $output = "   "x$depth unless($nospace);
239                 $output .= 'null';
240         } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
241                 $output = "   "x$depth unless($nospace);
242                 $output .= $perl;
243         } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
244                 $depth++;
245                 $output .= "\n";
246                 $output .= "   "x$depth;
247                 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
248                 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
249                         my %hash =  %$perl;
250                         $output .= perl2prettyJSON(\%hash,undef,1);
251                 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
252                         my @array =  @$perl;
253                         $output .= perl2prettyJSON(\@array,undef,1);
254                 }
255                 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
256                 $depth--;
257         } elsif (ref($perl) and ref($perl) =~ /HASH/) {
258                 $output .= "   "x$depth unless ($nospace);
259                 $output .= "{\n";
260                 my $c = 0;
261                 $depth++;
262                 for my $key (sort keys %$perl) {
263                         $output .= ",\n" if ($c); 
264                         
265                         $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
266                         $c++;
267                 }
268                 $depth--;
269                 $output .= "\n";
270                 $output .= "   "x$depth;
271                 $output .= '}';
272         } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
273                 $output .= "   "x$depth unless ($nospace);
274                 $output .= "[\n";
275                 my $c = 0;
276                 $depth++;
277                 for my $part (@$perl) {
278                         $output .= ",\n" if ($c); 
279                         
280                         $output .= perl2prettyJSON($part);
281                         $c++;
282                 }
283                 $depth--;
284                 $output .= "\n";
285                 $output .= "   "x$depth;
286                 $output .= "]";
287         } elsif (ref($perl) and "$perl" =~ /^([^=]+)=(\w{4,5})\(0x/) {
288                 my $type = $2;
289                 my $name = $1;
290                 register_class_hint(undef, name => $name, hint => $name, type => lc($type));
291                 $output .= perl2prettyJSON(undef,$perl);
292         } else {
293                 $perl =~ s/\\/\\\\/sgo;
294                 $perl =~ s/"/\\"/sgo;
295                 $perl =~ s/\t/\\t/sgo;
296                 $perl =~ s/\f/\\f/sgo;
297                 $perl =~ s/\r/\\r/sgo;
298                 $perl =~ s/\n/\\n/sgo;
299                 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
300                 $output .= "   "x$depth unless($nospace);
301                 $output .= '"'.$perl.'"';
302         }
303
304         return $output;
305 }
306
307 1;