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

Serialize Generator #1023

Closed
simPod opened this issue Dec 18, 2018 · 3 comments
Closed

Serialize Generator #1023

simPod opened this issue Dec 18, 2018 · 3 comments

Comments

@simPod
Copy link
Contributor

simPod commented Dec 18, 2018

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no

Steps required to reproduce the problem

function getData() {
   yield 1;
   yield 2;
}
...
$classToSerialize = new ClassToSerialize($this->getData()); // Save getData()'s result to inner iterable-typed property
// annotated with * @Serializer\Type("array<int>")
  1. $data = $serializer->serialize($classToSerialize, ...);

Expected Result

  • $data contains array [1, 2], Generator was converted to array

Actual Result

  • $data contains empty array, Generator was not converted to array

What do you think of auto converting generators to arrays when serializing?

@scyzoryck
Copy link
Collaborator

I can see two ways to add this feature.

  1. In SerializationGraphNavigator we can add
if ($data instanceof \Iterator) {
    return $this->visitor->visitArray(iterator_to_array($data), $type);
}
return $this->visitor->visitArray((array) $data, $type);
  1. Breaking change in SerializationVisitorInterface
public function visitArray(iterable $data, array $type);

@goetas
What do you think about it? 🤔

@goetas
Copy link
Collaborator

goetas commented Jan 14, 2019

Why not adding a custom handler for Generator and to exactly the same thing as for ArrayCollection ?

See https://github.com/schmittjoh/serializer/blob/master/src/Handler/ArrayCollectionHandler.php

@goetas
Copy link
Collaborator

goetas commented Feb 18, 2019

Solved in #1034

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

No branches or pull requests

3 participants