Module: JsonAssert::Helpers

Extended by:
Helpers
Included in:
Assertions, Helpers
Defined in:
lib/json_assert/helpers.rb

Instance Method Summary collapse

Instance Method Details

#generate_normalized_json(ruby) ⇒ Object



20
21
22
23
24
25
# File 'lib/json_assert/helpers.rb', line 20

def generate_normalized_json(ruby)
  case ruby
  when Hash, Array then JSON.pretty_generate(ruby)
  else ruby.to_json
  end
end

#normalize_json(json, path = nil) ⇒ Object



15
16
17
18
# File 'lib/json_assert/helpers.rb', line 15

def normalize_json(json, path = nil)
  ruby = parse_json(json, path)
  generate_normalized_json(ruby)
end

#parse_json(json, path = nil) ⇒ Object



8
9
10
11
12
13
# File 'lib/json_assert/helpers.rb', line 8

def parse_json(json, path = nil)
  ruby = MultiJson.decode(%([#{json}])).first
  value_at_json_path(ruby, path)
rescue MultiJson::DecodeError
  MultiJson.decode(json)
end