Skip to content

Instantly share code, notes, and snippets.

@kascote
Forked from brianhempel/enumerator_to_json.rb
Last active August 29, 2015 14:17
Show Gist options
  • Save kascote/778c0a9aec62cb8cb29c to your computer and use it in GitHub Desktop.
Save kascote/778c0a9aec62cb8cb29c to your computer and use it in GitHub Desktop.
# Natively, Enumerators get JSONized like "#<Enumerator::Lazy:0x007f8714807080>", or they explode, either of which is a problem.
# We want them to make an array, and do it lazily so we don't have to keep the items in memory!
class Enumerator
def to_json(state)
state.depth += 1
string = "[\n"
first_item = true
self.each do |item|
separator = ",\n" unless first_item
as_json = item.as_json
indentation = state.indent * state.depth
string << "#{separator}#{indentation}#{as_json.to_json(state)}"
first_item = false
end
state.depth -= 1
indentation = state.indent * state.depth
string << "\n#{indentation}]"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment