abstract class Serializer::Serializable

Overview

Base abstract superclass for serialization.

Extended Modules

Direct Known Subclasses

Defined in:

serializer/serializable.cr

Instance Method Summary

Instance Method Detail

def serialize(except : Array(Symbol) = [] of ::Symbol, includes : Array(Symbol) | Hash = [] of ::Symbol, opts : Hash | Nil = nil, meta : Hash(Symbol, MetaAny) | Nil = nil) #

Generates a JSON formatted string.

Arguments:

  • except - array of fields should be excluded from serialization;
  • includes - definition of relation that should be included into serialized string;
  • opts - options that will be passed to methods defined for if attribute options and .meta;
  • meta - meta attributes to be added under "meta" key at root level; it is merge into default meta attributes returned by .meta.
ModelSerializer.new(object).serialize(
  except: [:own_field],
  includes: {
    :children => {:address => nil, :dipper => [:address]},
  },
  meta: {:page => 0}
)

Includes

includes option accepts Array or Hash values. To define just a list of association of target object - just pass an array:

ModelSerializer.new(object).serialize(includes: [:children])

You can also specify deeper and more sophisticated schema by passing Hash. In this case hash values should be of Array(Symbol) | Hash | Nil type. nil is used to mark association which name is used for key as a leaf in schema tree.


[View source]
abstract def serialize_attributes(target, io, except, opts) #

Serializes target's attributes to io.


[View source]
abstract def serialize_relations(target, fields_count, io, includes, opts) #

Serializes target's relations to io.


[View source]