I'm not quite sure what you exactly mean, but can't you use the @postserialize and @postdeserialize (http://jmsyst.com/libs/serializer/master/reference/annotations#postserialize) annotations?
Symfony2 JMSSerializer replace inline text
-
19-09-2022 - |
Question
I'm looking for a simple, stupid solution, to change the output of serialized fields to replace several patterns (for user messages etc) in symfony2.
Unfortunately, the usage of Custom De-/Serialization Handlers destroys all my policies. I'm forced to return all serialized data by hand.
The Model class:
//src/Acme/DemoBundle/Model/Tiger
class Tiger implements WildAnimalInterface
{
/**
* @var integer
* @Expose
*/
protected $id;
/**
* @var string
* @Expose
* @Type('user_message')
*/
protected $text;
/**
* @var integer
* @Exclude
*/
protected $dangerLevel;
...
}
The Handler class:
class UserMessageHandler
{
...
public function serialize(VisitorInterface $visitor,
WildAnimalInterface $wildAnimal,
$type,
$visited)
{
// Hello {{username}}, I'm a Tiger ROOARRR!
$text = $wildAnimal->getText();
// (my custom replacement logic, it works already)
// Hello Joe Schmoe, I'm a Tiger ROOARRR!
return $text; // < Overwrites all rules and returns only text!
}
}
Solution
OTHER TIPS
I had the same problem and finally found the solution, I explain it here : Format input and output fields with JMSSerializer (handle single property)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow