views:

213

answers:

1

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.

A: 

I've come up with a working solution based on another posting: http://stackoverflow.com/questions/1017416/array-of-activerecords-to-json/1040575#1040575

  def to_json(options = {})
     hash = Serializer.new(self, options).serializable_record.reject {|key, value| value.nil? || key == "big_num_field"}
     hash[:big_num_field] = big_num_field.to_s
     hash.to_json
   end

This is still not optimal, as I would rather have the serializer handle automatically.

Jon Hoffman