end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

JSON.pm for perl には、日本語文字列のエスケープ?により複数の方法があります

http://search.cpan.org/perldoc?JSON
今のところ、JSON::to_json() / from_json() 派です

#!/usr/local/bin/perl
use strict;
use utf8;
use Encode;
use JSON;
use Data::Dumper;

main();

sub main {
    my $org_data =
        {hash_str_ascii =>      'ABCDE12345',
         hash_str_not_ascii =>  'あいうえお12345',
         hash_str_mix =>        'ABCDEあいうえお',
         array_int =>           [qw/01 2 3 4 5 6/]
        };

    to_from_json($org_data);            #### to_json / from_json       ####
    encode_decode_json($org_data);      #### encode_json / decode_json ####
    encode_decode_json_oo($org_data);   #### OO encode / decode        ####
}

sub to_from_json {
    my ($org_data) = @_;

    my $json_str = JSON::to_json($org_data);
    print "#### to_json / from_json ####\n";
    print Dumper($org_data);
    print encode('utf8',$json_str),"\n";
    print Dumper(JSON::from_json($json_str));
}

sub encode_decode_json {
    my ($org_data) = @_;

    my $json_str = JSON::encode_json($org_data);
    print "#### encode_json / decode_json ####\n";
    print Dumper($org_data);
    print encode('utf8',$json_str),"\n";
    print Dumper(JSON::decode_json($json_str));
}

sub encode_decode_json_oo {
    my ($org_data) = @_;

    # NOT generate characters outside the code range 0..127.
    # Any Unicode characters outside that range will be escaped
    # using either a single \uXXXX or a double \uHHHH\uLLLLL escape sequence,
    # as per RFC4627.
    my $json = JSON->new()->ascii(1);
    my $json_str = $json->encode($org_data);
    print "#### OO encode / decode ####\n";
    print Dumper($org_data);
    print encode('utf8',$json_str),"\n";
    print Dumper($json->decode($json_str));
}

↓RESULT

$ ./foo.pl 
#### to_json / from_json ####
$VAR1 = {
          'hash_str_mix' => "ABCDE\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}",
          'hash_str_not_ascii' => "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}\x{ff11}\x{ff12}\x{ff13}\x{ff14}\x{ff15}",
          'array_int' => [
                           '01',
                           '2',
                           '3',
                           '4',
                           '5',
                           '6'
                         ],
          'hash_str_ascii' => 'ABCDE12345'
        };
{"hash_str_mix":"ABCDEあいうえお","hash_str_not_ascii":"あいうえお12345","array_int":["01","2","3","4","5","6"],"hash_str_ascii":"ABCDE12345"}
$VAR1 = {
          'hash_str_ascii' => 'ABCDE12345',
          'array_int' => [
                         '01',
                         '2',
                         '3',
                         '4',
                         '5',
                         '6'
                       ],
          'hash_str_mix' => "ABCDE\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}",
          'hash_str_not_ascii' => "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}\x{ff11}\x{ff12}\x{ff13}\x{ff14}\x{ff15}"
        };
#### encode_json / decode_json ####
$VAR1 = {
          'hash_str_mix' => "ABCDE\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}",
          'hash_str_not_ascii' => "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}\x{ff11}\x{ff12}\x{ff13}\x{ff14}\x{ff15}",
          'array_int' => [
                           '01',
                           '2',
                           '3',
                           '4',
                           '5',
                           '6'
                         ],
          'hash_str_ascii' => 'ABCDE12345'
        };
{"hash_str_mix":"ABCDEあいうえお","hash_str_not_ascii":"あいうえお12345","array_int":["01","2","3","4","5","6"],"hash_str_ascii":"ABCDE12345"}
$VAR1 = {
          'hash_str_mix' => "ABCDE\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}",
          'hash_str_not_ascii' => "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}\x{ff11}\x{ff12}\x{ff13}\x{ff14}\x{ff15}",
          'array_int' => [
                           '01',
                           '2',
                           '3',
                           '4',
                           '5',
                           '6'
                         ],
          'hash_str_ascii' => 'ABCDE12345'
        };
#### OO encode / decode ####
$VAR1 = {
          'hash_str_mix' => "ABCDE\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}",
          'hash_str_not_ascii' => "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}\x{ff11}\x{ff12}\x{ff13}\x{ff14}\x{ff15}",
          'array_int' => [
                           '01',
                           '2',
                           '3',
                           '4',
                           '5',
                           '6'
                         ],
          'hash_str_ascii' => 'ABCDE12345'
        };
{"hash_str_mix":"ABCDE\u3042\u3044\u3046\u3048\u304a","hash_str_not_ascii":"\u3042\u3044\u3046\u3048\u304a\uff11\uff12\uff13\uff14\uff15","array_int":["01","2","3","4","5","6"],"hash_str_ascii":"ABCDE12345"}
$VAR1 = {
          'hash_str_mix' => "ABCDE\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}",
          'hash_str_not_ascii' => "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}\x{ff11}\x{ff12}\x{ff13}\x{ff14}\x{ff15}",
          'array_int' => [
                           '01',
                           '2',
                           '3',
                           '4',
                           '5',
                           '6'
                         ],
          'hash_str_ascii' => 'ABCDE12345'
        };
$