in reply to Re^4: Print unicode strings to pdf
in thread Print unicode strings to pdf

What do you mean by "view the JSON string"? Do you mean you print it? Do you print it using Data::Dumper?

If you want to turn the JSON back into a proper data structure, I suggest you use JSON or JSON::XS to do that. You might need to unescape the string before doing that if it contains doubled backslashes, so that it is proper JSON:

use JSON 'decode_json'; use Data::Dumper; my $mangled_json = '{ "hex_code":"\\x{a5}" }'; print $mangled_json; my $json = $mangled_json; $json =~ s!\\\\!\\!g; print $json; my $structure = decode_json( $json ); $Data::Dumper::Useqq = 1; print Dumper $structure; binmode STDOUT, ':encoding(UTF-8)'; # well, hopefully, your terminal u +nderstands UTF-8 print $structure->{'hex_code'};

Replies are listed 'Best First'.
Re^6: Print unicode strings to pdf
by Anonymous Monk on Mar 27, 2018 at 08:42 UTC
    Thanks Corion.

    Sorry, the view part is using the inbuilt "alert" function of Javascript. This is what I see of the json string from perl passed to Javascript using "alert":
    { "hex_code":"\\xa5" }
    When I display this in an html input field, it displays:
    \xa5
    After much googling with the right keywords, I found a solution by passing the value "\\xa5" (which originates from perl) to the function below:
    //https://stackoverflow.com/questions/4209104/decoding-hex-string-in-j +avascript String.prototype.decodeEscapeSequence = function() { return this.replace(/\\x([0-9A-Fa-f]{4})/g, function() { return String.fromCharCode(parseInt(arguments[1], 16)); }); };
    I think it's somewhat similar to your perl code below:
    sub unescape { my( $str ) = @_; $str =~ s!\\x\{([0-9a-f]{4})\}!chr(hex $1)!ge; $str };
    I am confused because when I have a variable set to a hex value in Javascript like this:
    var hex_code = '\xa5';
    That gets displayed correctly in the input field as a symbol.

    So I thought since "\\xa5" is what I see with Javascript's alert, if I remove the first backslash, it would work. It doesn't whether I remove the first backslash or not. I needed to unescape with the Javascript function found in stackoverflow.

      Maybe a better approach would be to look at what data Perl is sending instead of using Javascript and your browser as an intermediary?

      Also note that with JSON, you still have to parse it in Javascript as well, but I think JSON uses \U... and not \x... escapes, so you will need to think about how you generate your JSON and use a proper way.