-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] @SerializedName flattening nested attributes #32080
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
Comments
@javiereguiluz any info about this? |
I'm sorry but I don't know the Serializer component much, so we should wait for other people opinions. |
Thank you for this suggestion. |
Yeap, that would be nice to have something like that. |
Yes, I need to denormalize some nested objects into flat array |
Thank you for this suggestion. |
I think, this would be a useful addition, but somebody has to build it. 😃 |
I tried tackling this in flatten-nested-attributes. Some things are still missing and it's not properly tested yet, so I didn't create a pull request. Could you take a look at the branch, @derrabus, to check if this is the right way to go? Using the /**
* @SerializedName("First.Second.@attribute")
*/
private $foo
/**
* @SerializedName("First.Second.Third")
*/
private $bar together with the $output = $serializer->deserialize(
$input,
SomeClass::class,
'xml',
['flatten_nested_attributes' => true]
) and <First>
<Second attribute="foo">
<Third>bar</Third>
</Second>
</First> fills the annotated properties with |
I think, a pull request is always better to discuss changes and the course of action. Don't hesitate to create one, even if it's not ready yet. |
Thank you for this suggestion. |
Yep! The pull request for this is a bit stalled right now, but still active. |
…n nested attributes (boenner) This PR was merged into the 6.2 branch. Discussion ---------- [Serializer] Add `SerializedPath` annotation to flatten nested attributes | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #32080 | License | MIT | Doc PR | todo As suggested by `@derrabus` in #32080, I'm creating a PR for this. In order to normalize/denormalize nested attributes, the ``@SerializedPath`` annotation can be used: ```php class NestedDummy { /** * `@SerializedPath`("[one][two][three]") */ public $foo; /** * `@SerializedPath`("[one][four]") */ public $bar; } ``` with ```php $data = [ 'one' => [ 'two' => [ 'three' => 'foo', ], 'four' => 'bar', ], ]; $normalizer = new AbstractObjectNormalizerWithMetadata(); $normalizer->denormalize( $data, NestedDummy::class, 'any' ); ``` The annotations needs to be used with a valid `PropertyPath` string for this to work. Open todos: - [x] tests for new feature - [ ] update documentation Commits ------- 08a1119 [Serializer] Add `SerializedPath` annotation to flatten nested attributes
Description
Sometimes I want to flatten a JSON object into POPHPO.
For example Jackson provide following feature in annotation:
@JsonProperty("filter.key1")
.@JsonProperty
like annotation should handle nested values.Example
Following JSON:
And unmarshalled object:
The text was updated successfully, but these errors were encountered: