« Jifty/Plugin/ViewDeclarePage/Page.pm
↑ Summary
Jifty/Plugin/Authentication/Password/Action/GeneratePasswordToken.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Filter::JSON; | ||
| 5 | use base qw/ Jifty::DBI::Filter /; | ||
| 6 | use Jifty::JSON qw(encode_json decode_json); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Jifty::Filter::JSON - This filter stores arbitrary Perl via JSON | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use Jifty::DBI::Record schema { | ||
| 15 | column my_data => | ||
| 16 | type is 'text', | ||
| 17 | filters are qw/ Jifty::Filter::JSON /; | ||
| 18 | }; | ||
| 19 | |||
| 20 | my $thing = __PACKAGE__->new; | ||
| 21 | $thing->create( my_data => { foo => 'bar', baz => [ 1, 2, 3 ] } ); | ||
| 22 | |||
| 23 | my $my_data = $thing->my_data; | ||
| 24 | while (my ($key, $value) = %$my_data) { | ||
| 25 | # do something... | ||
| 26 | } | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This filter provides the ability to store arbitrary data structures into a database column using L<JSON>. This is very similar to the L<Jifty::DBI::Filter::Storable> filter except that the L<JSON> format remains human-readable in the database. You can store virtually any Perl data, scalar, hash, or array into the database using this filter. | ||
| 31 | |||
| 32 | In addition, JSON (at least the storage of scalars, hashes, and arrays) is compatible with data structures written in other languages, so you may store or read data between applications written in different languages. | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 encode | ||
| 37 | |||
| 38 | This method is used to encode the Perl data structure into JSON formatted text. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub encode { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | my $value_ref = $self->value_ref; | ||
| 46 | return unless defined $$value_ref; | ||
| 47 | |||
| 48 | $$value_ref = encode_json($$value_ref); | ||
| 49 | } | ||
| 50 | |||
| 51 | =head2 decode | ||
| 52 | |||
| 53 | This method is used to decode the JSON formatted text from the database into the Perl data structure. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub decode { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | my $value_ref = $self->value_ref; | ||
| 61 | return unless defined $$value_ref; | ||
| 62 | |||
| 63 | $$value_ref = decode_json($$value_ref); | ||
| 64 | } | ||
| 65 | |||
| 66 | =head1 SEE ALSO | ||
| 67 | |||
| 68 | L<Jifty::DBI::Filter>, L<Jifty::JSON> | ||
| 69 | |||
| 70 | =head1 AUTHOR | ||
| 71 | |||
| 72 | Luke Closs E<lt>cpan@5thplane.comE<gt> | ||
| 73 | |||
| 74 | =head1 LICENSE | ||
| 75 | |||
| 76 | This program is free software and may be modified or distributed under the same terms as Perl itself. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | 1 |