Skip to content

Commit 2e5cb98

Browse files
committed
Fix AM::Serializers::JSON#as_json method for timestamps
According to doc the method should return non-json compatible types as strings.
1 parent 1118e2c commit 2e5cb98

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

activemodel/lib/active_model/serializers/json.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,29 @@ module JSON
2626
# user = User.find(1)
2727
# user.as_json
2828
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
29-
# # "created_at" => "2006/08/01", "awesome" => true}
29+
# # "created_at" => "2006-08-01T17:27:133.000Z", "awesome" => true}
3030
#
3131
# ActiveRecord::Base.include_root_in_json = true
3232
#
3333
# user.as_json
3434
# # => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
35-
# # "created_at" => "2006/08/01", "awesome" => true } }
35+
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
3636
#
3737
# This behavior can also be achieved by setting the <tt>:root</tt> option
3838
# to +true+ as in:
3939
#
4040
# user = User.find(1)
4141
# user.as_json(root: true)
4242
# # => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
43-
# # "created_at" => "2006/08/01", "awesome" => true } }
43+
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
4444
#
4545
# Without any +options+, the returned Hash will include all the model's
4646
# attributes.
4747
#
4848
# user = User.find(1)
4949
# user.as_json
5050
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
51-
# # "created_at" => "2006/08/01", "awesome" => true}
51+
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true}
5252
#
5353
# The <tt>:only</tt> and <tt>:except</tt> options can be used to limit
5454
# the attributes included, and work similar to the +attributes+ method.
@@ -63,14 +63,14 @@ module JSON
6363
#
6464
# user.as_json(methods: :permalink)
6565
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
66-
# # "created_at" => "2006/08/01", "awesome" => true,
66+
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true,
6767
# # "permalink" => "1-konata-izumi" }
6868
#
6969
# To include associations use <tt>:include</tt>:
7070
#
7171
# user.as_json(include: :posts)
7272
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
73-
# # "created_at" => "2006/08/01", "awesome" => true,
73+
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true,
7474
# # "posts" => [ { "id" => 1, "author_id" => 1, "title" => "Welcome to the weblog" },
7575
# # { "id" => 2, "author_id" => 1, "title" => "So I was thinking" } ] }
7676
#
@@ -81,7 +81,7 @@ module JSON
8181
# only: :body } },
8282
# only: :title } })
8383
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
84-
# # "created_at" => "2006/08/01", "awesome" => true,
84+
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true,
8585
# # "posts" => [ { "comments" => [ { "body" => "1st post!" }, { "body" => "Second!" } ],
8686
# # "title" => "Welcome to the weblog" },
8787
# # { "comments" => [ { "body" => "Don't think too hard" } ],
@@ -93,11 +93,12 @@ def as_json(options = nil)
9393
include_root_in_json
9494
end
9595

96+
hash = serializable_hash(options).as_json
9697
if root
9798
root = model_name.element if root == true
98-
{ root => serializable_hash(options) }
99+
{ root => hash }
99100
else
100-
serializable_hash(options)
101+
hash
101102
end
102103
end
103104

activemodel/test/cases/serializers/json_serialization_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def @contact.favorite_quote; "Constraints are liberating"; end
129129
assert_equal :name, options[:except]
130130
end
131131

132+
test "as_json should serialize timestamps" do
133+
assert_equal "2006-08-01T00:00:00.000Z", @contact.as_json["created_at"]
134+
end
135+
132136
test "as_json should return a hash if include_root_in_json is true" do
133137
begin
134138
original_include_root_in_json = Contact.include_root_in_json

0 commit comments

Comments
 (0)