Skip to content
Browse files

Add support for JSON time_precision to Time and DateTime

  • Loading branch information...
1 parent 9484f4b commit c0965004486f2ea5a9656ba718a3377c9614f97d @pixeltrix pixeltrix committed Jan 26, 2014
View
8 activesupport/lib/active_support/core_ext/object/json.rb
@@ -16,12 +16,12 @@
# otherwise they will always use to_json gem implementation, which is backwards incompatible in
# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
-#
+#
# On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
# JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
# passes a ::JSON::State object as the only argument to to_json, we can detect that and forward the
# calls to the original to_json method.
-#
+#
# It should be noted that when using ::JSON.{generate,dump} directly, ActiveSupport's encoder is
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
@@ -163,7 +163,7 @@ def as_json(options = nil) #:nodoc:
class Time
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
- xmlschema(3)
+ xmlschema(ActiveSupport::JSON::Encoding.time_precision)
else
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
end
@@ -183,7 +183,7 @@ def as_json(options = nil) #:nodoc:
class DateTime
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
- xmlschema(3)
+ xmlschema(ActiveSupport::JSON::Encoding.time_precision)
else
strftime('%Y/%m/%d %H:%M:%S %z')
end
View
1 activesupport/lib/active_support/json/encoding.rb
@@ -166,6 +166,7 @@ def const_missing(name)
self.use_standard_json_time_format = true
self.escape_html_entities_in_json = true
self.json_encoder = JSONGemEncoder
+ self.time_precision = 3
end
end
end
View
3 activesupport/lib/active_support/time_with_zone.rb
@@ -154,8 +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.time_precision || 3
- xmlschema(digits)
+ xmlschema(ActiveSupport::JSON::Encoding.time_precision)
else
%(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
end
View
20 activesupport/test/json/encoding_test.rb
@@ -474,7 +474,25 @@ def test_twz_to_json_with_custom_time_precision
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
end
ensure
- ActiveSupport::JSON::Encoding.time_precision = nil
+ ActiveSupport::JSON::Encoding.time_precision = 3
+ end
+
+ def test_time_to_json_with_custom_time_precision
+ with_standard_json_time_format(true) do
+ ActiveSupport::JSON::Encoding.time_precision = 0
+ assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
+ end
+ ensure
+ ActiveSupport::JSON::Encoding.time_precision = 3
+ end
+
+ def test_datetime_to_json_with_custom_time_precision
+ with_standard_json_time_format(true) do
+ ActiveSupport::JSON::Encoding.time_precision = 0
+ assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
+ end
+ ensure
+ ActiveSupport::JSON::Encoding.time_precision = 3
end
def test_twz_to_json_when_wrapping_a_date_time

2 comments on commit c096500

@kreeger
kreeger commented on c096500 Feb 4, 2014

Thank you for this. I wish this had been in 28ab79d.

@robmathews

We just discovered that 28ab79d broke our mobile app (the extra digits cause the date parser to blow up), and are busy monkey patching rails as suggested. So, thanks for this.

Please sign in to comment.
Something went wrong with that request. Please try again.