Rails 3.0 to 3.1 gotcha: Changed ActiveSupport::JSON.backend by jgn on Thursday, October 6, 2011 in Rails, Ruby, and Code

Ow.

In Rails 3.0, the default ActiveSupport::JSON backend is YAML.

In Rails 3.1, it's YAJL.

They deserialize JSON times differently. In 3.0 . . .

[code language='ruby'] ruby-1.9.2-p290 :001 > ActiveSupport::JSON.decode( '{"at": "2011-10-13T12:00:00+00:00"}' ) => {"at"=>2011-10-13 07:00:00 -0500}

See the lack of quotation marks around what "at" points to? It's not a String. YAML converts the value to an actual DateTime. (There a DATE_REGEX defined in ActiveSupport::JSON that does the matching.

But in 3.1:

[code language='ruby'] ruby-1.9.2-p290 :001 > ActiveSupport::JSON.decode( '{"at": "2011-10-13T12:00:00+00:00"}' ) => {"at"=>"2011-10-13T12:00:00+00:00"}

We had the yajl-ruby gem installed for development, so were getting the the Strings. Then on our staging server, we were getting the already-parsed time value, and were getting errors.

This strikes me as a substantive change. I'm not sure I'd call it a bug, but it was unexpected.

comments powered by Disqus