Class: HammerCLI::Options::Normalizers::JSONInput

Inherits:
File show all
Defined in:
lib/hammer_cli/options/normalizers.rb

Instance Method Summary collapse

Methods inherited from File

#complete

Methods inherited from AbstractNormalizer

#complete, #description

Instance Method Details

#format(val) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/hammer_cli/options/normalizers.rb', line 219

def format(val)
  # The JSON input can be either the path to a file whose contents are
  # JSON or a JSON string.  For example:
  #   /my/path/to/file.json
  # or
  #   '{ "units":[ { "name":"zip", "version":"9.0", "inclusion":"false" } ] }')
  json_string = ::File.exist?(::File.expand_path(val)) ? super(val) : val
  ::JSON.parse(json_string)

rescue ::JSON::ParserError => e
  raise ArgumentError, _("Unable to parse JSON input.")
end