View | Details | Raw Unified | Return to bug 17932
Collapse All | Expand All

(-)a/Koha/Object.pm (+55 lines)
Lines 21-26 package Koha::Object; Link Here
21
use Modern::Perl;
21
use Modern::Perl;
22
22
23
use Carp;
23
use Carp;
24
use Mojo::JSON;
24
25
25
use Koha::Database;
26
use Koha::Database;
26
use Koha::Exceptions::Object;
27
use Koha::Exceptions::Object;
Lines 190-195 sub unblessed { Link Here
190
    return { $self->_result->get_columns };
191
    return { $self->_result->get_columns };
191
}
192
}
192
193
194
=head3 $object->TO_JSON
195
196
Returns an unblessed representation of the object, suitable for JSON output.
197
198
=cut
199
200
sub TO_JSON {
201
202
    my ($self) = @_;
203
    my $unblessed = $self->unblessed;
204
205
    return unless $unblessed;
206
207
    my $columns = Koha::Database->new->schema->resultset( $self->_type )
208
        ->result_source->{_columns};
209
    foreach my $col ( keys %{$columns} ) {
210
211
        if ( $columns->{$col}->{is_boolean} ) { # Handle booleans gracefully
212
            $unblessed->{$col}
213
                = ( $unblessed->{$col} )
214
                ? Mojo::JSON->true
215
                : Mojo::JSON->false;
216
        }
217
        elsif ( _numeric_column_type($columns->{$col}->{data_type}) ) {
218
            # TODO: Remove once the solution for
219
            # https://rt.cpan.org/Ticket/Display.html?id=119904
220
            # is ported to whatever distro we support by that time
221
            $unblessed->{$col} += 0;
222
        }
223
    }
224
    return $unblessed;
225
}
226
227
sub _numeric_column_type {
228
    # TODO: Remove once the solution for
229
    # https://rt.cpan.org/Ticket/Display.html?id=119904
230
    # is ported to whatever distro we support by that time
231
    my ($column_type) = @_;
232
233
    my @numeric_types = (
234
        'bigint',
235
        'integer',
236
        'int',
237
        'mediumint',
238
        'smallint',
239
        'tinyint',
240
        'decimal',
241
        'double precision',
242
        'float'
243
    );
244
245
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
246
}
247
193
=head3 $object->_result();
248
=head3 $object->_result();
194
249
195
Returns the internal DBIC Row object
250
Returns the internal DBIC Row object
(-)a/Koha/Objects.pm (-1 / +12 lines)
Lines 252-257 sub unblessed { Link Here
252
    return [ map { $_->unblessed } $self->as_list ];
252
    return [ map { $_->unblessed } $self->as_list ];
253
}
253
}
254
254
255
=head3 Koha::Objects->TO_JSON
256
257
Returns an unblessed representation of objects, suitable for JSON output.
258
259
=cut
260
261
sub TO_JSON {
262
    my ($self) = @_;
263
264
    return [ map { $_->TO_JSON } $self->as_list ];
265
}
266
255
=head3 Koha::Objects->_wrap
267
=head3 Koha::Objects->_wrap
256
268
257
wraps the DBIC object in a corresponding Koha object
269
wraps the DBIC object in a corresponding Koha object
258
- 

Return to bug 17932