Class: Lutaml::Xsd::Spa::SchemaSerializer Abstract

Inherits:
Object
  • Object
show all
Includes:
Utils::ExtractEnumeration
Defined in:
lib/lutaml/xsd/spa/schema_serializer.rb

Overview

This class is abstract.

Subclass and override template methods to customize serialization

Base class for schema serializers (Template Method Pattern)

Defines the overall algorithm for serializing XSD schema repositories into a format suitable for SPA documentation. Subclasses can override specific steps to customize the serialization process.

The serialization process follows these steps:

  1. Build metadata

  2. Serialize all schemas

  3. Build search index

  4. Assemble final structure

Examples:

Using the serializer

serializer = JsonSchemaSerializer.new(repository)
data = serializer.serialize
json = serializer.to_json

Constant Summary collapse

MERGEABLE_CONTENT_FIELDS =

Fields to merge when combining schemas with the same targetNamespace

%i[
  elements complex_types simple_types
  attributes groups attribute_groups
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::ExtractEnumeration

#extract_enumeration_default, #extract_enumeration_values

Constructor Details

#initialize(repository_or_package, config = {}) ⇒ SchemaSerializer

Initialize schema serializer

Parameters:



50
51
52
53
54
55
56
57
58
59
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 50

def initialize(repository_or_package, config = {})
  if repository_or_package.is_a?(SchemaRepositoryPackage)
    @package = repository_or_package
    @repository = repository_or_package.repository
  else
    @repository = repository_or_package
    @package = nil
  end
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



44
45
46
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 44

def config
  @config
end

#packageObject (readonly)

Returns the value of attribute package.



44
45
46
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 44

def package
  @package
end

#repositoryObject (readonly)

Returns the value of attribute repository.



44
45
46
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 44

def repository
  @repository
end

Instance Method Details

#serializeHash

Serialize repository to data structure (template method)

This method defines the overall algorithm for serialization. Subclasses can override individual steps as needed.

Returns:

  • (Hash)

    Serialized data structure



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 67

def serialize
   = 
  # Convert SpaMetadata model to hash for JSON serialization
  # SpaMetadata.to_hash returns string keys, but we need symbol keys for compatibility
   = if .respond_to?(:to_hash)
                    hash = .to_hash
                    # Convert string keys to symbols for backward compatibility
                    hash.to_h do |k, v|
                      [k.is_a?(String) ? k.to_sym : k, v]
                    end
                  elsif .respond_to?(:to_h)
                    .to_h
                  else
                    
                  end
  {
    metadata: ,
    schemas: serialize_schemas,
    namespaces: build_namespaces,
    index: build_index,
  }
end

#to_json(pretty: true) ⇒ String

Convert serialized data to JSON string

Parameters:

  • pretty (Boolean) (defaults to: true)

    Whether to pretty-print JSON

Returns:

  • (String)

    JSON string



94
95
96
97
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 94

def to_json(pretty: true)
  data = serialize
  pretty ? JSON.pretty_generate(data) : JSON.generate(data)
end