Download the PHP package violet/streaming-json-encoder without Composer

On this page you can find all versions of the php package violet/streaming-json-encoder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package 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:

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: http://violet.riimu.net/api/streaming-json-encoder/

CI Scrutinizer codecov Packagist

Requirements

Installation

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 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:

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

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:

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 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

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

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:

The most useful way to use this encoder, however, is to use it as an iterator. As the encoder implements the Iterator interface, you can simply loop over the generated JSON with a foreach loop:

It's also worth noting that the encoder supports iterators for values. What's more, any closure passed to the encoder will also be called and the return value used as the value instead. The previous example could also be written as:

As a side note, the encoder will respect the JsonSerializable interface as well and will call the jsonSerialize for objects that implement the interface.

Using StreamJsonEncoder

The stream encoder works very similarly to the BufferJsonEncoder as they extend the same abstract class. However, the key difference is in how they handle passing the JSON output.

The StreamJsonEncoder accepts a callable as the second constructor argument. Whenever JSON needs to be outputted, this callable is called with two arguments, the actual string to output and the type of the token to output (which is one of the JsonToken constants).

If no callable is passed, the StreamJsonEncoder will simply output the JSON using an echo statement. For example:

The encode() method in StreamJsonEncoder returns the total number of bytes it passed to the output. This encoder makes it convenient, for example, to write the JSON to file in a streaming manner. For example:

Using JsonStream

The stream class provides a PSR-7 compatible StreamInterface for streaming JSON content. It actually uses the BufferJsonEncoder to do the hard work and simply wraps the calls in a stream like fashion.

The constructor of JsonStream either accepts a value to encode as JSON or an instance of BufferJsonEncoder (which allows you to set the encoding options). You can then operate on the stream using the methods provided by the PSR-7 interface. For example:

For more information about PSR-7 streams, please refer to the PSR-7 documentation.

How the encoder resolves values

In many ways, the Streaming JSON encoder is intended to work mostly as a drop in replacement for json_encode(). However, since the encoder is intended to deal with large data sets, there are some notable differences in how it handles objects and arrays.

First, to determine how to encode an object, the encoder will attempt to resolve the object values in following ways:

The returned value is looped until it cannot be resolved further. After that, a decision is made on whether the array or object is encoded as an array or as an object. The following logic is used:

Note, however, that if the JSON encoding option JSON_FORCE_OBJECT is used, all objects and arrays are encoded as JSON objects.

Note that all objects are traversed via a foreach statement. This means that all Traversable objects are encoded using the values returned by the iterator. For other objects, this means that the public properties are used (as per default iteration behavior).

All other values (i.e. nulls, booleans, numbers and strings) are treated exactly the same way as json_encode() does (and in fact, it's used to encode those values).

JSON encoding options

Both BufferJsonEncoder and StreamJsonEncoder have a method setOptions() to change the JSON encoding options. The accepted options are the same as those accepted by json_encode() function. The encoder still internally uses the json_encode() method to encode values other than arrays or object. A few options also have additional effects on the encoders:

Credits

This library is Copyright (c) 2017-2022 Riikka Kalliomäki.

See LICENSE for license and copying information.


All versions of streaming-json-encoder with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6.0
psr/http-message Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package violet/streaming-json-encoder contains the following files

Loading the files please wait ....