Data-Roundtrip

 view release on metacpan or  search on metacpan

a.html  view on Meta::CPAN

# NOTE: long strings have been broken into multilines
# and/or truncated (replaced with ...)
#---
#Artist: Καζαντζίδης Στέλιος/Βίρβος Κώστας
#Songname: Απόκληρος της κοινωνίας

$yamlstr = json2yaml($jsonstr, {'escape-unicode'=>1});
print $yamlstr;
#---
#Artist: \u039a\u03b1\u03b6\u03b1 ...
#Songname: \u0391\u03c0\u03cc\u03ba ...

$backtojson = yaml2json($yamlstr);
# $backtojson is a string representation
# of following JSON structure:
# {"Artist":"Καζαντζίδης Στέλιος/Βίρβος Κώστας",
#  "Songname":"Απόκληρος της κοινωνίας"}

# This is useful when sending JSON via
# a POST request and it needs unicode escaped:
$backtojson = yaml2json($yamlstr, {'escape-unicode'=>1});
# $backtojson is a string representation
# of following JSON structure:
# but this time with unicode escaped
# (pod content truncated for readbility)
# {"Artist":"\u039a\u03b1\u03b6 ...",
#  "Songname":"\u0391\u03c0\u03cc ..."}
# this is the usual Data::Dumper dump:
print json2dump($jsonstr);
#$VAR1 = {
#  'Songname' => "\x{391}\x{3c0}\x{3cc} ...",
#  'Artist' => "\x{39a}\x{3b1}\x{3b6} ...",
#};

# and this is a more human-readable version:
print json2dump($jsonstr, {'dont-bloody-escape-unicode'=>1});
# $VAR1 = {
#   "Artist" => "Καζαντζίδης Στέλιος/Βίρβος Κώστας",
#   "Songname" => "Απόκληρος της κοινωνίας"
# };

# pass some parameters to Data::Dumper
# like: be terse (no $VAR1):
print json2dump($jsonstr,
  {'dont-bloody-escape-unicode'=>0, 'terse'=>1}
 #{'dont-bloody-escape-unicode'=>0, 'terse'=>1, 'indent'=>0}
);
# {
#  "Artist" => "Καζαντζίδης Στέλιος/Βίρβος Κώστας",
#  "Songname" => "Απόκληρος της κοινωνίας"
# }

# 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
json2json.pl -i "with-unicode.json" -o "unicode-escaped.json" --escape-unicode
# etc.

# only for *2dump: perl2dump, json2dump, yaml2dump
# and if no escape-unicode is required (i.e.
# setting 'dont-bloody-escape-unicode' => 1 permanently)
# and if efficiency is important,
# meaning that perl2dump is run in a loop thousand of times,
# then import the module like this:
use Data::Roundtrip qw/:all no-unicode-escape-permanently/;
# or like this
use Data::Roundtrip qw/:all unicode-escape-permanently/;

# then perl2dump() is more efficient but unicode characters
# will be permanently not-escaped (1st case) or escaped (2nd case).</code></pre>

<h1 id="EXPORT">EXPORT</h1>

<p>By default no symbols are exported. However, the following export tags are available (:all will export all of them):</p>

<ul>

<li><p><code>:json</code> : <code>perl2json()</code>, <code>json2perl()</code>, <code>json2dump()</code>, <code>json2yaml()</code>, <code>json2json()</code>, <code>jsonfile2perl()</code></p>

</li>
<li><p><code>:yaml</code> : <code>perl2yaml()</code>, <code>yaml2perl()</code>, <code>yaml2dump()</code>, <code>yaml2yaml()</code>, <code>yaml2json()</code>, <code>yamlfile2perl()</code></p>

</li>
<li><p><code>:dump</code> : <code>perl2dump()</code>, <code>perl2dump_filtered()</code>, <code>perl2dump_homebrew()</code></p>

</li>
<li><p><code>:io</code> : <code>read_from_file()</code>, <code>write_to_file()</code>, <code>read_from_filehandle()</code>, <code>write_to_filehandle()</code>,</p>

</li>
<li><p><code>:all</code> : everything above.</p>

