| 1 | package Class::DBI::Plugin::TO_JSON; | ||
| 2 | |||
| 3 | ############################################################################### | ||
| 4 | # Required inclusions. | ||
| 5 | ############################################################################### | ||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | ############################################################################### | ||
| 10 | # Export our methods. | ||
| 11 | ############################################################################### | ||
| 12 | 2 | ▶ | use base qw( Exporter ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | our @EXPORT = qw( | |
| 14 | TO_JSON | ||
| 15 | ); | ||
| 16 | |||
| 17 | ############################################################################### | ||
| 18 | # Version number. | ||
| 19 | ############################################################################### | ||
| 20 | 2 | our $VERSION = '0.04'; | |
| 21 | |||
| 22 | ############################################################################### | ||
| 23 | # Subroutine: TO_JSON() | ||
| 24 | ############################################################################### | ||
| 25 | # Turns the CDBI data record into a HASHREF suitable for use with 'JSON::XS' | ||
| 26 | ############################################################################### | ||
| 27 | 1 | ▶ | sub TO_JSON { |
|
Subroutine
TO_JSON: called CC=2 SCAR=6.9
Pod
documented
| |||
| 28 | 1 | my $self = shift; | |
| 29 | |||
| 30 | # get all of our data | ||
| 31 | 1 | my @cols = $self->columns(); | |
| 32 | 1 | my %data; | |
| 33 | 1 | @data{@cols} = $self->get(@cols); | |
| 34 | |||
| 35 | # deflate the data, giving us JUST a hash of the raw data | ||
| 36 | 1 | foreach my $column (@cols) { | |
| 37 | 3 | my $name = $column->name(); | |
| 38 | 3 | $data{$name} = $self->_deflated_column($column, $data{$name}); | |
| 39 | } | ||
| 40 | |||
| 41 | # return the data back to the caller. | ||
| 42 | 1 | return \%data; | |
| 43 | } | ||
| 44 | |||
| 45 | 2 | 1; | |
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | Class::DBI::Plugin::TO_JSON - Help integrate Class::DBI with JSON::XS | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | package MY::DB; | ||
| 54 | use base qw(Class::DBI); | ||
| 55 | use Class::DBI::Plugin::TO_JSON; | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | C<Class::DBI::Plugin::TO_JSON> helps integrate C<Class::DBI> with C<JSON::XS>, | ||
| 60 | by implementing a C<TO_JSON()> method which turns your data record into a | ||
| 61 | plain/raw HASHREF with no inflated values. | ||
| 62 | |||
| 63 | =head1 METHODS | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item TO_JSON() | ||
| 68 | |||
| 69 | Turns the C<Class::DBI> data record into a HASHREF suitable for use with | ||
| 70 | C<JSON::XS> | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head1 AUTHOR | ||
| 75 | |||
| 76 | Graham TerMarsch (cpan@howlingfrog.com) | ||
| 77 | |||
| 78 | =head1 COPYRIGHT | ||
| 79 | |||
| 80 | Copyright (C) 2008, Graham TerMarsch. All rights reserved. | ||
| 81 | |||
| 82 | This is free software; you can redistribute it and/or modify it under the same | ||
| 83 | terms as Perl itself. | ||
| 84 | |||
| 85 | =head1 SEE ALSO | ||
| 86 | |||
| 87 | L<JSON::XS>, | ||
| 88 | L<Class::DBI>. | ||
| 89 | |||
| 90 | =cut | ||