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;
101 for my $hint (values %{$_class_map{hints}}) {
102 $string =~ s/\/\*--\s*S\w*?\s+$$hint{hint_re}\s*--\*\// bless(/sog;
103 $string =~ s/\/\*--\s*E\w*?\s+$$hint{hint_re}\s*--\*\// => "$$hint{name}") /sog;
106 s/\/\*--\s*\w+\s+\S+\s*--\*\///sog;
110 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
113 # Change javascript stuff to perl...
115 s/true/ bless( {}, "JSON::bool::true") /sog;
116 s/false/ bless( {}, "JSON::bool::false") /sog;
120 return eval '$ret = '.$_;
124 my ($class, $json) = @_;
126 if (!defined($json)) {
130 #$json =~ s/\/\/.+$//gmo; # remove C++ comments
131 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
132 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
135 my $casting_depth = 0;
139 while (($json,$element) = _JSON_regex($json)) {
141 last unless ($element);
143 if ($element eq 'null') {
144 $output .= ' undef() ';
146 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
148 if (exists $_class_map{hints}{$hint}) {
149 $casts[$casting_depth] = $hint;
150 $output .= ' bless(';
153 } elsif ($element =~ /^\/\*/) {
155 } elsif ($element =~ /^\d/) {
156 $output .= "do { JSON::number::new($element) }";
158 } elsif ($element eq '{' or $element eq '[') {
160 } elsif ($element eq '}' or $element eq ']') {
162 my $hint = $casts[$casting_depth];
163 $casts[$casting_depth] = undef;
164 if (defined $hint and exists $_class_map{hints}{$hint}) {
165 $output .= $element . ',"'. $_class_map{hints}{$hint}{name} . '")';
168 } elsif ($element eq ':') {
171 } elsif ($element eq 'true') {
172 $output .= 'bless( {}, "JSON::bool::true")';
174 } elsif ($element eq 'false') {
175 $output .= 'bless( {}, "JSON::bool::false")';
186 my ($class, $perl) = @_;
189 if (!defined($perl)) {
191 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
193 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
194 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
195 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
197 $output .= perl2JSON(undef,\%hash);
198 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
200 $output .= perl2JSON(undef,\@array);
202 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
203 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
206 for my $key (sort keys %$perl) {
207 $output .= ',' if ($c);
209 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
213 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
216 for my $part (@$perl) {
217 $output .= ',' if ($c);
219 $output .= perl2JSON(undef,$part);
224 $perl =~ s/\\/\\\\/sgo;
225 $perl =~ s/"/\\"/sgo;
226 $perl =~ s/\t/\\t/sgo;
227 $perl =~ s/\f/\\f/sgo;
228 $perl =~ s/\r/\\r/sgo;
229 $perl =~ s/\n/\\n/sgo;
230 $output = '"'.$perl.'"';
237 sub perl2prettyJSON {
238 my ($class, $perl, $nospace) = @_;
242 if (!defined($perl)) {
244 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
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 .= " "x$depth;
259 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
261 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
263 $output .= " "x$depth unless ($nospace);
267 for my $key (sort keys %$perl) {
268 $output .= ",\n" if ($c);
270 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
275 $output .= " "x$depth;
278 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
280 $output .= " "x$depth unless ($nospace);
284 for my $part (@$perl) {
285 $output .= ",\n" if ($c);
287 $output .= perl2prettyJSON($part);
292 $output .= " "x$depth;
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 $output .= " "x$depth unless($nospace);
303 $output .= '"'.$perl.'"';