Skip to content

Commit

Permalink
Standardize all JSON encoded times to use 3 decimal fractional seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
ersatzryan committed Nov 7, 2013
1 parent 0fca7d6 commit a64d4e8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fix ActiveSupport `Time#to_json` and `DateTime#to_json` to return 3 decimal
places worth of fractional seconds, similar to `TimeWithZone`.

*Ryan Glover*

* Removed circular reference protection in JSON encoder, deprecated * Removed circular reference protection in JSON encoder, deprecated
ActiveSupport::JSON::Encoding::CircularReferenceError. ActiveSupport::JSON::Encoding::CircularReferenceError.


Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/core_ext/object/json.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def encode_json(encoder) #:nodoc:
class Time class Time
def as_json(options = nil) #:nodoc: def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format if ActiveSupport.use_standard_json_time_format
xmlschema xmlschema(3)
else else
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}) %(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
end end
Expand All @@ -200,7 +200,7 @@ def as_json(options = nil) #:nodoc:
class DateTime class DateTime
def as_json(options = nil) #:nodoc: def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format if ActiveSupport.use_standard_json_time_format
xmlschema xmlschema(3)
else else
strftime('%Y/%m/%d %H:%M:%S %z') strftime('%Y/%m/%d %H:%M:%S %z')
end end
Expand Down
6 changes: 3 additions & 3 deletions activesupport/test/json/encoding_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def as_json(options={})
DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]


StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]] StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]]
StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10Z") ]] StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10.000Z") ]]
StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]] StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10.000+00:00") ]]
StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]] StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]]


def sorted_json(json) def sorted_json(json)
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_time_to_json_includes_local_offset
prev = ActiveSupport.use_standard_json_time_format prev = ActiveSupport.use_standard_json_time_format
ActiveSupport.use_standard_json_time_format = true ActiveSupport.use_standard_json_time_format = true
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
assert_equal %("2005-02-01T15:15:10-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10)) assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
end end
ensure ensure
ActiveSupport.use_standard_json_time_format = prev ActiveSupport.use_standard_json_time_format = prev
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/message_encryptor_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_alternative_serialization_method
ActiveSupport.use_standard_json_time_format = true ActiveSupport.use_standard_json_time_format = true
encryptor = ActiveSupport::MessageEncryptor.new(SecureRandom.hex(64), SecureRandom.hex(64), :serializer => JSONSerializer.new) encryptor = ActiveSupport::MessageEncryptor.new(SecureRandom.hex(64), SecureRandom.hex(64), :serializer => JSONSerializer.new)
message = encryptor.encrypt_and_sign({ :foo => 123, 'bar' => Time.utc(2010) }) message = encryptor.encrypt_and_sign({ :foo => 123, 'bar' => Time.utc(2010) })
exp = { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" } exp = { "foo" => 123, "bar" => "2010-01-01T00:00:00.000Z" }
assert_equal exp, encryptor.decrypt_and_verify(message) assert_equal exp, encryptor.decrypt_and_verify(message)
ensure ensure
ActiveSupport.use_standard_json_time_format = prev ActiveSupport.use_standard_json_time_format = prev
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/message_verifier_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_alternative_serialization_method
ActiveSupport.use_standard_json_time_format = true ActiveSupport.use_standard_json_time_format = true
verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!", :serializer => JSONSerializer.new) verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!", :serializer => JSONSerializer.new)
message = verifier.generate({ :foo => 123, 'bar' => Time.utc(2010) }) message = verifier.generate({ :foo => 123, 'bar' => Time.utc(2010) })
exp = { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" } exp = { "foo" => 123, "bar" => "2010-01-01T00:00:00.000Z" }
assert_equal exp, verifier.verify(message) assert_equal exp, verifier.verify(message)
ensure ensure
ActiveSupport.use_standard_json_time_format = prev ActiveSupport.use_standard_json_time_format = prev
Expand Down

0 comments on commit a64d4e8

Please sign in to comment.