DBIx::...ToJSON(3pm)
π DBIx::...ToJSON β the DBIx::...ToJSON(3pm) glow-up π
okay bestie, sit down, hydrate, because we're talking about DBIx::Class::Helper::Row::ToJSON β the module whose whole entire job is removing the boilerplate from your TO_JSON functions. it's giving "I wrote that hand-rolled hash once and then never again." π
β¨ NAME β¨
DBIx::Class::Helper::Row::ToJSON β Remove the boilerplate from your TO_JSON functions. no cap, that's the whole pitch. π
πΈ SYNOPSIS πΈ
so picture this: a schema result class. and it's named after a KittenRobot, because of course it is. π±π€
package MyApp::Schema::Result::KittenRobot;
use parent 'DBIx::Class::Core';
__PACKAGE__->load_components(qw{Helper::Row::ToJSON});
__PACKAGE__->table('KittenRobot');
__PACKAGE__->add_columns(
id => {
data_type => 'integer',
is_auto_increment => 1,
},
kitten => {
data_type => 'integer',
},
robot => {
data_type => 'text',
is_nullable => 1,
},
your_mom => {
data_type => 'blob',
is_nullable => 1,
is_serializable => 1,
},
);
1;
and then this helper β¨ adds a JSON method β¨ like the following:
sub TO_JSON {
return {
id => $self->id,
kitten => $self->kitten,
# robot => $self->robot, # <-- doesn't serialize text columns
your_mom => $self->your_mom, # <-- normally wouldn't but explicitly
# asked for in the column spec above
}
}
see how robot is sitting there all commented out? that's not a typo, that's the text data type getting politely declined. and your_mom? normally it would get declined too (it's a blob π) but we literally wrote is_serializable => 1 in the column spec so it ate and made it through. periodt. π
π₯ METHODS π₯
π¦ _is_column_serializable
$self->_is_column_serializable('kitten')
returns true if a column should be serializable or not. currently this marks everything as serializable unless "is_serializable" is set to false, or "data_type" is a "blob", "text", or "ntext" column. (yes, "kitten" up there is a real example and yes it returns true, we love an integer column that minds its own business. π±)
if you wanted to only have explicit serialization β like, no vibes-based auto-serializing, only columns that literally asked for it β you might override this method to look like this:
sub _is_column_serializable {
my ( $self, $column ) = @_;
my $info = $self->column_info($column);
return defined $info->{is_serializable} && $info->{is_serializable};
}
π§ serializable_columns
$self->serializable_columns
simply returns a list of columns that TO_JSON should serialize. that's it. that's the method. lowkey iconic for how little it does. π
π TO_JSON
$self->TO_JSON
returns a hashref representing your object. and here's the fun part β override this method to add data to the returned hashref, like so:
sub TO_JSON {
my $self = shift;
return {
customer_name => $self->customer->name,
%{ $self->next::method },
}
}
see that %{ $self->next::method }? that's you calling up to the parent and being like "okay bestie, do your thing, I'll add my little extras on top." main character energy, honestly. β¨
π unserializable_data_types
$self->unserializable_data_types
simply returns a hashref of data types that TO_JSON should not serialize. defaults to "blob", "text", or "ntext".
if you wanted to allow serialization of text data types (bold move, but okay π ) you might override this method to look like this:
sub unserializable_data_types {
return {
blob => 1,
ntext => 1,
};
}
notice text is not in that hashref anymore. that's the whole trick. that's the move. π«Ά
π AUTHOR π
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay and that's the whole page, bestie!! this was DBIx::...ToJSON(3pm) from perl v5.40.0, dated 2024-11-16, and honestly? she ate. go serialize something responsibly. mwah. πβ¨ππ¦πΈ
π¦ where this page comes from
- debian trixie: libdbix-class-helpers-perl 2.037000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdbix-class-helpers-perl 2.037000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdbix-class-helpers-perl 2.037000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms