class ActiveRecord::Type::Json

Public Instance Methods

type() click to toggle source
# File lib/active_record/type/json.rb, line 14
def type
  :json
end
type_cast_for_database(value) click to toggle source
# File lib/active_record/type/json.rb, line 18
def type_cast_for_database(value)
  case value
  when Array, Hash
    value.to_json
  when ::String, nil
    value
  else
    raise ArgumentError, "Invalid data type for JSON serialization: #{value.class} (only Hash/Array/nil supported)"
  end
end

Private Instance Methods

cast_value(value) click to toggle source
# File lib/active_record/type/json.rb, line 31
def cast_value(value)
  if value.is_a?(::String)
    JSON.parse(value)
  else
    raise "Unexpected JSON data type when loading from the database: #{value.class}"
  end
end