Search code examples
phpsymfonyjmsserializerbundle

JMS Serializer exclude public properties


I have a class I want to serialize, but exclude a public property. So far I tried @Exclude but I did not work (see Result).

Any ideas how to solve this problem?

Entity:

class Checkout extends Entity
{
    /**
     * @Exclude()
     */
    public $normalizerCallbacks = [
        'a' => 1,
        'b' => 2,
    ];

    ..........

Serialize:

$checkout = $this->getDoctrine()->getRepository('App:Checkout')->find($id);

$serializer = $this->get('jms_serializer');
$data = $serializer->serialize($checkout, 'json');

Result:

{
    normalizer_callbacks: {
        a: 1
        b: 2
    }
}

Edit: Trial 2015-09-10 18 --> Not working

use JMS\Serializer\Annotation as JMS;

/**
 * User
 *
 * @ORM\Table(name="checkout", options={"collate"="utf8_general_ci"})
 * @ORM\Entity
 *
 * @JMS\ExclusionPolicy("none")
 */
class Checkout extends Entity
{
    /**
     * @JMS\Exclude();
     */
    public $normalizerCallbacks = [
...

Edit: Trial 2015-09-11 09 (Update 12) --> Configuration

  1. Add to "jms/serializer-bundle": "^1.0" composer and run update
  2. Add new JMS\SerializerBundle\JMSSerializerBundle() to AppKernel
  3. Add jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategyto parameters.yml
  4. Then used it as in Serialize

Solution

  • Try whether you included it correctly:

    use JMS\Serializer\Annotation as JMS;
    

    and then use it like:

    @JMS\Exclude();
    

    in your annotation.

    As well make sure your class is annoted with @ExclusionPolicy("none") (see http://jmsyst.com/libs/serializer/master/reference/annotations).

    You can as well do it the other way round, specifically @Expose while class is annotated with @ExlucsionPolicy("all").

    Make sure to clean your cache before re-testing it.