From a38ad4d64dc65170d690aaec6ff752f02758c078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 26 Jan 2017 01:32:16 +0100 Subject: [PATCH] json-format.pl: further fixes - skip_ws() -> gettoken() as it doesn't always skip whitespace - add a newline after each top-level token - since we've become a streaming parser, GNU parallel may not apply, so remove the comment at the top of the file --- json-format.pl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/json-format.pl b/json-format.pl index f43488c..571e89e 100755 --- a/json-format.pl +++ b/json-format.pl @@ -1,6 +1,4 @@ #!/usr/bin/env perl -# To speed up processing of large files, GNU parallel can be used: -# $ parallel --pipe -k json-format.pl INPUT use strict; use warnings; use Term::ANSIColor; @@ -75,7 +73,7 @@ sub nexttoken ($) { return 'ERROR', $text; } -sub skip_ws ($) { +sub gettoken ($) { my $json = shift; while (my ($token, $text) = nexttoken $json) { next if !$keep_ws && $token eq 'WS'; @@ -85,8 +83,7 @@ sub skip_ws ($) { } sub printindent () { - print "\n"; - print ' ' x $indent; + print "\n", ' ' x $indent; } sub do_value ($$$); @@ -94,7 +91,7 @@ sub do_object ($) { my $json = shift; my $in_field_name = 1; my $first = 1; - while (my ($token, $text) = skip_ws $json) { + while (my ($token, $text) = gettoken $json) { if ($token eq 'COLON') { $in_field_name = 0; } elsif ($token eq 'COMMA') { @@ -117,7 +114,7 @@ sub do_object ($) { sub do_array ($) { my $json = shift; my $first = 1; - while (my ($token, $text) = skip_ws $json) { + while (my ($token, $text) = gettoken $json) { if ($token eq 'RBRACKET') { $indent--; printindent unless $keep_ws; @@ -151,7 +148,7 @@ sub do_value ($$$) { } my @buffer; -while (my ($token, $text) = skip_ws \@buffer) { +while (my ($token, $text) = gettoken \@buffer) { do_value $token, $text, \@buffer; + print "\n" unless $keep_ws; } -print "\n" unless $keep_ws;