Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 6 commits into from
Closed

[Serializer] Add options to JsonDecode and JsonEncode to wrap/unwrap json data #28887

wants to merge 6 commits into from

Conversation

nonanerz
Copy link
Contributor

Add options to JsonDecode and JsonEncode to wrap/unwrap json data

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

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'])

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
Copy link
Contributor

@ogizanagi ogizanagi left a 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.

@nicolas-grekas nicolas-grekas added this to the next milestone Oct 17, 2018
@mcfedr
Copy link
Contributor

mcfedr commented Oct 17, 2018

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 serializer->deserilize($data, User:class, 'json', ['root'=> 'user'])

…n_decoder

# Conflicts:
#	src/Symfony/Component/Serializer/Encoder/JsonDecode.php
#	src/Symfony/Component/Serializer/Encoder/JsonEncode.php
@ogizanagi
Copy link
Contributor

Wild thoughts: do we need this as JsonEncoder option?
Instead, what about a denormalizer aware UnwrappingDenormalizer, registered with very high priority, unwrapping if UnwrappingDenormalizer::UNWRAP_PATH is provided & unsetting this key from context before calling injected denormalizer?
This way, this feature is seamlessly available for every formats. It'd also be available to any node, not only root.

@fabpot
Copy link
Member

fabpot commented Feb 21, 2019

What's the status of this PR?

nonanerz added 2 commits March 4, 2019 13:05
…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
@fabpot
Copy link
Member

fabpot commented Mar 31, 2019

This PR should be rebased as it contains a merge commit that will forbid it to be merged?

@fabpot
Copy link
Member

fabpot commented Apr 6, 2019

closing in favor of #30894

@fabpot fabpot closed this Apr 6, 2019
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
fabpot added a commit that referenced this pull request Apr 12, 2020
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
symfony-splitter pushed a commit to symfony/serializer that referenced this pull request Apr 12, 2020
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
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Apr 12, 2020
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants