jsonfmt: Add pretty option

That completes the json_format() function for now. At least, it now does
everything I had planned for it.

Ended up at a bit over 300 LOC. That's larger than I had expected, but
still alright.
This commit is contained in:
Yorhel 2025-01-30 07:52:22 +01:00
parent 1a0fb03205
commit aebe5a93dc
3 changed files with 56 additions and 8 deletions

View file

@ -68,7 +68,7 @@ my @errors = (
do { my $o = {}; bless $o, 'MyToJSONSelf' }, qr/MyToJSONSelf::TO_JSON method returned same object as was passed instead of a new one/,
);
plan tests => @tests*2 + @errors/2 + 9;
plan tests => @tests*2 + @errors/2 + 10;
for my($in, $exp) (@tests) {
my $out = json_format $in;
@ -90,6 +90,28 @@ for my ($in, $exp) (@errors) {
is json_format({qw/a 1 b 2 c 3 d 4 d1 5 d11 6/, do { use utf8; qw/ü 7 月 8 💩 9/ }}, canonical => 1),
do { use utf8; '{"a":"1","b":"2","c":"3","d":"4","d1":"5","d11":"6","ü":"7","月":"8","💩":"9"}' };
is json_format(
{ a => [], b => {}, c => { x => 1 }, d => { y => true, z => false }, e => [1,2,3] },
canonical => 1, pretty => 1
), <<_;
{
"a" : [],
"b" : {},
"c" : {
"x" : 1
},
"d" : {
"y" : true,
"z" : false
},
"e" : [
1,
2,
3
]
}
_
eval { json_format [[]], max_depth => 2 };
like $@, qr/max_depth exceeded while formatting JSON/;