Class: Tabula::Writers::JSONWriter

Inherits:
Writer
  • Object
show all
Defined in:
lib/tabula/writers/json_writer.rb

Overview

Writes tables in JSON format

Instance Method Summary collapse

Methods inherited from Writer

to_string, write

Constructor Details

#initialize(pretty: false, include_metadata: true, **options) ⇒ JSONWriter

Returns a new instance of JSONWriter.

Parameters:

  • pretty (Boolean) (defaults to: false) —

    pretty-print JSON (default: false)

  • include_metadata (Boolean) (defaults to: true) —

    include table metadata (default: true)



11
12
13
14
15
# File 'lib/tabula/writers/json_writer.rb', line 11

def initialize(pretty: false, include_metadata: true, **options)
  super(**options)
  @pretty = pretty
   = 
end

Instance Method Details

#write(tables, io) ⇒ Object

Write tables to an IO object

Parameters:

  • tables (Array<Table>) —

    tables to write

  • io (IO) —

    output destination



20
21
22
23
24
25
26
27
28
# File 'lib/tabula/writers/json_writer.rb', line 20

def write(tables, io)
  output = tables.map { |table| table_to_hash(table) }

  if @pretty
    io.puts JSON.pretty_generate(output)
  else
    io.puts JSON.generate(output)
  end
end