5 my $x = shift || $class;
6 return bless \$x => __PACKAGE__;
9 use overload ( '""' => \&toString );
11 sub toString { defined($_[1]) ? ${$_[1]} : ${$_[0]} }
13 package JSON::bool::true;
14 sub new { return bless {} => __PACKAGE__ }
15 use overload ( '""' => \&toString );
16 use overload ( 'bool' => sub { 1 } );
17 use overload ( '0+' => sub { 1 } );
19 sub toString { 'true' }
21 package JSON::bool::false;
22 sub new { return bless {} => __PACKAGE__ }
23 use overload ( '""' => \&toString );
24 use overload ( 'bool' => sub { 0 } );
25 use overload ( '0+' => sub { 0 } );
27 sub toString { 'false' }
30 use Unicode::Normalize;
31 use vars qw/%_class_map/;
33 sub register_class_hint {
37 $_class_map{hints}{$args{hint}} = \%args;
38 $_class_map{classes}{$args{name}} = \%args;
47 -?\d+\.?\d* | # number literal
48 "(?:(?:\\[\"])|[^\"])*" | # string literal
49 (?:\/\*.+?\*\/) | # C comment
53 : | # object key-value sep
62 sub _json_hint_to_class {
66 return $_class_map{hints}{$hint}{name} if (exists $_class_map{hints}{$hint});
68 $type = 'hash' if ($type eq '}');
69 $type = 'array' if ($type eq ']');
71 JSON->register_class_hint(name => $hint, hint => $hint, type => $type);
80 s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
81 s/(?<!\\)\@/\\\@/gmo; # fixup @ for later
82 s/(?<!\\)\%/\\\%/gmo; # fixup % for later
84 # Convert JSON Unicode...
85 s/\\u(\d{4})/chr(hex($1))/esog;
87 # handle class blessings
88 s/\/\*--\s*S\w*?\s+\S+\s*--\*\// bless(/sog;
89 s/(\]|\}|")\s*\/\*--\s*E\w*?\s+(\S+)\s*--\*\//$1 => _json_hint_to_class("$1", "$2")) /sog;
91 my $re = qr/((?<!\\)"(?>(?<=\\)"|[^"])*(?<!\\)")/;
93 my @strings = /$re/sog;
95 # Replace with code...
96 #s/"(?:(?:\\[\"])|[^\"])*"/ do{ \$t = '"'.shift(\@strings).'"'; eval \$t;} /sog;
97 s/$re/ eval shift(\@strings) /sog;
99 # Perlify hash notation
103 # 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;
111 return eval '$ret = '.$_;
115 my ($class, $json) = @_;
117 if (!defined($json)) {
121 $json =~ s/(?<!\\)\$/\\\$/gmo; # fixup $ for later
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, $strict) = @_;
180 if (!defined($perl)) {
181 $output = '' if $strict;
182 $output = 'null' unless $strict;
183 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
185 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
186 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
187 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
189 $output .= perl2JSON(undef,\%hash, $strict);
190 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
192 $output .= perl2JSON(undef,\@array, $strict);
194 $output .= '/*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
195 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
198 for my $key (sort keys %$perl) {
200 $output .= ',' if ($c);
202 $outkey =~ s{\\}{\\\\}sgo;
203 $outkey =~ s/"/\\"/sgo;
204 $outkey =~ s/\t/\\t/sgo;
205 $outkey =~ s/\f/\\f/sgo;
206 $outkey =~ s/\r/\\r/sgo;
207 $outkey =~ s/\n/\\n/sgo;
208 $outkey =~ s/(\pM|\pS)/sprintf('\u%0.4x',ord($1))/sgoe;
209 $outkey =~ s/[\x80-\xff]//sgoe;
211 $output .= '"'.$outkey.'":'. perl2JSON(undef,$$perl{$key}, $strict);
215 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
218 for my $part (@$perl) {
219 $output .= ',' if ($c);
221 $output .= perl2JSON(undef,$part, $strict);
225 } elsif (ref($perl) and ("$perl" =~ /^([^=]+)=(\w+)/o)) {
228 JSON->register_class_hint(name => $name, hint => $name, type => lc($type));
229 $output .= perl2JSON(undef,$perl, $strict);
232 $perl =~ s{\\}{\\\\}sgo;
233 $perl =~ s/"/\\"/sgo;
234 $perl =~ s/\t/\\t/sgo;
235 $perl =~ s/\f/\\f/sgo;
236 $perl =~ s/\r/\\r/sgo;
237 $perl =~ s/\n/\\n/sgo;
238 $perl =~ s/(\pM|\pC|\pM)/sprintf('\u%0.4x',ord($1))/sgoe;
239 $perl =~ s/[\x80-\xff]//sgoe;
240 if (length($perl) < 10 and $perl =~ /^(?:\+|-)?\d*\.?\d+$/o and $perl !~ /^(?:\+|-)?0\d+/o ) {
243 $output = '"'.$perl.'"';
251 sub perl2prettyJSON {
252 my ($class, $perl, $nospace) = @_;
256 if (!defined($perl)) {
257 $output = " "x$depth unless($nospace);
259 } elsif (ref($perl) and ref($perl) =~ /^JSON/) {
260 $output = " "x$depth unless($nospace);
262 } elsif ( ref($perl) && exists($_class_map{classes}{ref($perl)}) ) {
265 $output .= " "x$depth;
266 $output .= '/*--S '.$_class_map{classes}{ref($perl)}{hint}."--*/ ";
267 if (lc($_class_map{classes}{ref($perl)}{type}) eq 'hash') {
269 $output .= perl2prettyJSON(\%hash,undef,1);
270 } elsif (lc($_class_map{classes}{ref($perl)}{type}) eq 'array') {
272 $output .= perl2prettyJSON(\@array,undef,1);
274 $output .= ' /*--E '.$_class_map{classes}{ref($perl)}{hint}.'--*/';
276 } elsif (ref($perl) and ref($perl) =~ /HASH/) {
277 $output .= " "x$depth unless ($nospace);
281 for my $key (sort keys %$perl) {
282 $output .= ",\n" if ($c);
284 $output .= perl2prettyJSON($key)." : ".perl2prettyJSON($$perl{$key}, undef, 1);
289 $output .= " "x$depth;
291 } elsif (ref($perl) and ref($perl) =~ /ARRAY/) {
292 $output .= " "x$depth unless ($nospace);
296 for my $part (@$perl) {
297 $output .= ",\n" if ($c);
299 $output .= perl2prettyJSON($part);
304 $output .= " "x$depth;
306 } elsif (ref($perl) and "$perl" =~ /^([^=]+)=(\w{4,5})\(0x/) {
309 register_class_hint(undef, name => $name, hint => $name, type => lc($type));
310 $output .= perl2prettyJSON(undef,$perl);
313 $perl =~ s/\\/\\\\/sgo;
314 $perl =~ s/"/\\"/sgo;
315 $perl =~ s/\t/\\t/sgo;
316 $perl =~ s/\f/\\f/sgo;
317 $perl =~ s/\r/\\r/sgo;
318 $perl =~ s/\n/\\n/sgo;
319 $perl =~ s/(\pM|\pC)/sprintf('\u%0.4x',ord($1))/sgoe;
320 $perl =~ s/[\x80-\xff]//sgoe;
321 $output .= " "x$depth unless($nospace);
322 if (length($perl) < 10 and $perl =~ /^(?:\+|-)?\d*\.?\d+$/o and $perl !~ /^(?:\+|-)?0\d+/o ) {
325 $output = '"'.$perl.'"';
327 $output .= '"'.$perl.'"';