module SidekiqUniqueJobs::JSON

Handles loading and dumping of json

@author Mikael Henriksson <mikael@mhenrixon.com>

Public Instance Methods

dump_json(object) click to toggle source

Dumps an object into a JSON string

@param [Object] object a JSON convertible object

@return [String] a JSON string

# File lib/sidekiq_unique_jobs/json.rb, line 43
def dump_json(object)
  ::JSON.generate(object)
end
load_json(string) click to toggle source

Parses a JSON string into an object

@param [String] string the object to parse

@return [Object]

# File lib/sidekiq_unique_jobs/json.rb, line 17
def load_json(string)
  return if string.nil? || string.empty?

  ::JSON.parse(string)
end
safe_load_json(string) click to toggle source

Prevents trying JSON.load from raising errors given argument is a hash

@param [String, Hash] string the JSON string to parse

@return [Hash,Array]

# File lib/sidekiq_unique_jobs/json.rb, line 30
def safe_load_json(string)
  return string if string.is_a?(Hash)

  load_json(string)
end