Module: JSON::GeneratorMethods

Included in:
Object
Defined in:
lib/json/common.rb

Instance Method Summary collapse

Instance Method Details

#to_json(state = nil) ⇒ Object

call-seq: to_json(*)

Converts this object into a JSON string. If this object doesn’t directly maps to a JSON native type, first convert it to a string (calling #to_s), then 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.



1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/json/common.rb', line 1110

def to_json(state = nil, *)
  obj = case self
  when nil, false, true, Integer, Float, Array, Hash
    self
  else
    "#{self}"
  end

  if state.nil?
    JSON::State._generate_no_fallback(obj, nil, nil)
  else
    JSON::State.from_state(state)._generate_no_fallback(obj)
  end
end