Class: Canon::Comparison::JsonComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/comparison/json_comparator.rb

Overview

JSON comparison class Delegates to RubyObjectComparator for actual comparison logic

Constant Summary collapse

DEFAULT_OPTS =
{
  verbose: false,
  match_profile: nil,
  match: nil,
  preprocessing: nil,
  global_profile: nil,
  global_options: nil,
  diff: nil,
}.freeze

Class Method Summary collapse

Class Method Details

.equivalent?(json1, json2, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/canon/comparison/json_comparator.rb', line 25

def equivalent?(json1, json2, opts = {})
  opts = DEFAULT_OPTS.merge(opts)

  match_opts_hash = MatchOptions::Json.resolve(
    format: :json,
    match_profile: opts[:match_profile],
    match: opts[:match],
    preprocessing: opts[:preprocessing],
    global_profile: opts[:global_profile],
    global_options: opts[:global_options],
  )

  opts[:match_opts] = match_opts_hash

  obj1 = parse_json(json1)
  obj2 = parse_json(json2)

  differences = []
  result = RubyObjectComparator.compare_objects(obj1, obj2, opts,
                                                differences, "")

  if opts[:verbose]
    json_str1 = obj1.is_a?(String) ? obj1 : JSON.pretty_generate(obj1)
    json_str2 = obj2.is_a?(String) ? obj2 : JSON.pretty_generate(obj2)

    ComparisonResult.new(
      differences: differences,
      preprocessed_strings: [json_str1, json_str2],
      format: :json,
      match_options: match_opts_hash,
    )
  else
    result == Comparison::EQUIVALENT
  end
end

.parse(obj) ⇒ Object



21
22
23
# File 'lib/canon/comparison/json_comparator.rb', line 21

def parse(obj)
  parse_json(obj)
end