Sha256: 77e8ce08564a3307c3f6994a961a5f33888cbd9f584a4133e23e48183f8c271e

Contents?: true

Size: 884 Bytes

Versions: 16

Compression:

Stored size: 884 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: :compat,
        time_format: :xmlschema,  # xmlschema == iso8601
        use_to_json: false,
        second_precision: 3
      }
    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

16 entries across 16 versions & 1 rubygems

Version Path
mercury_amqp-0.8.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.7.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.6.1 lib/mercury/wire_serializer.rb
mercury_amqp-0.6.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.5.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.4.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.3.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.2.0 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.9 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.7 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.6 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.5 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.4 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.3 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.2 lib/mercury/wire_serializer.rb
mercury_amqp-0.1.0 lib/mercury/wire_serializer.rb