4 my $x = shift || $class;
5 return bless \$x => __PACKAGE__;
7 use overload ( '""' => \&toString );
8 use overload ( '0+' => sub { $_[0]->toString } );
9 use overload ( '+' => sub { int($_[0]) + int($_[1]) } );
10 use overload ( '-' => sub { int($_[0]) - int($_[1]) } );
11 use overload ( '*' => sub { int($_[0]) * int($_[1]) } );
12 use overload ( '/' => sub { int($_[0]) / int($_[1]) } );
13 use overload ( '%' => sub { int($_[0]) % int($_[1]) } );
14 use overload ( '**' => sub { int($_[0]) ** int($_[1]) } );
15 use overload ( 'neg' => sub { -int($_[0]) } );
17 sub toString { defined($_[1]) ? ${$_[1]} : ${$_[0]} }
19 package JSON::bool::true;
20 sub new { return bless {} => __PACKAGE__ }
21 use overload ( '""' => \&toString );
22 use overload ( 'bool' => sub { 1 } );
23 use overload ( '0+' => sub { 1 } );
25 sub toString { 'true' }
27 package JSON::bool::false;
28 sub new { return bless {} => __PACKAGE__ }
29 use overload ( '""' => \&toString );
30 use overload ( 'bool' => sub { 0 } );
31 use overload ( '0+' => sub { 0 } );
33 sub toString { 'false' }
36 use vars qw/%_class_map/;
38 sub register_class_hint {
42 $_class_map{hints}{$args{hint}} = \%args;
43 $_class_map{classes}{$args{name}} = \%args;
52 -?\d+\.?\d* | # number literal
53 "(?:(?:\\[\"])|[^\"])*" | # string literal
54 (?:\/\*.+?\*\/) | # C comment
58 : | # object key-value sep
67 sub _json_hint_to_class {
71 return $_class_map{hints}{$hint}{name} if (exists $_class_map{hints}{$hint});
73 $type = 'hash' if ($type eq '}');
74 $type = 'array' if ($type eq ']');
76 JSON->register_class_hint(name => $hint, hint => $hint, type => $type);
85 # Convert JSON Unicode...
86 s/\\u(\d{4})/chr(hex($1))/esog;
88 # handle class blessings
89 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
90 s/(\]|\}|")\s*\/\*--\s*E\w*?\s+(\S+)\s*--\*\//$1 => _json_hint_to_class("$1", "$2")) /sog;
93 my @strings = /"((?:(?:\\[\"])|[^\"])*)"/sog;
95 # Replace with code...
96 s/"(?:(?:\\[\"])|[^\"])*"/ shift(\@strings) /sog;
98 # Perlify hash notation
102 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
104 # Change javascript stuff to perl...
106 s/true/ bless( {}, "JSON::bool::true") /sog;
107 s/false/ bless( {}, "JSON::bool::false") /sog;
111 return eval '$ret = '.$_;
115 my ($class, $json) = @_;
117 if (!defined($json)) {
121 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
122 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
125 my $casting_depth = 0;
129 while (($json,$element) = _JSON_regex($json)) {
131 last unless ($element);
133 if ($element eq 'null') {
134 $output .= ' undef() ';
136 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
138 if (exists $_class_map{hints}{$hint}) {
139 $casts[$casting_depth] = $hint;
140 $output .= ' bless(';
143 } elsif ($element =~ /^\/\*/) {
145 } elsif ($element =~ /^\d/) {
146 $output .= "do { JSON::number::new($element) }";
148 } elsif ($element eq '{' or $element eq '[') {
150 } elsif ($element eq '}' or $element eq ']') {
152 my $hint = $casts[$casting_depth];
153 $casts[$casting_depth] = undef;
154 if (defined $hint and exists $_class_map{hints}{$hint}) {
155 $output .= $element . ',"'. $_class_map{hints}{$hint}{name} . '")';
158 } elsif ($element eq ':') {
161 } elsif ($element eq 'true') {
162 $output .= 'bless( {}, "JSON::bool::true")';
164 } elsif ($element eq 'false') {
165 $output .= 'bless( {}, "JSON::bool::false")';
176 my ($class, $perl) = @_;
179 if (!defined($perl)) {
181 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
183 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
184 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
185 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
187 $output .= perl2JSON(undef,\%hash);
188 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
190 $output .= perl2JSON(undef,\@array);
192 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
193 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
196 for my $key (sort keys %$perl) {
197 $output .= ',' if ($c);
199 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
203 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
206 for my $part (@$perl) {
207 $output .= ',' if ($c);
209 $output .= perl2JSON(undef,$part);
213 } elsif (ref($perl) and ("$perl" =~ /^([^=]+)=(\w+)/o)) {
216 JSON->register_class_hint(name => $name, hint => $name, type => lc($type));
217 $output .= perl2JSON(undef,$perl);
219 $perl =~ s/\\/\\\\/sgo;
220 $perl =~ s/"/\\"/sgo;
221 $perl =~ s/\t/\\t/sgo;
222 $perl =~ s/\f/\\f/sgo;
223 $perl =~ s/\r/\\r/sgo;
224 $perl =~ s/\n/\\n/sgo;
225 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
226 $output = '"'.$perl.'"';
233 sub perl2prettyJSON {
234 my ($class, $perl, $nospace) = @_;
238 if (!defined($perl)) {
239 $output = " "x$depth unless($nospace);
241 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
242 $output = " "x$depth unless($nospace);
244 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
247 $output .= " "x$depth;
248 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
249 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
251 $output .= perl2prettyJSON(\%hash,undef,1);
252 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
254 $output .= perl2prettyJSON(\@array,undef,1);
256 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
258 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
259 $output .= " "x$depth unless ($nospace);
263 for my $key (sort keys %$perl) {
264 $output .= ",\n" if ($c);
266 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
271 $output .= " "x$depth;
273 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
274 $output .= " "x$depth unless ($nospace);
278 for my $part (@$perl) {
279 $output .= ",\n" if ($c);
281 $output .= perl2prettyJSON($part);
286 $output .= " "x$depth;
288 } elsif (ref($perl) and "$perl" =~ /^([^=]+)=(\w{4,5})\(0x/) {
291 register_class_hint(undef, name => $name, hint => $name, type => lc($type));
292 $output .= perl2prettyJSON(undef,$perl);
294 $perl =~ s/\\/\\\\/sgo;
295 $perl =~ s/"/\\"/sgo;
296 $perl =~ s/\t/\\t/sgo;
297 $perl =~ s/\f/\\f/sgo;
298 $perl =~ s/\r/\\r/sgo;
299 $perl =~ s/\n/\\n/sgo;
300 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
301 $output .= " "x$depth unless($nospace);
302 $output .= '"'.$perl.'"';