module HashSerializer::Serializer

Serializes Ruby objects to JSON for storage in Postgres tables

Public Class Methods

dump(hash) click to toggle source

Dump the contents of hash to JSON

Example:

>> HashSerializer.dump({name: 'John'})
=> "{'name': 'John'}"

@param hash [Hash]

# File lib/hash_serializer/serializer.rb, line 14
def self.dump(hash)
  hash.to_json
end
load(hash) click to toggle source

Loads the contents of hash from JSON if hash is a String or returns the array otherwise

Example:

>> HashSerializer.load("{name: 'John'}")
=> {'name': 'John'}

>> HashSerializer.load({name: 'John'})
=> {'name': 'John'}

>> HashSerializer.load(nil)
=> {}

@param hash [String, Hash]

# File lib/hash_serializer/serializer.rb, line 31
def self.load(hash)
  hash = JSON.parse(hash) if hash.is_a?(String)
  (hash || {}).with_indifferent_access
end