Class: SNMPTableViewer::Formatter::JSON

Inherits:
SNMPTableViewer::Formatter show all
Defined in:
lib/snmp_table_viewer/formatter/json.rb

Overview

Formatting class for JSON output

Instance Method Summary collapse

Methods inherited from SNMPTableViewer::Formatter

#initialize

Constructor Details

This class inherits a constructor from SNMPTableViewer::Formatter

Instance Method Details

#outputString

Output the data. If no headings are provided you’ll get an Array or Arrays. If headings are provided you’ll get an Array of Hashes.

Returns:

  • (String)

    the JSON data



7
8
9
10
11
12
13
14
# File 'lib/snmp_table_viewer/formatter/json.rb', line 7

def output()
  data = @data
  if @headings.size > 0
    # Convert inner arrays to hashes
    data = data.map{ |row| Hash[row.map.with_index{ |value, index| [@headings[index] || '', value] }] }
  end
  ::JSON.generate(data)
end