Skip to content

Commit

Permalink
Merge fee6416 into 0e9a59f
Browse files Browse the repository at this point in the history
  • Loading branch information
kwakwaversal committed Jan 12, 2017
2 parents 0e9a59f + fee6416 commit 9cf6d41
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/DBIx/Class/Helper/Row/ToJSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use warnings;
use parent 'DBIx::Class::Row';

__PACKAGE__->mk_group_accessors(inherited => '_serializable_columns');
__PACKAGE__->mk_group_accessors(inherited => '_unserializable_data_types');

my $dont_serialize = {
text => 1,
Expand All @@ -22,7 +23,7 @@ sub _is_column_serializable {

if (!defined $info->{is_serializable}) {
if (defined $info->{data_type} &&
$dont_serialize->{lc $info->{data_type}}
$self->unserializable_data_types->{lc $info->{data_type}}
) {
$info->{is_serializable} = 0;
} else {
Expand Down Expand Up @@ -56,6 +57,14 @@ sub TO_JSON {
};
}

sub unserializable_data_types {
my $self = shift;
if (!$self->_unserializable_data_types) {
$self->_unserializable_data_types($dont_serialize);
}
return $self->_unserializable_data_types;
}

1;

=pod
Expand Down Expand Up @@ -140,3 +149,20 @@ to the returned hashref:
%{ $self->next::method },
}
}
=method unserializable_data_types
$self->unserializable_data_types
Simply returns a hashref of data types that TO_JSON should not serialize.
Defaults to C<blob>, C<text>, or C<ntext>.
If you wanted to allow serialization of text data types, you might override this
method to look like this:
sub unserializable_data_types {
return {
blob => 1,
ntext => 1,
};
}
18 changes: 18 additions & 0 deletions t/Row/ToJSON.t
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@ ACCESSOR_CLASS: {
}], 'accessor fields with TO_JSON works');
}

SERIALIZE_ALL_DATA_TYPES: {
my $datas = [
map $_->TO_JSON,
$schema->resultset('SerializeAll')->search(undef, { order_by => 'id' })->all
];

cmp_deeply($datas, [{
id => 1,
text_column => 'frew',
},{
id => 2,
text_column => 'frioux',
},{
id => 3,
text_column => 'frooh',
}], 'serialize all data types with TO_JSON');
}

done_testing;
7 changes: 7 additions & 0 deletions t/lib/TestSchema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ sub prepopulate {
[2,'cc','dd'],
[3,'ee','ff'],
]);

$self->populate(SerializeAll => [
[qw{id text_column}],
[1,'frew'],
[2,'frioux'],
[3,'frooh'],
]);
}

'kitten eater';
15 changes: 15 additions & 0 deletions t/lib/TestSchema/Result/SerializeAll.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package TestSchema::Result::SerializeAll;

use DBIx::Class::Candy
-components => [qw(
Helper::Row::ToJSON
)];

table 'SerializeAll';

primary_column id => { data_type => 'int' };;
column text_column => { data_type => 'text' };

sub unserializable_data_types { {} }

1;

0 comments on commit 9cf6d41

Please sign in to comment.