Data-Roundtrip

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

        handle input (Perl) data structures which may
        contain objects, with perl2json($data, {convert_blessed=>1}),
        provided the object implements a TO_JSON() method.
        Switched to Test::TempDir::Tiny in test files.
        And removed taint mode from test '12-from-file.t'

0.31    2025-12-03 01:00
        Experimentally adding one more dependency: Devel::StackTrace
        in order to get a stack trace when subs fail.
	Now json2* subs take optional parameters in the form of
	a hashref. At the moment it accepts 'boolean_values' parameter
	as an ARRAY ref, of 2 items, to be passed on to JSON (it allows the mapping
	of boolean values to the specified ARRAY instead of the idiotic object.

MANIFEST  view on Meta::CPAN

t/03-random-data-structure-UTF8.t
t/04-dump2-use-is-explicit.t
t/05-perl2dump-no-escape-unicode-temporarily.t
t/06-perl2dump-no-escape-unicode-permanently.t
t/07-perl2dump-escape-unicode-permanently.t
t/10-scripts.t
t/11-scripts-pod.t
t/12-from-file.t
t/13-yaml-tricky-cases.t
t/14-yaml-tainted-input.t
t/15-json2-boolean_values.t
t/20-synopsis.t
t/manifest.t
t/pod-coverage.t
t/pod.t
t/t-data/failing/test.json
t/t-data/failing/test.pl
t/t-data/failing/test.yaml
t/t-data/succeeding/test.json
t/t-data/succeeding/test.pl
t/t-data/succeeding/test.yaml

README  view on Meta::CPAN

        # }
    
        # this is how to reformat a JSON string to
        # have its unicode content escaped:
        my $json_with_unicode_escaped =
              json2json($jsonstr, {'escape-unicode'=>1});
    
        # sometimes we want JSON's true and false values
        # to be mapped to something other than JSON::PP::Boolean objects:
        my $json_with_custom_boolean_mapping = json2perl($jsonstr,
            {'boolean_values' => 'myfalse', 'mytrue'});
        my $json_with_custom_boolean_mapping = json2perl($jsonstr,
            {'boolean_values' => 0, 1});
    
        # With version 0.18 and up two more exported-on-demand
        # subs were added to read JSON or YAML directly from a file:
        # jsonfile2perl() and yamlfile2perl()
        my $perldata = jsonfile2perl("file.json");
        my $perldata = yamlfile2perl("file.yaml");
        die "failed" unless defined $perldata;
    
        # For some of the above functions there exist command-line scripts:
        perl2json.pl -i "perl-data-structure.pl" -o "output.json" --pretty

README  view on Meta::CPAN

 json2perl

        my $ret = json2perl($jsonstring, $optional_paramshashref)

    Arguments:

      * $jsonstring

      * $optional_paramshashref is an optional hashref as it is blindingly
      obvious from the name. At the moment only one parameter is
      understood: boolean_values. It must be an ARRAYREF of 0 or 2
      elements. If 0 elements, then it resets the boolean values mapping of
      the JSON converter to its default state (which is JSON::PP::Boolean
      objects. If it contains 2 elements, then the JSON converter will map
      a JSON false value to the first element of the ARRAYREF and a JSON
      true value to the second element of the ARRAYREF.

    Return value:

      * $ret

README.md  view on Meta::CPAN

    # }

    # this is how to reformat a JSON string to
    # have its unicode content escaped:
    my $json_with_unicode_escaped =
          json2json($jsonstr, {'escape-unicode'=>1});

    # sometimes we want JSON's true and false values
    # to be mapped to something other than JSON::PP::Boolean objects:
    my $json_with_custom_boolean_mapping = json2perl($jsonstr,
        {'boolean_values' => 'myfalse', 'mytrue'});
    my $json_with_custom_boolean_mapping = json2perl($jsonstr,
        {'boolean_values' => 0, 1});

    # With version 0.18 and up two more exported-on-demand
    # subs were added to read JSON or YAML directly from a file:
    # jsonfile2perl() and yamlfile2perl()
    my $perldata = jsonfile2perl("file.json");
    my $perldata = yamlfile2perl("file.yaml");
    die "failed" unless defined $perldata;

    # For some of the above functions there exist command-line scripts:
    perl2json.pl -i "perl-data-structure.pl" -o "output.json" --pretty

README.md  view on Meta::CPAN

Anything really.

## `json2perl`

    my $ret = json2perl($jsonstring, $optional_paramshashref)

Arguments:

- `$jsonstring`
- `$optional_paramshashref` is an optional hashref as it is blindingly obvious from
the name. At the moment only one parameter is understood: `boolean_values`.
It must be an ARRAYREF of 0 or 2 elements. If 0 elements, then it resets the boolean
values mapping of the JSON converter to its default state (which is [JSON::PP::Boolean](https://metacpan.org/pod/JSON%3A%3APP%3A%3ABoolean) objects.
If it contains 2 elements, then the JSON converter will map a JSON `false` value to
the first element of the ARRAYREF and a JSON `true` value to
the second element of the ARRAYREF.

Return value:

- `$ret`

a.html  view on Meta::CPAN

# }

# this is how to reformat a JSON string to
# have its unicode content escaped:
my $json_with_unicode_escaped =
      json2json($jsonstr, {'escape-unicode'=>1});

# sometimes we want JSON's true and false values
# to be mapped to something other than JSON::PP::Boolean objects:
my $json_with_custom_boolean_mapping = json2perl($jsonstr,
    {'boolean_values' => 'myfalse', 'mytrue'});
my $json_with_custom_boolean_mapping = json2perl($jsonstr,
    {'boolean_values' => 0, 1});

# With version 0.18 and up two more exported-on-demand
# subs were added to read JSON or YAML directly from a file:
# jsonfile2perl() and yamlfile2perl()
my $perldata = jsonfile2perl("file.json");
my $perldata = yamlfile2perl("file.yaml");
die "failed" unless defined $perldata;

# For some of the above functions there exist command-line scripts:
perl2json.pl -i "perl-data-structure.pl" -o "output.json" --pretty

a.html  view on Meta::CPAN


<pre><code>my $ret = json2perl($jsonstring, $optional_paramshashref)</code></pre>

<p>Arguments:</p>

<ul>

<li><p><code>$jsonstring</code></p>

</li>
<li><p><code>$optional_paramshashref</code> is an optional hashref as it is blindingly obvious from the name. At the moment only one parameter is understood: <code>boolean_values</code>. It must be an ARRAYREF of 0 or 2 elements. If 0 elements, then ...

</li>
</ul>

<p>Return value:</p>

<ul>

<li><p><code>$ret</code></p>

lib/Data/Roundtrip.pm  view on Meta::CPAN

	my $yaml_file = $_[0];
	#my $params = defined($_[1]) ? $_[1] : {};
	my $contents = read_from_file($yaml_file);
	if( ! defined $contents ){ print STDERR "yamlfile2perl() : error, failed to read from file '${yaml_file}'.\n"; return undef }
	my $pv = yaml2perl($contents);
	if( ! defined $pv ){ print STDERR "yamlfile2perl() : error, call to yaml2perl() has failed after reading yaml string from file '${yaml_file}'.\n"; return undef }
	return $pv;
}
# Optional input parameter following the required $json_string
# is a HASHREF:
#   'boolean_values' => [X, Y] : this is an ARRAYREF of 2 items json-false is mapped to X
#                                and json-true is mapped to Y, e.g. [0,1].
#                                JSON's default is JSON::PP::Boolean objects. Yikes
sub	json2perl {
	my $json_string = $_[0];
	my $params = defined($_[1]) ? $_[1] : {};
	my $pv;

	# NOTE: the JSON OO interface has encode() and decode()
	# whereas the function has encode_json() and decode_json()
	if( _has_utf8($json_string) ){
		if( exists($params->{'boolean_values'}) && defined($params->{'boolean_values'}) && ref($params->{'boolean_values'})eq'ARRAY' ){
			# in OO it needs decode()
			# boolean_values(x,y)
			$pv = eval {
			  JSON->new()
			      ->boolean_values(@{ $params->{'boolean_values'} })
			      ->utf8(0)->decode($json_string)
			};
		} else {
			# intercepting a die by wrapping in an eval
			$pv = eval { JSON::decode_json(Encode::encode_utf8($json_string)) };
		}
		if( ! defined($pv) ){ print STDERR Devel::StackTrace->new->as_string . "\njson2perl() :  error, call to json2perl() has failed".((defined($@)&&($@!~/^\s*$/))?" with this exception: $@:":".")."\n"; return undef }
	} else {
		if( exists($params->{'boolean_values'}) && defined($params->{'boolean_values'}) && ref($params->{'boolean_values'})eq'ARRAY' ){
			# boolean_values(x,y)
			$pv = eval {
			  JSON->new()
			      ->boolean_values(@{ $params->{'boolean_values'} })
			      ->decode($json_string)
			};
		} else {
			# intercepting a damn die by wrapping it around an eval
			$pv = eval { JSON::decode_json($json_string) };
		}
		if( ! defined($pv) ){ print STDERR Devel::StackTrace->new->as_string."\njson2perl() :  error, call to json2perl() has failed".((defined($@)&&($@!~/^\s*$/))?" with this exception: $@:":".")."\n"; return undef }
	}
	return $pv;
}

lib/Data/Roundtrip.pm  view on Meta::CPAN

    # }

    # this is how to reformat a JSON string to
    # have its unicode content escaped:
    my $json_with_unicode_escaped =
          json2json($jsonstr, {'escape-unicode'=>1});

    # sometimes we want JSON's true and false values
    # to be mapped to something other than JSON::PP::Boolean objects:
    my $json_with_custom_boolean_mapping = json2perl($jsonstr,
	{'boolean_values' => 'myfalse', 'mytrue'});
    my $json_with_custom_boolean_mapping = json2perl($jsonstr,
	{'boolean_values' => 0, 1});

    # With version 0.18 and up two more exported-on-demand
    # subs were added to read JSON or YAML directly from a file:
    # jsonfile2perl() and yamlfile2perl()
    my $perldata = jsonfile2perl("file.json");
    my $perldata = yamlfile2perl("file.yaml");
    die "failed" unless defined $perldata;

    # For some of the above functions there exist command-line scripts:
    perl2json.pl -i "perl-data-structure.pl" -o "output.json" --pretty

lib/Data/Roundtrip.pm  view on Meta::CPAN


    my $ret = json2perl($jsonstring, $optional_paramshashref)

Arguments:

=over 4

=item * C<$jsonstring>

=item * C<$optional_paramshashref> is an optional hashref as it is blindingly obvious from
the name. At the moment only one parameter is understood: C<boolean_values>.
It must be an ARRAYREF of 0 or 2 elements. If 0 elements, then it resets the boolean
values mapping of the JSON converter to its default state (which is L<JSON::PP::Boolean> objects.
If it contains 2 elements, then the JSON converter will map a JSON C<false> value to
the first element of the ARRAYREF and a JSON C<true> value to
the second element of the ARRAYREF.

=back

Return value:

t/15-json2-boolean_values.t  view on Meta::CPAN


my ($false, $true, $popo);

for my $jsonstr (
	'{"hello":1, "bt":true, "bf":false}',
	'{"γειά σας":1, "bt":true, "bf":false}',
){
	($false, $true) = (13, 12);
	$popo = Data::Roundtrip::json2perl(
		$jsonstr,
		{'boolean_values'=>[$false, $true]}
	);
	ok(defined $popo, 'json2perl()'." : called and got good result.") or BAIL_OUT;
	is($popo->{'bt'}, $true, 'json2perl()'." : result has true value mapped to $true.") or BAIL_OUT(perl2dump($popo)."no, see above results.");
	is($popo->{'bf'}, $false, 'json2perl()'." : result has false value mapped to $false.") or BAIL_OUT(perl2dump($popo)."no, see above results.");

	($false, $true) = ('abc', 'xyz');
	$popo = Data::Roundtrip::json2perl(
		$jsonstr,
		{'boolean_values'=>[$false, $true]}
	);
	ok(defined $popo, 'json2perl()'." : called and got good result.") or BAIL_OUT;
	is($popo->{'bt'}, $true, 'json2perl()'." : result has true value mapped to $true.") or BAIL_OUT(perl2dump($popo)."no, see above results.");
	is($popo->{'bf'}, $false, 'json2perl()'." : result has false value mapped to $false.") or BAIL_OUT(perl2dump($popo)."no, see above results.");

	# restore default boolean mapping
	$popo = Data::Roundtrip::json2perl(
		$jsonstr,
		{'boolean_values'=>[]}
	);
	ok(defined $popo, 'json2perl()'." : called and got good result.") or BAIL_OUT;
	is(ref($popo->{'bt'}), 'JSON::PP::Boolean', 'json2perl()'." : result has true value mapped to $true.") or BAIL_OUT(perl2dump($popo)."no, see above results.");
	is(ref($popo->{'bf'}), 'JSON::PP::Boolean', 'json2perl()'." : result has false value mapped to $false.") or BAIL_OUT(perl2dump($popo)."no, see above results.");
}
done_testing;



( run in 0.620 second using v1.01-cache-2.11-cpan-9ff20fc0ed8 )