phpjms-serializer

JMSSerializer > don't return null value and empty array


I want to serialize some object but don't return null values or empty array. Here is my json:

{
"success": true,
"detail": {
    "id": 588,
    "label": null,
    "show_label": null,
    "type": null,
    "click_type": null,
    "child_site_module": null,
    "regex": null,
    "classes": [],
    "predefined_classes": [],
    "actions": [],
    "values": [],
    "forms": [],
    "display": false
}

}

I wish that:

 {
"success": true,
"detail": {
    "id": 588,
    "display": false
}

}

Here my code:

 $context = SerializationContext::create();
    if ($groups) {
        $context->setGroups($groups);
    }
    // RETURN
    return new Response(
        $this->getSerializer()->serialize(
            $values,
            'json',
            $context
        )
    );

Thanks to $context->setSerializeNull(TRUE); null value are no more returned, but still empty array. is there a way or option to do that ?


Solution

  • There is no context function like "setSerializeNull", but it is possible with annotation "@SkipWhenEmpty".