#!/usr/bin/perl

my @righe=<>;
@right = grep { /\t/ } @righe;
my $headOptions = {};
my @heads = getHeads();
my $qt = $#heads;

my @objects;
while (my $riga = shift(@righe)) {
	chomp $riga;
	my @fields = split(/\t/,$riga);
	my @vals;
	for ($i=0; $i<=$qt; $i++) {
		my $h = $heads[$i];
		my $v = $fields[$i];
		my $switch = $headOptions->{$h} || {};
		next if ($v eq "" and ! $switch->{force});
		if ( $switch->{bool} ) {
			$v = ($v eq "" or $v =~ /^f0/i) ? "false" : "true";
		} elsif ( $switch->{Bool} ) {
			$v = ($v eq "" or $v =~ /^f0/i) ? "0" : "1";
		} elsif ( $v =~ m/^(true|false)$/i and ! $switch->{string}) {
			$v = lc($v);
		} elsif ( $switch->{string} or $v !~ /^[1-9][0-9]*$/ ) {
			$v =~ s/\\/\\\\/g;
			$v =~ s/"/\\"/g;
			$v = '"'.$v.'"';
		}
		push @vals,"\"$h\": $v";
	}
	push @objects, "\t\t".join(",\n\t\t",@vals)."\n" if @vals;
}
print "[\n\t{\n".join("\t},\n\t{\n",@objects)."\t}\n]\n";


sub getHeads {
	my $heads = shift @righe;
	chomp $heads;
	my @heads = split(/\t/,$heads);
	my @out;
	foreach my $head (@heads) {
		if ( $head =~ s/ *\{([^\}]*)\} *// ) {
			my $v = "$1";
			my $out = {};
			$out->{force} = 1 if $v =~ /\!/;
			$out->{string} = 1 if $v =~ /s/;
			$out->{bool} = 1 if $v =~ /b/;
			$out->{Bool} = 1 if $v =~ /B/;
			$headOptions->{$head} = $out;
		}
		push @out,$head;
	}
	return @out;
}
