This project is archived and is in readonly mode.
Unexpected behavior of render :json => obj
Reported by johnnypez | October 19th, 2010 @ 05:19 PM | in 3.x
Hi,
I hit this unexpected behaviour I call render :json =>
obj
in my controller and also using respond_with
obj
where JSON output is expected.
I have tracked this down to
http://github.com/rails/rails/blob/3-0-stable/actionpack/lib/action...
json = ActiveSupport::JSON.encode(json, options) unless json.respond_to?(:to_str)
So the object is not encoded to JSON if it responds to :to_str.
That doesn't make any sense to me.Comments and changes to this ticket
-
Neeraj Singh October 19th, 2010 @ 05:35 PM
- Importance changed from to Low
Can you elaborate on what your obj is ?
-
johnnypez October 19th, 2010 @ 05:50 PM
Hi,
sorry I left that out, long day.The object is completely basic subclass of Exception
class ApiException < Exception def to_json return { :error => self.class.name, :message => self.to_str }.to_json end end
Whenever the exception is raised, it is caught with this around_filter in my app controller:
#application_controller.rb respond_to :json around_filter :catch_api_errors def catch_api_errors begin yield rescue ApiException => e respond_with e, :status => 400 end end
I expect the output of this to be the result of
:to_json
being sent to the ApiException instance but instead I just the the value of:to_str
-
Neeraj Singh November 15th, 2010 @ 10:56 PM
- State changed from new to open
- Milestone set to 3.x
- Assigned user set to José Valim
- Tag changed from render json render_with to render json render_with, patched
Attached is patch with test.
Discussed this issue with Mr. Valim. Assigning it to him.
-
Repository November 24th, 2010 @ 09:09 PM
- State changed from open to resolved
(from [fa2a5ae0339c90d023a7559e681a588219dc3903]) If a user wants json output then try best to render json output. In such cases prefer kind_of(String) over respond_to?(to_str)
[#5841 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/fa2a5ae0339c90d023a7559e681a5... -
Repository November 24th, 2010 @ 09:10 PM
(from [4e2a981bdd162dfd52b15f34b4e45ba87726ff53]) If a user wants json output then try best to render json output. In such cases prefer kind_of(String) over respond_to?(to_str)
[#5841 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/4e2a981bdd162dfd52b15f34b4e45...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 5841 Unexpected behavior of render :json => obj [#5841 state:resolved]
- 5841 Unexpected behavior of render :json => obj [#5841 state:resolved]