Class: Canon::DiffFormatter::ByObject::JsonFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/canon/diff_formatter/by_object/json_formatter.rb

Overview

JSON tree formatter for by-object diffs Handles Ruby object differences (hashes and arrays)

Direct Known Subclasses

YamlFormatter

Instance Attribute Summary

Attributes inherited from BaseFormatter

#use_color, #visualization_map

Instance Method Summary collapse

Methods inherited from BaseFormatter

for_format, #format, #initialize

Constructor Details

This class inherits a constructor from Canon::DiffFormatter::ByObject::BaseFormatter

Instance Method Details

#render_diff_node(key, diff, prefix, connector) ⇒ String

Render a diff node for JSON/Ruby object differences

Parameters:

  • key (String)

    Hash key or array index

  • diff (Hash)

    Difference information

  • prefix (String)

    Tree prefix for indentation

  • connector (String)

    Box-drawing connector character

Returns:

  • (String)

    Formatted diff node



18
19
20
21
22
23
24
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
# File 'lib/canon/diff_formatter/by_object/json_formatter.rb', line 18

def render_diff_node(key, diff, prefix, connector)
  output = []

  # Show full path if available (path in cyan, no color on tree structure)
  path_display = if diff[:path] && !diff[:path].empty?
                   colorize(diff[:path].to_s,
                            theme_color(:informative, :content) || :cyan, :bold)
                 else
                   colorize(key.to_s,
                            theme_color(:informative,
                                        :content) || :cyan)
                 end

  output << "#{prefix}#{connector}#{path_display}:"

  # Determine continuation for nested values
  continuation = connector.start_with?("") ? "" : "    "
  value_prefix = prefix + continuation

  diff_code = diff[:diff_code] || diff[:diff1]

  case diff_code
  when Comparison::MISSING_HASH_KEY
    render_missing_key(diff, value_prefix, output)
  when Comparison::UNEQUAL_PRIMITIVES
    render_unequal_primitives(diff, value_prefix, output)
  when Comparison::UNEQUAL_HASH_VALUES
    render_unequal_hash_values(diff, value_prefix, output)
  when Comparison::UNEQUAL_ARRAY_ELEMENTS
    render_unequal_array_elements(diff, value_prefix, output)
  when Comparison::UNEQUAL_ARRAY_LENGTHS
    render_unequal_array_lengths(diff, value_prefix, output)
  when Comparison::UNEQUAL_TYPES
    render_unequal_types(diff, value_prefix, output)
  else
    # Fallback for unknown diff types
    render_fallback(diff, value_prefix, output)
  end

  output.join("\n")
end