Class: Ronin::Core::OutputFormats::JSON

Inherits:
OutputFile show all
Defined in:
lib/ronin/core/output_formats/json.rb

Overview

Represents a JSON (.json) output format.

Since:

  • 0.2.0

Instance Attribute Summary

Attributes inherited from OutputFile

#io

Instance Method Summary collapse

Methods inherited from OutputFile

open

Constructor Details

#initialize(io) ⇒ JSON

Initializes the JSON output format.

Parameters:

  • io (IO)

    The JSON output stream.

Since:

  • 0.2.0



41
42
43
44
45
# File 'lib/ronin/core/output_formats/json.rb', line 41

def initialize(io)
  super(io)

  @first_value = true
end

Instance Method Details

#<<(value) ⇒ self

Appends a value to the JSON stream.

Parameters:

  • value (#to_json)

    The value to append.

Returns:

  • (self)

Raises:

  • (NotImplementedError)

    The given value object does not define a to_json method.

Since:

  • 0.2.0



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ronin/core/output_formats/json.rb', line 58

def <<(value)
  unless value.respond_to?(:to_json)
    raise(NotImplementedError,"output value does not define a #to_json method: #{value.inspect}")
  end

  if @first_value
    @io.write("[#{value.to_json}")
    @first_value = false
  else
    @io.write(",#{value.to_json}")
  end

  @io.flush
  return self
end

#closeObject

Closes the JSON output stream.

Since:

  • 0.2.0



77
78
79
80
81
82
83
84
85
# File 'lib/ronin/core/output_formats/json.rb', line 77

def close
  if @first_value
    @io.write("[]")
  else
    @io.write("]")
  end

  super
end