How to show null value in JSON in FOS Rest Bundle with JMS Serializer? How to show null value in JSON in FOS Rest Bundle with JMS Serializer? symfony symfony

How to show null value in JSON in FOS Rest Bundle with JMS Serializer?


You can set the following option in the config since recently:

fos_rest:    serializer:        serialize_null: true


Try this

in your controller

    $entity = $this->getEntity($id);    $context = new SerializationContext();    $context->setSerializeNull(true);    $serializer = $this->get('jms_serializer');    $response = new Response($serializer->serialize($entity, 'json', $context));    $response->headers->set('Content-Type', 'application/json');    return $response;

But the interaction with the fosrestbundle about configs is not known to me.


The easiest way to make this feature works like a charm

Add the following extra configuration to your fos_rest config option:

fos_rest:    serializer:        serialize_null: true