Skip to content

Add test for JSON deserialization of a list into PointList object, ve…#83

Closed
shanginn wants to merge 2 commits intoCrell:masterfrom
shanginn:deserialize_test_example
Closed

Add test for JSON deserialization of a list into PointList object, ve…#83
shanginn wants to merge 2 commits intoCrell:masterfrom
shanginn:deserialize_test_example

Conversation

@shanginn
Copy link
Copy Markdown

This is a test to demonstrate desired behaviour of list deserialization. Such JSON array lists are common in the REST world and it's unclear how to deserialize them.

The only way to do it now it to json_decode the response, then iterate over it, json_encode each element and deserialize them individually. sure there is a better way?

@Crell
Copy link
Copy Markdown
Owner

Crell commented Jun 19, 2025

Unfortunately, this could be quite tricky. Serde is very much built around serializing to/from objects, not arbitrary arrays (or arrays of objects). I'm not sure off hand how we could do that. My best guess would be to not do so, but wrap it in another layer that first json_decode()s the value, then loops and calls Serde deserialize on each element of that, as an array.

That wouldn't work for streaming, of course, but we don't support streaming deserialization anyway yet.

@shanginn
Copy link
Copy Markdown
Author

Symfony serializer supports it if you pass the type as Point::class . '[]', like an array of objects (not just arbitrary array).

You can see it implemented in Symfony\Component\Serializer\Normalizer\ArrayDenormalizer. maybe you can get some inspiration from there? or is it too different?

@Crell
Copy link
Copy Markdown
Owner

Crell commented Jun 19, 2025

Fun fact! This turned out to be the same issue as #82, where someone suggested an almost-solution. I was able to carry it the rest of the way.

It still doesn't deserialize to a PHP array, but to an object that wraps an array. But it should work for a JSON array if you're careful with it.

Calling this issue closed as a duplicate of that one.

@Crell Crell closed this Jun 19, 2025
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

Successfully merging this pull request may close these issues.

2 participants