in reply to customized JSON pretty print
First use pretty and then remove superfluous whitespace:
use JSON; sub remove_newlines { my $str = shift; $str =~ s/^\s+//gsm; $str =~ s/\n//gsm; return $str; } %HoA = ('foo' => [1,2,3], 'bar' => ['a','b','c']); print "plain:\n".JSON->new->encode(\%HoA)."\n"; my $pretty = JSON->new->pretty->encode(\%HoA); $pretty =~ s/(\[.*?\])/remove_newlines($1)/egsm; print "pretty:\n$pretty";
This will not work with nested brackets or brackets within strings, though.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: customized JSON pretty print
by Anonymous Monk on Feb 16, 2015 at 13:37 UTC | |
by hdb (Monsignor) on Feb 16, 2015 at 14:37 UTC | |
by MidLifeXis (Monsignor) on Feb 16, 2015 at 14:25 UTC |