Skip to content

Commit c096500

Browse files
committed
Add support for JSON time_precision to Time and DateTime
1 parent 9484f4b commit c096500

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

activesupport/lib/active_support/core_ext/object/json.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
# otherwise they will always use to_json gem implementation, which is backwards incompatible in
1717
# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
1818
# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
19-
#
19+
#
2020
# On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
2121
# JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
2222
# passes a ::JSON::State object as the only argument to to_json, we can detect that and forward the
2323
# calls to the original to_json method.
24-
#
24+
#
2525
# It should be noted that when using ::JSON.{generate,dump} directly, ActiveSupport's encoder is
2626
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
2727
# 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:
163163
class Time
164164
def as_json(options = nil) #:nodoc:
165165
if ActiveSupport.use_standard_json_time_format
166-
xmlschema(3)
166+
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
167167
else
168168
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
169169
end
@@ -183,7 +183,7 @@ def as_json(options = nil) #:nodoc:
183183
class DateTime
184184
def as_json(options = nil) #:nodoc:
185185
if ActiveSupport.use_standard_json_time_format
186-
xmlschema(3)
186+
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
187187
else
188188
strftime('%Y/%m/%d %H:%M:%S %z')
189189
end

activesupport/lib/active_support/json/encoding.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def const_missing(name)
166166
self.use_standard_json_time_format = true
167167
self.escape_html_entities_in_json = true
168168
self.json_encoder = JSONGemEncoder
169+
self.time_precision = 3
169170
end
170171
end
171172
end

activesupport/lib/active_support/time_with_zone.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ def xmlschema(fraction_digits = 0)
154154
# # => "2005/02/01 05:15:10 -1000"
155155
def as_json(options = nil)
156156
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
157-
digits = ActiveSupport::JSON::Encoding.time_precision || 3
158-
xmlschema(digits)
157+
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
159158
else
160159
%(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
161160
end

activesupport/test/json/encoding_test.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,25 @@ def test_twz_to_json_with_custom_time_precision
474474
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
475475
end
476476
ensure
477-
ActiveSupport::JSON::Encoding.time_precision = nil
477+
ActiveSupport::JSON::Encoding.time_precision = 3
478+
end
479+
480+
def test_time_to_json_with_custom_time_precision
481+
with_standard_json_time_format(true) do
482+
ActiveSupport::JSON::Encoding.time_precision = 0
483+
assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
484+
end
485+
ensure
486+
ActiveSupport::JSON::Encoding.time_precision = 3
487+
end
488+
489+
def test_datetime_to_json_with_custom_time_precision
490+
with_standard_json_time_format(true) do
491+
ActiveSupport::JSON::Encoding.time_precision = 0
492+
assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
493+
end
494+
ensure
495+
ActiveSupport::JSON::Encoding.time_precision = 3
478496
end
479497

480498
def test_twz_to_json_when_wrapping_a_date_time

0 commit comments

Comments
 (0)