2017 © Pedro Peláez
 

library paperboat

A streaming JSON emitter

image

austinhyde/paperboat

A streaming JSON emitter

  • Sunday, May 17, 2015
  • by austinhyde
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PaperBoat - Streaming JSON Output

Build Status Latest Version MIT Licensed, (*1)

This is an extremely simple, and very alpha quality implementation of a Streaming JSON outputter., (*2)

Show me the goods

$stream = new PaperBoat\JsonStream();
$stream
  ->startObject()
  ->property('data')
  ->startArray();

$i = 0;
while ($row = $pdoStmt->fetch(PDO::FETCH_ASSOC)) {
  $i++;
  $stream->value($row);
}

$stream
  ->stopArray()
  ->property('meta')
  ->startObject()
    ->property('count', $i)
  ->stopObject()
  ->stopObject();  

This outputs, for example,, (*3)

{"data":[{"id":1,"name":"Bill Murray"},{"id":2,"name":"Tom Hanks"},{"id":3,"name":"Sigourney Weaver"}],"meta":{"count":3}}

Usage

The primary class you interact with is PaperBoat\JsonStream. The constructor takes an optional PaperBoat\OutputStream parameter, which tells it where to output JSON to. By default, this is a PaperBoat\OutputStream\StdoutStream, which just prints to STDOUT or the HTTP response., (*4)

The following methods construct JSON output:, (*5)

  • startArray() - Begins outputting an array.
  • stopArray() - Closes an array.
  • startObject() - Begins outputting an object.
  • stopObject() - Closes an object.
  • property($name[, $data]) - Adds a property to an object. If you do not provide the value here, you must call startArray(), startObject(), or value() next.
  • value($data) - Adds data to the stream, by JSON encoding it.

The following methods control how data is output:, (*6)

  • setAutomaticFlushing($value) - Controls whether the OutputStream is flushed automatically after data is written to the stream. Defaults to true. If you set this false, you are responsible for calling ->flush() when appropriate.
  • setJsonFlags($value) - Sets the flags passed to json_encode()
  • flush() - Simply calls the provided OutputStream's flush() method.

Installation

Via Composer., (*7)

$ composer require austinhyde/paperboat

Contributing

See CONTRIBUTING, (*8)

FAQ

Why PaperBoat?

Streams of lightweight data => paper boats floating down a stream of water, (*9)

Why do I need this?

You would use this if you need to output a large amount of JSON data without holding the whole data structure in memory at once., (*10)

Most people probably don't need this., (*11)

Why did you make this?
  1. I was bored
  2. It didn't exist yet
  3. Someone might need it, someday
Are these really frequently asked questions?

No, this is a sham, just like all the other FAQs on GitHub., (*12)

The Versions

17/05 2015

dev-master

9999999-dev https://github.com/austinhyde/paperboat

A streaming JSON emitter

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Austin Hyde

json streaming

04/05 2015

v0.1.0

0.1.0.0 https://github.com/austinhyde/paperboat

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Austin Hyde