| 1 | package DBIx::Class::InflateColumn::Serializer::JSON; | ||||||||||
| 2 | { | ||||||||||
| 3 | 3 | $DBIx::Class::InflateColumn::Serializer::JSON::VERSION = '0.06'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | DBIx::Class::InflateColumn::Serializer::JSON - JSON Inflator | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | package MySchema::Table; | ||||||||||
| 13 | use base 'DBIx::Class'; | ||||||||||
| 14 | |||||||||||
| 15 | __PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); | ||||||||||
| 16 | __PACKAGE__->add_columns( | ||||||||||
| 17 | 'data_column' => { | ||||||||||
| 18 | 'data_type' => 'VARCHAR', | ||||||||||
| 19 | 'size' => 255, | ||||||||||
| 20 | 'serializer_class' => 'JSON' | ||||||||||
| 21 | } | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | Then in your code... | ||||||||||
| 25 | |||||||||||
| 26 | my $struct = { 'I' => { 'am' => 'a struct' }; | ||||||||||
| 27 | $obj->data_column($struct); | ||||||||||
| 28 | $obj->update; | ||||||||||
| 29 | |||||||||||
| 30 | And you can recover your data structure with: | ||||||||||
| 31 | |||||||||||
| 32 | my $obj = ...->find(...); | ||||||||||
| 33 | my $struct = $obj->data_column; | ||||||||||
| 34 | |||||||||||
| 35 | The data structures you assign to "data_column" will be saved in the database in JSON format. | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 3 | ▶ | use JSON qw//; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | =over 4 | ||||||||||
| 45 | |||||||||||
| 46 | =item get_freezer | ||||||||||
| 47 | |||||||||||
| 48 | Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes | ||||||||||
| 49 | the data passed to it. Returns a coderef. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | sub get_freezer{ | ||||||||||
| 54 | 6 | ▶ | my ($class, $column, $info, $args) = @_; | ||||||||
|
Subroutine
get_freezer: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 55 | |||||||||||
| 56 | 6 | ▶ | if (defined $info->{'size'}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 3 | my $size = $info->{'size'}; | |||||||||
| 58 | return sub { | ||||||||||
| 59 | 2 | ▶ | my $s = JSON::to_json(shift); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 2 | ▶ | croak "serialization too big" if (length($s) > $size); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 1 | return $s; | |||||||||
| 62 | 3 | }; | |||||||||
| 63 | } else { | ||||||||||
| 64 | return sub { | ||||||||||
| 65 | 2 | ▶ | return JSON::to_json(shift); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 3 | }; | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | =item get_unfreezer | ||||||||||
| 71 | |||||||||||
| 72 | Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes | ||||||||||
| 73 | the data stored in the column. Returns a coderef. | ||||||||||
| 74 | |||||||||||
| 75 | =back | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | sub get_unfreezer { | ||||||||||
| 80 | return sub { | ||||||||||
| 81 | 3 | ▶ | return JSON::from_json(shift); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 6 | ▶ | }; | ||||||||
|
Subroutine
get_unfreezer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | 3 | 1; | |||||||||