symfonyjmsserializerbundlejms-serializer

@JMS\Exclude only if a property is empty


I am using JMS\Serializer in my project and I want to ignore one property only if the array in it is empty.

I tried something like :

@JMS\Exclude(if="count('$this->required') === 0")
or 
@JMS\Exclude(if="empty('required')")

but got a syntax error.

Can anyone help me on this?

thank.


Solution

  • What you need was implemented recently and it is in release-1.7 so you might as well wait for it. It is called @SkipWhenEmpty

    @SkipWhenEmpty This annotation can be defined on a property to indicate that the property should not be serialized if the result will be "empty".

    This is the bug related it.