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

(-)a/Koha/Object.pm (+56 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
204
    my $unblessed    = $self->unblessed;
205
    my $columns_info = Koha::Database->new->schema->resultset( $self->_type )
206
        ->result_source->{_columns};
207
208
    foreach my $col ( keys %{$columns_info} ) {
209
210
        if ( $columns_info->{$col}->{is_boolean} )
211
        {    # Handle booleans gracefully
212
            $unblessed->{$col}
213
                = ( $unblessed->{$col} )
214
                ? Mojo::JSON->true
215
                : Mojo::JSON->false;
216
        }
217
        elsif ( _numeric_column_type( $columns_info->{$col}->{data_type} ) ) {
218
219
            # TODO: Remove once the solution for
220
            # https://rt.cpan.org/Ticket/Display.html?id=119904
221
            # is ported to whatever distro we support by that time
222
            $unblessed->{$col} += 0;
223
        }
224
    }
225
    return $unblessed;
226
}
227
228
sub _numeric_column_type {
229
    # TODO: Remove once the solution for
230
    # https://rt.cpan.org/Ticket/Display.html?id=119904
231
    # is ported to whatever distro we support by that time
232
    my ($column_type) = @_;
233
234
    my @numeric_types = (
235
        'bigint',
236
        'integer',
237
        'int',
238
        'mediumint',
239
        'smallint',
240
        'tinyint',
241
        'decimal',
242
        'double precision',
243
        'float'
244
    );
245
246
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
247
}
248
193
=head3 $object->_result();
249
=head3 $object->_result();
194
250
195
Returns the internal DBIC Row object
251
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