Module: KwalifyToJsonSchema::Serialization

Defined in:
lib/kwalify_to_json_schema/serialization.rb

Overview

Abstract JSON/YAML serialization/deserialization

Defined Under Namespace

Classes: Json, Language, Yaml

Class Method Summary collapse

Class Method Details

.deserialize_from_file(file) ⇒ Object



5
6
7
# File 'lib/kwalify_to_json_schema/serialization.rb', line 5

def self.deserialize_from_file(file)
  serialization_for_file(file).deserialize(File.read(file))
end

.deserialize_from_string(string, format = "yaml") ⇒ Object



13
14
15
# File 'lib/kwalify_to_json_schema/serialization.rb', line 13

def self.deserialize_from_string(string, format = "yaml")
  serialization_for_format(format).deserialize(string)
end

.serialization_for_file(file) ⇒ Object

Returns a Hash giving serialization/deserialization module and methods for a given file extension (.json/.yaml/.yml).

Returns:

  • a Hash giving serialization/deserialization module and methods for a given file extension (.json/.yaml/.yml)



22
23
24
# File 'lib/kwalify_to_json_schema/serialization.rb', line 22

def self.serialization_for_file(file)
  serialization_for_format(File.extname(file)[1..-1])
end

.serialization_for_format(format) ⇒ Object

Returns a Hash giving serialization/deserialization module and methods for a format (json/yaml).

Returns:

  • a Hash giving serialization/deserialization module and methods for a format (json/yaml)



27
28
29
30
31
32
33
34
35
# File 'lib/kwalify_to_json_schema/serialization.rb', line 27

def self.serialization_for_format(format)
  mod = {
    "json" => Json,
    "yaml" => Yaml,
    "yml" => Yaml,
  }[format]
  raise "Unsupported format '#{format}'" unless mod
  mod
end

.serialize_to_file(file, object) ⇒ Object



9
10
11
# File 'lib/kwalify_to_json_schema/serialization.rb', line 9

def self.serialize_to_file(file, object)
  File.write(file, serialization_for_file(file).serialize(object))
end

.serialize_to_string(object, format = "json") ⇒ Object



17
18
19
# File 'lib/kwalify_to_json_schema/serialization.rb', line 17

def self.serialize_to_string(object, format = "json")
  serialization_for_format(format).serialize(object)
end