Class: Alf::Renderer::JSON

Inherits:
Alf::Renderer show all
Defined in:
lib/alf/renderer/json.rb

Overview

Implements Alf::Renderer contract for outputing JSON.

Constant Summary

Constants inherited from Alf::Renderer

DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Alf::Renderer

#input, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Alf::Renderer

by_mime_type, #execute, #initialize, register, renderer

Methods included from Support::Registry

#listen, #listeners, #register, #registered

Constructor Details

This class inherits a constructor from Alf::Renderer

Class Method Details

.mime_typeObject


8
9
10
# File 'lib/alf/renderer/json.rb', line 8

def self.mime_type
  "application/json"
end

Instance Method Details

#eachObject


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/alf/renderer/json.rb', line 12

def each
  return to_enum unless block_given?
  require 'json'
  if TupleLike===input
    yield ::JSON.dump(input.to_hash)
    yield "\n"
  else
    i = 0
    yield "["
    input.each do |t|
      yield ',' unless i==0
      yield ::JSON.dump(t)
      i += 1
    end
    yield "]\n"
  end
end