Class: Canon::Comparison::JsonComparator
- Inherits:
-
Object
- Object
- Canon::Comparison::JsonComparator
- Defined in:
- lib/canon/comparison/json_comparator.rb
Overview
JSON comparison class Handles comparison of JSON objects with various options
Constant Summary collapse
- DEFAULT_OPTS =
Default comparison options for JSON
{ # Output options verbose: false, # Match system options match_profile: nil, match: nil, preprocessing: nil, global_profile: nil, global_options: nil, # Diff display options diff: nil, }.freeze
Class Method Summary collapse
-
.equivalent?(json1, json2, opts = {}) ⇒ Boolean, ComparisonResult
Compare two JSON objects for equivalence.
Class Method Details
.equivalent?(json1, json2, opts = {}) ⇒ Boolean, ComparisonResult
Compare two JSON objects for equivalence
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/canon/comparison/json_comparator.rb', line 35 def equivalent?(json1, json2, opts = {}) opts = DEFAULT_OPTS.merge(opts) # Resolve match options with format-specific defaults 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], ) # Wrap in ResolvedMatchOptions for consistency with XML/HTML Canon::Comparison::ResolvedMatchOptions.new( match_opts_hash, format: :json, ) # Store resolved match options for use in comparison logic opts[:match_opts] = match_opts_hash # Parse JSON if strings obj1 = parse_json(json1) obj2 = parse_json(json2) differences = [] result = compare_ruby_objects(obj1, obj2, opts, differences, "") if opts[:verbose] # Format JSON for display 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 |