Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize json array to array of entities #376

Closed
manonthemoon42 opened this issue Apr 14, 2014 · 2 comments
Closed

Deserialize json array to array of entities #376

manonthemoon42 opened this issue Apr 14, 2014 · 2 comments

Comments

@manonthemoon42
Copy link

I have a json object that I received by making a get API call. I make this call to receive a list of objects. It's a list of post... So I have an array of Post Objects.

Here the output :

{
"total":2, 
"data":[
  {
    "id":2,
    "user":{
      "id":1,
      "username":"sandro.tchikovani"             
    },
    "description":"cool",
    "nb_comments":0,
    "nb_likes":0,
    "date_creation":"2014-04-13T20:07:34-0700"
  },
  {
    "id":1,
    "user":{
       "id":1,
       "username":"sandro.tchikovani",
     },
    "description":"Premier pooooste #lol",
    "nb_comments":0,
    "nb_likes":0,
    "date_creation":"2014-04-13T15:15:35-0700"
  }
  ]
 }

I would like to deserialize the data part... The problem is that the Serializer in Symfony gives me an error ...

The error that I have :

Class array<Moodress\Bundle\PosteBundle\Entity\Poste> does not exist

How I do deserialize :

$lastPosts = $serializer->deserialize($data['data'], 'array<Moodress\Bundle\PosteBundle\Entity\Poste>', 'json');

How can I deserialze the data array... To have an array of Postes. I want to give to my view .twig an array Poste... I did precise the type when I deserialize... So I can't find what is the problem...

Thanks.

@zeevin
Copy link

zeevin commented Dec 5, 2017

Have you solved the problem?

@BonBonSlick
Copy link

@zeevin

/**
 * @var TaxDetailsDTO[]|ArrayCollection
 *
 * @Assert\All({
 *    @Assert\Type("BuyerQuest\Tax\Domain\DTO\TaxDetailsDTO")
 * })
 * @Assert\Valid
 *
 * @Serializer\Type("ArrayCollection<BuyerQuest\Tax\Domain\DTO\TaxDetailsDTO>")
 * @Serializer\Expose
 */
public ArrayCollection $details;


/**
 * @param string $taxServiceShippingResponseJSON
 *
 * @return ArrayCollection<SupplierShippingTaxDTO>
 */
public function fromCollectionOfTaxServiceShippingResponsesJSON(
    string $taxServiceShippingResponseJSON
): ArrayCollection {
    return $this->serializer->deserialize(
        $taxServiceShippingResponseJSON,
        sprintf('ArrayCollection<integer, %s>', SupplierShippingTaxDTO::class),
        JsonEncoder::FORMAT
    );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants