Class: Outstream::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/outstream/json.rb

Overview

Produce a stream of JSON tokens.

Defined Under Namespace

Classes: Collector, Receiver

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(&body_block) ⇒ Object

end



18
19
20
# File 'lib/outstream/json.rb', line 18

def self.create(&body_block)
  new body_block
end

Instance Method Details

#each(&out_block) ⇒ Object

Iterate the output tokens. The block will receive JSON delimeters individually as strings, and string values as quoted strings. If called without a block, returns an enumerator.

Example:

json.each {|token| puts token}


27
28
29
30
31
32
33
# File 'lib/outstream/json.rb', line 27

def each(&out_block)
  e = Enumerator.new {|yielder|
    Collector.new(yielder).collect &@body_block
  }

  out_block ? e.each(&out_block) : e
end

#to_sObject

Produce a compact string of the JSON. The entire string is produced at once; this is not suitable for very large JSON output.



37
38
39
# File 'lib/outstream/json.rb', line 37

def to_s
  "".tap {|s| each {|str| s.concat str } }
end