class Marten::DB::Field::JSON

Overview

Represent's a JSON field.

JSON model fields allow to automatically parse JSON columns and expose the corresponding JSON::Any object, which is the default behavior:

class MyModel < Marten::Model
  # Other fields...
  field :metadata, :json
end

It should be noted that it is also possible to specify a serializable option in order to specify a class that makes use of JSON::Serializable. When doing so, the parsing of the JSON values will result in the initialization of the corresponding serializable objects:

class MySerializable
  include JSON::Serializable

  property a : Int32 | Nil
  property b : String | Nil
end

class MyModel < Marten::Model
  # Other fields...
  field :metadata, :json, serializable: MySerializable
end

Defined in:

marten/db/field.cr
marten/db/field/json.cr

Constructors

Instance Method Summary

Instance methods inherited from class Marten::DB::Field::Base

blank? : Bool blank?, db_column db_column, db_column! : ::String db_column!, db_column? db_column?, default default, empty_value?(value) : ::Bool empty_value?, from_db(value) from_db, from_db_result_set(result_set : ::DB::ResultSet) from_db_result_set, getter_value?(value) : ::Bool getter_value?, id : String id, index? : Bool index?, null? : Bool null?, prepare_save(record, new_record = false) prepare_save, primary_key? : Bool primary_key?, related_model related_model, relation? relation?, relation_name relation_name, to_column : Management::Column::Base | Nil to_column, to_db(value) : ::DB::Any to_db, truthy_value?(value) truthy_value?, unique? : Bool unique?, validate(record, value) validate

Constructor methods inherited from class Marten::DB::Field::Base

new(id : ::String, primary_key : ::Bool = false, blank : ::Bool = false, null : ::Bool = false, unique : ::Bool = false, index : ::Bool = false, db_column : ::String | Symbol | Nil = nil) new

Constructor Detail

def self.new(id : ::String, primary_key = false, default : ::JSON::Any | ::JSON::Serializable | Nil = nil, blank = false, null = false, unique = false, index = false, db_column = nil, **kwargs) #

[View source]

Instance Method Detail

def default : JSON::Any | JSON::Serializable | Nil #
Description copied from class Marten::DB::Field::Base

Returns the default value of the field if any.


[View source]
def from_db(value) : ::String | Nil #
Description copied from class Marten::DB::Field::Base

Converts the raw DB value to the corresponding field value.


[View source]
def from_db_result_set(result_set : ::DB::ResultSet) : ::String | Nil #
Description copied from class Marten::DB::Field::Base

Extracts the field value from a DB result set and returns the right object corresponding to this value.


[View source]
def to_column : Management::Column::Base | Nil #
Description copied from class Marten::DB::Field::Base

Returns a migration column object corresponding to the field at hand.


[View source]
def to_db(value) : ::DB::Any #
Description copied from class Marten::DB::Field::Base

Converts the field value to the corresponding DB value.


[View source]