Module: SidekiqUniqueJobs::JSON

Overview

Handles loading and dumping of json

Author:

Class Method Summary collapse

Class Method Details

.dump_json(object) ⇒ String

Dumps an object into a JSON string



43
44
45
# File 'lib/sidekiq_unique_jobs/json.rb', line 43

def dump_json(object)
  ::JSON.generate(object)
end

.load_json(string) ⇒ Object

Parses a JSON string into an object



17
18
19
20
21
# 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) ⇒ Hash, Array

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



30
31
32
33
34
# 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