/** * Asserts during serialization, that provided data is either an object, or has custom handler registered. * * @param mixed $data * @param array $type * @param \JMS\Serializer\Context $context * @throws \LogicException Thrown, when a primitive type has no custom handler registered. */ public function assertObjectOrCustomHandlerForSerialization($data, $type, Context $context) { //Ok during deserialization if ($context->getDirection() === static::DIRECTION_DESERIALIZATION) { return; } //Ok, if data is an object if (is_object($data) || $data === null) { return; } //Ok, if custom handler exists if (null !== $this->handlerRegistry->getHandler($context->getDirection(), $type['name'], $context->getFormat())) { return; } //Not ok - throw an exception throw new LogicException('Expected object but got ' . gettype($data) . '. Do you have the wrong @Type mapping or could this be a Doctrine many-to-many relation?'); }