Module: Wardrobe::Plugins::JsonInitializer

Extended by:
Wardrobe::Plugin
Defined in:
lib/wardrobe/plugins/json_initializer.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Methods included from Wardrobe::Plugin

option, options

Class Method Details

.parse(json) ⇒ Object

Raises:

  • (InvalidJsonError)


8
9
10
11
12
13
14
15
16
# File 'lib/wardrobe/plugins/json_initializer.rb', line 8

def self.parse(json)
  raise InvalidJsonError unless json.match(/\A[\[{].+[\]}]\z/m)
  case parser.to_s
  when 'JSON'
    parser.parse(json, symbolize_names: true)
  when 'MultiJson'
    parser.load(json, symbolize_keys: true)
  end
end

.parserObject



18
19
20
# File 'lib/wardrobe/plugins/json_initializer.rb', line 18

def self.parser
  @parser ||= select_parser
end

.select_parserObject



22
23
24
25
26
27
# File 'lib/wardrobe/plugins/json_initializer.rb', line 22

def self.select_parser
  return MultiJson if defined? MultiJson
  return JSON if defined? JSON
  require 'json'
  JSON
end