-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add options to JsonDecode and JsonEncode to wrap/unwrap json data #28887
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
Conversation
Add options to JsonDecode and JsonEncode to wrap/unwrap json data Add options to JsonDecode and JsonEncode to wrap/unwrap json data Add options to JsonDecode and JsonEncode to wrap/unwrap json data Add options to JsonDecode and JsonEncode to wrap/unwrap json data Add options to JsonDecode and JsonEncode to wrap/unwrap json data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nonanerz for suggesting. I'm not sure this is a feature we want in core, but let's see what others think.
I like this idea, its a common JSON API patternt to have the top level object nested, and you have to create lots of extra model classes to handle this. E.g. some api gives {user: {name: fred, id: 1}} - at the moment to use the serializer with this I have to create a class that I dont really need: class UserResponse {
function setUser(User $u) {...}
} With this it can be avoided and become |
…n_decoder # Conflicts: # src/Symfony/Component/Serializer/Encoder/JsonDecode.php # src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Wild thoughts: do we need this as |
What's the status of this PR? |
…n_decoder # Conflicts: # src/Symfony/Component/Serializer/Encoder/JsonDecode.php # src/Symfony/Component/Serializer/Encoder/JsonEncode.php # src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php # src/Symfony/Component/Serializer/Tests/Encoder/JsonEncodeTest.php
This PR should be rebased as it contains a merge commit that will forbid it to be merged? |
closing in favor of #30894 |
This PR was merged into the 5.1-dev branch. Discussion ---------- [Serializer] UnwrappingDenormalizer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a UnwrappingDenormalizer, registered with very high priority. Unwrapping the data if UNWRAP_PATH is provided. Very often some APIs give nested responses in which we need only the child object. With UnwrappingDenormalizer we can get the needed object without creating unnecessary Model class that we don't really need. Regarding to #28887 and #30894 Usage: `$serialiser->deserialize('{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}', Object::class, ['UnwrappingDenormalizer::UNWRAP_PATH' => '[baz][inner]'])` Commits ------- 00d103d UnwrappingDenormalizer
This PR was merged into the 5.1-dev branch. Discussion ---------- [Serializer] UnwrappingDenormalizer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a UnwrappingDenormalizer, registered with very high priority. Unwrapping the data if UNWRAP_PATH is provided. Very often some APIs give nested responses in which we need only the child object. With UnwrappingDenormalizer we can get the needed object without creating unnecessary Model class that we don't really need. Regarding to symfony/symfony#28887 and symfony/symfony#30894 Usage: `$serialiser->deserialize('{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}', Object::class, ['UnwrappingDenormalizer::UNWRAP_PATH' => '[baz][inner]'])` Commits ------- 00d103d5f7 UnwrappingDenormalizer
This PR was merged into the 5.1-dev branch. Discussion ---------- [Serializer] UnwrappingDenormalizer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a UnwrappingDenormalizer, registered with very high priority. Unwrapping the data if UNWRAP_PATH is provided. Very often some APIs give nested responses in which we need only the child object. With UnwrappingDenormalizer we can get the needed object without creating unnecessary Model class that we don't really need. Regarding to symfony/symfony#28887 and symfony/symfony#30894 Usage: `$serialiser->deserialize('{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}', Object::class, ['UnwrappingDenormalizer::UNWRAP_PATH' => '[baz][inner]'])` Commits ------- 00d103d5f7 UnwrappingDenormalizer
Add options to JsonDecode and JsonEncode to wrap/unwrap json data
Add options to JsonDecode and JsonEncode to wrap/unwrap json data.
So you can use:
$serialiser->deserialize("{object: {value: 1, other: 2}}", Object::class, ['json_root_key' => 'object'])