PHPackages                             wyrihaximus/react-stream-json - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. wyrihaximus/react-stream-json

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wyrihaximus/react-stream-json
=============================

Incremental JSON encoder writing to a react/stream

1.4.2(2y ago)762.4k—0%2[8 PRs](https://github.com/WyriHaximus/reactphp-stream-json/pulls)2MITPHPPHP ^8.1

Since Jul 21Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/WyriHaximus/reactphp-stream-json)[ Packagist](https://packagist.org/packages/wyrihaximus/react-stream-json)[ GitHub Sponsors](https://github.com/WyriHaximus)[ RSS](/packages/wyrihaximus-react-stream-json/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (26)Used By (2)

Incremental JSON stream for [ReactPHP](https://github.com/reactphp/) streams
============================================================================

[](#incremental-json-stream-for-reactphp-streams)

[![Continuous Integration](https://github.com/wyrihaximus/reactphp-stream-json/workflows/Continuous%20Integration/badge.svg)](https://github.com/wyrihaximus/reactphp-stream-json/workflows/Continuous%20Integration/badge.svg)[![Latest Stable Version](https://camo.githubusercontent.com/73ca09df345d54ad5f5dfe2fe4357216cf624385bcf8ee4c6255f8e9d1641f7f/68747470733a2f2f706f7365722e707567782e6f72672f77797269686178696d75732f72656163742d73747265616d2d6a736f6e2f762f737461626c652e706e67)](https://packagist.org/packages/wyrihaximus/react-stream-json)[![Total Downloads](https://camo.githubusercontent.com/c067335f49246fa83a146c221e36844e799d236001e43267cf4206a345696674/68747470733a2f2f706f7365722e707567782e6f72672f77797269686178696d75732f72656163742d73747265616d2d6a736f6e2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/wyrihaximus/react-stream-json/stats)[![Type Coverage](https://camo.githubusercontent.com/1a9656dee27acaf58f2719aa098f4c5634378dc9680e2567eebe14c60a489a21/68747470733a2f2f73686570686572642e6465762f6769746875622f57797269486178696d75732f72656163747068702d73747265616d2d6a736f6e2f636f7665726167652e737667)](https://shepherd.dev/github/WyriHaximus/reactphp-stream-json)[![License](https://camo.githubusercontent.com/afe352460f4784485a7795a0304e05f695e1066d34e434454d0d570388292782/68747470733a2f2f706f7365722e707567782e6f72672f77797269686178696d75732f72656163742d73747265616d2d6a736f6e2f6c6963656e73652e706e67)](https://packagist.org/packages/wyrihaximus/react-stream-json)

### Installation

[](#installation)

To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `^`.

```
composer require wyrihaximus/react-stream-json

```

### Usage

[](#usage)

The `JsonStream` implements the `ReadableStreamInterface` from `react/stream` and behaves like the `ThroughStream`, the moment you `write*` to it, it will emit data.

The following example has a fixed number of items in the JSON and can be written to `end` with out needed a `write*` call.

```
$stream = new ThroughStream();
$anotherStream = new ThroughStream();

$jsonStream = new JsonStream();
$jsonStream->end([
    'key' => 'value',
    'promise' => resolve('value'),
    'stream' => $stream,
    'nested' => [
        'a' => 'b',
        'c' => 'd',
    ],
    'nested_promises' => [
        resolve('first'),
        resolve('second'),
    ],
    'nested_mixed' => [
        'first',
        resolve($anotherStream),
        resolve('third'),
    ],
]);

$stream->end('stream contents');
$anotherStream->end('second');
```

Stream contents will be: `{"key":"value","promise":"value","stream":"stream contents","nested":{"a":"b","c":"d"},"nested_promises":["first","second"],"nested_mixed":["first","second","third"]}`

### Methods

[](#methods)

All the following methods try to resolve `$value`, when it encounters a promise it will wait for the promise to resolve, and when it encounters a stream it will forward the stream's contents to it's own listeners. Promises can resolve to a stream but not vise versa. Any other parameters will be run though `json_encode`, except for arrays, those will be searched through for promises and streams.

##### write

[](#write)

`write(string $key, $value)` accepts a key and a value as argument. Writing a new key and value pair to the stream.

##### writeValue

[](#writevalue)

`write($value)` accepts only a value as argument. Writing the value pair to the stream.

##### writeArray

[](#writearray)

`writeArray(array $values)` will iterate over the items in the array and call `write` or `writeValue` depending on the type of the key.

##### writeObservable

[](#writeobservable)

`writeObservable(ObservableInterface $values)` will subscribe to the observable and call `writeValue` on each item coming in.

##### end

[](#end)

`end(array $values = [])` will call `writeArray` when `$values` contains something and then or otherwise end the stream. At that point no new values are accepted and it continues to operate any outstanding promises or streams have been resolve/completed.

### Caveats

[](#caveats)

The stream doesn't know if you want to write an object or an array so it assumes an object. It does try to detect when you haven't written anything yet and call `writeArray` or `end`with an array of items. You can force writing an array or object by calling `JsonStream::createArray`or `JsonStream::createObject` when creating an instance of `JsonStream`. Writing object items to a stream set up as array or vise versa will result in malformed `JSON`. In short you MUST know what kind of `JSON` you will be writing.

When using [`write`](#write) the key parameter isn't checked duplicates resulting in writing it out again to the stream. Bear in mind that while `PHP` considers this perfectly valid `JSON`, the [`JSON` spec](https://tools.ietf.org/html/rfc7159) doesn't specify a behavior for this. So your milage might vary, as described in [section 4](https://tools.ietf.org/html/rfc7159#section-4) of RFC7159, in `PHP`'s case it will only use the value from the last occurrence.

### Factories

[](#factories)

This package ships with a factory containing factory methods for arrays `JsonStreamFactory::createFromArray` and observables `JsonStreamFactory::createFromObservable`. Both will create a few stream, pause it, write the array/observable to it, and end it.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

License
-------

[](#license)

Copyright 2019 [Cees-Jan Kiewiet](http://wyrihaximus.net/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance61

Regular maintenance activity

Popularity34

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 69.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~239 days

Recently: every ~377 days

Total

10

Last Release

1057d ago

PHP version history (4 changes)1.0.0PHP ^7.0

1.1.0PHP ^7.2

1.4.0PHP ^8 || ^7.4

1.4.1PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/147145?v=4)[Cees-Jan Kiewiet](/maintainers/WyriHaximus)[@WyriHaximus](https://github.com/WyriHaximus)

---

Top Contributors

[![WyriHaximus](https://avatars.githubusercontent.com/u/147145?v=4)](https://github.com/WyriHaximus "WyriHaximus (92 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (16 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (15 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (6 commits)")[![wpjscc](https://avatars.githubusercontent.com/u/76907477?v=4)](https://github.com/wpjscc "wpjscc (2 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (1 commits)")

---

Tags

hacktoberfestjsonjson-serializationphpphp7reactphpstream

### Embed Badge

![Health badge](/badges/wyrihaximus-react-stream-json/health.svg)

```
[![Health](https://phpackages.com/badges/wyrihaximus-react-stream-json/health.svg)](https://phpackages.com/packages/wyrihaximus-react-stream-json)
```

###  Alternatives

[react/react

ReactPHP: Event-driven, non-blocking I/O with PHP.

9.1k3.6M63](/packages/react-react)[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

22.8k69.3k](/packages/grumpydictator-firefly-iii)[react/child-process

Event-driven library for executing child processes with ReactPHP.

34076.1M136](/packages/react-child-process)[laragraph/utils

Utilities for using GraphQL with Laravel

2113.8M7](/packages/laragraph-utils)[firefly-iii/data-importer

Firefly III Data Import Tool.

7545.8k](/packages/firefly-iii-data-importer)[wyrihaximus/react-child-process-messenger

Messenger decorator for react/child-process

32279.4k4](/packages/wyrihaximus-react-child-process-messenger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
