Class: Canon::PrettyPrinter::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/pretty_printer/json.rb

Overview

Pretty printer for JSON with consistent indentation

Instance Method Summary collapse

Constructor Details

#initialize(indent: 2, indent_type: "space") ⇒ Json

Returns a new instance of Json.



9
10
11
12
# File 'lib/canon/pretty_printer/json.rb', line 9

def initialize(indent: 2, indent_type: "space")
  @indent = indent.to_i
  @indent_type = indent_type
end

Instance Method Details

#format(json_string) ⇒ Object

Pretty print JSON with consistent indentation



15
16
17
18
19
20
21
22
# File 'lib/canon/pretty_printer/json.rb', line 15

def format(json_string)
  obj = JSON.parse(json_string)

  # Determine indent string
  indent_str = @indent_type == "tab" ? "\t" : " " * @indent

  JSON.pretty_generate(obj, indent: indent_str)
end