Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renderer calls object.to_json when rendering :json => object [#5655 s…
…tate:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
- Loading branch information
Showing
2 changed files
with
14 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72f37bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we were trying to get rid of to_json entirely? (or as much as possible at least)
72f37bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the problem with to_json?
72f37bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSON gem, ActiveSupport and yajl-ruby's JSON gem compatibility API all override that method. If someone includes 'yajl/json_gem' after ActiveSupport has been loaded, the above code will work but not the way the caller expects since the options hash for ActiveSupport's version of to_json is completely different than that of the one in yajl-ruby's JSON gem compatibility API.
I was under the assumption as_json was added to help get around this by allowing one to provide a JSON encodable version of their object using primitives that are directly mappable to JSON from Ruby, and the other way around. Then to actually encode the object, it would be passed through ActiveSupport::JSON.encode which has no chance of being overridden elsewhere and as such would have consistent behavior everywhere.
Rails actually has a hack for this from long-standing issues in the past with the JSON gem and ActiveSupport both overriding to_json.