crack
crack copied to clipboard
Date/time conversion is inconsistent with other JSON libraries
I know support for the ISO 8601 time format was added a while back, but it seems to be inconsistent with how everyone else parses stuff. Observe:
>> require 'activesupport'
>> require 'crack/json'
>> require 'json'
>> str = "{\"last_activated_at\":\"2010-01-01T00:00:00Z\"}"
=> "{\"last_activated_at\":\"2010-01-01T00:00:00Z\"}"
>> ActiveSupport::JSON.decode(str)
=> {"last_activated_at"=>"2010-01-01T00:00:00Z"}
>> JSON.parse(str)
=> {"last_activated_at"=>"2010-01-01T00:00:00Z"}
>> Crack::JSON.parse(str)
=> {"last_activated_at"=>Fri Jan 01 00:00:00 UTC 2010}
That's activesupport 2.3.8, json 1.4.6, crack 0.1.8. Here's yajl-ruby 0.7.8:
>> require 'yajl'
=> []
>> Yajl::Parser.new.parse(str)
=> {"last_activated_at"=>"2010-01-01T00:00:00Z"}
And here's the default behavior of activesupport 3.0.1:
ruby-1.8.7-p174 > ActiveSupport::JSON.decode(str)
=> {"last_activated_at"=>"2010-01-01T00:00:00Z"}
So, I'm not sure who's right here. Well, actually Crack is probably right -- honestly it surprises me that no other parser recognizes this time format, unless it's not in the JSON spec or something. However, that's the reality.
Here's the context. I'm trying to use Webmock to test some HTTP requests in our Rails app. Data from the database is being sent across the wire as JSON, so it's being JSONized by ActiveSupport. However, Webmock uses Crack (presumably to be framework agnostic) to compare what's being sent. Even though the expected data is the exact same as the actual data, Webmock doesn't see it that way, because Crack is converting something ActiveSupport isn't.
So, I can certainly get around this, but it kinda bothers me. Not only are all the other parsers in agreement, if Crack is just borrowing code from Rails -- and surely whoever wrote the JSON code in Rails consulted other JSON parsers and/or the spec -- I'm not sure there's a reason Crack should make up its own rules (even if it's actually an improvement). At the same time, I realize it's tough, because at this point people are probably relying on what Crack does now.
Thoughts/ideas?