tests/run-tests.html: move noscript into body
[git-browser.git] / JSON / Converter.pm
blob30f3c8a6e0e3d49d9c538d5eb1a27ae06f88c282
1 package JSON::Converter;
3 use vars qw($AUTOCONVERT $VERSION);
4 use Carp;
6 $VERSION = 0.9902;
8 $AUTOCONVERT = 1;
10 sub new { bless {AUTOCONVERT=>$AUTOCONVERT}, shift; }
12 sub objToJson {
13 my $self = shift;
14 my $obj = 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);
26 elsif(!ref($obj)){
27 return $self->valueToJson($obj);
29 else{
30 return;
34 sub hashToJson {
35 my $self = shift;
36 my $obj = shift;
37 my ($k,$v);
38 my %res;
40 if(grep { $_ == $obj } @{ $self->{_stack_myself} }){
41 die "circular ref!";
44 push @{ $self->{_stack_myself} },$obj;
46 for my $k (keys %$obj){
47 my $v = $obj->{$k};
48 if(ref($v) eq "HASH"){
49 $res{$k} = $self->hashToJson($v);
51 elsif(ref($v) eq "ARRAY"){
52 $res{$k} = $self->arrayToJson($v);
54 else{
55 $res{$k} = $self->valueToJson($v);
59 pop @{ $self->{_stack_myself} };
61 return '{' . join(',',map { _stringfy($_) . ':' .$res{$_} } sort keys %res) . '}';
65 sub arrayToJson {
66 my $self = shift;
67 my $obj = shift;
68 my @res;
70 if(grep { $_ == $obj } @{ $self->{_stack_myself} }){
71 die "circular ref!";
74 push @{ $self->{_stack_myself} },$obj;
76 for my $v (@$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);
83 else{
84 push @res,$self->valueToJson($v);
88 pop @{ $self->{_stack_myself} };
90 return '[' . join(',',@res) . ']';
94 sub valueToJson {
95 my $self = shift;
96 my $value = shift;
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') ){
109 die "Invalid value";
112 return defined $value->{value} ? $value->{value} : 'null';
116 sub _stringfy {
117 my $arg = shift;
118 my $l = length $arg;
119 my $s = '"';
120 my $i = 0;
122 while($i < $l){
123 my $c = substr($arg,$i++,1);
124 if($c ge ' '){
125 $c =~ s{(["\\/])}{\\$1};
126 $s .= $c;
128 elsif($c =~ tr/\n\r\t\f\b/nrtfb/){
129 $s .= '\\' . $c;
131 else{
132 $s .= '\\u00' . unpack('H2',$c);
135 $s =~ s/\]\]>/]]\\u003E/gos;
137 return $s . '"';
144 =head1 METHODs
146 =over
148 =item parse
150 alias of C<objToJson>.
152 =item objToJson
154 convert a passed perl data structure into JSON object.
155 can't parse blessed object.
157 =item hashToJson
159 convert a passed hash into JSON object.
161 =item arrayToJson
163 convert a passed array into JSON array.
165 =item valueToJson
167 convert a passed data into a string of JSON.
169 =back
171 =head1 COPYRIGHT
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.
178 =head1 SEE ALSO
180 L<http://www.crockford.com/JSON/index.html>
182 =cut