JSON-Create
view release on metacpan or search on metacpan
lib/JSON/Create.pod view on Meta::CPAN
(This example is included as L<F<examples/cmp.pl>|https://fastapi.metacpan.org/source/BKB/JSON-Create-0.36/examples/cmp.pl> in the distribution.)
This method was added in version 0.29 of the module.
=head2 downgrade_utf8
$jc->downgrade_utf8 (1);
If this is set to a true value, the return value of L</create_json> or
L</create> is never upgraded to character strings, or C<utf8>. This
overrides the default behaviour, which is to upgrade the output to
C<utf8> if any part of the input is C<utf8>, or if the user has
requested replacement with L</replace_bad_utf8> and there are bad
characters in the user's input. See L</UNICODE HANDLING> for
details. All output of JSON::Create is valid UTF-8, regardless of what
this flag is set to. See L</Output is valid UTF-8>.
This method was added in version 0.18 of the module.
=head2 escape_slash
$jc->escape_slash (1);
Call this with a true value to make the slash (known as the "solidus"
in the JSON specification) be escaped with a backslash, so C</> gets
turned into C<\/>. Call this with any false value to make the slash
not be escaped (the default behaviour).
=head3 Escaping slashes
use JSON::Create;
my $jc = JSON::Create->new ();
my $in = {'/dog/' => '/run/'};
print $jc->run ($in), "\n";
$jc->escape_slash (1);
print $jc->run ($in), "\n";
$jc->escape_slash (0);
print $jc->run ($in), "\n";
produces output
{"/dog/":"/run/"}
{"\/dog\/":"\/run\/"}
{"/dog/":"/run/"}
(This example is included as L<F<examples/escape-slash.pl>|https://fastapi.metacpan.org/source/BKB/JSON-Create-0.36/examples/escape-slash.pl> in the distribution.)
See also L</Other escapes>.
This method was added in version 0.07 of the module.
=head2 indent
$jc->indent (1);
Add whitespace indentation to the output. The formula applied is to
add a newline plus indentation after each opening bracket, add the
same after each comma, and add the same before each closing
bracket. Tabs are used for all indentation. The number of tabs is
decided by the number of brackets open.
=head3 Example of indentation
use JSON::Create;
my %thing = ("it's your thing" => [qw! do what you !, {wanna => 'do'}],
"I can't tell you" => [{who => 2}, qw! sock it!, 2]);
my $jc = JSON::Create->new ();
$jc->indent (1);
print $jc->run (\%thing);
produces output
{
"it's your thing":[
"do",
"what",
"you",
{
"wanna":"do"
}
],
"I can't tell you":[
{
"who":2
},
"sock",
"it",
2
]
}
(This example is included as L<F<examples/indent.pl>|https://fastapi.metacpan.org/source/BKB/JSON-Create-0.36/examples/indent.pl> in the distribution.)
=head3 Reformatting the indentation
Users who prefer a different style of indentation should easily be
able to modify this output to their needs using simple substitutions,
for example C<s/^(\t+)/ " " x length ($1) /gesm;> will convert from tabs
to two space indentation.
use JSON::Create;
my %thing = ("it's your thing" => [qw! do what you !, {wanna => 'do'}],
"I can't tell you" => [{who => 2}, qw! sock it!, 2]);
my $jc = JSON::Create->new ();
$jc->indent (1);
my $out = $jc->run (\%thing);
$out =~ s/^(\t+)/ " " x length ($1) /gesm;
print $out;
produces output
( run in 1.354 second using v1.01-cache-2.11-cpan-e1769b4cff6 )