简体   繁体   中英

How do I encode an object/hash containing strings into latin1 using JSON 1.x in Perl?

I have a CMS and a server which uses the old perl JSON module (version 1.54). I need to encode a hash/object with strings into latin-1 (the target page is 8859-1 encoded).

How can this be done?

My code so far:

use JSON;

my $json = JSON->new();
$json->to_json($str),

The JSON 2.x module has the $json->ascii and $json->latin1 methods, which tell the module to replace characters outside the ASCII and ISO-8859-1 character sets, respectively, with the equivalent \\uNNNN escape sequences.

Unfortunately, the 1.x versions of the JSON module apparently (thanks, Quentin!) don't have these methods. However, given that non-ASCII characters should not occur anywhere outside strings in valid JSON, it should be possible to escape them after the encoding step, like this:

use JSON;
my $output = to_json( $data );

# encode non-ASCII characters as \uNNNN escapes:
$output =~ s/([^\x20-\x7e])/sprintf "\\u%04x", ord $1/eg;

Note that the replacement above won't work for characters outside the Basic Multilingual Plane (ie with code points above U+FFFF). To handle those, we need to use surrogate pairs , which are probably best handled in a separate pass:

# encode non-BMP characters as surrogate pairs:
$output =~ s{([^\0-\x{FFFF}])}{
    my $c = ord($1) - 0x10000;
    pack "W2", ($c >> 10) + 0xD800, ($c & 1023) + 0xDC00;
}eg;

# encode non-ASCII characters as \uNNNN escapes:
$output =~ s/([^\x20-\x7e])/sprintf "\\u%04x", ord $1/eg;

I think you mean to either encode a perl data structure into JSON, or decode a string (which is in the JSON format) to a perl data structure.

But just in terms of encoding a string to latin-1, have you tried Encode ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM