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 { neg(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 $args{hint_re} = qr/(?:\b$args{hint})\b/;
45 $args{class_re} = qr/(?:\b$args{name})\b/;
47 $_class_map{hints}{$args{hint}} = \%args;
48 $_class_map{classes}{$args{name}} = \%args;
50 JSON->register_class_hint(
51 name => 'JSON::object_unknown',
62 -?\d+\.?\d* | # number literal
63 "(?:(?:\\[\"])|[^\"])*" | # string literal
64 (?:\/\*.+?\*\/) | # C comment
68 : | # object key-value sep
77 sub _json_hint_to_class {
79 return $_class_map{hints}{$hint}{name} if (exists $_class_map{hints}{$hint});
80 return 'JSON::object_unknown';
88 my @strings = /"((?:(?:\\[\"])|[^\"])*)"/sog;
89 # Replace with code...
90 s/"(?:(?:\\[\"])|[^\"])*"/ shift(\@strings) /sog;
95 # handle class blessings
96 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
97 s/\/\*--\s*E\w*?\s+(\S+)\s*--\*\// => _json_hint_to_class("$1")) /sog;
100 for my $hint (values %{$_class_map{hints}}) {
101 $string =~ s/\/\*--\s*S\w*?\s+$$hint{hint_re}\s*--\*\// bless(/sog;
102 $string =~ s/\/\*--\s*E\w*?\s+$$hint{hint_re}\s*--\*\// => "$$hint{name}") /sog;
105 s/\/\*--\s*\w+\s+\S+\s*--\*\///sog;
109 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
112 # Change javascript stuff to perl...
114 s/true/ bless( {}, "JSON::bool::true") /sog;
115 s/false/ bless( {}, "JSON::bool::false") /sog;
119 return eval '$ret = '.$_;
123 my ($class, $json) = @_;
125 if (!defined($json)) {
129 #$json =~ s/\/\/.+$//gmo; # remove C++ comments
130 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
131 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
134 my $casting_depth = 0;
138 while (($json,$element) = _JSON_regex($json)) {
140 last unless ($element);
142 if ($element eq 'null') {
143 $output .= ' undef() ';
145 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
147 if (exists $_class_map{hints}{$hint}) {
148 $casts[$casting_depth] = $hint;
149 $output .= ' bless(';
152 } elsif ($element =~ /^\/\*/) {
154 } elsif ($element =~ /^\d/) {
155 $output .= "do { JSON::number::new($element) }";
157 } elsif ($element eq '{' or $element eq '[') {
159 } elsif ($element eq '}' or $element eq ']') {
161 my $hint = $casts[$casting_depth];
162 $casts[$casting_depth] = undef;
163 if (defined $hint and exists $_class_map{hints}{$hint}) {
164 $output .= $element . ',"'. $_class_map{hints}{$hint}{name} . '")';
167 } elsif ($element eq ':') {
170 } elsif ($element eq 'true') {
171 $output .= 'bless( {}, "JSON::bool::true")';
173 } elsif ($element eq 'false') {
174 $output .= 'bless( {}, "JSON::bool::false")';
185 my ($class, $perl) = @_;
188 if (!defined($perl)) {
190 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
192 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
193 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
194 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
196 $output .= perl2JSON(undef,\%hash);
197 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
199 $output .= perl2JSON(undef,\@array);
201 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
202 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
205 for my $key (sort keys %$perl) {
206 $output .= ',' if ($c);
208 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
212 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
215 for my $part (@$perl) {
216 $output .= ',' if ($c);
218 $output .= perl2JSON(undef,$part);
223 $perl =~ s/\\/\\\\/sgo;
224 $perl =~ s/"/\\"/sgo;
225 $perl =~ s/\t/\\t/sgo;
226 $perl =~ s/\f/\\f/sgo;
227 $perl =~ s/\r/\\r/sgo;
228 $perl =~ s/\n/\\n/sgo;
229 $output = '"'.$perl.'"';
236 sub perl2prettyJSON {
237 my ($class, $perl, $nospace) = @_;
241 if (!defined($perl)) {
243 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
245 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
248 $output .= " "x$depth;
249 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
250 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
252 $output .= perl2prettyJSON(\%hash,undef,1);
253 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
255 $output .= perl2prettyJSON(\@array,undef,1);
257 #$output .= " "x$depth;
258 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
260 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
262 $output .= " "x$depth unless ($nospace);
266 for my $key (sort keys %$perl) {
267 $output .= ",\n" if ($c);
269 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
274 $output .= " "x$depth;
277 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
279 $output .= " "x$depth unless ($nospace);
283 for my $part (@$perl) {
284 $output .= ",\n" if ($c);
286 $output .= perl2prettyJSON($part);
291 $output .= " "x$depth;
295 $perl =~ s/\\/\\\\/sgo;
296 $perl =~ s/"/\\"/sgo;
297 $perl =~ s/\t/\\t/sgo;
298 $perl =~ s/\f/\\f/sgo;
299 $perl =~ s/\r/\\r/sgo;
300 $perl =~ s/\n/\\n/sgo;
301 $output .= " "x$depth unless($nospace);
302 $output .= '"'.$perl.'"';