Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method unserializable_data_types to ToJSON helper #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;