/usr/share/perl5/DBIx/Class/InflateColumn/Serializer/JSON.pm is in libdbix-class-inflatecolumn-serializer-perl 0.09-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | package DBIx::Class::InflateColumn::Serializer::JSON;
$DBIx::Class::InflateColumn::Serializer::JSON::VERSION = '0.09';
=head1 NAME
DBIx::Class::InflateColumn::Serializer::JSON - JSON Inflator
=head1 SYNOPSIS
package MySchema::Table;
use base 'DBIx::Class';
__PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
__PACKAGE__->add_columns(
'data_column' => {
'data_type' => 'VARCHAR',
'size' => 255,
'serializer_class' => 'JSON',
'serializer_options' => { allow_blessed => 1, convert_blessed => 1, pretty => 1 }, # optional
}
);
Then in your code...
my $struct = { 'I' => { 'am' => 'a struct' };
$obj->data_column($struct);
$obj->update;
And you can recover your data structure with:
my $obj = ...->find(...);
my $struct = $obj->data_column;
The data structures you assign to "data_column" will be saved in the database in JSON format.
Any arguments included in C<serializer_options> will be passed to the L<JSON::MaybeXS> constructor,
to be used by the JSON backend for both serializing and deserializing.
=cut
use strict;
use warnings;
use JSON::MaybeXS;
use Carp;
use namespace::clean;
=over 4
=item get_freezer
Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes
the data passed to it. Returns a coderef.
=cut
sub get_freezer{
my ($class, $column, $info, $args) = @_;
my $opts = $info->{serializer_options};
my $serializer = JSON::MaybeXS->new($opts && %$opts ? %$opts: ());
if (defined $info->{'size'}){
my $size = $info->{'size'};
return sub {
my $s = $serializer->encode(shift);
croak "serialization too big" if (length($s) > $size);
return $s;
};
} else {
return sub {
return $serializer->encode(shift);
};
}
}
=item get_unfreezer
Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes
the data stored in the column. Returns a coderef.
=back
=cut
sub get_unfreezer {
my ($class, $column, $info, $args) = @_;
my $opts = $info->{serializer_options};
return sub {
JSON::MaybeXS->new($opts && %$opts ? %$opts : ())->decode(shift);
};
}
1;
|