DBIx::...ToJSON(3pm)

πŸ“¦
version 2.037000 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– 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>


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