[Dbix-class] Getting numeric columns as numeric values

Ben Tilly btilly at gmail.com
Wed Feb 25 22:49:42 GMT 2015


It gets a lot worse if the JSON is being consumed by a typed language
like ObjectiveC.  Data type issues can easily crash the client.  I
remember working with a mobile app backed by JSON data being emitted
from Moose, and it was a constant nightmare that a trivial change on
the Perl side would stringify data.  The resulting JSON would work
fine for us in AJAX calls, and the mobile client would crash.

You would think that declaring something to be numeric would be enough
of a hint for Moose to be able to emit JSON with numeric data.  But
you would think wrong...

On Wed, Feb 25, 2015 at 2:43 PM, Lasse Makholm <lasse at unity3d.com> wrote:
>
>
> On Wed, Feb 25, 2015 at 10:59 PM, Darren Duncan <darren at darrenduncan.net>
> wrote:
>>
>> Augustus, what is the problem with DBIC stringifying numeric values?  That
>> ensures full precision and in particular when you want to use them as
>> numbers you can just do so, Perl does that automatically. -- Darren Duncan
>
>
> One example of it being a problem is when converting row objects to JSON.
> Javascripts === operator, for example, evaluates to false for for 42 ===
> "42".
>
> /L
>
>
>>
>>
>> On 2015-02-25 1:15 PM, Augustus Saunders wrote:
>>>
>>> For reasons unknown to us, DBIx is stringifying numeric values somewhere
>>> along the way. In order to ensure they come out numeric, we made this small
>>> patch:
>>>
>>> --- a/lib/perl5/DBIx/Class/Row.pm
>>> +++ b/lib/perl5/DBIx/Class/Row.pm
>>> @@ -661,10 +661,15 @@ To retrieve all loaded column values as a hash, use
>>> L</get_columns>.
>>>   sub get_column {
>>>     my ($self, $column) = @_;
>>>     $self->throw_exception( "Can't fetch data as class method" ) unless
>>> ref $self;
>>> -  return $self->{_column_data}{$column} if exists
>>> $self->{_column_data}{$column};
>>> +  if (exists $self->{_column_data}{$column}) {
>>> +    return 0 + $self->{_column_data}{$column} if
>>> $self->_is_column_numeric($column);
>>> +    return $self->{_column_data}{$column};
>>> +  }
>>>     if (exists $self->{_inflated_column}{$column}) {
>>> -    return $self->store_column($column,
>>> +    my $ret = $self->store_column($column,
>>>         $self->_deflated_column($column,
>>> $self->{_inflated_column}{$column}));
>>> +    return 0 + $ret if $self->_is_column_numeric($column);
>>> +    return $ret;
>>>     }
>>>     $self->throw_exception( "No such column '${column}'" ) unless
>>> $self->has_column($column);
>>>     return undef;
>>>
>>> If there's a better way to do this, or some way to prevent DBIx from
>>> stringifying the values in the first place, that would be nice to know too.
>>> Thanks-
>>>
>>> Augustus
>>
>>
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive:
>> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk



More information about the DBIx-Class mailing list