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;
90 my @strings = /"((?:(?:\\[\"])|[^\"])*)"/sog;
92 # Replace with code...
93 s/"(?:(?:\\[\"])|[^\"])*"/ shift(\@strings) /sog;
95 # Perlify hash notation
98 # handle class blessings
99 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
100 s/\/\*--\s*E\w*?\s+(\S+)\s*--\*\// => _json_hint_to_class("$1")) /sog;
102 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
105 # Change javascript stuff to perl...
107 s/true/ bless( {}, "JSON::bool::true") /sog;
108 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);
215 $perl =~ s/\\/\\\\/sgo;
216 $perl =~ s/"/\\"/sgo;
217 $perl =~ s/\t/\\t/sgo;
218 $perl =~ s/\f/\\f/sgo;
219 $perl =~ s/\r/\\r/sgo;
220 $perl =~ s/\n/\\n/sgo;
221 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
222 $output = '"'.$perl.'"';
229 sub perl2prettyJSON {
230 my ($class, $perl, $nospace) = @_;
234 if (!defined($perl)) {
235 $output = " "x$depth unless($nospace);
237 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
238 $output = " "x$depth unless($nospace);
240 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
243 $output .= " "x$depth;
244 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
245 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
247 $output .= perl2prettyJSON(\%hash,undef,1);
248 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
250 $output .= perl2prettyJSON(\@array,undef,1);
252 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
254 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
255 $output .= " "x$depth unless ($nospace);
259 for my $key (sort keys %$perl) {
260 $output .= ",\n" if ($c);
262 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
267 $output .= " "x$depth;
269 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
270 $output .= " "x$depth unless ($nospace);
274 for my $part (@$perl) {
275 $output .= ",\n" if ($c);
277 $output .= perl2prettyJSON($part);
282 $output .= " "x$depth;
285 $perl =~ s/\\/\\\\/sgo;
286 $perl =~ s/"/\\"/sgo;
287 $perl =~ s/\t/\\t/sgo;
288 $perl =~ s/\f/\\f/sgo;
289 $perl =~ s/\r/\\r/sgo;
290 $perl =~ s/\n/\\n/sgo;
291 $perl =~ s/(\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
292 $output .= " "x$depth unless($nospace);
293 $output .= '"'.$perl.'"';