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