yajl-ruby icon indicating copy to clipboard operation
yajl-ruby copied to clipboard

Allow encoding of Lazy Enumerable collections (Ruby 2.0)

Open mkonecny opened this issue 11 years ago • 1 comments
trafficstars

Consider the following:

z = [{"x" => 5}, 2, 3].lazy.map{ |i| i}
#=> #<Enumerator::Lazy: #<Enumerator::Lazy: [{"x"=>5}, 2, 3]>:map>
z.first
#=> {"x"=>5}

Encoding hash z returns the following unexpected chunk:

=> ""#Enumerator::Lazy:0x00000001cb0448""

encoder = Yajl::Encoder.new
encoder.encode(z) do |chunk|
    if chunk.nil? # got our terminator, encoding is done
      close_connection_after_writing
    else
      puts chunk
    end
end

It seems to me that one of the potential advantages of yajl is I could avoid using to_a on large lists to save memory.

mkonecny avatar Aug 12 '14 03:08 mkonecny

This would be very nice.

joevandyk avatar Jul 29 '16 19:07 joevandyk