From 5ab59e97cc09ff1681a8295e2ede7e28cf6b3de0 Mon Sep 17 00:00:00 2001 From: Steve Atkins Date: Mon, 25 May 2015 14:06:26 -0700 Subject: [PATCH 1/4] add Mojo::Collection::TO_JSON to support serializing with Mojo::JSON --- lib/Mojo/Collection.pm | 2 ++ t/mojo/collection.t | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm index 7f6ed27fe0..840f5fdf52 100644 --- a/lib/Mojo/Collection.pm +++ b/lib/Mojo/Collection.pm @@ -90,6 +90,8 @@ sub tap { shift->Mojo::Base::tap(@_) } sub to_array { [@{shift()}] } +sub TO_JSON { [@{shift()}] } + sub uniq { my %seen; return $_[0]->new(grep { !$seen{$_}++ } @{$_[0]}); diff --git a/t/mojo/collection.t b/t/mojo/collection.t index 804453829b..2be3b004de 100644 --- a/t/mojo/collection.t +++ b/t/mojo/collection.t @@ -3,6 +3,7 @@ use Mojo::Base -strict; use Test::More; use Mojo::ByteStream 'b'; use Mojo::Collection 'c'; +use Mojo::JSON 'encode_json'; # Array is c(1, 2, 3)->[1], 2, 'right result'; @@ -154,4 +155,9 @@ is_deeply $collection->uniq->to_array, [1, 2, 3, 4, 5], 'right result'; is_deeply $collection->uniq->reverse->uniq->to_array, [5, 4, 3, 2, 1], 'right result'; +# json +$collection = c(1, 2, 3); +my $bytes = encode_json $collection; +is $bytes, '[1,2,3]', 'TO_JSON'; + done_testing(); From 58cabcd0755427f9d4cfaebf5188f0e023704787 Mon Sep 17 00:00:00 2001 From: Steve Atkins Date: Tue, 26 May 2015 15:25:13 -0700 Subject: [PATCH 2/4] add documentation for Mojo::Collection::TO_JSON --- lib/Mojo/Collection.pm | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm index 840f5fdf52..07b7bb1113 100644 --- a/lib/Mojo/Collection.pm +++ b/lib/Mojo/Collection.pm @@ -324,6 +324,16 @@ Alias for L. Turn collection into array reference. +=head2 TO_JSON + + my $array = $collection->TO_JSON; + +Turn collection into array reference allowing conversion to JSON by +L. + + # {"baz":["foo","bar"]} + $c->render(json => { baz => Mojo::Collection->new('foo', 'bar') }); + =head2 uniq my $new = $collection->uniq; From 8d2168d00576fd82717834b99d4e2bd63e5bc10d Mon Sep 17 00:00:00 2001 From: Steve Atkins Date: Tue, 26 May 2015 16:09:09 -0700 Subject: [PATCH 3/4] reorder methods; document TO_JSON solely as an alias for to_array --- lib/Mojo/Collection.pm | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm index 07b7bb1113..88b3656a5f 100644 --- a/lib/Mojo/Collection.pm +++ b/lib/Mojo/Collection.pm @@ -9,6 +9,8 @@ use Scalar::Util 'blessed'; our @EXPORT_OK = ('c'); +sub TO_JSON { [@{shift()}] } + sub c { __PACKAGE__->new(@_) } sub compact { @@ -90,8 +92,6 @@ sub tap { shift->Mojo::Base::tap(@_) } sub to_array { [@{shift()}] } -sub TO_JSON { [@{shift()}] } - sub uniq { my %seen; return $_[0]->new(grep { !$seen{$_}++ } @{$_[0]}); @@ -328,11 +328,7 @@ Turn collection into array reference. my $array = $collection->TO_JSON; -Turn collection into array reference allowing conversion to JSON by -L. - - # {"baz":["foo","bar"]} - $c->render(json => { baz => Mojo::Collection->new('foo', 'bar') }); +Alias for L. =head2 uniq From dc1ab3ae65e0aa2a9825ea478fd9435482aa6e6d Mon Sep 17 00:00:00 2001 From: Steve Atkins Date: Tue, 26 May 2015 16:15:47 -0700 Subject: [PATCH 4/4] reorder method documentation --- lib/Mojo/Collection.pm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm index 88b3656a5f..523c941815 100644 --- a/lib/Mojo/Collection.pm +++ b/lib/Mojo/Collection.pm @@ -154,6 +154,12 @@ Construct a new array-based L object. L implements the following methods. +=head2 TO_JSON + + my $array = $collection->TO_JSON; + +Alias for L. + =head2 compact my $new = $collection->compact; @@ -324,12 +330,6 @@ Alias for L. Turn collection into array reference. -=head2 TO_JSON - - my $array = $collection->TO_JSON; - -Alias for L. - =head2 uniq my $new = $collection->uniq;