Monday, July 20, 2009

How do you Encode JSON Dates?

‹prev | My Chain | next›

I continue the work today of resolving my failing Cucumber scenarios associated with the navigation between meals. After roughly two minutes of investigation, I come to the following conclusion...

"Ah, nuts!"

I have not been as disciplined as I ought to be with my dates. I have a mixture of two different date formats—"2009-07-20" and "2009/07/20". Rails, it seems, outputs the latter, by default, when Date.today.to_json is invoked. I prefer the pure ISO 8601 formatting of the former.

Sooo....

I need to enforce JSON output in the legacy Rails app and redo the import. In more recent versions of Rails, JSON date formatting is controlled by setting ActiveSupport::JSON::Encoding.use_standard_json_time_format to true. In my legacy application, which is running 2.1.2, I need to set ActiveSupport.use_standard_json_time_format to true. That seems like an odd default. Why would something called "use standard" anything default to false? No matter, I am migrating away from Rails here, so I explicitly set this to a sane value.

After re-running the procedure from the other night, with proper JSON date formatting, I get:



Yay! ISO 8601 feels much better.

The switch back to ISO 8601 date representation not only makes me feel better, but it also resolves every one of my failing Cucumber scenarios:
cstrom@jaynestown:~/repos/eee-code$ cucumber features
...
27 scenarios
9 skipped steps
2 undefined steps
226 passed steps
Nice! All of my previous work was done with the assumption of ISO 8601. I allowed Rails' odd default to influence me over the last couple of days, the end result being non-ISO 8601 dates creeping into the application. Reversing course (including correcting some specs) resolves all.
(commit)

Up next: maybe a little more smoke testing and then onto finally adding CSS.

No comments:

Post a Comment