Last active
December 27, 2015 04:18
-
-
Save digitalbot/7265507 to your computer and use it in GitHub Desktop.
about using JSON and JSON::Syck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature qw/say/; | |
use Encode qw//; | |
use Encode::JavaScript::UCS; | |
use JSON qw//; | |
use JSON::Syck qw//; | |
use Test::More; | |
sub _json { | |
my @data = @_; | |
my $json = JSON->new->ascii->utf8(0)->encode(\@data); | |
$json =~ s/([<>\/\+])/sprintf("\\u%04x",ord($1))/eg; | |
return $json; | |
} | |
sub _syck { | |
my @data = @_; | |
my $json = Encode::encode('JavaScript-UCS', JSON::Syck::Dump(\@data)); | |
$json =~ s/([<>\/\+])/sprintf("\\u%04x",ord($1))/eg; | |
return $json; | |
} | |
my @members = ({ | |
age => 41, | |
name => '[倉持]', | |
}, { | |
age => 42, | |
name => '"浅沼"/\/', | |
}, { | |
age => 49, | |
name => '"\"<恩田>', | |
}, { | |
age => 49, | |
name => ';{五十嵐};', | |
}); | |
ok(_json(@members) eq _syck(@members)); | |
done_testing; | |
# say _json(@members); | |
##=> [{"age":41,"name":"[\u00e5\u0080\u0089\u00e6\u008c\u0081]"},{"name":"\"\u00e6\u00b5\u0085\u00e6\u00b2\u00bc\"\u002f\\\u002f","age":42},{"name":"\"\\\"\u003c\u00e6\u0081\u00a9\u00e7\u0094\u00b0\u003e","age":49},{"age":49,"name":";{\u00e4\u00ba\u0094\u00e5\u008d\u0081\u00e5\u00b5\u0090};"}] | |
# say _syck(@members); | |
##=> [{"age":41,"name":"[\u00e5\u0080\u0089\u00e6\u008c\u0081]"},{"name":"\"\u00e6\u00b5\u0085\u00e6\u00b2\u00bc\"\u002f\\\u002f","age":42},{"name":"\"\\\"\u003c\u00e6\u0081\u00a9\u00e7\u0094\u00b0\u003e","age":49},{"age":49,"name":";{\u00e4\u00ba\u0094\u00e5\u008d\u0081\u00e5\u00b5\u0090};"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment