Sha256: 8dd36a21d3553380b7921b80a9588d01660596d191088e4e64867309265a8d36

Contents?: true

Size: 888 Bytes

Versions: 2

Compression:

Stored size: 888 Bytes

Contents

require 'oj'

class Mercury
  class WireSerializer
    # TODO: DRY with hyperion once we know more

    def write(struct_or_hash)
      write_json(struct_or_hash)
    end

    def read(bytes)
      read_json(bytes)
    end

    private

    def write_json(obj)
      if obj.is_a?(String)
        obj
      else
        Oj.dump(hashify(obj), oj_options)
      end
    end

    def read_json(bytes)
      begin
        Oj.compat_load(bytes, oj_options)
      rescue Oj::ParseError => e
        bytes
      end
    end

    def oj_options
      {
        mode: :rails,
        time_format: :xmlschema,  # xmlschema == iso8601
        second_precision: 3,
        bigdecimal_load: :float
      }
    end

    def hashify(x)
      case x
      when Hash
        x
      when Struct
        x.to_h
      else
        raise "Could not convert to hash: #{x.inspect}"
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mercury_amqp-0.10.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.9.0 lib/mercury/wire_serializer.rb