JsonSerializable::jsonSerialize

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

JsonSerializable::jsonSerializeEspecifica los datos que deben ser serializados en JSON

Descripción

public function JsonSerializable::jsonSerialize(): mixed

Serializa el objeto en un valor que puede ser serializado nativamente por la función json_encode().

Parámetros

Esta función no contiene ningún parámetro.

Valores devueltos

Devuelve los datos que pueden ser serializados por la función json_encode(), que pueden ser valores de cualquier tipo excepto un resource.

Ejemplos

Ejemplo #1 Ejemplo con JsonSerializable::jsonSerialize() devolviendo un array

<?php
class ArrayValue implements JsonSerializable {
    private $array;
    public function __construct(array $array) {
        $this->array = $array;
    }

    public function jsonSerialize(): mixed {
        return $this->array;
    }
}

$array = [1, 2, 3];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>

El ejemplo anterior mostrará:

[
    1,
    2,
    3
]

Ejemplo #2 Ejemplo con JsonSerializable::jsonSerialize() devolviendo un array asociativo

<?php
class ArrayValue implements JsonSerializable {
    private $array;
    public function __construct(array $array) {
        $this->array = $array;
    }

    public function jsonSerialize() {
        return $this->array;
    }
}

$array = ['foo' => 'bar', 'quux' => 'baz'];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>

El ejemplo anterior mostrará:

{
    "foo": "bar",
    "quux": "baz"
}

Ejemplo #3 Ejemplo con JsonSerializable::jsonSerialize() devolviendo un int

<?php
class IntegerValue implements JsonSerializable {
    private $number;
    public function __construct($number) {
        $this->number = (int) $number;
    }

    public function jsonSerialize() {
        return $this->number;
    }
}

echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
?>

El ejemplo anterior mostrará:

1

Ejemplo #4 Ejemplo con JsonSerializable::jsonSerialize() devolviendo una string

<?php
class StringValue implements JsonSerializable {
    private $string;
    public function __construct($string) {
        $this->string = (string) $string;
    }

    public function jsonSerialize() {
        return $this->string;
    }
}

echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
?>

El ejemplo anterior mostrará:

"Hello!"