r55499 - in /branches/upstream/libjson-perl/current: Changes MANIFEST META.yml README eg/bench_decode.pl eg/bench_encode.pl eg/bench_pp_xs.pl lib/JSON.pm
angelabad-guest at users.alioth.debian.org
angelabad-guest at users.alioth.debian.org
Mon Apr 5 12:45:51 UTC 2010
Author: angelabad-guest
Date: Mon Apr 5 12:44:59 2010
New Revision: 55499
URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=55499
Log:
[svn-upgrade] Integrating new upstream version, libjson-perl (2.21)
Added:
branches/upstream/libjson-perl/current/eg/bench_decode.pl
branches/upstream/libjson-perl/current/eg/bench_encode.pl
Removed:
branches/upstream/libjson-perl/current/eg/bench_pp_xs.pl
Modified:
branches/upstream/libjson-perl/current/Changes
branches/upstream/libjson-perl/current/MANIFEST
branches/upstream/libjson-perl/current/META.yml
branches/upstream/libjson-perl/current/README
branches/upstream/libjson-perl/current/lib/JSON.pm
Modified: branches/upstream/libjson-perl/current/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/Changes?rev=55499&op=diff
==============================================================================
--- branches/upstream/libjson-perl/current/Changes (original)
+++ branches/upstream/libjson-perl/current/Changes Mon Apr 5 12:44:59 2010
@@ -15,6 +15,12 @@
!! Since 2.16, PP's relaxed option caused an infinite loop in some condition.
!! Recommend to update old versions.
+
+2.21 Mon Apr 5 14:56:52 2010
+ [JSON]
+ - enhanced 'HOW DO I DECODE A DATA FROM OUTER AND ENCODE TO OUTER'
+ - renamed eg/bench_pp_xs.pl to eg/bench_decode.pl
+ - added eg/bench_encode.pl
2.20 Fri Apr 2 12:50:08 2010
[JSON]
Modified: branches/upstream/libjson-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/MANIFEST?rev=55499&op=diff
==============================================================================
--- branches/upstream/libjson-perl/current/MANIFEST (original)
+++ branches/upstream/libjson-perl/current/MANIFEST Mon Apr 5 12:44:59 2010
@@ -1,5 +1,6 @@
Changes
-eg/bench_pp_xs.pl
+eg/bench_decode.pl
+eg/bench_encode.pl
lib/JSON.pm
lib/JSON/PP.pm
lib/JSON/PP/Boolean.pm
Modified: branches/upstream/libjson-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/META.yml?rev=55499&op=diff
==============================================================================
--- branches/upstream/libjson-perl/current/META.yml (original)
+++ branches/upstream/libjson-perl/current/META.yml Mon Apr 5 12:44:59 2010
@@ -1,6 +1,6 @@
--- #YAML:1.0
name: JSON
-version: 2.20
+version: 2.21
abstract: JSON (JavaScript Object Notation) encoder/decoder
author:
- Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
Modified: branches/upstream/libjson-perl/current/README
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/README?rev=55499&op=diff
==============================================================================
--- branches/upstream/libjson-perl/current/README (original)
+++ branches/upstream/libjson-perl/current/README Mon Apr 5 12:44:59 2010
@@ -1,4 +1,4 @@
-JSON version 2.20
+JSON version 2.21
=================
INSTALLATION
@@ -45,7 +45,7 @@
# recommend to use (en|de)code_json.
VERSION
- 2.20
+ 2.21
This version is compatible with JSON::XS 2.27 and later.
@@ -240,8 +240,8 @@
If you know a JSON text from an outer world - a network, a file content,
and so on, is encoded in UTF-8, you should use "decode_json" or "JSON"
- module object with "utf8" enable. And the decoded data contains UNICODE
- characters.
+ module object with "utf8" enable. And the decoded result will contain
+ UNICODE characters.
# from network
my $json = JSON->new->utf8;
@@ -254,50 +254,67 @@
$json_text = <$fh>;
$perl_scalar = decode_json( $json_text );
- If your data is not encoded in UTF-8, firstly you should "decode" it.
+ If an outer data is not encoded in UTF-8, firstly you should "decode"
+ it.
use Encode;
local $/;
- open( my $fh, '<', 'json.data' ); # ex. this data is encoded in cp932.
- $json_text = decode( 'cp932', <$fh> ); # UNICODE
+ open( my $fh, '<', 'json.data' );
+ my $encoding = 'cp932';
+ my $unicode_json_text = decode( $encoding, <$fh> ); # UNICODE
# or you can write the below code.
#
- # open( my $fh, '<:encoding(cp932)', 'json.data' );
- # $json_text = <$fh>;
-
- In this case, $json_text is UNICODE string. So you cannot use
- "decode_json" nor "JSON" module object with "utf8" enable. Instead of
- them, you use "JSON" module object with "utf8" disable or "from_json".
-
- $perl_scalar = $json->utf8(0)->decode( $json_text );
+ # open( my $fh, "<:encoding($encoding)", 'json.data' );
+ # $unicode_json_text = <$fh>;
+
+ In this case, $unicode_json_text is of course UNICODE string. So you
+ cannot use "decode_json" nor "JSON" module object with "utf8" enable.
+ Instead of them, you use "JSON" module object with "utf8" disable or
+ "from_json".
+
+ $perl_scalar = $json->utf8(0)->decode( $unicode_json_text );
# or
- $perl_scalar = from_json( $json_text );
+ $perl_scalar = from_json( $unicode_json_text );
+
+ Or "encode 'utf8'" and "decode_json":
+
+ $perl_scalar = decode_json( encode( 'utf8', $unicode_json_text ) );
+ # this way is not efficient.
And now, you want to convert your $perl_scalar into JSON data and send
it to an outer world - a network or a file content, and so on.
- If your data contains UNICODE strings and you want the converted data to
- be encoded in UTF-8, you should use "encode_json" or "JSON" module
- object with "utf8" enable.
+ Your data usually contains UNICODE strings and you want the converted
+ data to be encoded in UTF-8, you should use "encode_json" or "JSON"
+ module object with "utf8" enable.
print encode_json( $perl_scalar ); # to a network? file? or display?
# or
print $json->utf8->encode( $perl_scalar );
- And if $perl_scalar does not contain UNICODE, then your characters are
- latin1 for perl. So you cannot use "encode_json" nor "JSON" module
- object with "utf8" enable. Instead of them, you use "JSON" module object
- with "utf8" disable or "to_json".
-
- $json_text = $json->utf8(0)->encode( $perl_scalar );
+ If $perl_scalar does not contain UNICODE but $encoding-encoded strings
+ for some reason, then its characters are regarded as latin1 for perl
+ (because it does not concern with your $encoding). You cannot use
+ "encode_json" nor "JSON" module object with "utf8" enable. Instead of
+ them, you use "JSON" module object with "utf8" disable or "to_json".
+ Note that the resulted text is a UNICODE string but no problem to print
+ it.
+
+ # $perl_scalar contains $encoding encoded string values
+ $unicode_json_text = $json->utf8(0)->encode( $perl_scalar );
# or
- $json_text = to_json( $perl_scalar );
-
- # and then print or convert to UNICODE
- print $json_text;
-
- $unicode_json_text = encode( $encoding, $json_text );
+ $unicode_json_text = to_json( $perl_scalar );
+ # $unicode_json_text consists of characters less than 0x100
+ print $unicode_json_text;
+
+ Or "decode $encoding" all string values and "encode_json":
+
+ $perl_scalar->{ foo } = decode( $encoding, $perl_scalar->{ foo } );
+ # ... do it to each string values, then encode_json
+ $json_text = encode_json( $perl_scalar );
+
+ This method is a proper way but probably not efficient.
See to Encode, perluniintro.
Added: branches/upstream/libjson-perl/current/eg/bench_decode.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/eg/bench_decode.pl?rev=55499&op=file
==============================================================================
--- branches/upstream/libjson-perl/current/eg/bench_decode.pl (added)
+++ branches/upstream/libjson-perl/current/eg/bench_decode.pl Mon Apr 5 12:44:59 2010
@@ -1,0 +1,70 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Benchmark qw( cmpthese timethese );
+
+our $VERSION = '1.00';
+
+my $wanttime = $ARGV[1] || 5;
+
+use JSON qw( -support_by_pp -no_export ); # for JSON::PP::Boolean inheritance
+use JSON::PP ();
+use JSON::XS ();
+use utf8;
+
+my $pp = JSON::PP->new->utf8;
+my $xs = JSON::XS->new->utf8;
+
+local $/;
+
+my $json = <>;
+my $perl = JSON::XS::decode_json $json;
+my $result;
+
+
+printf( "JSON::PP %s\n", JSON::PP->VERSION );
+printf( "JSON::XS %s\n", JSON::XS->VERSION );
+
+
+print "-----------------------------------\n";
+print "->decode()\n";
+print "-----------------------------------\n";
+
+$result = timethese( -$wanttime,
+ {
+ 'JSON::PP' => sub { $pp->decode( $json ) },
+ 'JSON::XS' => sub { $xs->decode( $json ) },
+ },
+ 'none'
+);
+cmpthese( $result );
+
+print "-----------------------------------\n";
+
+
+__END__
+
+=pod
+
+=head1 SYNOPSYS
+
+ bench_decode.pl json-file
+ # or
+ bench_decode.pl json-file minimum-time
+
+=head1 DESCRIPTION
+
+L<JSON::PP> and L<JSON::XS> decoding benchmark.
+
+=head1 AUTHOR
+
+makamaka
+
+=head1 LISENCE
+
+This library is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
Added: branches/upstream/libjson-perl/current/eg/bench_encode.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/eg/bench_encode.pl?rev=55499&op=file
==============================================================================
--- branches/upstream/libjson-perl/current/eg/bench_encode.pl (added)
+++ branches/upstream/libjson-perl/current/eg/bench_encode.pl Mon Apr 5 12:44:59 2010
@@ -1,0 +1,86 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Benchmark qw( cmpthese timethese );
+
+our $VERSION = '1.00';
+
+my $wanttime = $ARGV[1] || 5;
+
+use JSON qw( -support_by_pp -no_export ); # for JSON::PP::Boolean inheritance
+use JSON::PP ();
+use JSON::XS ();
+use utf8;
+
+my $pp = JSON::PP->new->utf8;
+my $xs = JSON::XS->new->utf8;
+
+local $/;
+
+my $json = <>;
+my $perl = JSON::XS::decode_json $json;
+my $result;
+
+
+printf( "JSON::PP %s\n", JSON::PP->VERSION );
+printf( "JSON::XS %s\n", JSON::XS->VERSION );
+
+
+print "-----------------------------------\n";
+print "->encode()\n";
+print "-----------------------------------\n";
+
+$result = timethese( -$wanttime,
+ {
+ 'JSON::PP' => sub { $pp->encode( $perl ) },
+ 'JSON::XS' => sub { $xs->encode( $perl ) },
+ },
+ 'none'
+);
+cmpthese( $result );
+
+print "-----------------------------------\n";
+print "->pretty->canonical->encode()\n";
+print "-----------------------------------\n";
+
+$pp->pretty->canonical;
+$xs->pretty->canonical;
+
+$result = timethese( -$wanttime,
+ {
+ 'JSON::PP' => sub { $pp->encode( $perl ) },
+ 'JSON::XS' => sub { $xs->encode( $perl ) },
+ },
+ 'none'
+);
+cmpthese( $result );
+
+print "-----------------------------------\n";
+
+
+__END__
+
+=pod
+
+=head1 SYNOPSYS
+
+ bench_encode.pl json-file
+ # or
+ bench_encode.pl json-file minimum-time
+
+=head1 DESCRIPTION
+
+L<JSON::PP> and L<JSON::XS> encoding benchmark.
+
+=head1 AUTHOR
+
+makamaka
+
+=head1 LISENCE
+
+This library is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
Modified: branches/upstream/libjson-perl/current/lib/JSON.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libjson-perl/current/lib/JSON.pm?rev=55499&op=diff
==============================================================================
--- branches/upstream/libjson-perl/current/lib/JSON.pm (original)
+++ branches/upstream/libjson-perl/current/lib/JSON.pm Mon Apr 5 12:44:59 2010
@@ -7,7 +7,7 @@
@JSON::EXPORT = qw(from_json to_json jsonToObj objToJson encode_json decode_json);
BEGIN {
- $JSON::VERSION = '2.20';
+ $JSON::VERSION = '2.21';
$JSON::DEBUG = 0 unless (defined $JSON::DEBUG);
}
@@ -608,7 +608,7 @@
=head1 VERSION
- 2.20
+ 2.21
This version is compatible with JSON::XS B<2.27> and later.
@@ -819,7 +819,7 @@
If you know a JSON text from an outer world - a network, a file content, and so on,
is encoded in UTF-8, you should use C<decode_json> or C<JSON> module object
-with C<utf8> enable. And the decoded data contains UNICODE characters.
+with C<utf8> enable. And the decoded result will contain UNICODE characters.
# from network
my $json = JSON->new->utf8;
@@ -832,49 +832,63 @@
$json_text = <$fh>;
$perl_scalar = decode_json( $json_text );
-If your data is not encoded in UTF-8, firstly you should C<decode> it.
+If an outer data is not encoded in UTF-8, firstly you should C<decode> it.
use Encode;
local $/;
- open( my $fh, '<', 'json.data' ); # ex. this data is encoded in cp932.
- $json_text = decode( 'cp932', <$fh> ); # UNICODE
+ open( my $fh, '<', 'json.data' );
+ my $encoding = 'cp932';
+ my $unicode_json_text = decode( $encoding, <$fh> ); # UNICODE
# or you can write the below code.
#
- # open( my $fh, '<:encoding(cp932)', 'json.data' );
- # $json_text = <$fh>;
-
-In this case, C<$json_text> is UNICODE string.
+ # open( my $fh, "<:encoding($encoding)", 'json.data' );
+ # $unicode_json_text = <$fh>;
+
+In this case, C<$unicode_json_text> is of course UNICODE string.
So you B<cannot> use C<decode_json> nor C<JSON> module object with C<utf8> enable.
Instead of them, you use C<JSON> module object with C<utf8> disable or C<from_json>.
- $perl_scalar = $json->utf8(0)->decode( $json_text );
+ $perl_scalar = $json->utf8(0)->decode( $unicode_json_text );
# or
- $perl_scalar = from_json( $json_text );
+ $perl_scalar = from_json( $unicode_json_text );
+
+Or C<encode 'utf8'> and C<decode_json>:
+
+ $perl_scalar = decode_json( encode( 'utf8', $unicode_json_text ) );
+ # this way is not efficient.
And now, you want to convert your C<$perl_scalar> into JSON data and
send it to an outer world - a network or a file content, and so on.
-If your data contains UNICODE strings and you want the converted data to be encoded
+Your data usually contains UNICODE strings and you want the converted data to be encoded
in UTF-8, you should use C<encode_json> or C<JSON> module object with C<utf8> enable.
print encode_json( $perl_scalar ); # to a network? file? or display?
# or
print $json->utf8->encode( $perl_scalar );
-And if C<$perl_scalar> does not contain UNICODE, then your characters are latin1 for perl.
-So you B<cannot> use C<encode_json> nor C<JSON> module object with C<utf8> enable.
+If C<$perl_scalar> does not contain UNICODE but C<$encoding>-encoded strings
+for some reason, then its characters are regarded as B<latin1> for perl
+(because it does not concern with your $encoding).
+You B<cannot> use C<encode_json> nor C<JSON> module object with C<utf8> enable.
Instead of them, you use C<JSON> module object with C<utf8> disable or C<to_json>.
-
- $json_text = $json->utf8(0)->encode( $perl_scalar );
+Note that the resulted text is a UNICODE string but no problem to print it.
+
+ # $perl_scalar contains $encoding encoded string values
+ $unicode_json_text = $json->utf8(0)->encode( $perl_scalar );
# or
- $json_text = to_json( $perl_scalar );
-
- # and then print or convert to UNICODE
- print $json_text;
-
- $unicode_json_text = encode( $encoding, $json_text );
-
+ $unicode_json_text = to_json( $perl_scalar );
+ # $unicode_json_text consists of characters less than 0x100
+ print $unicode_json_text;
+
+Or C<decode $encoding> all string values and C<encode_json>:
+
+ $perl_scalar->{ foo } = decode( $encoding, $perl_scalar->{ foo } );
+ # ... do it to each string values, then encode_json
+ $json_text = encode_json( $perl_scalar );
+
+This method is a proper way but probably not efficient.
See to L<Encode>, L<perluniintro>.
More information about the Pkg-perl-cvs-commits
mailing list