Module: MuchRails::JSON

Defined in:
lib/much-rails/json.rb

Overview

MuchRails::JSON is an adapter for encoding and decoding JSON values. It uses Oj to do the work: github.com/ohler55/oj#-gem

Constant Summary collapse

InvalidError =
Class.new(TypeError)

Class Method Summary collapse

Class Method Details

.decode(json, **options) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/much-rails/json.rb', line 21

def self.decode(json, **options)
  options[:mode] ||= default_mode
  ::Oj.load(json, options)
rescue ::Oj::ParseError => ex
  error = InvalidError.new("Oj::ParseError: #{ex.message}")
  error.set_backtrace(ex.backtrace)
  raise error
end

.default_modeObject



12
13
14
# File 'lib/much-rails/json.rb', line 12

def self.default_mode
  :strict
end

.encode(obj, **options) ⇒ Object



16
17
18
19
# File 'lib/much-rails/json.rb', line 16

def self.encode(obj, **options)
  options[:mode] ||= default_mode
  ::Oj.dump(obj, options)
end