class Mercury::WireSerializer

Public Instance Methods

read(bytes) click to toggle source
# File lib/mercury/wire_serializer.rb, line 11
def read(bytes)
  read_json(bytes)
end
write(struct_or_hash) click to toggle source

TODO: DRY with hyperion once we know more

# File lib/mercury/wire_serializer.rb, line 7
def write(struct_or_hash)
  write_json(struct_or_hash)
end

Private Instance Methods

hashify(x) click to toggle source
# File lib/mercury/wire_serializer.rb, line 42
def hashify(x)
  case x
  when Hash
    x
  when Struct
    x.to_h
  else
    raise "Could not convert to hash: #{x.inspect}"
  end
end
oj_options() click to toggle source
# File lib/mercury/wire_serializer.rb, line 33
def oj_options
  {
    mode: :rails,
    time_format: :xmlschema,  # xmlschema == iso8601
    second_precision: 3,
    bigdecimal_load: :float
  }
end
read_json(bytes) click to toggle source
# File lib/mercury/wire_serializer.rb, line 25
def read_json(bytes)
  begin
    Oj.compat_load(bytes, oj_options)
  rescue Oj::ParseError => e
    bytes
  end
end
write_json(obj) click to toggle source
# File lib/mercury/wire_serializer.rb, line 17
def write_json(obj)
  if obj.is_a?(String)
    obj
  else
    Oj.dump(hashify(obj), oj_options)
  end
end