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;
92 my $re = qr/((?<!\\)"(?>(?<=\\)"|[^"])*(?<!\\)")/;
94 my @strings = /$re/sog;
96 # Replace with code...
97 #s/"(?:(?:\\[\"])|[^\"])*"/ do{ \$t = '"'.shift(\@strings).'"'; eval \$t;} /sog;
98 s/$re/ eval shift(\@strings) /sog;
100 # Perlify hash notation
104 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
106 # Change javascript stuff to perl...
108 s/true/ bless( {}, "JSON::bool::true") /sog;
109 s/false/ bless( {}, "JSON::bool::false") /sog;
112 return eval '$ret = '.$_;
116 my ($class, $json) = @_;
118 if (!defined($json)) {
122 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
123 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
126 my $casting_depth = 0;
130 while (($json,$element) = _JSON_regex($json)) {
132 last unless ($element);
134 if ($element eq 'null') {
135 $output .= ' undef() ';
137 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
139 if (exists $_class_map{hints}{$hint}) {
140 $casts[$casting_depth] = $hint;
141 $output .= ' bless(';
144 } elsif ($element =~ /^\/\*/) {
146 } elsif ($element =~ /^\d/) {
147 $output .= "do { JSON::number::new($element) }";
149 } elsif ($element eq '{' or $element eq '[') {
151 } elsif ($element eq '}' or $element eq ']') {
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} . '")';
159 } elsif ($element eq ':') {
162 } elsif ($element eq 'true') {
163 $output .= 'bless( {}, "JSON::bool::true")';
165 } elsif ($element eq 'false') {
166 $output .= 'bless( {}, "JSON::bool::false")';
177 my ($class, $perl) = @_;
180 if (!defined($perl)) {
182 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
184 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
185 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
186 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
188 $output .= perl2JSON(undef,\%hash);
189 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
191 $output .= perl2JSON(undef,\@array);
193 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
194 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
197 for my $key (sort keys %$perl) {
198 $output .= ',' if ($c);
200 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
204 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
207 for my $part (@$perl) {
208 $output .= ',' if ($c);
210 $output .= perl2JSON(undef,$part);
214 } elsif (ref($perl) and ("$perl" =~ /^([^=]+)=(\w+)/o)) {
217 JSON->register_class_hint(name => $name, hint => $name, type => lc($type));
218 $output .= perl2JSON(undef,$perl);
220 $perl =~ s{\\}{\\\\}sgo;
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.'"';