
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
An easy, streaming way to generate JSON.
gem install json-write-stream
require 'json-write-stream'
There are two types of JSON write stream: one that uses blocks and yield
to delimit arrays and objects, and one that's purely stateful. Here are two examples that produce the same output:
Yielding:
stream = StringIO.new
JsonWriteStream.from_stream(stream) do |writer|
writer.write_object do |obj_writer|
obj_writer.write_key_value('foo', 'bar')
obj_writer.write_array('baz') do |arr_writer|
arr_writer.write_element('goo')
end
end
end
Stateful:
stream = StringIO.new
writer = JsonWriteStream.from_stream(stream)
writer.write_object
writer.write_key_value('foo', 'bar')
writer.write_array('baz')
writer.write_element('goo')
writer.close # automatically adds closing punctuation for all nested types
Output:
stream.string # => {"foo":"bar","baz":["goo"]}
As far as yielding writers go, the example above contains everything you need. The stream will be automatically closed when the outermost block terminates.
Stateful writers have a number of additional methods:
stream = StringIO.new
writer = JsonWriteStream.from_stream(stream)
writer.write_object
writer.in_object? # => true, currently writing an object
writer.in_array? # => false, not currently writing an array
writer.eos? # => false, the stream is open and the outermost object hasn't been closed yet
writer.close_object # explicitly close the current object
writer.eos? # => true, the outermost object has been closed
writer.write_array # => raises JsonWriteStream::EndOfStreamError
writer.close_array # => raises JsonWriteStream::NotInArrayError
writer.closed? # => false, the stream is still open
writer.close # close the stream
writer.closed? # => true, the stream has been closed
JsonWriteStream also supports streaming to a file via the open
method:
Yielding:
JsonWriteStream.open('path/to/file.json') do |writer|
writer.write_object do |obj_writer|
...
end
end
Stateful:
writer = JsonWriteStream.open('path/to/file.json')
writer.write_object
...
writer.close
JsonWriteStream supports generating "pretty" JSON, i.e. JSON formatted in a more human-readable way. Currently only the stateful writer supports pretty generation. Example:
stream = StringIO.new
writer = JsonWriteStream.from_stream(stream, pretty: true)
writer.write_object
writer.write_key_value('foo', 'bar')
writer.write_array('baz')
writer.write_element('goo')
writer.close
Now stream.string
will contain
{
"foo": "bar",
"baz": [
"goo"
]
}
No external requirements.
bundle exec rake
should do the trick. Alternatively you can run bundle exec rspec
, which does the same thing.
FAQs
Unknown package
We found that json-write-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.