https://github.com/tarlepp/badr-ferrassi
https://github.com/tarlepp/badr-ferrassi
community-help example serializer symfony
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tarlepp/badr-ferrassi
- Owner: tarlepp
- License: mit
- Created: 2020-10-17T12:52:36.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-10T19:00:31.000Z (about 4 years ago)
- Last Synced: 2024-04-09T21:52:31.164Z (almost 2 years ago)
- Topics: community-help, example, serializer, symfony
- Language: PHP
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# What is this?
Just a quick demo how to use Symfony `Serializer` component.
## Question
Someone asked following on Symfony Slack workspace;
```
Hello,
i follow the Serializer component documentation, to turn specific json into object
https://symfony.com/doc/current/components/serializer.html#deserializing-an-object
I make this call to receive a list of objects.
$response = $httpClient->request('GET', 'https://www.example.com/web/app.php/api/articles?next=1&Type=ep');
dump($response->getContent());
So I have an list of Article Objects.
{
"header": {
"error": 0,
"message": "ok",
"next": 2
},
"results": [
{
"id": 17007,
"title": "test article title 1"
},
{
"id": 17008,
"title": "sample article 2"
}
]
}
I would like to deserialize the results part... The problem is that the Symfony Serializer gives me one null object
$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$articles = $serializer->deserialize($response, Articles::class, 'json');
dump($articles);
what is the probleme please ?
```
So this repository contains simple example how to make that happen
## Authors
* [Tarmo Leppänen](https://github.com/tarlepp)
## License
[The MIT License (MIT)](LICENSE)
Copyright © 2020 Tarmo Leppänen