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

Parameters:

  • object (Object)

    a JSON convertible object

Returns:

  • (String)

    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

Parameters:

  • string (String)

    the object to parse

Returns:

  • (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

Parameters:

  • string (String, Hash)

    the JSON string to parse

Returns:



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