1 package JSON
::Converter
;
3 use vars
qw($AUTOCONVERT $VERSION);
10 sub new { bless {AUTOCONVERT=>$AUTOCONVERT}, shift; }
16 $self->{_stack_myself} = [];
18 local $AUTOCONVERT = $self->{AUTOCONVERT};
20 if(ref($obj) eq 'HASH'){
21 return $self->hashToJson($obj);
23 elsif(ref($obj) eq 'ARRAY'){
24 return $self->arrayToJson($obj);
27 return $self->valueToJson($obj);
40 if(grep { $_ == $obj } @{ $self->{_stack_myself} }){
44 push @{ $self->{_stack_myself} },$obj;
46 for my $k (keys %$obj){
48 if(ref($v) eq "HASH"){
49 $res{$k} = $self->hashToJson($v);
51 elsif(ref($v) eq "ARRAY"){
52 $res{$k} = $self->arrayToJson($v);
55 $res{$k} = $self->valueToJson($v);
59 pop @{ $self->{_stack_myself} };
61 return '{' . join(',',map { _stringfy($_) . ':' .$res{$_} } sort keys %res) . '}';
70 if(grep { $_ == $obj } @{ $self->{_stack_myself} }){
74 push @{ $self->{_stack_myself} },$obj;
77 if(ref($v) eq "HASH"){
78 push @res,$self->hashToJson($v);
80 elsif(ref($v) eq "ARRAY"){
81 push @res,$self->arrayToJson($v);
84 push @res,$self->valueToJson($v);
88 pop @{ $self->{_stack_myself} };
90 return '[' . join(',',@res) . ']';
98 return 'null' if(!defined $value);
100 if($AUTOCONVERT and !ref($value)){
101 return $value if($value =~ /^-?(?:0|[1-9][\d]*)(?:\.[\d]+)?$/);
102 return 'true' if($value =~ /^true$/i);
103 return 'false' if($value =~ /^false$/i);
106 return _stringfy($value) unless(ref($value));
108 if( $value->isa('JASON::NotSring') ){
112 return defined $value->{value} ? $value->{value} : 'null';
123 my $c = substr($arg,$i++,1);
125 $c =~ s{(["\\/])}{\\$1};
128 elsif($c =~ tr/\n\r\t\f\b/nrtfb/){
132 $s .= '\\u00' . unpack('H2',$c);
135 $s =~ s/\]\]>/]]\\u003E/gos;
150 alias of C<objToJson>.
154 convert a passed perl data structure into JSON object.
155 can't parse blessed object.
159 convert a passed hash into JSON object.
163 convert a passed array into JSON array.
167 convert a passed data into a string of JSON.
173 makamaka [at] donzoko.net
175 This library is free software; you can redistribute it
176 and/or modify it under the same terms as Perl itself.
180 L<http://www.crockford.com/JSON/index.html>