0.0
No commit activity in last 3 years
No release in over 3 years
A library which provides a means for writing a large number of object as json without building the entire set in memory first.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Gem Version

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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request