Skip to content

Deserialize return array #91

@dev-yann

Description

@dev-yann

Hi, I would like to perform a recursive deserialization. But the problem is that the function return an array instead of object.
services.yaml =>

  # Add DateTime Normalizer to Dunglas' Doctrine JSON ODM Bundle
    dunglas_doctrine_json_odm.serializer:
        class: Dunglas\DoctrineJsonOdm\Serializer
        arguments:
            - ['@dunglas_doctrine_json_odm.normalizer.array', '@serializer.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.object']
            - ['@serializer.encoder.json']
        public: true
$userOrder = $serializer->deserialize($request->getContent(), UserOrder::class, 'json');

$userorder is an array instead of object.

this is my entity =>

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\MaxDepth;

/**
 * @ORM\Entity
 */
class UserOrder
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="UUID")
     * @ORM\Column(type="guid", unique=true)
     */
    private $id;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $status;

    /**
     * @var OrderDetail[] | ArrayCollection
     * @ORM\OneToMany(targetEntity=OrderDetail::class, mappedBy="order", orphanRemoval=true, cascade={"persist"})
     */
    private $orderDetails;
....

I can get an object with this instruction in my controller :

$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
$serializer = new Serializer([new ArrayDenormalizer(), $normalizer], [new JsonEncoder()]);
$userOrder = $serializer->deserialize($request->getContent(), UserOrder::class, 'json');

But I would like to use the serializer service directly, is there any way for perform a recursive deserialization with doctrine-json-odm and get real object, not an array ? Thanks for your time

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions