Skip to content

Commit

Permalink
Respect JSON encoding customizations
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeclaghorn committed May 14, 2019
1 parent b20dfd8 commit e2e8623
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def merge!(hash_or_array)

# Encodes the current builder as JSON.
def target!
::JSON.dump(@attributes)
@attributes.to_json
end

private
Expand Down
7 changes: 7 additions & 0 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,11 @@ class JbuilderTest < ActiveSupport::TestCase
end
end
end

test "respects JSON encoding customizations" do
# Active Support overrides Time#as_json for custom formatting.
# Ensure we call #to_json on the final attributes instead of JSON.dump.
result = JSON.load(Jbuilder.new { |json| json.time Time.parse("2018-05-13 11:51:00.485 -0400") }.target!)
assert_equal "2018-05-13T11:51:00.485-04:00", result["time"]
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "active_support"
require "active_support/core_ext/array/access"
require "active_support/cache/memory_store"
require "active_support/json"
require "active_model"
require "action_view"
require "rails/version"
Expand Down

0 comments on commit e2e8623

Please sign in to comment.