class method
JSON.[]
(untyped object, ?::JSON::options opts) -> untyped
JSON[object] → new_array or new_string
ext/json/lib/json/common.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ext/json/lib/json/common.rb, line 132
def [](object, opts = nil)
if object.is_a?(String)
return JSON.parse(object, opts)
elsif object.respond_to?(:to_str)
str = object.to_str
if str.is_a?(String)
return JSON.parse(str, opts)
end
end
JSON.generate(object, opts)
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]'