Outstream::Json
A library for producing JSON output in a streaming fashion. It is designed to work with lambdas and lazy enumerators, to minimize the need to create the entire JSON-encoding string at once.
Installation
Add this line to your application's Gemfile:
gem 'outstream-json'
And then execute:
$ bundle
Or install it yourself as:
$ gem install outstream-json
Usage
The Json.create method defines a JSON object
out = Outstream::Json.create do
...
end
# use out.to_s or out.each to produce result
Use with basic ruby types
Outstream::Json.create do
add string: "hello", number: 42
add array: [1,2,3]
end
# {"string":"hello","number":42,"array":[1,2,3]}
Use with hashes or blocks to create nested objects
Outstream::Json.create do
add "block_object" do
add "foo" => "bar"
end
add "hash_object" => {
"wow" => "cool"
}
end
# {"block_object":{"foo":"bar"},"hash_object":{"wow":"cool"}}
Use with transformed SQL result set
client = Mysql2::Client.new
results = client.query("SELECT * FROM hugetable", stream: true)
Outstream::Json.create do
add results: results.lazy.map {|row| transform_row(row)}
end
# {"results":[{<transformed_result>},...]}
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request