I have a very big entity with a lot of number values. Because most of these values have a unit, I created a @Unit
annotation to put that right into the model. Now I want to Serialize that entity.
Let's assume an entity like this:
class Entity {
/**
* @var string
*
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="smallint", nullable=true)
* @Unit("m")
*/
private $distance;
}
The output looks something like this:
{
"name": "bla",
"distance": 1.20
}
I want it to look like this:
{
"name": "bla",
"distance": {"value": 1.20, "unit": "m", "string": "1.20 m"}
}
I tried to use a custom handler but there is no way to call the default handler as far as I know.
public function handler(JsonSerializationVisitor $visitor, $data, array $type, Context $context)
{
$result = $context->accept($data);
// result is empty
// i can add stuff here but not call the default handler
return $result;
}
So how can I extend (not overwrite) the default behavior?
EDIT: I also tried pre- and post serialization event which doesn't work either because it seems the serializer assumes the type before calling the handler.