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.

exception_class.to_json == "{\"inheritable_attributes\":{}}"

#3622

The way to_json treats classes is strange, and new in 2.3 (this worked as expected in 2.2):

StandardError.to_json == "{\"inheritable_attributes\":{}}"
StandardError.to_s == "StandardError"

However:

irb(main):001:0> require 'json'
=> true
irb(main):002:0> StandardError.to_json
=> "\"StandardError\""
irb(main):003:0> StandardError.to_s
=> "StandardError"

Reported by Greg Hazel · December 27th, 2009 @ 07:24 AM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • State changed from new to wontfix
    • Importance changed from to Low

    Hi,

    From your report, I think you are suggesting that Rails' to_json should have the same outcome as pure irb, right?

    I can confirm the above outcome on Rails 3.0.1. However, I don't think this is a bug, as this is the way Rails make sure that everything can be converted to JSON. Calling to_json on an object would actually calling to_hash or returning instance_values if it doesn't implements to_hash. Unintentionally, StandardError doesn't implements to_hash, and so you got the instance variables (or class variables, in this case) instead.

    October 17th, 2010 @ 05:09 PM

  2. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)

    One more thing,

    and you saw inheritable_attributes in the list of attributes because that's actually a hash storing the list of class's inheritable attributes.

    October 17th, 2010 @ 05:11 PM

  3. Greg Hazel
    Greg Hazel

    Why should Rails to_json work any differently than regular to_json for non-Rails objects?

    Also, as you can see from Marshal, there is no need to dump anything about inheritable_attributes in order to instantiate StandardError again.

    >> Marshal.dump(StandardError)
    => "\004\bc\022StandardError"
    >> Marshal.load(Marshal.dump(StandardError))
    => StandardError
    

    October 18th, 2010 @ 12:16 AM