Module: JSON::TruffleRuby::Generator::GeneratorMethods::Object
- Defined in:
- lib/json/truffle_ruby/generator.rb
Instance Method Summary collapse
-
#to_json(state = nil) ⇒ Object
Converts this object to a string (calling #to_s), converts it to a JSON string, and returns the result.
Instance Method Details
#to_json(state = nil) ⇒ Object
Converts this object to a string (calling #to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method #to_json was defined for some object.
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/json/truffle_ruby/generator.rb', line 468 def to_json(state = nil, *) state = State.from_state(state) if state if state&.strict? value = self if state.strict? && !Generator.native_type?(value) if state.as_json value = state.as_json.call(value, false) unless Generator.native_type?(value) raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value) end value.to_json(state) else raise GeneratorError.new("#{value.class} not allowed in JSON", value) end end else to_s.to_json end end |