Skip to content

Core\Test\Response::jsonAs

$response->jsonAs<T>({maxDepth?: uint}): T$response->jsonAs<T>(
    {maxDepth?: uint},
): T

jsonAs<T>() reads the body of the answer to a request that Core\Test::request() sent, as one JSON object, and returns a new instance of the class T. The class needs the #[Core\Json\Derive] attribute. Write array<T> to read a JSON array with one object for each element. It works like Core\Request::jsonAs, which reads the body of a request.

If a field is missing or has the wrong type, jsonAs throws a ParseError. The error has one entry in issues for each wrong field. An empty body throws a ParseError too. The maxDepth option sets how deep the document may nest. The default is 512.

The body is text that your own program wrote. So a string property of T does not need to be tainted. Each call creates new objects.

The examples below show how to read an answer into a class, how to read a list, and what happens when a field has the wrong type.

Options

  • maxDepthuintoptionalHow deep the document may nest. The default is 512, and the value must be from 1 to 1024. A document with no array or object in it has depth 1.

Return value

Returns T. A new T with its properties read from the body, or one T for each element for an array<T>. Every call creates new objects.

Errors

  • LogicErrorT has no #[Core\Json\Derive] attribute. Or maxDepth is not from 1 to 1024.
  • ParseErrorThe body is not one whole JSON object, or it nests deeper than maxDepth. An empty body throws this error too. It is also thrown when a field is missing or has the wrong type. The issues list has one entry for each wrong field. The Content-Type header is not checked.
  • RuntimeErrorThe body is not valid UTF-8, so it is not text.
  • RecursionErrorThe call stack is full when a constructor runs. This happens when the call starts with the stack almost full, or when a constructor recurses deeply. A deep body within maxDepth does not throw this error.

Examples

Read an answer into a class

<?nvs
// Shows how to read the JSON answer of an API into an instance of your own class.
use Core\Http\Method;
use Core\Test;
// `#[Core\Json\Derive]` gives the class a JSON format. Each public property is one key.
#[Core\Json\Derive]
class Product
{
public string $sku;
public int $price;
public function constructor(string $sku, int $price)
{
$this->sku = $sku;
$this->price = $price;
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sends one product as JSON.
Core\Response::sendJson(['sku' => 'mug-7', 'price' => 12]);
} else {
var $response = Test::request(Method::Get, '/products/mug-7');
// The class between `<` and `>` is the class to create. The result is a `Product`.
var $product = $response->jsonAs<Product>();
// This prints `mug-7 costs 12`.
echo $product->sku, ' costs ', $product->price, "\n";
}
Output
mug-7 costs 12

Read a list

<?nvs
// Shows how to read a JSON array of objects into a list of your own class.
use Core\Http\Method;
use Core\Test;
#[Core\Json\Derive]
class Product
{
public string $sku;
public int $price;
public function constructor(string $sku, int $price)
{
$this->sku = $sku;
$this->price = $price;
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sends a list of two products.
Core\Response::sendJson([['sku' => 'mug-7', 'price' => 12], ['sku' => 'cup-2', 'price' => 8]]);
} else {
var $response = Test::request(Method::Get, '/products');
// `array<Product>` creates one `Product` for each element.
var $products = $response->jsonAs<array<Product>>();
// This prints `mug-7` and then `cup-2`.
foreach ($products as Product $product) {
echo $product->sku, "\n";
}
}
Output
mug-7
cup-2

A field with the wrong type

<?nvs
// Shows how a test finds an answer whose JSON has a field with the wrong type.
use Core\Http\Method;
use Core\Test;
#[Core\Json\Derive]
class Product
{
public string $sku;
public int $price;
public function constructor(string $sku, int $price)
{
$this->sku = $sku;
$this->price = $price;
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. The price is a string, but `Product`
// needs an `int`.
Core\Response::sendJson(['sku' => 'mug-7', 'price' => '12']);
} else {
var $response = Test::request(Method::Get, '/products/mug-7');
// `jsonAs` throws a `ParseError`. Its `issues` list has one entry for
// each wrong field. This prints `1 wrong field`.
try {
$response->jsonAs<Product>();
} catch (ParseError $error) {
echo Core\Arr::count($error->issues), " wrong field\n";
}
}
Output
1 wrong field