This simple code segment shows an issue I am having with JSON::XS encoding in Perl:
#!/usr/bin/perl use strict; use warnings; use JSON::XS; use utf8; binmode STDOUT, ":encoding(utf8)"; my (%data); $data{code} = "Gewürztraminer"; print "data{code} = " . $data{code} . "\n"; my $json_text = encode_json \%data; print $json_text . "\n"; The output this yields is:
johnnyb@boogie:~/Projects/repos > ./jsontest.pl data{code} = Gewürztraminer {"code":"Gewürztraminer"} Now if I comment out the binmode line above I get:
johnnyb@boogie:~/Projects/repos > ./jsontest.pl data{code} = Gew�rztraminer {"code":"Gewürztraminer"} What is happening here? Note that I am trying to fix this behavior in a perl CGI script in which binmode can not be used but I always get the "ü" characters as above returned in the JSON stream. How do I debug this? What am I missing?
print decode('UTF-8', $json_text, Encode::FB_CROAK) . "\n";