you need to use the object interface
use v5.12; use warnings; use Devel::Peek; use Data::Dump qw/pp/; use JSON::XS; use utf8; my $str = "\x{feff}" . '["whät","över"]'; # Internal Unicode b/c of use +utf8 Dump($str); # hence shows UTF8 flag $str =~ s/^\x{feff}//; # strip BOM by Unicode code-po +int Dump($str); # shows UTF8 flag my $JSON = JSON::XS->new; # coder for all unicode in/out my $data = $JSON->decode($str); warn pp '$data: ', $data; # ["wh\xE4t", "\xF6ver"] # NB: \xE4, \xF6 correct codep +oints for umlauts # even if each character neede +d 2 bytes my $str2 = $JSON->encode($data); # roundtrip Dump($str2); # shows UTF8 flag warn '$str eq $str2: ', $str eq $str2; # same
SV = PV(0x6bcea8) at 0x26485f0 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x28229e8 "\357\273\277[\"wh\303\244t\",\"\303\266ver\"]"\0 [UT +F8 "\x{feff}["wh\x{e4}t","\x{f6}ver"]"] CUR = 20 LEN = 22 SV = PV(0x6bcea8) at 0x26485f0 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x28227e8 "[\"wh\303\244t\",\"\303\266ver\"]"\0 [UTF8 "["wh\x{e +4}t","\x{f6}ver"]"] CUR = 17 LEN = 24 ("\$data: ", ["wh\xE4t", "\xF6ver"]) at d:/perl/pm/t_devel_peek.pl lin +e 21. SV = PV(0x6bd158) at 0x27482a0 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x281d9c8 "[\"wh\303\244t\",\"\303\266ver\"]"\0 [UTF8 "["wh\x{e +4}t","\x{f6}ver"]"] CUR = 17 LEN = 66 $str eq $str2: 1 at d:/perl/pm/t_devel_peek.pl line 28.
Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery
°) or fortunately? depends on the perspective
In reply to Re^11: Rogue character(s) at start of JSON file (BOM; dumping references)
by LanX
in thread Rogue character(s) at start of JSON file
by Bod
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |