Jerome
Jerome

Reputation: 6217

rails to_json quoting decimal values

(update) An array of geocoordinates, built from a collection of records

[{"point_lon"=>0.1307336132e3, "point_lat"=>-0.252978933e2, "title"=>"kata tjuta"}, 
{"point_lon"=>0.154984876e3, "point_lat"=>-0.17e2, "title"=>"error case"}, 
{"point_lon"=>0.1310369614747e3, "point_lat"=>-0.253455545e2, "title"=>"uluru"}]

has proper quoting structure, but for JSON input to a javascript needs the rockets to be replaced by a colon.

Transforming the array via JSON.generate or to_json unfortunately leads to quoting of the decimal values and being ignored by the javascript

[{"point_lon":"130.7336132","point_lat":"-25.2978933","point_name":"kata tjuta"},
{"point_lon":"154.984876","point_lat":"-17.0","point_name":"error case"},
{"point_lon":"131.0369614747","point_lat":"-25.3455545","point_name":"uluru"}] 

How can this array be transformed without quoting decimals?

Upvotes: 1

Views: 1105

Answers (1)

user000001
user000001

Reputation: 33367

This is because you use decimal numbers instead of float, so rails quotes the strings to preserve the precision. You can find methods to avoid this in this related question: Rails JSON Serialization of Decimal adds Quotes

Upvotes: 2

Related Questions