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 $_class_map{$args{hint}} = \%args;
45 $_class_map{$args{name}} = \%args;
47 JSON->register_class_hint(
48 name => 'JSON::object_unknown',
58 -?\d+\.?\d* | # number literal
59 "(?:(?:\\[\"])|[^\"])*" | # string literal
60 (?:\/\*.+?\*\/) | # C comment
64 : | # object key-value sep
73 sub _json_hint_to_class {
75 return $_class_map{$hint}{name} if (exists $_class_map{$hint});
76 return 'JSON::object_unknown';
84 my @strings = /"((?:(?:\\[\"])|[^\"])*)"/sog;
85 # Replace with code...
86 s/"(?:(?:\\[\"])|[^\"])*"/ shift(\@strings) /sog;
88 # handle class blessings
89 s/\/\*--\s*S\w*?\s+\w+\s*--\*\// bless(/sog;
90 s/\/\*--\s*E\w*?\s+(\w+)\s*--\*\// => _json_hint_to_class("$1")) /sog;
91 s/\b(-?\d+\.?\d*)/ do { JSON::number::new($1) } /sog;
93 # Change javascript stuff to perl...
96 s/true/ bless( {}, "JSON::bool::true") /sog;
97 s/false/ bless( {}, "JSON::bool::false") /sog;
100 return eval '$ret = '.$_;
104 my ($class, $json) = @_;
106 if (!defined($json)) {
110 #$json =~ s/\/\/.+$//gmo; # remove C++ comments
111 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
112 $json =~ s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
115 my $casting_depth = 0;
119 while (($json,$element) = _JSON_regex($json)) {
121 last unless ($element);
123 if ($element eq 'null') {
124 $output .= ' undef() ';
126 } elsif ($element =~ /^\/\*--\s*S\w*?\s+(\w+)\s*--\*\/$/) {
128 if (exists $_class_map{$hint}) {
129 $casts[$casting_depth] = $hint;
130 $output .= ' bless(';
133 } elsif ($element =~ /^\/\*/) {
135 } elsif ($element =~ /^\d/) {
136 $output .= "do { JSON::number::new($element) }";
138 } elsif ($element eq '{' or $element eq '[') {
140 } elsif ($element eq '}' or $element eq ']') {
142 my $hint = $casts[$casting_depth];
143 $casts[$casting_depth] = undef;
144 if (defined $hint and exists $_class_map{$hint}) {
145 $output .= $element . ',"'. $_class_map{$hint}{name} . '")';
148 } elsif ($element eq ':') {
151 } elsif ($element eq 'true') {
152 $output .= 'bless( {}, "JSON::bool::true")';
154 } elsif ($element eq 'false') {
155 $output .= 'bless( {}, "JSON::bool::false")';
166 my ($class, $perl) = @_;
169 if (!defined($perl)) {
171 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
173 } elsif ( ref($perl) && exists($_class_map{ref($perl)}) ) {
174 $output .= '/*--S '.$_class_map{ref($perl)}{hint}.'--*/';
175 if (lc($_class_map{ref($perl)}{type}) eq 'hash') {
177 $output .= perl2JSON(undef,\%hash);
178 } elsif (lc($_class_map{ref($perl)}{type}) eq 'array') {
180 $output .= perl2JSON(undef,\@array);
182 $output .= '/*--E '.$_class_map{ref($perl)}{hint}.'--*/';
183 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
186 for my $key (sort keys %$perl) {
187 $output .= ',' if ($c);
189 $output .= perl2JSON(undef,$key).':'.perl2JSON(undef,$$perl{$key});
193 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
196 for my $part (@$perl) {
197 $output .= ',' if ($c);
199 $output .= perl2JSON(undef,$part);
204 $perl =~ s/\\/\\\\/sgo;
205 $perl =~ s/"/\\"/sgo;
206 $perl =~ s/\t/\\t/sgo;
207 $perl =~ s/\f/\\f/sgo;
208 $perl =~ s/\r/\\r/sgo;
209 $perl =~ s/\n/\\n/sgo;
210 $output = '"'.$perl.'"';
217 sub perl2prettyJSON {
218 my ($class, $perl, $nospace) = @_;
222 if (!defined($perl)) {
224 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
226 } elsif ( ref($perl) && exists($_class_map{ref($perl)}) ) {
229 $output .= " "x$depth;
230 $output .= '/*--S '.$_class_map{ref($perl)}{hint}."--*/ ";
231 if (lc($_class_map{ref($perl)}{type}) eq 'hash') {
233 $output .= perl2prettyJSON(\%hash,undef,1);
234 } elsif (lc($_class_map{ref($perl)}{type}) eq 'array') {
236 $output .= perl2prettyJSON(\@array,undef,1);
238 #$output .= " "x$depth;
239 $output .= ' /*--E '.$_class_map{ref($perl)}{hint}.'--*/';
241 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
243 $output .= " "x$depth unless ($nospace);
247 for my $key (sort keys %$perl) {
248 $output .= ",\n" if ($c);
250 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
255 $output .= " "x$depth;
258 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
260 $output .= " "x$depth unless ($nospace);
264 for my $part (@$perl) {
265 $output .= ",\n" if ($c);
267 $output .= perl2prettyJSON($part);
272 $output .= " "x$depth;
276 $perl =~ s/\\/\\\\/sgo;
277 $perl =~ s/"/\\"/sgo;
278 $perl =~ s/\t/\\t/sgo;
279 $perl =~ s/\f/\\f/sgo;
280 $perl =~ s/\r/\\r/sgo;
281 $perl =~ s/\n/\\n/sgo;
282 $output .= " "x$depth unless($nospace);
283 $output .= '"'.$perl.'"';