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

Deserializing top-level sequence #52

Open
balthild opened this issue Feb 17, 2024 · 2 comments
Open

Deserializing top-level sequence #52

balthild opened this issue Feb 17, 2024 · 2 comments

Comments

@balthild
Copy link

balthild commented Feb 17, 2024

Detailed description

I wonder that how to deserialize top-level sequences into an object. I found that something similar already exists in this library, but limited to CSV.

For example, I have such a PHP array

$data = [
  ['id' => 1],
  ['id' => 2],
];

And the DTOs

readonly class MyList {
  public array $items;
}

readonly class MyItem {
  public int $id;
}

It seems that I cannot deserialize $data into an instance of MyList. I tried adding these attributes to MyList#items

#[Field(flatten: true)]
#[SequenceField(arrayType: MyItem::class)]

It deserializes the array without errors, but the type of items becomes associative array rather than MyItem. Seems like the SequenceField attribute is ignored if flatten: true.

Context

I am refactoring the API in an old project. Some of the routes accepts JSON arrays as its body, and I found that Serde does not provide a simple way to deserialize the arrays into strictly-typed objects.

Possible implementation

Generalize the feature existing in the CSV handler. The analogy in Rust's serde (transparent) could be a good API design for this feature.

Your environment

  • Version used: PHP 8.3
  • Operating system and version: Arch Linux
  • Link to your project: N/A
@Crell
Copy link
Owner

Crell commented Feb 18, 2024

I think you just need to remove the Flatten directive?

I just ran a quick test using the PointList class, used in the CSV formatter tests. It worked fine with the standard formatters. What part of CSV-specific handling do you need?

Can you PR a failing test to better describe what you're trying to do? That would make it easier to determine what to do with it.

@balthild
Copy link
Author

That's strange... I tried removing #[Field(flatten: true)] before and it didn't work. I tried in an MWE just now, and it still doesn't work.

The MWE: https://gist.github.com/balthild/37f72106f775e6b25417d4e778a38d5b

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

2 participants