Config-JSON-Enhanced

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    This module was created because I needed to include long shell scripts
    containing lots of quotes and newlines, in a configuration file which
    started as JSON.

    The process is simple: so-called "enhanced JSON" is parsed by
    config2perl. Comments are removed, variables are substituted, verbatim
    sections become one line again and standard JSON is created. This is
    parsed with JSON (via Data::Roundtrip::json2perl) to produce a Perl
    data structure which is returned.

    It has been tested with unicode data (see
    t/070-config2perl-complex-utf8.t) with success. But who knows ?!?!

    Here is an example:

        use Config::JSON::Enhanced;
    
        # simple "enhanced" JSON with comments in 3 styles: C,shell,CPP
        my $configdata = <<'EOJ';
         {
            /* 'a' is ... */

README.md  view on Meta::CPAN

long shell scripts containing lots of quotes and newlines,
in a configuration file which started as JSON.

The process is simple: so-called "enhanced JSON" is parsed
by [config2perl](https://metacpan.org/pod/config2perl). Comments are removed, variables are
substituted, verbatim sections become one line again
and standard JSON is created. This is parsed with
[JSON](https://metacpan.org/pod/JSON) (via [Data::Roundtrip::json2perl](https://metacpan.org/pod/Data%3A%3ARoundtrip%3A%3Ajson2perl)) to
produce a Perl data structure which is returned.

It has been tested with unicode data
(see `t/070-config2perl-complex-utf8.t`)
with success. But who knows ?!?!

Here is an example:

       use Config::JSON::Enhanced;

       # simple "enhanced" JSON with comments in 3 styles: C,shell,CPP
       my $configdata = <<'EOJ';
        {

lib/Config/JSON/Enhanced.pm  view on Meta::CPAN

use warnings;

our $VERSION = '0.10';

use strict;
use warnings;

# which loads JSON::XS with a purel-perl JSON fallback
use JSON;

use Data::Roundtrip qw/json2perl perl2dump no-unicode-escape-permanently/;

use Exporter; # we have our own import() don't import it
our @ISA = qw(Exporter);
our @EXPORT = qw/
	config2perl
/;

# Convert enhanced JSON string into a Perl data structure.
# The input parameters hashref:
#  * specify where is the content to be parsed via:

lib/Config/JSON/Enhanced.pm  view on Meta::CPAN

long shell scripts containing lots of quotes and newlines,
in a configuration file which started as JSON.

The process is simple: so-called "enhanced JSON" is parsed
by L<config2perl>. Comments are removed, variables are
substituted, verbatim sections become one line again
and standard JSON is created. This is parsed with
L<JSON> (via L<Data::Roundtrip::json2perl>) to
produce a Perl data structure which is returned.

It has been tested with unicode data
(see C<t/070-config2perl-complex-utf8.t>)
with success. But who knows ?!?!

Here is an example:

    use Config::JSON::Enhanced;

    # simple "enhanced" JSON with comments in 3 styles: C,shell,CPP
    my $configdata = <<'EOJ';
     {

script/configjson2json.pl  view on Meta::CPAN


our $VERSION = '0.10';

binmode STDERR, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
binmode STDIN, ':encoding(UTF-8)';

use Getopt::Long qw(:config no_ignore_case);

use Config::JSON::Enhanced;
use Data::Roundtrip qw/perl2json no-unicode-escape-permanently/;

my $OUTPUT_FILE = undef;
my %params = (
	'commentstyle' => 'custom(</*)(*/>)',
	'tags' => ['<%', '%>'],
);

sub usage { return
	"Usage : $0 [--I inputstring] [--i 'afile.json'] [--o afile] [--c commentstyle] [--tags tags]\n"
	."\nIt will read a JSON string from command line (-I), or from a file (-i)\n"
	."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
	."It will print its contents as a Perl variable (dump) to STDOUT or to a file (--o).\n"
	."It can escape/un-escape unicode characters (--escape-unicode) and/or --terse and/or --indent.\n"
}
if( ! Getopt::Long::GetOptions(
  'i=s' => \$params{'filename'},
  'I=s' => sub { $params{'string'} = Encode::decode_utf8($_[1]) },
  'o=s' => \$OUTPUT_FILE,
  'commentstyle|c=s' => \$params{'commentstyle'},
  'tags|t=s' => \$params{'tags'},
) ){ die usage() }

if( ! exists($params{'filename'}) && ! exists($params{'string'}) ){

script/configjson2json.pl  view on Meta::CPAN

=head1 NAME

json2perl.pl : convert JSON data to a Perl variable (dump) which can be parsed or eval'ed by any Perl script.

=head1 VERSION

Version 0.10

=head1 SYNOPSIS

    json2perl.pl -i "input.json" -o "output.pl" --no-escape-unicode --terse --no-indent

    json2perl.pl -e < "input.json" > "output.pl"

    # press CTRL-D when done typing JSON to STDIN
    # input must be less than 4K long!
    json2perl.pl

    # Read input from clipboard or write output to clipboard
    # Only in: Unix / Linux / OSX
    # (must have already installed xclip or xsel or pbpaste (on OSX))

script/configjson2json.pl  view on Meta::CPAN

=item * C<--i filename> : specify a filename which contains a JSON
data structure.

=item * C<--I "string"> : specify a string  which contains a JSON
data structure.

=item * C<--o outputfilename> : specify the output filename to write
the result to, which will be as a Perl variable, as a dump,
which can be parsed or eval'ed from any Perl script.

=item * C<--escape-unicode> : it will escape all unicode characters, and
convert them to something like "\u0386". This is the default option.

=item * C<--no-escape-unicode> : it will NOT escape unicode characters. Output
will not contain "\u0386" or "\x{386}" but "α" (that's a greek alpha).

=item * C<--terse> / C<--no-terse> : Terse form of output (no $VAR1 for example).
The second is the default option.

=item * C<--indent> / C<--no-indent> : do not use indentation. The first is the default option.

=back

Input can be read from an input file (--i), from a string at the

script/configjson2perl.pl  view on Meta::CPAN


our $VERSION = '0.10';

binmode STDERR, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
binmode STDIN, ':encoding(UTF-8)';

use Getopt::Long qw(:config no_ignore_case);

use Config::JSON::Enhanced;
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

my $OUTPUT_FILE = undef;
my %params = (
	'commentstyle' => 'custom(</*)(*/>)',
	'tags' => ['<%', '%>']
);
my %dr_params = (
	'dont-bloody-escape-unicode' => 1,
	'terse' => 0,
	'indent' => 1
);
sub usage { return
	"Usage : $0 [--I inputstring] [--i 'afile.json'] [--o afile] [--c commentstyle] [--tags tags] [--(no-)escape-unicode|-e] [--(no-)terse] [--(no-)indent]\n"
	."\nIt will read an Enhanced-JSON string from command line (-I), or from a file (-i)\n"
	."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
	."It will print its contents as a Perl variable (dump) to STDOUT or to a file (--o).\n"
	."It can escape/un-escape unicode characters (--escape-unicode) and/or --terse and/or --indent.\n"
}
if( ! Getopt::Long::GetOptions(
  'i=s' => \$params{'filename'},
  'I=s' => sub { $params{'string'} = Encode::decode_utf8($_[1]) },
  'o=s' => \$OUTPUT_FILE,
  'commentstyle|c=s' => \$params{'commentstyle'},
  'tags|t=s' => \$params{'tags'},
  'terse|r!' => \$dr_params{'terse'},
  'indent|d!' => \$dr_params{'indent'},
  'escape-unicode|e!' => sub { $dr_params{'dont-bloody-escape-unicode'} = $_[1] ? 0 : 1 },
) ){ die usage() }

if( ! exists($params{'filename'}) && ! exists($params{'string'}) ){
	# read from STDIN
	$params{'string'} = do { local $/; <STDIN> }
}

my $result = Config::JSON::Enhanced::config2perl(\%params);
if( ! defined $result ){ print STDERR "$0 : error, call to ".'Config::JSON::Enhanced::config2perl()'." has failed.\n"; exit(1) }

script/configjson2perl.pl  view on Meta::CPAN

=head1 NAME

json2perl.pl : convert JSON data to a Perl variable (dump) which can be parsed or eval'ed by any Perl script.

=head1 VERSION

Version 0.10

=head1 SYNOPSIS

    json2perl.pl -i "input.json" -o "output.pl" --no-escape-unicode --terse --no-indent

    json2perl.pl -e < "input.json" > "output.pl"

    # press CTRL-D when done typing JSON to STDIN
    # input must be less than 4K long!
    json2perl.pl

    # Read input from clipboard or write output to clipboard
    # Only in: Unix / Linux / OSX
    # (must have already installed xclip or xsel or pbpaste (on OSX))

script/configjson2perl.pl  view on Meta::CPAN

=item * C<--i filename> : specify a filename which contains a JSON
data structure.

=item * C<--I "string"> : specify a string  which contains a JSON
data structure.

=item * C<--o outputfilename> : specify the output filename to write
the result to, which will be as a Perl variable, as a dump,
which can be parsed or eval'ed from any Perl script.

=item * C<--escape-unicode> : it will escape all unicode characters, and
convert them to something like "\u0386". This is the default option.

=item * C<--no-escape-unicode> : it will NOT escape unicode characters. Output
will not contain "\u0386" or "\x{386}" but "α" (that's a greek alpha).

=item * C<--terse> / C<--no-terse> : Terse form of output (no $VAR1 for example).
The second is the default option.

=item * C<--indent> / C<--no-indent> : do not use indentation. The first is the default option.

=back

Input can be read from an input file (--i), from a string at the

script/configjson2yaml.pl  view on Meta::CPAN


our $VERSION = '0.10';

binmode STDERR, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
binmode STDIN, ':encoding(UTF-8)';

use Getopt::Long qw(:config no_ignore_case);

use Config::JSON::Enhanced;
use Data::Roundtrip qw/perl2yaml no-unicode-escape-permanently/;

my $OUTPUT_FILE = undef;
my %params = (
	'commentstyle' => 'custom(</*)(*/>)',
	'tags' => ['<%', '%>'],
);

sub usage { return
	"Usage : $0 [--I inputstring] [--i 'afile.json'] [--o afile] [--c commentstyle] [--tags tags]\n"
	."\nIt will read a JSON string from command line (-I), or from a file (-i)\n"
	."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
	."It will print its contents as a Perl variable (dump) to STDOUT or to a file (--o).\n"
	."It can escape/un-escape unicode characters (--escape-unicode) and/or --terse and/or --indent.\n"
}
if( ! Getopt::Long::GetOptions(
  'i=s' => \$params{'filename'},
  'I=s' => sub { $params{'string'} = Encode::decode_utf8($_[1]) },
  'o=s' => \$OUTPUT_FILE,
  'commentstyle|c=s' => \$params{'commentstyle'},
  'tags|t=s' => \$params{'tags'},
) ){ die usage() }

if( ! exists($params{'filename'}) && ! exists($params{'string'}) ){

script/configjson2yaml.pl  view on Meta::CPAN

=head1 NAME

json2perl.pl : convert JSON data to a Perl variable (dump) which can be parsed or eval'ed by any Perl script.

=head1 VERSION

Version 0.10

=head1 SYNOPSIS

    json2perl.pl -i "input.json" -o "output.pl" --no-escape-unicode --terse --no-indent

    json2perl.pl -e < "input.json" > "output.pl"

    # press CTRL-D when done typing JSON to STDIN
    # input must be less than 4K long!
    json2perl.pl

    # Read input from clipboard or write output to clipboard
    # Only in: Unix / Linux / OSX
    # (must have already installed xclip or xsel or pbpaste (on OSX))

script/configjson2yaml.pl  view on Meta::CPAN

=item * C<--i filename> : specify a filename which contains a JSON
data structure.

=item * C<--I "string"> : specify a string  which contains a JSON
data structure.

=item * C<--o outputfilename> : specify the output filename to write
the result to, which will be as a Perl variable, as a dump,
which can be parsed or eval'ed from any Perl script.

=item * C<--escape-unicode> : it will escape all unicode characters, and
convert them to something like "\u0386". This is the default option.

=item * C<--no-escape-unicode> : it will NOT escape unicode characters. Output
will not contain "\u0386" or "\x{386}" but "α" (that's a greek alpha).

=item * C<--terse> / C<--no-terse> : Terse form of output (no $VAR1 for example).
The second is the default option.

=item * C<--indent> / C<--no-indent> : do not use indentation. The first is the default option.

=back

Input can be read from an input file (--i), from a string at the

t/100-tags-for-comments-and-verbatim-and-variables.t  view on Meta::CPAN

#!perl

use 5.010;
use strict;
use warnings;

use Test::More;
use Test2::Plugin::UTF8; # rids of the Wide Character in TAP message!
use File::Temp qw/tempfile tempdir/;
use File::Spec;
use Data::Roundtrip qw/json2perl perl2dump no-unicode-escape-permanently/;

our $VERSION = '0.10';

use Config::JSON::Enhanced;

my $simple_json = <<'EOJ';
__COOPTAG__ this is a comment which may confuse regex __COCLTAG__
{
	"a" : "hello __TVOPTAG__ var1 __TVCLTAG__",
	"b" :

t/110-config2perl-dont-remove-comments-from-strings.t  view on Meta::CPAN

#!perl

use 5.010;
use strict;
use warnings;

use Test::More;
use File::Temp qw/tempfile/;
use Test2::Plugin::UTF8; # rids of the Wide Character in TAP message!
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

our $VERSION = '0.10';

use Config::JSON::Enhanced;

# check if we can deal with comments
my $jsonstr_C = <<'EOJ';
{
/* gone1 */ "/*comment1*/κατούρ/*comment2*/" /*gone2*/: /* gone 

t/130-config2perl-remove-comments-from-strings.t  view on Meta::CPAN

#!perl

use 5.010;
use strict;
use warnings;

use Test::More;
use File::Temp qw/tempfile/;
use Test2::Plugin::UTF8; # rids of the Wide Character in TAP message!
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

our $VERSION = '0.10';

use Config::JSON::Enhanced;

# check if we can deal with comments
my $jsonstr_C = <<'EOJ';
{
/* gone1 */ "/*comment1*/κατούρ/*comment2*/" /*gone2*/: /* gone 

t/150-config2perl-comments-in-verbatim-section.t  view on Meta::CPAN

#!perl

use 5.010;
use strict;
use warnings;

use Test::More;
use Test2::Plugin::UTF8; # rids of the Wide Character in TAP message!
use FindBin;
use Cwd 'abs_path';
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

our $VERSION = '0.10';

use Config::JSON::Enhanced;

my $appdir = Cwd::abs_path($FindBin::Bin);

# this json is in the module's pod
# Testing it works
my $con = <<'EOJ';

t/300-tags-clashing.t  view on Meta::CPAN

use strict;
use warnings;

our $VERSION = '0.10';

use Test::More;
use Test2::Plugin::UTF8; # rids of the Wide Character in TAP message!

use Config::JSON::Enhanced;

use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

my $jsonstr = <<'EOJ';
{
	"a" : "hello"
}
EOJ

my @tests = (
  {
	'commentstyle' => 'custom(<:*)(*:>)',

t/800-config2perl-pod-synopsis.t  view on Meta::CPAN

#!perl

use 5.010;
use strict;
use warnings;

use Test::More;
use Test2::Plugin::UTF8; # rids of the Wide Character in TAP message!
use FindBin;

use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

our $VERSION = '0.10';

use Config::JSON::Enhanced;

# these json are in the module's pod, section SYNOPSIS
# Testing it works and also to dump their output back in the pod
my $con = <<'EOJ';
     {
        /* 'a' is ... */

t/810-config2perl-pod.t  view on Meta::CPAN

	'variable-substitutions' => {
		'appdir' => $appdir,
		'expected-res123' => 42
	},
});
ok(defined $json, 'config2perl()'." : called and got defined result.") or BAIL_OUT;
is(ref($json), 'HASH', 'config2perl()'." : called and result is HASHref.");

# print the result in order to copy-paste it into the pod
# under L<Verbatim Sections>
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/; diag perl2dump($json);

ok(exists($json->{'long bash script'}), 'config2perl()'." : called and result contains required key.");
ok(defined($json->{'long bash script'}), 'config2perl()'." : called and result contains required key and it is defined.");
is(ref($json->{'long bash script'}), 'ARRAY', 'config2perl()'." : called and result contains required key and it is an ARRAY.");

my $x = $json->{'long bash script'};
is(scalar(@$x), 2, 'config2perl()'." : returned result contains key 'long bash script' and it is an ARRAY of 2 items.");
unlike($x->[1], qr/<%\s*appdir\s*%>/, 'config2perl()'." : template substitution (1) was correct.");

ok(exists($json->{'expected result'}), 'config2perl()'." : called and result contains required key.");



( run in 0.460 second using v1.01-cache-2.11-cpan-88abd93f124 )