Class: DjiMqttConnect::Railties::MessageSerializer

Inherits:
ActiveJob::Serializers::ObjectSerializer
  • Object
show all
Defined in:
lib/dji_mqtt_connect/railties/message_serializer.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(hash) ⇒ Object



18
19
20
21
22
23
# File 'lib/dji_mqtt_connect/railties/message_serializer.rb', line 18

def deserialize(hash)
  # Rebuild the message, re-insert the data hash
  message_class = hash["type"].constantize
  message_payload = hash["payload"].merge("_data" => {})
  message_class.new(message_payload)
end

#serialize(message) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dji_mqtt_connect/railties/message_serializer.rb', line 6

def serialize(message)
  stringified_hash = message.to_h.deep_stringify_keys

  # Strip the extra data element
  stringified_hash.delete("_data")

  super(
    "type" => message.class.name,
    "payload" => stringified_hash
  )
end