Class: EventRouter::Serializers::Json
- Defined in:
- lib/event_router/serializers/json.rb
Constant Summary collapse
- EVENT_CLASS_ATTRIBUTE_NAME =
'er_class'
Class Method Summary collapse
Class Method Details
.deserialize(payload) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/event_router/serializers/json.rb', line 20 def deserialize(payload) object = JSON.parse(payload) return object unless object.is_a?(Hash) return object unless object.key?(EVENT_CLASS_ATTRIBUTE_NAME) event_class = object.delete(EVENT_CLASS_ATTRIBUTE_NAME) event_instance = const_get(event_class).new object.each do |attribute, value| event_instance.instance_variable_set(:"@#{attribute}", value) end event_instance end |
.serialize(object) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/event_router/serializers/json.rb', line 11 def serialize(object) return JSON.generate(object) unless object.is_a?(EventRouter::Event) attributes = object.to_h attributes[EVENT_CLASS_ATTRIBUTE_NAME] = object.class.name JSON.generate(attributes) end |