Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
nonanerz wants to merge6 commits intosymfony:masterfromnonanerz:add_option_to_json_decoder
Closed

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

nonanerz wants to merge6 commits intosymfony:masterfromnonanerz:add_option_to_json_decoder

Conversation

@nonanerz
Copy link
Contributor

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

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRn/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 dataAdd options to JsonDecode and JsonEncode to wrap/unwrap json dataAdd options to JsonDecode and JsonEncode to wrap/unwrap json dataAdd options to JsonDecode and JsonEncode to wrap/unwrap json dataAdd options to JsonDecode and JsonEncode to wrap/unwrap json data
Copy link
Contributor

@ogizanagiogizanagi 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-grekasnicolas-grekas added this to thenext milestoneOct 17, 2018
@mcfedr
Copy link
Contributor

mcfedr commentedOct 17, 2018
edited
Loading

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 {functionsetUser(User$u) {...}}

With this it can be avoided and becomeserializer->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 asJsonEncoder option?
Instead, what about a denormalizer awareUnwrappingDenormalizer, registered with very high priority, unwrapping ifUnwrappingDenormalizer::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

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
@fabpot
Copy link
Member

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

@fabpot
Copy link
Member

closing in favor of#30894

@fabpotfabpot closed thisApr 6, 2019
@nicolas-grekasnicolas-grekas modified the milestones:next,4.3Apr 30, 2019
fabpot added a commit that referenced this pull requestApr 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/aUnwrappingDenormalizer, 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#30894Usage:`$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 requestApr 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/aUnwrappingDenormalizer, 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 tosymfony/symfony#28887 andsymfony/symfony#30894Usage:`$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 requestApr 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/aUnwrappingDenormalizer, 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 tosymfony/symfony#28887 andsymfony/symfony#30894Usage:`$serialiser->deserialize('{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}', Object::class, ['UnwrappingDenormalizer::UNWRAP_PATH' => '[baz][inner]'])`Commits-------00d103d5f7 UnwrappingDenormalizer
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@ogizanagiogizanagiogizanagi left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

6 participants

@nonanerz@mcfedr@ogizanagi@fabpot@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp