Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Unexpected behavior of render :json => obj

#5841

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.

Reported by johnnypez · October 19th, 2010 @ 05:19 PM

State: resolved
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    Can you elaborate on what your obj is ?

    October 19th, 2010 @ 05:35 PM

  2. johnnypez
    johnnypez

    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

    October 19th, 2010 @ 05:50 PM

  3. Neeraj Singh
    Neeraj Singh
    • 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.

    November 15th, 2010 @ 10:56 PM

  4. Repository
    Repository
    • 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 Unexpected behavior of render :json => obj state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    https://github.com/rails/rails/commit/fa2a5ae0339c90d023a7559e681a5...

    November 24th, 2010 @ 09:09 PM

  5. Repository
    Repository

    (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 Unexpected behavior of render :json => obj state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    https://github.com/rails/rails/commit/4e2a981bdd162dfd52b15f34b4e45...

    November 24th, 2010 @ 09:10 PM