in reply to Re: customized JSON pretty print
in thread customized JSON pretty print

Thank you for the quick reply, this is indeed a good solution for my example and a nice use of multi-line regexes. However for more complex JSON (the type one would usually want pretty printing for) this will not work, as you have noted. Any suggestions for that case?

A related issue in pretty printing (sorry if this goes against the customs of the Monastery) is the ordering of keys. The JSON module allows to use the order provided by the Perl hash (which typically changes between executions) or to sort the keys alphabetically. However, I would like to use a custom sort order to improve readability of the output. Any thoughts?

Replies are listed 'Best First'.
Re^3: customized JSON pretty print
by hdb (Monsignor) on Feb 16, 2015 at 14:37 UTC

    A quick look into JSON::PP reveals that there is a function array_to_json and in there a line

    my ($pre, $post) = $indent ? $self->_up_indent() : ('', '');

    If you change that to

    my ($pre, $post) = ('', '');

    pretty stops prettifying arrays.

    use JSON::PP; my %HoA = ('foo' => [1,2,3], 'bar' => ['a','b','c']); print "plain:\n".JSON::PP->new->encode(\%HoA)."\n"; print "pretty:\n".JSON::PP->new->pretty->encode(\%HoA);
Re^3: customized JSON pretty print
by MidLifeXis (Monsignor) on Feb 16, 2015 at 14:25 UTC

    Custom sort or ASCIIbetical sort? ->canonical(1) on the json object will sort the keys in a stable manner.

    --MidLifeXis