module JSON::TruffleRuby::Generator::GeneratorMethods::Object

Public Instance Methods

to_json(state = nil, *) click to toggle source

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.

# File lib/json/truffle_ruby/generator.rb, line 455
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