Class: Makit::Serializer

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

Overview

This class provide methods for serializing and deserializing objects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proto3_format = Proto3Formats::PRETTY_JSON) ⇒ Serializer

Returns a new instance of Serializer.



62
63
64
65
66
# File 'lib/makit/serializer.rb', line 62

def initialize(proto3_format = Proto3Formats::PRETTY_JSON)
  @format_maps = {
    ::Google::Protobuf::MessageExts => proto3_format,
  }
end

Instance Attribute Details

#format_mapObject

Returns the value of attribute format_map.



29
30
31
# File 'lib/makit/serializer.rb', line 29

def format_map
  @format_map
end

Class Method Details

.open(filename, type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/makit/serializer.rb', line 47

def self.open(filename, type)
  extension = File.extname(filename)
  case extension
  when ".json"
    Makit::Serializer.new(Makit::Proto3Formats::PRETTY_JSON).deserialize(type, File.read(filename))
  when ".yml"
    Makit::Serializer.new(Makit::Proto3Formats::YAML).deserialize(type, File.read(filename))
  when ".pb"
    data = File.binread(filename)
    Makit::Serializer.new(Makit::Proto3Formats::BINARY).deserialize(type, data)
  else
    raise "unsupported file extension: #{extension}"
  end
end

.save_as(filename, object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/makit/serializer.rb', line 31

def self.save_as(filename, object)
  extension = File.extname(filename)
  case extension
  when ".json"
    File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::PRETTY_JSON).serialize(object))
  when ".yml"
    File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::YAML).serialize(object))
  when ".pb"
    File.binwrite(filename, Makit::Serializer.new(Makit::Proto3Formats::BINARY).serialize(object))

    # File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::BINARY).serialize(object))
  else
    raise "unsupported file extension: #{extension}"
  end
end

Instance Method Details

#deserialize(type, data) ⇒ Object

Deserialize a byte[] or a string to an object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/makit/serializer.rb', line 94

def deserialize(type, data)
  raise "data is nil" if data.nil?

  case @format_maps[::Google::Protobuf::MessageExts]
  when Proto3Formats::JSON
    raise "data is not of type string" unless data.is_a? String

    json = data
    # puts "Deserializing from protobuf json format into type #{type}"
    # use the from_json method to deserialize the object
    type.decode_json(json)
  when Proto3Formats::PRETTY_JSON
    raise "data is not of type string" unless data.is_a? String

    json = data
    # puts "Deserializing from protobuf pretty json format into type #{type}"
    # use the from_json method to deserialize the object
    type.decode_json(json)
  when Proto3Formats::YAML
    raise "data is not of type string" unless data.is_a? String

    yaml = data
    # convert yaml to json
    json = JSON.pretty_generate(YAML.safe_load(yaml))
    # puts "Deserializing from protobuf yaml format into type #{type}"
    type.decode_json(json)
  when Proto3Formats::BINARY
    # raise "data is not of type byte[]" if !data.is_a? Array
    bytes = data
    # puts "Deserializing from protobuf binary format into type #{type}"
    type.decode(bytes)
  else
    raise "Unknown serialization format"
  end
end

#serialize(object) ⇒ Object

Serialize an object to a byte[] or a string



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/makit/serializer.rb', line 69

def serialize(object)
  raise "Object is nil" if object.nil?
  raise "object is not of type ::Google::Protobuf::MessageExts" unless object.is_a? ::Google::Protobuf::MessageExts

  case @format_maps[::Google::Protobuf::MessageExts]
  when Proto3Formats::JSON
    # puts "Serializing to protobuf json format"
    object.to_json
  when Proto3Formats::PRETTY_JSON
    # puts "Serializing to protobuf pretty json format"
    json = object.to_json
    JSON.pretty_generate(JSON.parse(json))
  when Proto3Formats::YAML
    # puts "Serializing to protobuf yaml format"
    data = JSON.parse(object.to_json)
    data.to_yaml.sub(/^---\n/, "")
  when Proto3Formats::BINARY
    # puts "Serializing to protobuf binary format"
    object.to_proto
  else
    raise "Unknown serialization format"
  end
end