From 8ccf3fd5dea33a277ba268dc1ff37ef847caf730 Mon Sep 17 00:00:00 2001 From: Marc Mims Date: Thu, 22 Sep 2011 11:24:20 -0700 Subject: [PATCH] Replaced JSON::Any with JSON --- Changes | 1 + Makefile.PL | 20 +------------------- lib/Net/Twitter.pm | 3 ++- lib/Net/Twitter/Core.pm | 6 +++--- t/20_exceptions.t | 3 ++- t/50_inflate_objects.t | 5 +++-- t/51_rate_limit.t | 5 +++-- t/51_since.t | 3 ++- t/auto-cursor.t | 6 +++--- 9 files changed, 20 insertions(+), 32 deletions(-) diff --git a/Changes b/Changes index ae23167..7097875 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - Replaced JSON::Any with JSON - documented new arguments for friendship_exists 3.18000_01 2011-09-21 diff --git a/Makefile.PL b/Makefile.PL index 989915f..2bdf11f 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -31,7 +31,7 @@ requires 'Digest::HMAC_SHA1'; requires 'Encode'; requires 'HTML::Entities'; requires 'HTTP::Request::Common'; -requires 'JSON::Any' => 1.21; +requires 'JSON'; requires 'LWP::UserAgent' => 2.032; requires 'Moose' => 0.90; requires 'Moose::Exporter'; @@ -49,24 +49,6 @@ requires 'Try::Tiny' => 0.03; test_requires 'Test::Fatal'; test_requires 'Test::More' => 0.88; # for done_testing -# Don't rely on the JSON::Any requirement to pull in a suitable JSON backend. -# The user may already have JSON::Any installed, but since removed all valid -# providers. (Yes, this seems to have happened in the wild.) -sub has_json_xs () { - my @order = qw/JSON::XS/; - for my $provider ( @order ) { - eval "require $provider"; - return 1 unless $@; - } - return; -} - -if (has_json_xs()) { - requires 'JSON::XS' => 0; -} else { - requires 'JSON' => '2.02'; -} - no_index directory => 'src'; no_index directory => 'examples'; diff --git a/lib/Net/Twitter.pm b/lib/Net/Twitter.pm index 8aac0f9..5b5960a 100644 --- a/lib/Net/Twitter.pm +++ b/lib/Net/Twitter.pm @@ -1,6 +1,7 @@ package Net::Twitter; use Moose; use Carp; +use JSON; use Net::Twitter::Core; use Digest::SHA qw/sha1_hex/; @@ -88,7 +89,7 @@ sub _create_anon_class { my $t = shift @t; if ( ref $t[0] eq 'HASH' ) { my $params = shift @t; - my $sig = sha1_hex(JSON::Any->to_json($params)); + my $sig = sha1_hex(JSON->new->utf8->encode($params)); my $sn = $serial_for_params{$sig} ||= ++$serial_number; $t .= "_$sn"; } diff --git a/lib/Net/Twitter/Core.pm b/lib/Net/Twitter/Core.pm index 9970e40..7fa1f3d 100644 --- a/lib/Net/Twitter/Core.pm +++ b/lib/Net/Twitter/Core.pm @@ -3,7 +3,7 @@ use 5.008001; use Moose; use MooseX::Aliases; use Carp; -use JSON::Any qw/XS JSON/; +use JSON; use URI::Escape; use HTTP::Request::Common; use Net::Twitter::Error; @@ -41,8 +41,8 @@ has clienturl => ( isa => 'Str', is => 'ro', default => 'http://search.cpa has _base_url => ( is => 'rw' ); ### keeps role composition from bitching ?? has _json_handler => ( is => 'rw', - default => sub { JSON::Any->new(utf8 => 1) }, - handles => { _from_json => 'from_json' }, + default => sub { JSON->new->utf8 }, + handles => { _from_json => 'decode' }, ); sub _synthetic_args { qw/authenticate since/ } diff --git a/t/20_exceptions.t b/t/20_exceptions.t index 5841d2f..322fed0 100644 --- a/t/20_exceptions.t +++ b/t/20_exceptions.t @@ -3,6 +3,7 @@ use warnings; use strict; use Test::More; use Test::Fatal; +use JSON qw/to_json/; use lib qw(t/lib); use Net::Twitter; @@ -18,7 +19,7 @@ my $nt = Net::Twitter->new( my $t = TestUA->new($nt->ua); my $response = HTTP::Response->new(404, 'Not Found'); -$response->content(JSON::Any->to_json({ +$response->content(to_json({ request => '/direct_messages/destroy/456.json', error => 'No direct message with that ID found.', })); diff --git a/t/50_inflate_objects.t b/t/50_inflate_objects.t index 8d970d7..c927441 100644 --- a/t/50_inflate_objects.t +++ b/t/50_inflate_objects.t @@ -3,6 +3,7 @@ use warnings; use strict; use Scalar::Util qw/blessed/; use Test::More; +use JSON qw/to_json/; use lib qw(t/lib); eval 'use TestUA'; @@ -23,7 +24,7 @@ my $dt = DateTime->now; $dt->subtract(minutes => 6); my $t = TestUA->new($nt->ua); -$t->response->content(JSON::Any->to_json([{ +$t->response->content(to_json([{ text => 'Hello, twittersphere!', user => { screen_name => 'net_twitter', @@ -42,7 +43,7 @@ is $object->relative_created_at, '6 minutes ago', 'relative_created_at'; is $object->user->screen_name, 'net_twitter', 'nested objects'; # make sure we don't co-mingle our object methods -$t->response->content(JSON::Any->to_json({ +$t->response->content(to_json({ foo => 'foo', bar => 'bar', baz => 'and of course, baz', diff --git a/t/51_rate_limit.t b/t/51_rate_limit.t index 5961c57..83a68d2 100644 --- a/t/51_rate_limit.t +++ b/t/51_rate_limit.t @@ -2,6 +2,7 @@ use warnings; use strict; use Test::More; +use JSON qw/to_json/; use lib qw(t/lib); use Net::Twitter; @@ -12,7 +13,7 @@ my $nt = Net::Twitter->new(traits => [qw/API::REST RateLimit/]); my $reset = time + 1800; my $t = TestUA->new($nt->ua); -$t->response->content(JSON::Any->to_json({ +$t->response->content(to_json({ remaining_hits => 75, reset_time_in_seconds => $reset, hourly_limit => 150, @@ -32,7 +33,7 @@ ok $until > 890 && $until < 910, 'until_rate(2.0) is about 900'; # test clock mismatch -$t->response->content(JSON::Any->to_json({ +$t->response->content(to_json({ remaining_hits => 10, reset_time_in_seconds => $nt->_rate_limit_status->{rate_reset} = time - 10, hourly_limit => 150, diff --git a/t/51_since.t b/t/51_since.t index 648ec0b..2a5582b 100644 --- a/t/51_since.t +++ b/t/51_since.t @@ -4,6 +4,7 @@ use strict; use Try::Tiny; use Scalar::Util qw/blessed/; use Test::More; +use JSON qw/to_json/; use lib qw(t/lib); eval 'use TestUA'; @@ -24,7 +25,7 @@ my $dt = DateTime->now; $dt->subtract(minutes => 6); my $t = TestUA->new($nt->ua); -$t->response->content(JSON::Any->to_json([ +$t->response->content(to_json([ { text => 'Hello, twittersphere!', id => 1234, diff --git a/t/auto-cursor.t b/t/auto-cursor.t index 7972ac0..29ce8b9 100644 --- a/t/auto-cursor.t +++ b/t/auto-cursor.t @@ -3,10 +3,10 @@ use warnings; use strict; use Test::More; use Net::Twitter; -use JSON::Any; +use JSON qw/to_json/; use HTTP::Response; -my $json_result = JSON::Any->to_json({ ids => ['test'], next_cursor => 1 }); +my $json_result = to_json({ ids => ['test'], next_cursor => 1 }); sub mock_ua { my $nt = shift; @@ -25,7 +25,7 @@ sub mock_ua { my $nt_with_max_calls_4 = Net::Twitter->new(traits => ['API::REST', AutoCursor => { max_calls => 4 }]); my $class_for_max_calls_4 = ref $nt_with_max_calls_4; - my $json_result = JSON::Any->to_json({ ids => ['test'], next_cursor => 1 }); + my $json_result = to_json({ ids => ['test'], next_cursor => 1 }); mock_ua($_) for $nt_with_max_calls_2, $nt_with_max_calls_4;