JsonSerializable

The JsonSerializable interface is used to customize how an object is serialized into JSON using json_encode().

JsonSerializable simplifies the conversion to JSON by introducing a method to prepare the data. These data may be based on the properties of the object, but they may also change these names, create virtual and hide existing properties; the values may also be formatted to fit any specification.

There is no reverse method, where a JSON representation is converted into an object. It has to be done in a custom method.

<?php

class Point implements JsonSerializable {
    public function __construct(
        private int $x,
        private int $y,
        ) {
    }

    public function jsonSerialize(): mixed {
        return [
            'latitude' => $this->x,
            'longitude' => $this->y
        ];
    }
}

$point = new Point(10, 20);
echo json_encode($point); // {latitude:10,longitude:20}

?>

Documentation

See also How to Use the JsonSerializable Interface and The Problem with JsonSerializable and Doctrine when using Symfony.

Related : JavaScript Object Notation (JSON), Serialization, Parquet, PHP Native Interfaces