Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Convert JSON to Perl and back with unicode

by Corion (Pope)
on Apr 09, 2020 at 10:02 UTC ( #11115271=note: print w/replies, xml ) Need Help??


in reply to Convert JSON to Perl and back with unicode

After some source diving into the (Perl) source of the pure Perl implementation of Data::Dumper, and some confusion, because usually Perl uses the XS version of Data::Dumper, which behaves differently in this case, I found the following approach works. It monkey-patches Data::Dumper (instead of inheriting from it, because Data::Dumper isn't written that way):

#!perl use strict; use warnings; use utf8; use charnames ':full'; # just so I can use ASCII to represent Greek le +tters use Data::Dumper; $Data::Dumper::Useperl = 1; $Data::Dumper::Useqq='utf8'; sub Data::Dumper::qquote { local($_) = shift; s/([\\\"\@\$])/\\$1/g; return qq("$_") unless /[[:^print:]]/; # fast exit if only printabl +es # Here, there is at least one non-printable to output. First, trans +late the # escapes. s/([\a\b\t\n\f\r\e])/$Data::Dumper::esc{$1}/g; # no need for 3 digits in escape for octals not followed by a digit. s/($Data::Dumper::low_controls)(?!\d)/'\\'.sprintf('%o',ord($1))/eg; # But otherwise use 3 digits s/($Data::Dumper::low_controls)/'\\'.sprintf('%03o',ord($1))/eg; # all but last branch below not supported --BEHAVIOR SUBJECT TO CH +ANGE-- my $high = shift || ""; if ($high eq "iso8859") { # Doesn't escape the Latin1 printables if ($Data::Dumper::IS_ASCII) { s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg; } elsif ($] ge 5.007_003) { my $high_control = utf8::unicode_to_native(0x9F); s/$high_control/sprintf('\\%o',ord($1))/eg; } } elsif ($high eq "utf8") { # Some discussion of what to do here is in # https://rt.perl.org/Ticket/Display.html?id=113088 # use utf8; # $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; } elsif ($high eq "8bit") { # leave it as it is } else { s/([[:^ascii:]])/'\\'.sprintf('%03o',ord($1))/eg; #s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; } return qq("$_"); }; my $pv = { "\N{GREEK SMALL LETTER ALPHA}\N{GREEK SMALL LETTER BETA}" => "\N{G +REEK CAPITAL LETTER GAMMA}\N{GREEK CAPITAL LETTER DELTA}" }; print Dumper $pv;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-07-06 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?