Rails ActiveRecord BigNum to JSON

Posted by Jon Hoffman on Stack Overflow See other posts from Stack Overflow or by Jon Hoffman
Published on 2009-07-28T20:56:11Z Indexed on 2010/03/28 19:03 UTC
Read the original article Hit count: 408

Filed under:
|
|
|

Hi,

I am serializing an ActiveRecord model in rails 2.3.2 to_json and have noticed that BigNum values are serialized to JSON without quotes, however, javascript uses 64 bits to represent large numbers and only ~52(?) of those bits are available for the integer part, the rest are for the exponent.

So my 17 digit numbers become rounded off, grrr.

Try the following in the Firebug console:

console.log(123456789012345678)

So, I'm thinking that the json encoder should be smart enough to quote numbers that are too big for the javascript engines to handle. How do I fix up rails to do that? Or, is there a way to override the encoding for a single property on the model (I don't want to_s elsewhere)?

Thanks.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails