Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Convert JSON to Perl and back with unicode

by choroba (Archbishop)
on Apr 11, 2020 at 00:00 UTC ( #11115353=note: print w/replies, xml ) Need Help??


in reply to Re: Convert JSON to Perl and back with unicode
in thread Convert JSON to Perl and back with unicode

Nice and easy! But, it's prone to Unicode injection:
#! /usr/bin/perl use warnings; use strict; use Data::Dumper; my %mydatatoshow = ( "\N{GREEK SMALL LETTER ALPHA}" => "\\\N{GREEK SMALL LETTER BETA}", '\\x{3b1}' => '\\\\x{3b2}', ); binmode STDERR, ':encoding(UTF-8)'; print STDERR Dumper(\%mydatatoshow); print STDERR Dumper(\%mydatatoshow) =~ s/\\x\{([0-9a-f]{2,})\}/chr(hex +($1))/ger;

Update: Possibly fixable by

s/((\\+)x\{([0-9a-f]{2,})\})/ (length($2) % 2) ? substr($2, 1) . chr hex $3 : $1/ger;

Verified by

#! /usr/bin/perl use warnings; use strict; use Data::Dumper; my %datatoshow = ( "\N{GREEK SMALL LETTER ALPHA}" => "\\\N{GREEK SMALL LETTER BETA}", '\\x{3b1}' => '\\\\x{3b2}', ); binmode STDERR, ':encoding(UTF-8)'; print STDERR Dumper(\%datatoshow); print STDERR Dumper(\%datatoshow) =~ s/((\\+)x\{([0-9a-f]{2,})\})/ (length($2) % 2) ? substr($2, 1) . chr hex $3 : $1/ger; use Test::More tests => 3; my $dumper_orig = Dumper(\%datatoshow); my $dumper_leszekdubiel = $dumper_orig =~ s/\\x\{([0-9a-f]{2,})\}/chr(hex($1))/ger; my $dumper_choroba = $dumper_orig =~ s/((\\+)x\{([0-9a-f]{2,})\})/ (length($2) % 2) ? substr($2, 1) . chr hex $3 +: $1/ger; my $VAR1; eval $dumper_orig; is_deeply $VAR1, \%datatoshow; eval $dumper_leszekdubiel; is_deeply $VAR1, \%datatoshow; eval $dumper_choroba; is_deeply $VAR1, \%datatoshow;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11115353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-07-06 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?