mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fix #16334: Serializer support \JsonSerializable
This commit is contained in:
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
||||
2.0.33 under development
|
||||
------------------------
|
||||
|
||||
- Bug #16334: Serializer support `\JsonSerializable` (germanow)
|
||||
- Bug #17798: Avoid creating folder for stream log targets in `FileTarget` (wapmorgan)
|
||||
- Bug #17850: Update to `ReplaceArrayValue` config exception message (alex-code)
|
||||
- Bug #17843: Fix `yii\web\Session::setCookieParamsInternal` check param "samesite" (schevgeny)
|
||||
|
@ -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) {
|
||||
|
@ -414,6 +414,17 @@ class SerializerTest extends TestCase
|
||||
|
||||
$this->assertEquals($expectedResult, $serializer->serialize($dataProvider));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/yiisoft/yii2/issues/16334
|
||||
*/
|
||||
public function testSerializeJsonSerializable()
|
||||
{
|
||||
$serializer = new Serializer();
|
||||
$model = new TestModel3();
|
||||
|
||||
$this->assertEquals(['customField' => 'test3/test4'], $serializer->serialize($model));
|
||||
}
|
||||
}
|
||||
|
||||
class TestModel extends Model
|
||||
@ -457,3 +468,30 @@ class TestModel2 extends Model
|
||||
return static::$extraFields;
|
||||
}
|
||||
}
|
||||
|
||||
class TestModel3 extends Model implements \JsonSerializable
|
||||
{
|
||||
public static $fields = ['field3', 'field4'];
|
||||
public static $extraFields = [];
|
||||
|
||||
public $field3 = 'test3';
|
||||
public $field4 = 'test4';
|
||||
public $extraField4 = 'testExtra2';
|
||||
|
||||
public function fields()
|
||||
{
|
||||
return static::$fields;
|
||||
}
|
||||
|
||||
public function extraFields()
|
||||
{
|
||||
return static::$extraFields;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'customField' => $this->field3.'/'.$this->field4,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user