If arrays cannot contain hashes, the following could be workaround:
use warnings;
use strict;
use JSON::XS;
use Text::Balanced qw(extract_bracketed extract_delimited);
use Text::CSV;
my $csv = Text::CSV->new( { sep_char => ',', allow_whitespace => 1 } );
my $h = { a => "[", g => [ "[", 2, "bb]]", 4 ], b => 3, c => [ 1, 2, 3, 4 ] };
my $coder = JSON::XS->new->pretty;
my $txt = $coder->encode($h);
my $str = "";
while (1) {
my $ind1 = index( $txt, '"' );
my $ind2 = index( $txt, '[' );
if ( $ind1 >= 0 && $ind2 >= 0 ) {
if ( $ind1 < $ind2 ) {
skipQuoted( \$txt, \$str );
next;
}
}
elsif ( $ind2 < 0 ) {
$str .= $txt;
last;
}
my ( $etxt, $end, $beg ) = extract_bracketed( $txt, '["]', '[^[]*' );
die "Unexpected!" if !defined $etxt;
$str .= $beg;
$etxt = substr( $etxt, 1, length($etxt) - 2 )
; #strip leading and trailing brackets
$etxt =~ s{\n}{}g;
my @elem;
if ( $csv->parse($etxt) ) {
@elem = $csv->fields();
}
else {
die "Unexpected!";
}
$str .= '[ ' . processFields( \@elem ) . ' ]';
$txt = $end;
}
print $str;
sub skipQuoted {
my ( $txt, $str ) = @_;
my ( $s1, $s2, $s3 ) = extract_delimited( $$txt, '"', '[^"]*' );
die "Unexpected!" if !defined $s1;
$$str .= $s3 . $s1;
$$txt = $s2;
}
sub processFields {
my ($a) = @_;
for (@$a) {
if ( $_ !~ /^-?(0|([1-9][0-9]*))(\.[0-9]+)?([eE][-+]?[0-9]+)?$/ ) {
$_ = '"' . $_ . '"';
}
}
return join( ", ", @$a );
}
Output:
{
"a" : "[",
"g" : [ "[", 2, "bb]]", 4 ],
"b" : 3,
"c" : [ 1, 2, 3, 4 ]
}