Symfony: Deserialize to empty array instead of null

Is it possible to tell the symfony deserializer to set an empty array instead of null, whenever it finds an empty value and the target type is array?

The following setter in my model will lead to an exception in the deserializer class, because the deserializer tries to set null, but only array is allowed as param.

But I don’t want to set array to null, instead it should be an empty array ([]) to have it easier on other places (no null checks before loops etc.)

public function setError(array $error)
    $this->error = $error;
    return $this;

"Failed to denormalize attribute "error" value for class …: Expected argument of type "array", "null" given at property path "error".

I am using a simple config for the serializer

new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);

Any ideas?


Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *