Sha256: 777f350595f04c4a904214c54e1e2c5cc5f8cfb11d1c78de48092362cb839f44

Contents?: true

Size: 1.12 KB

Versions: 10

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require "json"

module Mimi
  module Messaging
    #
    # JSON serializer.
    #
    # De/Serializes a message (Hash) from/into a JSON object
    #
    module JsonSerializer
      #
      # Serialize given message into JSON object
      #
      # @param message [Hash]
      # @return [String]
      #
      def self.serialize(message)
        unless message.is_a?(Hash)
          raise ArgumentError, "Invalid message passed to #{self}#serialize, Hash is expected"
        end

        message.to_json
      rescue StandardError => e
        raise "#{self} failed to serialize a message: #{e}"
      end

      # Deserializes a JSON into a message
      #
      # @param message [String]
      # @return [Hash]
      #
      def self.deserialize(message)
        unless message.is_a?(String)
          raise ArgumentError, "Invalid message passed to #{self}#deserialize, String is expected"
        end

        JSON.parse(message)
      rescue StandardError => e
        raise "#{self} failed to deserialize a message: #{e}"
      end
    end # module JsonSerializer
  end # module Messaging
end # module Mimi

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mimi-messaging-1.2.8 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.2.7 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.2.6 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.2.5 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.2.4 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.2.2 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.2.1 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.1.1 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.1.0 lib/mimi/messaging/json_serializer.rb
mimi-messaging-1.0.0 lib/mimi/messaging/json_serializer.rb