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';
85 my @strings = /"((?:(?:\\[\"])|[^\"])*)"/sog;
86 # Replace with code...
87 s/"(?:(?:\\[\"])|[^\"])*"/ shift(\@strings) /sog;
92 # handle class blessings
93 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
94 s/\/\*--\s*E\w*?\s+(\S+)\s*--\*\// => _json_hint_to_class("$1")) /sog;
98 #for my $hint (values %{$_class_map{hints}}) {
99 # $string =~ s/\/\*--\s*S\w*?\s+$$hint{hint_re}\s*--\*\// bless(/sog;
100 # $string =~ s/\/\*--\s*E\w*?\s+$$hint{hint_re}\s*--\*\// => "$$hint{name}") /sog;
103 #s/\/\*--\s*\w+\s+\S+\s*--\*\///sog;
107 s/\b(-?\d+\.?\d*)\b/ JSON::number::new($1) /sog;
110 # Change javascript stuff to perl...
112 s/true/ bless( {}, "JSON::bool::true") /sog;
113 s/false/ bless( {}, "JSON::bool::false") /sog;
117 return eval '$ret = '.$_;
121 my ($class, $json) = @_;
123 if (!defined($json)) {
127 #$json =~ s/\/\/.+$//gmo; # remove C++ comments
128 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
129 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
132 my $casting_depth = 0;
136 while (($json,$element) = _JSON_regex($json)) {
138 last unless ($element);
140 if ($element eq 'null') {
141 $output .= ' undef() ';
143 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
145 if (exists $_class_map{hints}{$hint}) {
146 $casts[$casting_depth] = $hint;
147 $output .= ' bless(';
150 } elsif ($element =~ /^\/\*/) {
152 } elsif ($element =~ /^\d/) {
153 $output .= "do { JSON::number::new($element) }";
155 } elsif ($element eq '{' or $element eq '[') {
157 } elsif ($element eq '}' or $element eq ']') {
159 my $hint = $casts[$casting_depth];
160 $casts[$casting_depth] = undef;
161 if (defined $hint and exists $_class_map{hints}{$hint}) {
162 $output .= $element . ',"'. $_class_map{hints}{$hint}{name} . '")';
165 } elsif ($element eq ':') {
168 } elsif ($element eq 'true') {
169 $output .= 'bless( {}, "JSON::bool::true")';
171 } elsif ($element eq 'false') {
172 $output .= 'bless( {}, "JSON::bool::false")';
183 my ($class, $perl) = @_;
186 if (!defined($perl)) {
188 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
190 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
191 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
192 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
194 $output .= perl2JSON(undef,\%hash);
195 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
197 $output .= perl2JSON(undef,\@array);
199 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
200 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
203 for my $key (sort keys %$perl) {
204 $output .= ',' if ($c);
206 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
210 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
213 for my $part (@$perl) {
214 $output .= ',' if ($c);
216 $output .= perl2JSON(undef,$part);
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 $output = '"'.$perl.'"';
234 sub perl2prettyJSON {
235 my ($class, $perl, $nospace) = @_;
239 if (!defined($perl)) {
241 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
243 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
246 $output .= " "x$depth;
247 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
248 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
250 $output .= perl2prettyJSON(\%hash,undef,1);
251 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
253 $output .= perl2prettyJSON(\@array,undef,1);
255 #$output .= " "x$depth;
256 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
258 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
260 $output .= " "x$depth unless ($nospace);
264 for my $key (sort keys %$perl) {
265 $output .= ",\n" if ($c);
267 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
272 $output .= " "x$depth;
275 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
277 $output .= " "x$depth unless ($nospace);
281 for my $part (@$perl) {
282 $output .= ",\n" if ($c);
284 $output .= perl2prettyJSON($part);
289 $output .= " "x$depth;
293 $perl =~ s/\\/\\\\/sgo;
294 $perl =~ s/"/\\"/sgo;
295 $perl =~ s/\t/\\t/sgo;
296 $perl =~ s/\f/\\f/sgo;
297 $perl =~ s/\r/\\r/sgo;
298 $perl =~ s/\n/\\n/sgo;
299 $output .= " "x$depth unless($nospace);
300 $output .= '"'.$perl.'"';