Ruby 4.1 リファレンスマニュアル

instance method Exception#to_json

to_json(*args) -> StringRuby 1.9.3 から[permalink][rdoc][edit] [added by json/add/exception]

自身を JSON 形式の文字列に変換して返します。

内部的にはハッシュにデータをセットしてから JSON::Generator::GeneratorMethods::Hash#to_json を呼び出しています。

[PARAM] args:
引数はそのまま JSON::Generator::GeneratorMethods::Hash#to_json に渡されます。
require "json/add/core"

begin
  0/0
rescue => e
  e.to_json # => "{\"json_class\":\"ZeroDivisionError\",\"m\":\"divided by 0\",\"b\":[\"/path/to/test.rb:4:in '/'\",\"/path/to/test.rb:4:in '<main>'\"]}"
end

[SEE_ALSO] JSON::Generator::GeneratorMethods::Hash#to_json