Сериализация и десериализация объектов UUID в Symfony - PullRequest
0 голосов
/ 13 января 2020

Я пытаюсь сериализовать объекты до JSON, а затем десериализовать их. Я использую ramsey / uuid для всех идентификаторов.

/**
 * NotificationService constructor.
 */
public function __construct(ObjectManager $manager, EntityManagerInterface $entityManager, SerializerInterface $serializer)
{
    $this->manager = $manager;
    $this->entityManager = $entityManager;
    $this->pollRepository = $this->entityManager->getRepository(Poll::class);
    $this->threadRepository = $this->entityManager->getRepository(ForumThread::class);
    $this->notificationRepository = $this->entityManager->getRepository(Notification::class);
    $this->serializer = $serializer;
}

/**
 * @param $objectToSerialize
 *
 * @return string
 */
public function jsonEncode($objectToSerialize)
{
    // Serialize my object in Json
    return $this->serializer->serialize($objectToSerialize, 'json', [
        'circular_reference_handler' => function ($object) {
            return $object->getId();
        },
    ]);
}

/**
 * @param $json
 * @param $class
 *
 * @return mixed
 */
public function jsonDecode($json, $class)
{
    return $this->serializer->deserialize($json, $class, 'json');
}

Моя функция jsonEncode работает отлично. Но второй jsonDecode показывает мне эту ошибку:

Symfony\Component\Serializer\Exception\NotNormalizableValueException

The type of the "id" attribute for class "App\Entity\Poll" must be one of "Ramsey\Uuid\UuidInterface" ("string" given).

Спасибо за помощь

...