Giter VIP home page Giter VIP logo

json-write-stream's Introduction

json-write-stream

Build Status

An easy, streaming way to generate JSON.

Installation

gem install json-write-stream

Usage

require 'json-write-stream'

Examples for the Impatient

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"]}

Yielding Writers

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

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

Writing to a File

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

Options

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"
  ]
}

Requirements

No external requirements.

Running Tests

bundle exec rake should do the trick. Alternatively you can run bundle exec rspec, which does the same thing.

Authors

json-write-stream's People

Contributors

camertron avatar

Stargazers

Akshay Birajdar avatar Ted Behling avatar  avatar nefuhs avatar  avatar Petr Pučil avatar Artur Hebda avatar Heshie Brody avatar Dmitriy  avatar JP Camara avatar Eric Franz avatar Nick Schwaderer avatar Caroline Artz avatar  avatar Nikita avatar Alexey Mikhaylov avatar Дмитрий avatar Bruz Marzolf avatar

Watchers

Shiv Indap avatar  avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.