| 1 | package DBIx::Class::InflateColumn::JSON2Object; | ||
| 2 | |||
| 3 | # ABSTRACT: convert JSON columns to Perl objects | ||
| 4 | |||
| 5 | our $VERSION = '0.900'; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use JSON::MaybeXS qw(encode_json decode_json ); | ||
| 10 | use Encode qw(encode_utf8 decode_utf8); | ||
| 11 | use Module::Runtime 'use_module'; | ||
| 12 | use Scalar::Util qw(blessed); | ||
| 13 | |||
| 14 | sub class_in_column { | ||
| 15 | my ($class,@args) = @_; | ||
| 16 | my $caller = caller(0); | ||
| 17 | |||
| 18 | foreach my $def (@args) { | ||
| 19 | |||
| 20 | my $class_column = $def->{class_column}; | ||
| 21 | my $data_column = $def->{data_column}; | ||
| 22 | my $namespace = $def->{namespace}; | ||
| 23 | my $result_source = $def->{result_source} || $caller; | ||
| 24 | |||
| 25 | use_module($namespace); | ||
| 26 | |||
| 27 | $result_source->inflate_column( | ||
| 28 | $data_column, | ||
| 29 | { | ||
| 30 | inflate => sub { | ||
| 31 | my ($data,$self) = @_; | ||
| 32 | my $package = $namespace->package($self->$class_column); | ||
| 33 | return $package->thaw($data); | ||
| 34 | }, | ||
| 35 | deflate => sub { | ||
| 36 | my ($data,$self) = @_; | ||
| 37 | if (blessed $data) { | ||
| 38 | if ($data->isa($namespace)) { | ||
| 39 | $self->$class_column($data->moniker); | ||
| 40 | } else { | ||
| 41 | die('Supplied args object is not a '.$namespace); | ||
| 42 | } | ||
| 43 | } else { | ||
| 44 | my $package = $namespace->package($self->$class_column); | ||
| 45 | $data = $package->thaw($data); | ||
| 46 | } | ||
| 47 | return $data->freeze; | ||
| 48 | }, | ||
| 49 | } | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub fixed_class { | ||
| 55 | my ($class,@args) = @_; | ||
| 56 | my $caller = caller(0); | ||
| 57 | |||
| 58 | foreach my $def (@args) { | ||
| 59 | |||
| 60 | my $data_column = $def->{column}; | ||
| 61 | my $package = $def->{class}; | ||
| 62 | my $result_source = $def->{result_source} || $caller; | ||
| 63 | |||
| 64 | use_module($package); | ||
| 65 | |||
| 66 | $result_source->inflate_column( | ||
| 67 | $data_column, | ||
| 68 | { | ||
| 69 | inflate => sub { | ||
| 70 | my ($data,$self) = @_; | ||
| 71 | return $package->thaw($data); | ||
| 72 | }, | ||
| 73 | deflate => sub { | ||
| 74 | my ($data,$self) = @_; | ||
| 75 | if (blessed $data) { | ||
| 76 | if (!$data->isa($package)) { | ||
| 77 | die('Supplied args object is not a '.$package); | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | $data = $package->thaw($data); | ||
| 81 | } | ||
| 82 | return $data->freeze; | ||
| 83 | }, | ||
| 84 | } | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub no_class { | ||
| 90 | my ($class,@args) = @_; | ||
| 91 | my $caller = caller(0); | ||
| 92 | |||
| 93 | foreach my $def (@args) { | ||
| 94 | |||
| 95 | my $data_column = $def->{column}; | ||
| 96 | my $result_source = $def->{result_source} || $caller; | ||
| 97 | |||
| 98 | $result_source->inflate_column( | ||
| 99 | $data_column, | ||
| 100 | { | ||
| 101 | inflate => sub { | ||
| 102 | my ($data,$self) = @_; | ||
| 103 | return {} | ||
| 104 | if !defined $data | ||
| 105 | || $data =~ m/^\s*$/; | ||
| 106 | return decode_json( encode_utf8($data) ); | ||
| 107 | }, | ||
| 108 | deflate => sub { | ||
| 109 | my ($data,$self) = @_; | ||
| 110 | if ( ref($data) =~ m/^(HASH|ARRAY)$/ ) { | ||
| 111 | return decode_utf8( encode_json($data) ); | ||
| 112 | } | ||
| 113 | return $data; | ||
| 114 | }, | ||
| 115 | } | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | __END__ | ||
| 123 | |||
| 124 | =pod | ||
| 125 | |||
| 126 | =encoding UTF-8 | ||
| 127 | |||
| 128 | =head1 NAME | ||
| 129 | |||
| 130 | DBIx::Class::InflateColumn::JSON2Object - convert JSON columns to Perl objects | ||
| 131 | |||
| 132 | =head1 VERSION | ||
| 133 | |||
| 134 | version 0.900 | ||
| 135 | |||
| 136 | =head1 SYNOPSIS | ||
| 137 | |||
| 138 | # In a DBIx::Class Result | ||
| 139 | package MyApp::Schema::Result::SomeTable; | ||
| 140 | ... lots of DBIx::Class code... | ||
| 141 | |||
| 142 | use DBIx::Class::InflateColumn::JSON2Object; | ||
| 143 | DBIx::Class::InflateColumn::JSON2Object->fixed_class({ | ||
| 144 | column=>'data', # a column storing JSON | ||
| 145 | class=>'MyApp::SomeClass', | ||
| 146 | }); | ||
| 147 | |||
| 148 | # later, in some code far, far away... | ||
| 149 | my $row = $schema->resultset('SomeTable')->find(42); | ||
| 150 | my $obj = $row->data; | ||
| 151 | $obj->foo; # $obj ISA MyApp::SomeClass | ||
| 152 | |||
| 153 | # store a hash as JSON after "validating" it through MyApp::SomeClass | ||
| 154 | $schema->resultset('SomeTable')->create({ | ||
| 155 | data => { | ||
| 156 | foo=>'bar', | ||
| 157 | }; | ||
| 158 | }) | ||
| 159 | |||
| 160 | # you can also use it to just deflate/inflate JSON to a Perl hash | ||
| 161 | DBIx::Class::InflateColumn::JSON2Object->no_class({ | ||
| 162 | column=>'args', | ||
| 163 | }); | ||
| 164 | |||
| 165 | # or have a complex set of objects | ||
| 166 | DBIx::Class::InflateColumn::JSON2Object->class_in_column({ | ||
| 167 | data_column => 'object', # some JSON | ||
| 168 | class_column => 'type', # here we store the name of the object | ||
| 169 | namespace => 'MyApp::Object', # the namespace of the objects | ||
| 170 | }); | ||
| 171 | |||
| 172 | =head1 DESCRIPTION | ||
| 173 | |||
| 174 | TODO: short overview | ||
| 175 | |||
| 176 | =head2 Booleans, JSON, oh my... | ||
| 177 | |||
| 178 | TODO: describe problem and the (hacky/crappy?) solution | ||
| 179 | |||
| 180 | =head2 METHODS | ||
| 181 | |||
| 182 | Please note that you can pass more than one HASHREF per method to | ||
| 183 | install several inflator/deflators at once. | ||
| 184 | |||
| 185 | =head3 no_class | ||
| 186 | |||
| 187 | Install a JSON inflator/deflator for each column. | ||
| 188 | |||
| 189 | DBIx::Class::InflateColumn::JSON2Object->no_class({ | ||
| 190 | column=>'args' | ||
| 191 | }); | ||
| 192 | |||
| 193 | You can pass a Perl datastructure to the row and it will be stored as JSON: | ||
| 194 | |||
| 195 | $resultset->create( { | ||
| 196 | id => 123, | ||
| 197 | args => { | ||
| 198 | some => 'data', | ||
| 199 | more => [1,1,2,3,5,8] | ||
| 200 | } | ||
| 201 | }); | ||
| 202 | # will be stored as '{"some":"data","more":[1,1,2,3,5,8]}' | ||
| 203 | |||
| 204 | You can also access the data directly as a Perl hash: | ||
| 205 | |||
| 206 | my $row = $resultset->find(123); | ||
| 207 | $row->args->{some}; # 'data' | ||
| 208 | $row->args->{more}[5]; # 8 | ||
| 209 | |||
| 210 | =head3 fixed_class | ||
| 211 | |||
| 212 | If plain JSON is to wobbly for you, you can define Moose objects and | ||
| 213 | have them serialized to JSON. Not only can you now add some custom | ||
| 214 | methods to the objects, but you can (ab)use the object initalisation | ||
| 215 | and all features Moose provides to define your objects. | ||
| 216 | |||
| 217 | TODO ->fixed_class(..) | ||
| 218 | |||
| 219 | Just pass an object to the row and it will be stored as JSON: | ||
| 220 | |||
| 221 | TODO | ||
| 222 | |||
| 223 | And you get back the initated object: | ||
| 224 | |||
| 225 | TODO | ||
| 226 | |||
| 227 | =head3 class_in_column | ||
| 228 | |||
| 229 | Sometimes you have a set of similar objects you want to store. TODO: explain oe1.article.paragraph | ||
| 230 | |||
| 231 | TODO ->class_in_column(..) | ||
| 232 | |||
| 233 | You can pass an object to the row and the infered type will be stored together with the JSON-payload: | ||
| 234 | |||
| 235 | TODO | ||
| 236 | |||
| 237 | To get back the object: | ||
| 238 | |||
| 239 | TODO | ||
| 240 | |||
| 241 | =head1 THANKS | ||
| 242 | |||
| 243 | =over | ||
| 244 | |||
| 245 | =item * | ||
| 246 | |||
| 247 | Parts of this code were orginally developed for L<validad.com|https://www.validad.com/> and released as Open Source. | ||
| 248 | |||
| 249 | =item * L<Maroš Kollár|https://metacpan.org/author/MAROS> wrote the prototype of C<class_in_column>, orginally developed for L<http://oe1.orf.at>. | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =head1 AUTHOR | ||
| 254 | |||
| 255 | Thomas Klausner <domm@cpan.org> | ||
| 256 | |||
| 257 | =head1 COPYRIGHT AND LICENSE | ||
| 258 | |||
| 259 | This software is copyright (c) 2017 by Thomas Klausner. | ||
| 260 | |||
| 261 | This is free software; you can redistribute it and/or modify it under | ||
| 262 | the same terms as the Perl 5 programming language system itself. | ||
| 263 | |||
| 264 | =cut |