Module: PlatformosCheck::JsonHelpers

Included in:
Check, Corrector, LanguageServer::DocumentChangeCorrector
Defined in:
lib/platformos_check/json_helpers.rb

Instance Method Summary collapse

Instance Method Details

#format_json_parse_error(error) ⇒ Object



5
6
7
8
# File 'lib/platformos_check/json_helpers.rb', line 5

def format_json_parse_error(error)
  message = error.message[/\d+: (.+)$/, 1] || 'Invalid syntax'
  "#{message} in JSON"
end

#pretty_json(hash, start_level: 1, indent: " ") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/platformos_check/json_helpers.rb', line 10

def pretty_json(hash, start_level: 1, indent: "  ")
  start_indent = indent * start_level

  <<~JSON

    #{start_indent}#{JSON.pretty_generate(
      hash,
      indent:,
      array_nl: "\n#{start_indent}",
      object_nl: "\n#{start_indent}"
    )}
  JSON
end