Reputation: 7750
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
"jms/serializer-bundle": "^1.0"
composer and run updatenew JMS\SerializerBundle\JMSSerializerBundle()
to AppKernel
jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy
to parameters.yml
Upvotes: 2
Views: 7303
Reputation: 4089
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.
Upvotes: 3