aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorParker Selbert <parker@sorentwo.com>2013-07-16 16:16:33 -0400
committerAndrew White <andyw@pixeltrix.co.uk>2014-01-26 21:25:54 +0000
commit4cfc467594da86090efa63f1852fb82df9458c2b (patch)
treedbc771cf9747b324fe5614b569ff687d745de810
parente1e17a55d246d485f546766606580e8e4919442b (diff)
downloadrails-4cfc467594da86090efa63f1852fb82df9458c2b.tar.gz
rails-4cfc467594da86090efa63f1852fb82df9458c2b.tar.bz2
rails-4cfc467594da86090efa63f1852fb82df9458c2b.zip
Customize subsecond digits when encoding DateWithTime
The subsecond fraction digits had been hardcoded to 3. This forced all timestamps to include the subsecond digits with no way to customize the value. While the subsecond format is part of the ISO8601 spec, it is not adhered to by all parsers (notably mobile clients). This adds the ability to customize the number of digits used, optionally setting them to 0 in order to eliminate the subsecond fraction entirely: ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 0
-rw-r--r--activesupport/lib/active_support/json/encoding.rb7
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb3
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb29
3 files changed, 30 insertions, 9 deletions
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index 2859075e10..23896316e2 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -4,6 +4,7 @@ require 'active_support/core_ext/module/delegation'
module ActiveSupport
class << self
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
+ :subsecond_fraction_digits, :subsecond_fraction_digits=,
:escape_html_entities_in_json, :escape_html_entities_in_json=,
:encode_big_decimal_as_string, :encode_big_decimal_as_string=,
:json_encoder, :json_encoder=,
@@ -60,7 +61,7 @@ module ActiveSupport
end
# Mark these as private so we don't leak encoding-specific constructs
- private_constant :ESCAPED_CHARS, :ESCAPE_REGEX_WITH_HTML_ENTITIES,
+ private_constant :ESCAPED_CHARS, :ESCAPE_REGEX_WITH_HTML_ENTITIES,
:ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, :EscapedString
# Convert an object into a "JSON-ready" representation composed of
@@ -105,6 +106,10 @@ module ActiveSupport
# 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 encoder used by Rails to encode Ruby objects into JSON strings
# in +Object#to_json+ and +ActiveSupport::JSON.encode+.
attr_accessor :json_encoder
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index d47aee00f4..26d4e686fd 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -154,7 +154,8 @@ module ActiveSupport
# # => "2005/02/01 05:15:10 -1000"
def as_json(options = nil)
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
- xmlschema(3)
+ digits = ActiveSupport::JSON::Encoding.subsecond_fraction_digits || 3
+ xmlschema(digits)
else
%(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
end
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 43fe572bfc..4ea8d2bed6 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -67,17 +67,25 @@ class TimeWithZoneTest < ActiveSupport::TestCase
end
def test_to_json_with_use_standard_json_time_format_config_set_to_false
- old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, false
- assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(@twz)
- ensure
- ActiveSupport.use_standard_json_time_format = old
+ with_standard_json_time_format(false) do
+ assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(@twz)
+ end
end
def test_to_json_with_use_standard_json_time_format_config_set_to_true
- old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, true
- assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(@twz)
+ with_standard_json_time_format(true) do
+ assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(@twz)
+ end
+ end
+
+ def test_to_json_with_custom_subsecond_resolution
+ with_standard_json_time_format(true) do
+ ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 0
+
+ assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(@twz)
+ end
ensure
- ActiveSupport.use_standard_json_time_format = old
+ ActiveSupport::JSON::Encoding.subsecond_fraction_digits = nil
end
def test_to_json_when_wrapping_a_date_time
@@ -814,6 +822,13 @@ class TimeWithZoneTest < ActiveSupport::TestCase
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
+
+ def with_standard_json_time_format(boolean = true)
+ old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
+ yield
+ ensure
+ ActiveSupport.use_standard_json_time_format = old
+ end
end
class TimeWithZoneMethodsForTimeAndDateTimeTest < ActiveSupport::TestCase