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. ["co\u00f6perative"]).
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('["co\u00f6perative"]')->[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 "co\303\266perative"\0 [UTF8 "co\x{f6}perative"] CUR = 12 LEN = 14 COW_REFCNT = 0 SV = PV(0xd6cb20) at 0xd977f0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0xe0d120 "co\366perative"\0 CUR = 11 LEN = 14
File::Findhas a similar facility. Perhaps I'll write something.