1DBIx::Class::Helper::RoUws:e:rToCJoSnOtNr(i3b)uted PerlDDBoIcxu:m:eCnltaastsi:o:nHelper::Row::ToJSON(3)
2
3
4
6 DBIx::Class::Helper::Row::ToJSON - Remove the boilerplate from your
7 TO_JSON functions
8
10 package MyApp::Schema::Result::KittenRobot;
11
12 use parent 'DBIx::Class::Core';
13
14 __PACKAGE__->load_components(qw{Helper::Row::ToJSON});
15
16 __PACKAGE__->table('KittenRobot');
17 __PACKAGE__->add_columns(
18 id => {
19 data_type => 'integer',
20 is_auto_increment => 1,
21 },
22 kitten => {
23 data_type => 'integer',
24 },
25 robot => {
26 data_type => 'text',
27 is_nullable => 1,
28 },
29 your_mom => {
30 data_type => 'blob',
31 is_nullable => 1,
32 is_serializable => 1,
33 },
34 );
35
36 1;
37
38 This helper adds a JSON method like the following:
39
40 sub TO_JSON {
41 return {
42 id => $self->id,
43 kitten => $self->kitten,
44 # robot => $self->robot, # <-- doesn't serialize text columns
45 your_mom => $self->your_mom, # <-- normally wouldn't but explicitly
46 # asked for in the column spec above
47 }
48 }
49
51 _is_column_serializable
52 $self->_is_column_serializable('kitten')
53
54 returns true if a column should be serializable or not. Currently this
55 marks everything as serializable unless "is_serializable" is set to
56 false, or "data_type" is a "blob", "text", or "ntext" columns. If you
57 wanted to only have explicit serialization you might override this
58 method to look like this:
59
60 sub _is_column_serializable {
61 my ( $self, $column ) = @_;
62
63 my $info = $self->column_info($column);
64
65 return defined $info->{is_serializable} && $info->{is_serializable};
66 }
67
68 serializable_columns
69 $self->serializable_columns
70
71 simply returns a list of columns that TO_JSON should serialize.
72
73 TO_JSON
74 $self->TO_JSON
75
76 returns a hashref representing your object. Override this method to
77 add data to the returned hashref:
78
79 sub TO_JSON {
80 my $self = shift;
81
82 return {
83 customer_name => $self->customer->name,
84 %{ $self->next::method },
85 }
86 }
87
88 unserializable_data_types
89 $self->unserializable_data_types
90
91 Simply returns a hashref of data types that TO_JSON should not
92 serialize. Defaults to "blob", "text", or "ntext".
93
94 If you wanted to allow serialization of text data types, you might
95 override this method to look like this:
96
97 sub unserializable_data_types {
98 return {
99 blob => 1,
100 ntext => 1,
101 };
102 }
103
105 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
106
108 This software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.
109
110 This is free software; you can redistribute it and/or modify it under
111 the same terms as the Perl 5 programming language system itself.
112
113
114
115perl v5.32.0 2020-07-28DBIx::Class::Helper::Row::ToJSON(3)