use JSON -support_by_pp;
say JSON->new->escape_slash()->encode({foo => 'b/ar'}) eq '{"foo":"b\\/ar"}' ? 'klappt' : 'fail';
1
2
3
4
5
6
> perl -E "
require JSON;
JSON->import('-support_by_pp');
say JSON->new->escape_slash()->encode( { foo => qq~b/ar~}) eq qq~{\"foo\":\"b\\/ar\"}~ ? 'klappt' : 'nicht';
"
klappt
1 2 3 4 5 6 7 8
#!/usr/bin/perl use strict; use warnings; use 5.010; require JSON; JSON->import('-support_by_pp'); my %test = (1 => 'foo',2 => 'bar'); say "Ergebnis: ".JSON->new->utf8->sort_by(sub {$JSON::PP::a <=> $JSON::PP::b})->escape_slash()->encode(\%test);
perldoc JSONCode: (dl )1
2
3
4
5
6
7
8
9
10
11
12
13
14
15$JSON::KeySort
$json->canonical->encode($perl_scalar)
This is the ascii sort.
If you want to use with your own sort routine, check the sort_by method.
(Only with JSON::PP, even if -support_by_pp is used currently.)
$json->sort_by($sort_routine_ref)->encode($perl_scalar)
$json->sort_by(sub { $JSON::PP::a <=> $JSON::PP::b })->encode($perl_scalar)
Can't access $a and $b but $JSON::PP::a and $JSON::PP::b.
1 2 3
require JSON::PP; JSON->import('-support_by_pp'); my %test = (1 => 'foo',2 => 'bar'); say "Ergebnis: ".JSON::PP->new->utf8->sort_by(sub {$JSON::PP::a <=> $JSON::PP::b})->escape_slash()->encode(\%test);