Module: JSON::TruffleRuby::Generator::GeneratorMethods::Float

Defined in:
lib/json/truffle_ruby/generator.rb

Instance Method Summary collapse

Instance Method Details

#to_json(state = nil, *args) ⇒ Object

Returns a JSON string representation for this Float number.



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/json/truffle_ruby/generator.rb', line 579

def to_json(state = nil, *args)
  state = State.from_state(state)
  if infinite? || nan?
    if state.allow_nan?
      to_s
    elsif state.strict? && state.as_json
      casted_value = state.as_json.call(self)

      if casted_value.equal?(self)
        raise GeneratorError.new("#{self} not allowed in JSON", self)
      end

      state.check_max_nesting
      state.depth += 1
      result = casted_value.to_json(state, *args)
      state.depth -= 1
      result
    else
      raise GeneratorError.new("#{self} not allowed in JSON", self)
    end
  else
    to_s
  end
end