-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml2json
executable file
·79 lines (69 loc) · 1.37 KB
/
yaml2json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env perl
#
# $Id: yaml2json,v 1.15 2012/01/13 06:57:15 ryo Exp $
#
# Copyright(c) 2009 Ryo SHIMIZU <ryo@misakimix.org>
#
use strict;
use warnings;
use Data::Dumper;
use Encode;
use Getopt::Std;
use JSON::XS;
use LWP::Simple;
use YAML::Syck;
use YAML::XS;
sub usage {
die <<__USAGE__;
usage: yaml2json [options] [file ...]
-a escape 8bit character using \\uXXXX
-s line separated mode. output json object per line
-x use YAML::XS instead of YAML::Syck
-q quiet
__USAGE__
}
my %opts;
getopts('asxq', \%opts) or usage();
my $json = new JSON::XS;
$json->ascii(exists $opts{a} ? 1 : 0);
if ($#ARGV >= 0) {
for my $file (@ARGV) {
print "== $file ==\n" if (($#ARGV > 0) && !exists($opts{q}));
yaml2json(scalar(fileread($file)));
}
} else {
my $file = join("", <>);
yaml2json($file);
}
exit;
sub yaml2json {
my $yaml = shift;
my $data;
if (exists $opts{x}) {
$data = YAML::XS::Load($yaml);
} else {
$data = YAML::Syck::Load($yaml);
}
if (exists($opts{s}) && (ref($data) eq 'ARRAY')) {
for (@$data) {
print $json->encode($_), "\n";
}
} else {
print $json->encode($data), "\n";
}
}
sub fileread {
my $file = shift;
if ($file =~ m#^(http|https|ftp)://#i) {
my $yaml = get($file);
unless (defined $yaml) {
warn "$file: error\n";
$yaml = '';
}
$yaml;
} else {
open my $fh, "< $file" or die "open: $file: $!\n";
local $/;
<$fh>;
}
}