</li>
<li><p>Additionally, these four subs: <code>dump2perl()</code>, <code>dump2json()</code>, <code>dump2yaml()</code>, <code>dump2dump()</code> do not belong to any export tag. However they can be imported explicitly by the caller in the usual way (e.g....

</li>
<li><p><code>no-unicode-escape-permanently</code> : this is not an export keyword/parameter but a parameter which affects all the <code>*2dump*</code> subs by setting unicode escaping permanently to false. See <a href="#EFFICIENCY">&quot;EFFICIENCY&q...

</li>
<li><p><code>unicode-escape-permanently</code> : this is not an export keyword/parameter but a parameter which affects all the <code>*2dump*</code> subs by setting unicode escaping permanently to true. See <a href="#EFFICIENCY">&quot;EFFICIENCY&quot;...

</li>
</ul>

<h1 id="EFFICIENCY">EFFICIENCY</h1>

a.html  view on Meta::CPAN

<dt id="indentation-is-very-basic">1. indentation is very basic,</dt>
<dd>

</dd>
<dt id="it-supports-only-scalars-hashes-and-arrays-which-will-dive-into-them-no-problem-This-sub-can-be-used-in-conjuction-with-DataDumpFilterino-to-create-a-Data::Dump-filter-like">2. it supports only scalars, hashes and arrays, (which will dive int...
<dd>

<pre><code>     Data::Dump::Filtered::add_dump_filter( \&amp; DataDumpFilterino );
or
     dumpf($perl_var, \&amp; DataDumpFilterino);</code></pre>

<p>the input is a Perl variable as a reference, so no <code>%inp</code> but <code>$inp={}</code> and <code>$inp=[]</code>.</p>

<p>This function is recursive. Beware of extremely deep nested data structures. Deep not long! But it probably is as efficient as it can be but definetely lacks in aesthetics and functionality compared to Dump and Dumper.</p>

<p>The output is a, possibly multiline, string. Which it can then be fed back to <a href="#dump2perl">&quot;dump2perl&quot;</a>.</p>

</dd>
</dl>

<h2 id="dump2perl"><code>dump2perl</code></h2>

<pre><code># CAVEAT: it will eval($dumpstring) internally, so
#         check $dumpstring for malicious code beforehand
#         it is a security risk if you don&#39;t.
#         Don&#39;t use it if $dumpstring comes from
#         untrusted sources (user input for example).
my $ret = dump2perl($dumpstring)</code></pre>

<p>Arguments:</p>

<ul>

<li><p><code>$dumpstring</code>, this comes from the output of <a>Data::Dump</a>, <a>Data::Dumper</a> or our own <a href="#perl2dump">&quot;perl2dump&quot;</a>, <a href="#perl2dump_filtered">&quot;perl2dump_filtered&quot;</a>, <a href="#perl2dump_hom...

</li>
</ul>

<p>Return value:</p>

<ul>

<li><p><code>$ret</code>, the Perl data structure on success or <code>undef</code> on failure.</p>

</li>
</ul>

<p>CAVEAT: it <b>eval()</b>&#39;s the input <code>$dumpstring</code> in order to create the Perl data structure. <b>eval()</b>&#39;ing unknown or unchecked input is a security risk. Always check input to <b>eval()</b> which comes from untrusted sourc...

<h2 id="json2perl1"><code>json2perl</code></h2>

<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>

</li>
</ul>

<p>Given an input <code>$jsonstring</code> as a string, it will return the equivalent Perl data structure using <code>JSON::decode_json(Encode::encode_utf8($jsonstring))</code>.</p>

<p>It returns the Perl data structure on success or <code>undef</code> on failure.</p>

<h2 id="jsonfile2perl"><code>jsonfile2perl</code></h2>

<pre><code>my $ret = jsonfile2perl($filename)</code></pre>

<p>Arguments:</p>

<ul>

<li><p><code>$filename</code></p>

</li>
</ul>

<p>Return value:</p>

<ul>

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

</li>
</ul>

<p>Given an input <code>$filename</code> which points to a file containing JSON content, it will return the equivalent Perl data structure.</p>

<p>It returns the Perl data structure on success or <code>undef</code> on failure.</p>

<h2 id="json2yaml"><code>json2yaml</code></h2>

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

<p>Arguments:</p>

<ul>

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

</li>
<li><p><code>$optional_paramshashref</code></p>

</li>
</ul>

<p>Return value:</p>



( run in 0.960 second using v1.01-cache-2.11-cpan-39bf76dae61 )