Class: Lutaml::Json::Adapter::MultiJsonAdapter

Inherits:
Document show all
Defined in:
lib/lutaml/json/adapter/multi_json_adapter.rb

Constant Summary collapse

INTERNAL_LUTAML_KEYS =
i[register adapter _adapter_override].freeze

Instance Attribute Summary

Attributes inherited from KeyValue::Document

#attributes, #register

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KeyValue::Document

#[], #[]=, #initialize, #key?, #to_h

Constructor Details

This class inherits a constructor from Lutaml::KeyValue::Document

Class Method Details

.parse(json, _options = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/lutaml/json/adapter/multi_json_adapter.rb', line 11

def self.parse(json, _options = {})
  require "multi_json"
  MultiJson.load(json)
rescue LoadError
  raise LoadError,
        "multi_json gem is not available. Please add 'multi_json' to your Gemfile."
end

Instance Method Details

#to_json(*args) ⇒ Object

rubocop:disable Style/ArgumentsForwarding -- anonymous * requires Ruby 3.2+



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
# File 'lib/lutaml/json/adapter/multi_json_adapter.rb', line 22

def to_json(*args)
  require "multi_json"
  # Handle KeyValueElement input (new symmetric architecture)
  attributes_to_serialize = if @attributes.is_a?(Lutaml::KeyValue::DataModel::Element)
                              # Unwrap __root__ wrapper to get actual content
                              @attributes.to_hash["__root__"]
                            else
                              # Legacy Hash input (backward compatibility)
                              @attributes
                            end

  # LutaML's own threading keys must not reach the backend; the
  # rest (e.g. :pretty) are meaningful to multi_json. The json
  # gem 3.x and Oj raise on unknown generator options, unlike
  # json 2.x which ignored them.
  dump_args = args.map do |arg|
    next arg unless arg.is_a?(::Hash)

    arg.except(*INTERNAL_LUTAML_KEYS)
  end
  MultiJson.dump(attributes_to_serialize, *dump_args)
# rubocop:enable Style/ArgumentsForwarding
rescue LoadError
  raise LoadError,
        "multi_json gem is not available. Please add 'multi_json' to your Gemfile."
end