tools/mkjson: indent objects but not arrays
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 28 Jan 2024 15:22:08 +0000 (16:22 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sun, 25 Feb 2024 15:03:30 +0000 (16:03 +0100)
Pretty but not too long; assumes arrays are used as unkeyed hashes.

tools/mkjson

index 6a7d9489a46bcd75fe59e1a2f1b6fab531c547fc..dace34afce91d2f6ca610c2d82b10bd6d039f65b 100755 (executable)
@@ -2,6 +2,12 @@
 use 5.012;
 use warnings;
 use JSON;
+use re '/msx';
+
+my %opt;
+my $jsonify = JSON->new->utf8->canonical;
+$jsonify->pretty if $opt{pretty};
 
 my $data = do "./$ARGV[0]" or die $@;
-print JSON->new->utf8->canonical->encode($data);
+print $jsonify->encode($data)
+       =~ s{\[ \K\n ([^][]+) (?=\])}{$1 =~ s/(?:\A|\n) \s*//gr}reg;