class

Marten::DB::Field::JSON

Inherits Marten::DB::Field::Base / Reference / Object

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

Constructors

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)
Source

Instance methods

default

Returns the default value of the field if any.

Source
from_db(value) : ::String | Nil

Converts the raw DB value to the corresponding field value.

Source
from_db_result_set(result_set : ::DB::ResultSet) : ::String | Nil

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

Source
to_column

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

Source
to_db(value) : ::DB::Any

Converts the field value to the corresponding DB value.

Source