Core\Response::sendJson
Core\Response::sendJson(mixed $value): voidCore\Response::sendJson( mixed $value, ): void
Converts a value to JSON and sends it as the response. It also adds the header Content-Type: application/json, so the client knows that the body is JSON. Use it for an API that a program or a script in the browser reads.
It uses the same encoder as Core\Json::encode. The JSON is on one line, with no spaces. The value can be a shape, an array, a number, a string, null, or an object of a class with #[Core\Json\Derive]. Text from a visitor is allowed anywhere inside the value. Each string is written as a JSON string, so its quotes are escaped and it cannot change the JSON around it.
If the value contains something JSON cannot write, such as a float that is NaN, sendJson throws a LogicError and sends nothing.
The examples show a small object, a value that JSON cannot write, and a list of products for an API.
Parameters
$valuemixedThe value to send, of any typeCore\Json::encodeaccepts. Ataintedvalue is allowed anywhere inside it. Each string is written as a JSON string, so its quotes are escaped and it cannot change the JSON around it.
Return value
Returns void. Nothing. Using this and echo in one response does not compile.
Errors
LogicError$valuecontains something JSON cannot write: afloatthat is NaN or infinite, a value of a type JSON has no form for, an object of a class without#[Core\Json\Derive], an object that points to itself, or nesting deeper than 1024 levels. Nothing is sent.
Changelog
| Version | Change |
|---|---|
0.0.6 | The Core\Response methods that send a body now start with send: sendJson, sendHtml, sendText, sendBytes and sendSlotted. startStream starts a streamed response. The old names were json, html, text, bytes, slotted and stream. |
Examples
A small object
<?nvs// Shows how to send a small JSON object.
// A shape becomes a JSON object, with its keys in the order they are written.// This sends `{"status":"ok","version":3}`.Core\Response::sendJson({ status: 'ok', version: 3 });{"status":"ok","version":3}A value json cannot write
<?nvs// Shows what happens when a value contains a number that JSON cannot write.
// `Core\Math::divide(0.0, 0.0)` gives NaN, which has no JSON form.// So `sendJson` throws a `LogicError` and sends nothing.float $average = Core\Math::divide(0.0, 0.0);try { Core\Response::sendJson({ average: $average });} catch (LogicError $error) { // This sends a status 500 and a JSON object with the error message. Core\Response::setStatus(500); Core\Response::sendJson({ error: $error->message });}{"error":"Core\\Response::sendJson(): `NAN` has no JSON spelling — JSON has no `NaN` and no `Infinity`"}A list of products for an api
<?nvs// Shows how an API sends a list of products as JSON.
// `#[Core\Json\Derive]` lets JSON write an object of this class.// Each property becomes one key, in the order the properties are declared.#[Core\Json\Derive]class Product{ public int $id; public string $name; public float $price;
public function constructor(int $id, string $name, float $price) { $this->id = $id; $this->name = $name; $this->price = $price; }}
array<Product> $products = [ new Product(1, 'Green tea', 4.5), new Product(2, "Coffee \"Dark\"", 7.25),];
// The list becomes a JSON array. The quotes in the second name are escaped.Core\Response::sendJson({ count: 2, products: $products });{"count":2,"products":[{"id":1,"name":"Green tea","price":4.5},{"id":2,"name":"Coffee \"Dark\"","price":7.25}]}