End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
JSON.[]
JSON[object] → new_array or new_string
ext/json/lib/json/common.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.2.0/ext/json/lib/json/common.rb, line 18
def [](object, opts = {})
if object.respond_to? :to_str
JSON.parse(object.to_str, opts)
else
JSON.generate(object, opts)
end
end
If object is a String, calls JSON.parse with object and opts (see method parse):
Example
json = '[0, 1, null]' JSON[json]# => [0, 1, nil]
Otherwise, calls JSON.generate with object and opts (see method generate):
Example
ruby = [0, 1, nil] JSON[ruby] # => '[0,1,null]'