diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm index 7f6ed27fe0..523c941815 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 { @@ -152,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; 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();