Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Created May 4, 2014 01:32
Show Gist options
  • Save harshavardhana/a9c0ce83ae752add9af6 to your computer and use it in GitHub Desktop.
Save harshavardhana/a9c0ce83ae752add9af6 to your computer and use it in GitHub Desktop.
Json parse validate
#!/usr/bin/env perl
# Borrowed from - http://search.cpan.org/~mlehmann/JSON-XS-3.01/bin/json_xs
# Modified for internal use in GlusterFS
use strict;
use Encode;
use JSON::XS;
my $opt_from = "json";
my $opt_to = "null";
my %F = (
"json" => sub {
my $enc =
/^\x00\x00\x00/s ? "utf-32be"
: /^\x00.\x00/s ? "utf-16be"
: /^.\x00\x00\x00/s ? "utf-32le"
: /^.\x00.\x00/s ? "utf-16le"
: "utf-8";
JSON::XS->new->decode (decode $enc, $_)
},
);
my %T = (
"null" => sub { "" },
);
$F{$opt_from}
or die "$opt_from: not a valid fromformat\n";
$T{$opt_to}
or die "$opt_from: not a valid toformat\n";
{
local $/;
binmode STDIN; # stupid perl sometimes thinks its funny
$_ = <STDIN>;
}
$_ = $F{$opt_from}->();
$_ = $T{$opt_to}->();
binmode STDOUT;
print $_;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment