diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 23896316e2313712ea0e97a2992c188fa8f98f6d..bafd62984846481bd7eb5ce0fe198c8275ed9053 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -4,7 +4,7 @@ module ActiveSupport class << self delegate :use_standard_json_time_format, :use_standard_json_time_format=, - :subsecond_fraction_digits, :subsecond_fraction_digits=, + :time_precision, :time_precision=, :escape_html_entities_in_json, :escape_html_entities_in_json=, :encode_big_decimal_as_string, :encode_big_decimal_as_string=, :json_encoder, :json_encoder=, @@ -106,9 +106,9 @@ class << self # as a safety measure. attr_accessor :escape_html_entities_in_json - # Configures the inclusion of subsecond resolution when serializing instances - # of ActiveSupport::TimeWithZone. - attr_accessor :subsecond_fraction_digits + # Sets the precision of encoded time values. + # Defaults to 3 (equivalent to millisecond precision) + attr_accessor :time_precision # Sets the encoder used by Rails to encode Ruby objects into JSON strings # in +Object#to_json+ and +ActiveSupport::JSON.encode+. diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 26d4e686fd443ceea0f4a6221c4b7811734327f4..1ed18fa473713437df601b53e74e11b0e0558535 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -154,7 +154,7 @@ def xmlschema(fraction_digits = 0) # # => "2005/02/01 05:15:10 -1000" def as_json(options = nil) if ActiveSupport::JSON::Encoding.use_standard_json_time_format - digits = ActiveSupport::JSON::Encoding.subsecond_fraction_digits || 3 + digits = ActiveSupport::JSON::Encoding.time_precision || 3 xmlschema(digits) else %(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}) diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index ef76d63f29bcdd11cd822b52804bf60418c6eb71..52d2708d1e026b4ff949cdfd1ed3c636a69f7d2b 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -466,15 +466,15 @@ def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true end end - def test_twz_to_json_with_custom_subsecond_resolution + def test_twz_to_json_with_custom_time_precision with_standard_json_time_format(true) do - ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 0 + ActiveSupport::JSON::Encoding.time_precision = 0 zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone) assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time) end ensure - ActiveSupport::JSON::Encoding.subsecond_fraction_digits = nil + ActiveSupport::JSON::Encoding.time_precision = nil end def test_twz_to_json_when_wrapping_a_date_time diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 4afd0600c4178ed82fc163b5577435860a7c0a2e..693991304b0b4de1e4f62ae9b2539eabf5860838 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -441,6 +441,8 @@ There are a few configuration options available in Active Support: * `config.active_support.use_standard_json_time_format` enables or disables serializing dates to ISO 8601 format. Defaults to `true`. +* `config.active_support.time_precision` sets the precision of JSON encoded time values. Defaults to `3`. + * `ActiveSupport::Logger.silencer` is set to `false` to disable the ability to silence logging in a block. The default is `true`. * `ActiveSupport::Cache::Store.logger` specifies the logger to use within cache store operations.