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]) } );
16 use overload ( '==' => sub { int($_[0]->toString) == int($_[1])} );
18 sub toString { defined($_[1]) ? ${$_[1]} : ${$_[0]} }
20 package JSON::bool::true;
21 sub new { return bless {} => __PACKAGE__ }
22 use overload ( '""' => \&toString );
23 use overload ( 'bool' => sub { 1 } );
24 use overload ( '0+' => sub { 1 } );
26 sub toString { 'true' }
28 package JSON::bool::false;
29 sub new { return bless {} => __PACKAGE__ }
30 use overload ( '""' => \&toString );
31 use overload ( 'bool' => sub { 0 } );
32 use overload ( '0+' => sub { 0 } );
34 sub toString { 'false' }
37 use vars qw/%_class_map/;
39 sub register_class_hint {
43 $_class_map{hints}{$args{hint}} = \%args;
44 $_class_map{classes}{$args{name}} = \%args;
53 -?\d+\.?\d* | # number literal
54 "(?:(?:\\[\"])|[^\"])*" | # string literal
55 (?:\/\*.+?\*\/) | # C comment
59 : | # object key-value sep
68 sub _json_hint_to_class {
72 return $_class_map{hints}{$hint}{name} if (exists $_class_map{hints}{$hint});
74 $type = 'hash' if ($type eq '}');
75 $type = 'array' if ($type eq ']');
77 JSON->register_class_hint(name => $hint, hint => $hint, type => $type);
86 # Convert JSON Unicode...
87 s/\\u(\d{4})/chr(hex($1))/esog;
89 # handle class blessings
90 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
91 s/(\]|\}|")\s*\/\*--\s*E\w*?\s+(\S+)\s*--\*\//$1 => _json_hint_to_class("$1", "$2")) /sog;
93 my $re = qr/((?<!\\)"(?>(?<=\\)"|[^"])*(?<!\\)")/;
95 my @strings = /$re/sog;
97 # Replace with code...
98 #s/"(?:(?:\\[\"])|[^\"])*"/ do{ \$t = '"'.shift(\@strings).'"'; eval \$t;} /sog;
99 s/$re/ eval shift(\@strings) /sog;
101 # Perlify hash notation
105 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
107 # Change javascript stuff to perl...
109 s/true/ bless( {}, "JSON::bool::true") /sog;
110 s/false/ bless( {}, "JSON::bool::false") /sog;
113 return eval '$ret = '.$_;
117 my ($class, $json) = @_;
119 if (!defined($json)) {
123 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
124 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
127 my $casting_depth = 0;
131 while (($json,$element) = _JSON_regex($json)) {
133 last unless ($element);
135 if ($element eq 'null') {
136 $output .= ' undef() ';
138 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
140 if (exists $_class_map{hints}{$hint}) {
141 $casts[$casting_depth] = $hint;
142 $output .= ' bless(';
145 } elsif ($element =~ /^\/\*/) {
147 } elsif ($element =~ /^\d/) {
148 $output .= "do { JSON::number::new($element) }";
150 } elsif ($element eq '{' or $element eq '[') {
152 } elsif ($element eq '}' or $element eq ']') {
154 my $hint = $casts[$casting_depth];
155 $casts[$casting_depth] = undef;
156 if (defined $hint and exists $_class_map{hints}{$hint}) {
157 $output .= $element . ',"'. $_class_map{hints}{$hint}{name} . '")';
160 } elsif ($element eq ':') {
163 } elsif ($element eq 'true') {
164 $output .= 'bless( {}, "JSON::bool::true")';
166 } elsif ($element eq 'false') {
167 $output .= 'bless( {}, "JSON::bool::false")';
178 my ($class, $perl) = @_;
181 if (!defined($perl)) {
183 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
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') {
189 $output .= perl2JSON(undef,\%hash);
190 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
192 $output .= perl2JSON(undef,\@array);
194 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
195 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
198 for my $key (sort keys %$perl) {
199 $output .= ',' if ($c);
201 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
205 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
208 for my $part (@$perl) {
209 $output .= ',' if ($c);
211 $output .= perl2JSON(undef,$part);
215 } elsif (ref($perl) and ("$perl" =~ /^([^=]+)=(\w+)/o)) {
218 JSON->register_class_hint(name => $name, hint => $name, type => lc($type));
219 $output .= perl2JSON(undef,$perl);
221 $perl =~ s{\\}{\\\\}sgo;
222 $perl =~ s/"/\\"/sgo;
223 $perl =~ s/\t/\\t/sgo;
224 $perl =~ s/\f/\\f/sgo;
225 $perl =~ s/\r/\\r/sgo;
226 $perl =~ s/\n/\\n/sgo;
227 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
228 $output = '"'.$perl.'"';
235 sub perl2prettyJSON {
236 my ($class, $perl, $nospace) = @_;
240 if (!defined($perl)) {
241 $output = " "x$depth unless($nospace);
243 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
244 $output = " "x$depth unless($nospace);
246 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
249 $output .= " "x$depth;
250 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
251 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
253 $output .= perl2prettyJSON(\%hash,undef,1);
254 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
256 $output .= perl2prettyJSON(\@array,undef,1);
258 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
260 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
261 $output .= " "x$depth unless ($nospace);
265 for my $key (sort keys %$perl) {
266 $output .= ",\n" if ($c);
268 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
273 $output .= " "x$depth;
275 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
276 $output .= " "x$depth unless ($nospace);
280 for my $part (@$perl) {
281 $output .= ",\n" if ($c);
283 $output .= perl2prettyJSON($part);
288 $output .= " "x$depth;
290 } elsif (ref($perl) and "$perl" =~ /^([^=]+)=(\w{4,5})\(0x/) {
293 register_class_hint(undef, name => $name, hint => $name, type => lc($type));
294 $output .= perl2prettyJSON(undef,$perl);
296 $perl =~ s/\\/\\\\/sgo;
297 $perl =~ s/"/\\"/sgo;
298 $perl =~ s/\t/\\t/sgo;
299 $perl =~ s/\f/\\f/sgo;
300 $perl =~ s/\r/\\r/sgo;
301 $perl =~ s/\n/\\n/sgo;
302 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
303 $output .= " "x$depth unless($nospace);
304 $output .= '"'.$perl.'"';