Class: ObjectStream::JsonStream

Inherits:
Object
  • Object
show all
Includes:
ObjectStream
Defined in:
lib/object-stream.rb

Constant Summary collapse

DEFAULT_CHUNK_SIZE =
2000

Constants included from ObjectStream

DEFAULT_MAX_OUTBOX, JSON_TYPE, MARSHAL_TYPE, MSGPACK_TYPE, TYPES, VERSION, YAML_TYPE

Instance Attribute Summary collapse

Attributes included from ObjectStream

#io, #max_outbox

Instance Method Summary collapse

Methods included from ObjectStream

#checked_read_from_stream, #close, #closed?, #each, #eof?, #flush_buffer, #flush_outbox, new, #read, #read_one, register_type, stream_class_for, #to_io, #to_s, #write, #write_to_buffer, #write_to_outbox

Constructor Details

#initialize(io, chunk_size: DEFAULT_CHUNK_SIZE, symbolize_keys: false) ⇒ JsonStream

See the discussion in examples/symbolize-keys.rb.



251
252
253
254
255
256
# File 'lib/object-stream.rb', line 251

def initialize io, chunk_size: DEFAULT_CHUNK_SIZE, symbolize_keys: false
  super
  @parser = Yajl::Parser.new(symbolize_keys: symbolize_keys)
  @encoder = Yajl::Encoder.new
  @chunk_size = chunk_size
end

Instance Attribute Details

#chunk_sizeObject

Returns the value of attribute chunk_size.



246
247
248
# File 'lib/object-stream.rb', line 246

def chunk_size
  @chunk_size
end

Instance Method Details

#read_from_stream(&bl) ⇒ Object

Blocks only if no data available on io.



259
260
261
262
# File 'lib/object-stream.rb', line 259

def read_from_stream(&bl)
  @parser.on_parse_complete = bl
  @parser << io.readpartial(chunk_size)
end

#write_to_stream(object) ⇒ Object



264
265
266
267
# File 'lib/object-stream.rb', line 264

def write_to_stream object
  @encoder.encode object, io
  self
end