Class: Makit::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/serializer.rb

Class Method Summary collapse

Class Method Details

.deserialize_from_string(type, data, format) ⇒ Object

Deserialize a string into an instance of type. Type must respond to decode_json, from_json, or from_hash.



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
# File 'lib/makit/serializer.rb', line 35

def self.deserialize_from_string(type, data, format)
  raise "data is nil" if data.nil?
  raise "data is not a String" unless data.is_a?(String)
  case format
  when Proto3Formats::JSON, Proto3Formats::PRETTY_JSON
    if type.respond_to?(:decode_json)
      type.decode_json(data)
    elsif type.respond_to?(:from_json)
      type.from_json(data)
    else
      raise "Type #{type} must respond to decode_json or from_json for JSON"
    end
  when Proto3Formats::YAML
    hash = YAML.safe_load(data)
    if type.respond_to?(:from_hash)
      type.from_hash(hash)
    elsif type.respond_to?(:decode_json)
      type.decode_json(JSON.pretty_generate(hash))
    else
      raise "Type #{type} must respond to from_hash or decode_json for YAML"
    end
  else
    raise "Unknown format: #{format}"
  end
end

.open(filename, type) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/makit/serializer.rb', line 73

def self.open(filename, type)
  ext = File.extname(filename)
  data = File.read(filename)
  case ext
  when ".json"
    deserialize_from_string(type, data, Proto3Formats::PRETTY_JSON)
  when ".yml"
    deserialize_from_string(type, data, Proto3Formats::YAML)
  else
    raise "unsupported file extension: #{ext} (supported: .json, .yml)"
  end
end

.save_as(filename, object) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/makit/serializer.rb', line 61

def self.save_as(filename, object)
  ext = File.extname(filename)
  case ext
  when ".json"
    File.write(filename, serialize_to_string(object, Proto3Formats::PRETTY_JSON))
  when ".yml"
    File.write(filename, serialize_to_string(object, Proto3Formats::YAML))
  else
    raise "unsupported file extension: #{ext} (supported: .json, .yml)"
  end
end

.serialize_to_string(object, format) ⇒ Object

Serialize an object to a string (JSON or YAML). Object must respond to to_json or to_h.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/makit/serializer.rb', line 17

def self.serialize_to_string(object, format)
  raise "Object is nil" if object.nil?
  case format
  when Proto3Formats::JSON
    obj = object.respond_to?(:to_json) ? JSON.parse(object.to_json) : object.to_h
    JSON.generate(obj)
  when Proto3Formats::PRETTY_JSON
    obj = object.respond_to?(:to_json) ? JSON.parse(object.to_json) : object.to_h
    JSON.pretty_generate(obj)
  when Proto3Formats::YAML
    obj = object.respond_to?(:to_json) ? JSON.parse(object.to_json) : (object.respond_to?(:to_h) ? object.to_h : {})
    YAML.dump(obj).sub(/\A---\n/, "")
  else
    raise "Unknown format: #{format}"
  end
end