Fix #16334: Serializer support \JsonSerializable

This commit is contained in:
Ivan Hermanov
2020-02-21 16:20:29 +02:00
committed by GitHub
parent 79dbd91246
commit 18f7970d80
3 changed files with 42 additions and 1 deletions

View File

@ -137,7 +137,7 @@ class Serializer extends Component
* Serializes the given data into a format that can be easily turned into other formats.
* This method mainly converts the objects of recognized types into array representation.
* It will not do conversion for unknown object types or non-object data.
* The default implementation will handle [[Model]] and [[DataProviderInterface]].
* The default implementation will handle [[Model]], [[DataProviderInterface]] and [\JsonSerializable](https://www.php.net/manual/ru/class.jsonserializable.php).
* You may override this method to support more object types.
* @param mixed $data the data to be serialized.
* @return mixed the converted data.
@ -146,6 +146,8 @@ class Serializer extends Component
{
if ($data instanceof Model && $data->hasErrors()) {
return $this->serializeModelErrors($data);
} elseif ($data instanceof \JsonSerializable) {
return $data->jsonSerialize();
} elseif ($data instanceof Arrayable) {
return $this->serializeModel($data);
} elseif ($data instanceof DataProviderInterface) {