phpscript

manual

ClassGo

JSON\Encoder

class JSON\Encoder {

}

Description

The streaming pair. json_encode() and json_decode() work on a whole string, which means holding the whole document; these work on a stream, as a request body and a response are. `new JSON\Decoder(fopen("php://input", "r"))` reads a POST body without a string of it existing first, and an encoder over php://output writes the response as the script builds it. Both take the io.Reader or io.Writer they wrap and any handle fopen() returns works, and so do STDIN and STDOUT.

JSON\Encoder is a class phpscript declares.

$object = new JSON\Encoder($stream);

Methods

public function encode(mixed $value): void

encode writes $value to the stream as JSON, followed by a newline.

The newline is Go's, not php's: an Encoder writes a stream of values, and the newline is what separates one from the next. A single value followed by a newline is still valid JSON to any reader.

The go method Encode.

public function set_indent(string $prefix, string $indent): void

set_indent is json.Encoder.SetIndent: the encoder breaks each value across lines, $indent per level under $prefix, and two empty strings go back to one line per value.

The go method SetIndent.

Binding

See also