Module: Lutaml::Xml::Adapter::XmlSerialization

Defined in:
lib/lutaml/xml/adapter/xml_serialization.rb

Overview

Shared XML serialization logic for all adapters

This module provides the common three-phase XML serialization architecture:

  1. Collect namespace needs from XmlElement tree

  2. Plan namespace declarations with hoisting

  3. Render using parallel traversal (XmlElement + DeclarationPlan)

All adapters follow the same pattern:

  • Case A: Parsed element (adapter-specific) → build_xml

  • Case B: XmlElement → build_xml_element_with_plan

  • Case C: Model instance → transform to XmlElement OR legacy path

Examples:

Include in adapter

class NokogiriAdapter < BaseAdapter
  include XmlSerialization

  private

  def build_xml_element_with_plan(xml, xml_element, plan, options)
    # Adapter-specific implementation
  end
end

Instance Method Summary collapse

Instance Method Details

#to_xml(options = {}) ⇒ String

Serialize to XML using three-phase architecture

Parameters:

  • options (Hash) (defaults to: {})

    Serialization options

Options Hash (options):

  • :mapper_class (Class)

    Model class for mapping lookup

  • :encoding (String)

    Character encoding

  • :declaration (Boolean)

    Include XML declaration

  • :pretty (Boolean)

    Pretty-print output

Returns:

  • (String)

    XML document



37
38
39
40
41
42
43
# File 'lib/lutaml/xml/adapter/xml_serialization.rb', line 37

def to_xml(options = {})
  # Accept xml_declaration from options if present (for model serialization)
  @xml_declaration = options[:xml_declaration] if options[:xml_declaration]

  # Build XML using adapter-specific builder
  build_xml_document(options)
end