Created
July 5, 2011 18:55
-
-
Save psema4/1065570 to your computer and use it in GitHub Desktop.
Braindead JSON encoder/decoder for Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package JSONSimple; | |
use strict; | |
sub new { | |
my $class = shift; | |
my $self = {}; | |
bless($self, $class); | |
return $self; | |
} | |
sub decode { | |
my $self = shift; | |
return undef unless (@_); | |
my $buf = shift; | |
$buf =~ s/:/=>/g; | |
return eval($buf); | |
} | |
sub encode { | |
my $self = shift; | |
return undef unless (@_); | |
my $buf = $self->stringify(@_); | |
return $buf; | |
} | |
sub stringify { | |
my $self = shift; | |
my $o = shift; | |
my $result = ''; | |
my $ref_type = lc(ref($o)); | |
if ($ref_type eq 'hash') { | |
$result .= '{'; | |
foreach my $key (keys(%{$o})) { | |
$result .= '"' . $key . '":'; | |
my $ref_type_sub = lc(ref(%{$o}->{$key})); | |
if ($ref_type_sub eq 'hash') { | |
$result .= $self->stringify(%{$o}->{$key}) . ","; | |
} elsif ($ref_type_sub eq 'array') { | |
$result .= $self->stringify(%{$o}->{$key}) . ","; | |
} else { | |
$result .= '"' . %{$o}->{$key} . '",'; | |
} | |
} | |
$result =~ s/,$//; | |
$result .= '}'; | |
} elsif ($ref_type eq 'array') { | |
$result .= '['; | |
for (my $i=0; $i<=$#$o; $i++) { | |
my $ref_type_sub = lc(ref(@{$o}[$i])); | |
if ($ref_type_sub eq 'hash') { | |
$result .= $self->stringify(@{$o}[$i]) . ","; | |
} elsif ($ref_type_sub eq 'array') { | |
$result .= $self->stringify(@{$o}[$i]) . ","; | |
} else { | |
$result .= '"' . @{$o}[$i] . '",'; | |
} | |
} | |
$result =~ s/,$//; | |
$result .= ']'; | |
} else { | |
$result .= '"' . $o . '"'; | |
} | |
$result =~ s/,$//; | |
return $result; | |
} | |
1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'foo': [ | |
'bar', 'baz' | |
], | |
'bar': 1, | |
'baz': { | |
'a': 1, | |
'b': 2, | |
'c': { | |
'key1': 'val1', | |
'key2': 'val2', | |
'key3': 'val3' | |
}, | |
'd': [ | |
{ | |
'a': 'a1', | |
'b': 'b1', | |
'c': 'c1' | |
}, | |
{ | |
'a': 'a2', | |
'b': 'b2', | |
'c': 'c2' | |
}, | |
{ | |
'a': 'a3', | |
'b': 'b3', | |
'c': 'c3' | |
} | |
] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use JSONSimple; | |
use Data::Dumper; | |
my $j = JSONSimple->new(); | |
open(FH, "< test.json") || die "Couldn't open file for reading: $!"; | |
my @lines = <FH>; | |
close FH; | |
my $buf = join('', @lines); | |
my $decoded = $j->decode($buf); | |
print "Decoded:\n"; | |
print Dumper($decoded) . "\n"; | |
my $encoded = $j->encode({ | |
foo => 'bar', | |
bar => ['foo', 'bar', 'baz'], | |
baz => { | |
a => 1, | |
b => 2, | |
c => 3, | |
d => [ | |
{ | |
a => 'a1', | |
b => 'b1', | |
c => 'c1', | |
},{ | |
a => 'a2', | |
b => 'b2', | |
c => 'c2', | |
},{ | |
a => 'a3', | |
b => 'b3', | |
c => 'c3', | |
} | |
] | |
} | |
}); | |
print "Encoded:\n"; | |
print Dumper($encoded) . "\n"; |
Author
psema4
commented
Jul 5, 2011
- Don't use in production ;-)
- Intended for testing in a microperl environment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment