PHPackages                             violet/streaming-json-encoder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. violet/streaming-json-encoder

ActiveLibrary[HTTP &amp; Networking](/categories/http)

violet/streaming-json-encoder
=============================

Library for iteratively encoding large JSON documents piece by piece

v1.1.5(3y ago)3162.1M↓19.9%21[3 PRs](https://github.com/violet-php/streaming-json-encoder/pulls)5MITPHPPHP &gt;=5.6.0

Since Feb 26Pushed 2y ago10 watchersCompare

[ Source](https://github.com/violet-php/streaming-json-encoder)[ Packagist](https://packagist.org/packages/violet/streaming-json-encoder)[ Docs](http://violet.riimu.net)[ RSS](/packages/violet-streaming-json-encoder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (8)Used By (5)

Streaming JSON Encoder
======================

[](#streaming-json-encoder)

*Streaming JSON Encoder* is a PHP library that provides a set of classes to help with encoding JSON in a streaming manner, i.e. allowing you to encode the JSON document bit by bit rather than encoding the whole document at once. Compared to the built in `json_encode` function, there are two main advantages:

- You will not need to load the entire data set into memory, as the encoder supports iterating over both arrays and any kind of iterators, like generators, for example.
- You will not need to load the entire resulting JSON document into the memory, since the JSON document will be encoded value by value and it's possible to output the encoded document piece by piece.

In other words, the Streaming JSON Encoder can provide the greatest benefit when you need to handle large data sets that may otherwise take up too much memory to process.

In order to increase interoperability, the library also provides a PSR-7 compatible stream to use with frameworks and HTTP requests.

The API documentation is available at:

[![CI](https://camo.githubusercontent.com/a8ecf56514368d20cf226efc2e9640af2561bcbdec885f5dcbdb4a105273aafd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f76696f6c65742d7068702f73747265616d696e672d6a736f6e2d656e636f6465722f43492f6d61696e3f7374796c653d666c61742d737175617265)](https://github.com/violet-php/streaming-json-encoder/actions)[![Scrutinizer](https://camo.githubusercontent.com/ac62efa957f7e64e576055e04d96b3ad3c77998bdeaf4c94d0d39cbb984c5f21/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f76696f6c65742d7068702f73747265616d696e672d6a736f6e2d656e636f6465722f6d61696e3f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/violet-php/streaming-json-encoder/)[![codecov](https://camo.githubusercontent.com/4d67abd6b35dad83e847470195a16064053cffecdabba806adb557acb12adf08/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f76696f6c65742d7068702f73747265616d696e672d6a736f6e2d656e636f6465722f6d61696e3f7374796c653d666c61742d737175617265)](https://codecov.io/gh/violet-php/streaming-json-encoder)[![Packagist](https://camo.githubusercontent.com/88d8a910f73a096ce790dc8dc2d6d99d08b37ed19dc0a3343dca6f750b1d7d29/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f76696f6c65742f73747265616d696e672d6a736f6e2d656e636f6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/violet/streaming-json-encoder)

Requirements
------------

[](#requirements)

- The minimum supported PHP version is 5.6
- The library depends on the following external PHP libraries:
    - [psr/http-message](https://packagist.org/packages/psr/http-message) (`^1.0`)

Installation
------------

[](#installation)

### Installation with Composer

[](#installation-with-composer)

The easiest way to install this library is to use Composer to handle your dependencies. In order to install this library via Composer, simply follow these two steps:

1. Acquire the `composer.phar` by running the Composer [Command-line installation](https://getcomposer.org/download/)in your project root.
2. Once you have run the installation script, you should have the `composer.phar`file in you project root and you can run the following command:

    ```
    php composer.phar require "violet/streaming-json-encoder:^1.1"

    ```

After installing this library via Composer, you can load the library by including the `vendor/autoload.php` file that was generated by Composer during the installation.

### Adding the library as a dependency

[](#adding-the-library-as-a-dependency)

If you are already familiar with how to use Composer, you may alternatively add the library as a dependency by adding the following `composer.json` file to your project and running the `composer install` command:

```
{
    "require": {
        "violet/streaming-json-encoder": "^1.1"
    }
}
```

### Manual installation

[](#manual-installation)

If you do not wish to use Composer to load the library, you may also download the library manually by downloading the [latest release](https://github.com/violet-php/streaming-json-encoder/releases/latest)and extracting the `src` folder to your project. You may then include the provided `src/autoload.php` file to load the library classes.

Please note that using Composer will also automatically download the other required PHP libraries. If you install this library manually, you will also need to make those other required libraries available.

Usage
-----

[](#usage)

This library offers 3 main different ways to use the library via the classes `BufferJsonEncoder`, `StreamJsonEncoder` and the PSR-7 compatible stream `JsonStream`.

### Using BufferJsonEncoder

[](#using-bufferjsonencoder)

The buffer encoder is most useful when you need to generate the JSON document in a way that does not involve passing callbacks to handle the generated JSON.

The easiest way to use the `BufferJsonEncoder` is to instantiate it with the JSON value to encode and call the `encode()` method to return the entire output as a string:

```
