I am using some XS modules that are expecting latin1 string data (and ignoring perl’s UTF8 flag). In some cases, I am passing the result of JSON decoding, which should only include latin1 characters, but in some cases has them escaped (e.g. ["cou00f6perative"]
).
Is there a JSON decoding module that offers an option to return strings downgraded (at least where possible)? I’m not finding such an option in JSON, JSON::XS, or Cpanel::JSON::XS.
use strict;
use warnings;
use Cpanel::JSON::XS;
use Devel::Peek;
my $got = Cpanel::JSON::XS->new->decode('["cou00f6perative"]')->[0];
Dump $got;
my $wanted = $got;
utf8::downgrade($wanted);
Dump $wanted;
output:
SV = PV(0xd6cbf0) at 0xd8a460
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK,UTF8)
PV = 0xd83b40 "co303266perative" [UTF8 "cox{f6}perative"]
CUR = 12
LEN = 14
COW_REFCNT = 0
SV = PV(0xd6cb20) at 0xd977f0
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0xe0d120 "co366perative"
CUR = 11
LEN = 14
2
Answers
The safest approach is to fix the data structure after the fact.
This is existing code that can be simplified a little since it handles things not present in data structures created by JSON modules.
You could monkey-patch JSON::PP to produce the desired effect.
Where you want JSON::PP to produce a “downgraded structure”, add the following before the call to decode: