Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: JSON, Data::Dumper and accented chars in utf-8 [OFF/Gripe]

by silentius (Scribe)
on Jan 21, 2012 at 22:42 UTC ( #949194=note: print w/ replies, xml ) Need Help??


in reply to Re^3: JSON, Data::Dumper and accented chars in utf-8 [OFF/Gripe]
in thread JSON, Data::Dumper and accented chars in utf-8

Thank you both for your replies, although they did not solve my problem.

I kept searching and solved it simply like this:

use Encode; use Encode::Escape; use Data::Dumper; use JSON; ... while ($line = <IN>) { $strut = from_json($line); print decode('unicode-escape', Dumper($strut)) . "\n"; }

This now gives me the output I need, which is the accented chars displayed as they are, since the output is to be redirected to a text file and read by humans on a regular text editor.

Thank you all once again.


Comment on Re^4: JSON, Data::Dumper and accented chars in utf-8 [OFF/Gripe]
Download Code
Re^5: JSON, Data::Dumper and accented chars in utf-8
by Ralesk (Pilgrim) on Jan 21, 2012 at 23:04 UTC

    And why isnít the JSON data enough? JSON has a pretty option, which would pretty-print the data for you, not requiring you to (ab)use Dumper.

    Keep in mind, that any JSON data generated by JSON is a byte stream and not a (Unicode) string.

    • To print it in a terminal you should decode it into a string and tell Perl your terminal is UTF-8, as suggested above. Sending byte streams to terminals is a bad thing, definitely donít do that.
    • To print it into a file,
      • you might be best off with a file that you opened as a binary (open my $FH, '>:raw', "myfilename.txt"). Any byte thrown at a raw file will appear there as intended.
      • Or of course, you can decode it into a string, print it into the file, Perl will know that itís a string and either figure out the byte stream format (aka. encoding) for the file, or you should be specifying one, eg. '>:encoding(UTF-8)'. Letting Perl do it for you is not necessarily a good idea :)

    Yes, fully aware it looks extremely complicated :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949194]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-09-18 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls