Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The JSON module has a few options for pretty printing, like adding spaces and indenting (see JSON on CPAN). However, the output can be a bit too pretty (i.e., unreadable because of too much whitespace). For example:
printsuse JSON; %HoA = ('foo' => [1,2,3], 'bar' => ['a','b','c']); print "plain:\n".JSON->new->encode(\%HoA)."\n"; print "pretty:\n".JSON->new->pretty->encode(\%HoA);
Instead, I would like to keep arrays on one line while retaining newlines and indents for key-value pairs, i.e.:plain: {"foo":[1,2,3],"bar":["a","b","c"]} pretty: { "foo" : [ 1, 2, 3 ], "bar" : [ "a", "b", "c" ] }
What is the best way to do this? I could try to custom pretty print the plain string in Perl, but that seems to defy the purpose. Are there any other Perl to JSON modules, or pattern-based string printers, is it easy after all to reimplement such a pretty printer, or is altering JSON.pm a good option?{ "foo" : [1,2,3], "bar" : ["a","b","c"] }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: customized JSON pretty print
by hdb (Monsignor) on Feb 16, 2015 at 12:37 UTC | |
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 | |
|
Re: customized JSON pretty print
by LanX (Saint) on Feb 16, 2015 at 15:19 UTC |