1 package JSON::object_unknown;
6 my $x = shift || $class;
7 return bless \$x => __PACKAGE__;
9 use overload ( '""' => \&toString );
10 use overload ( '0+' => sub { $_[0]->toString } );
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 ( '%' => sub { int($_[0]) % int($_[1]) } );
16 use overload ( '**' => sub { int($_[0]) ** int($_[1]) } );
17 use overload ( 'neg' => sub { -int($_[0]) } );
19 sub toString { defined($_[1]) ? ${$_[1]} : ${$_[0]} }
21 package JSON::bool::true;
22 sub new { return bless {} => __PACKAGE__ }
23 use overload ( '""' => \&toString );
24 use overload ( 'bool' => sub { 1 } );
25 use overload ( '0+' => sub { 1 } );
27 sub toString { 'true' }
29 package JSON::bool::false;
30 sub new { return bless {} => __PACKAGE__ }
31 use overload ( '""' => \&toString );
32 use overload ( 'bool' => sub { 0 } );
33 use overload ( '0+' => sub { 0 } );
35 sub toString { 'false' }
38 use vars qw/%_class_map/;
40 sub register_class_hint {
44 $_class_map{hints}{$args{hint}} = \%args;
45 $_class_map{classes}{$args{name}} = \%args;
47 JSON->register_class_hint(
48 name => 'JSON::object_unknown',
59 -?\d+\.?\d* | # number literal
60 "(?:(?:\\[\"])|[^\"])*" | # string literal
61 (?:\/\*.+?\*\/) | # C comment
65 : | # object key-value sep
74 sub _json_hint_to_class {
76 return $_class_map{hints}{$hint}{name} if (exists $_class_map{hints}{$hint});
77 return 'JSON::object_unknown';
84 # Convert JSON Unicode...
85 s/\\u(\d{4})/chr(hex($1))/esog;
88 my @strings = /"((?:(?:\\[\"])|[^\"])*)"/sog;
90 # Replace with code...
91 s/"(?:(?:\\[\"])|[^\"])*"/ shift(\@strings) /sog;
93 # Perlify hash notation
96 # handle class blessings
97 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
98 s/\/\*--\s*E\w*?\s+(\S+)\s*--\*\// => _json_hint_to_class("$1")) /sog;
100 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
103 # Change javascript stuff to perl...
105 s/true/ bless( {}, "JSON::bool::true") /sog;
106 s/false/ bless( {}, "JSON::bool::false") /sog;
110 return eval '$ret = '.$_;
114 my ($class, $json) = @_;
116 if (!defined($json)) {
120 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
121 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
124 my $casting_depth = 0;
128 while (($json,$element) = _JSON_regex($json)) {
130 last unless ($element);
132 if ($element eq 'null') {
133 $output .= ' undef() ';
135 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
137 if (exists $_class_map{hints}{$hint}) {
138 $casts[$casting_depth] = $hint;
139 $output .= ' bless(';
142 } elsif ($element =~ /^\/\*/) {
144 } elsif ($element =~ /^\d/) {
145 $output .= "do { JSON::number::new($element) }";
147 } elsif ($element eq '{' or $element eq '[') {
149 } elsif ($element eq '}' or $element eq ']') {
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} . '")';
157 } elsif ($element eq ':') {
160 } elsif ($element eq 'true') {
161 $output .= 'bless( {}, "JSON::bool::true")';
163 } elsif ($element eq 'false') {
164 $output .= 'bless( {}, "JSON::bool::false")';
175 my ($class, $perl) = @_;
178 if (!defined($perl)) {
180 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
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') {
186 $output .= perl2JSON(undef,\%hash);
187 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
189 $output .= perl2JSON(undef,\@array);
191 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
192 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
195 for my $key (sort keys %$perl) {
196 $output .= ',' if ($c);
198 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
202 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
205 for my $part (@$perl) {
206 $output .= ',' if ($c);
208 $output .= perl2JSON(undef,$part);
213 $perl =~ s/\\/\\\\/sgo;
214 $perl =~ s/"/\\"/sgo;
215 $perl =~ s/\t/\\t/sgo;
216 $perl =~ s/\f/\\f/sgo;
217 $perl =~ s/\r/\\r/sgo;
218 $perl =~ s/\n/\\n/sgo;
219 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
220 $output = '"'.$perl.'"';
227 sub perl2prettyJSON {
228 my ($class, $perl, $nospace) = @_;
232 if (!defined($perl)) {
233 $output = " "x$depth unless($nospace);
235 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
236 $output = " "x$depth unless($nospace);
238 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
241 $output .= " "x$depth;
242 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
243 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
245 $output .= perl2prettyJSON(\%hash,undef,1);
246 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
248 $output .= perl2prettyJSON(\@array,undef,1);
250 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
252 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
253 $output .= " "x$depth unless ($nospace);
257 for my $key (sort keys %$perl) {
258 $output .= ",\n" if ($c);
260 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
265 $output .= " "x$depth;
267 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
268 $output .= " "x$depth unless ($nospace);
272 for my $part (@$perl) {
273 $output .= ",\n" if ($c);
275 $output .= perl2prettyJSON($part);
280 $output .= " "x$depth;
283 $perl =~ s/\\/\\\\/sgo;
284 $perl =~ s/"/\\"/sgo;
285 $perl =~ s/\t/\\t/sgo;
286 $perl =~ s/\f/\\f/sgo;
287 $perl =~ s/\r/\\r/sgo;
288 $perl =~ s/\n/\\n/sgo;
289 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
290 $output .= " "x$depth unless($nospace);
291 $output .= '"'.$perl.'"';