Class: Castkit::DefaultSerializer

Inherits:
Serializer show all
Defined in:
lib/castkit/default_serializer.rb

Overview

Default serializer for Castkit::DataObject instances.

Serializes attributes into a plain Ruby hash, applying access rules, nil/blank filtering, and nested structure handling. The output format supports JSON-compatible structures and respects the class-level serialization configuration.

Instance Attribute Summary collapse

Attributes inherited from Serializer

#obj

Instance Method Summary collapse

Methods inherited from Serializer

call

Instance Attribute Details

#attributesHash{Symbol => Castkit::Attribute} (readonly)

Returns the attributes to serialize.

Returns:



13
14
15
# File 'lib/castkit/default_serializer.rb', line 13

def attributes
  @attributes
end

#optionsHash (readonly)

Returns serialization config flags like :root, :ignore_nil, :allow_unknown.

Returns:

  • (Hash)

    serialization config flags like :root, :ignore_nil, :allow_unknown



19
20
21
# File 'lib/castkit/default_serializer.rb', line 19

def options
  @options
end

#unknown_attributesHash{Symbol => Object} (readonly)

Returns unrecognized attributes captured during deserialization.

Returns:

  • (Hash{Symbol => Object})

    unrecognized attributes captured during deserialization



16
17
18
# File 'lib/castkit/default_serializer.rb', line 16

def unknown_attributes
  @unknown_attributes
end

Instance Method Details

#callHash

Serializes the object to a hash.

Includes unknown attributes if configured, and wraps in a root key if defined.

Returns:

  • (Hash)

    the fully serialized result



26
27
28
29
30
31
# File 'lib/castkit/default_serializer.rb', line 26

def call
  result = serialize_attributes
  result.merge!(unknown_attributes) if options[:allow_unknown]

  options[:root] ? { options[:root].to_sym => result } : result
end