Module: Lutaml::Xml::TransformationSupport::ValueSerializer

Included in:
ElementBuilder
Defined in:
lib/lutaml/xml/transformation/value_serializer.rb

Overview

Module for serializing values to XML strings.

Handles value serialization including:

  • Boolean value_map mappings

  • as_list and delimiter for array values

  • Reference type serialization

  • Custom Value type serialization (to_xml)

  • Standard type serialization

Instance Method Summary collapse

Instance Method Details

#serialize_value(value, rule, model_class, register_id) ⇒ String?

Serialize a value to string for XML output

Parameters:

  • value (Object)

    The value to serialize

  • rule (CompiledRule)

    The rule containing serialization options

  • model_class (Class)

    The model class for attribute lookup

  • register_id (Symbol, nil)

    The register ID

Returns:

  • (String, nil)

    Serialized value or nil



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lutaml/xml/transformation/value_serializer.rb', line 22

def serialize_value(value, rule, model_class, register_id)
  return nil if value.nil?
  return nil if Lutaml::Model::Utils.uninitialized?(value)

  # Handle boolean value_map for true: :empty
  # This MUST be checked before the Value type wrapping below
  # When boolean true maps to :empty, serialize as empty string (<Active/>)
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
    result = serialize_boolean_value(value, rule)
    return result if result.is_a?(String) || result.nil?
  end

  # Handle as_list and delimiter for array values BEFORE serialization
  # These features convert arrays to delimited strings for XML attributes
  if value.is_a?(Array)
    value = serialize_array_value(value, rule)
  end

  # For Reference types, use attribute's serialize method
  attr = find_attribute(model_class, rule.attribute_name, register_id)
  if attr && attr.unresolved_type == Lutaml::Model::Type::Reference
    return attr.serialize(value, :xml, register_id, {})
  end

  # For custom Value types with instance methods (to_xml, to_json, etc.)
  # wrap the value and call the instance method
  # NOTE: Skip to_xml for content mappings - content should preserve original value
  # for round-trip scenarios. Only attributes should use custom serialization.
  is_content_mapping = rule.option(:mapping_type) == :content
  if !is_content_mapping && custom_value_type?(rule.attribute_type)
    result = serialize_custom_value(value, rule.attribute_type)
    return result unless result.nil?
  end

  # Use type's serialization if available
  if rule.attribute_type.respond_to?(:serialize)
    rule.attribute_type.serialize(value)
  else
    value.to_s
  end
end