-
-
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 #30894
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
[Serializer] Add options to JsonDecode and JsonEncode to wrap/unwrap json data #30894
Conversation
@ogizanagi was not sure if that's suitable for core. |
} | ||
|
||
/** | ||
* Decodes data. | ||
* | ||
* @param string $data The encoded JSON string to decode | ||
* @param string $format Must be set to JsonEncoder::FORMAT | ||
* @param array $context An optional set of options for the JSON decoder; see below | ||
* @param array $context an optional set of options for the JSON decoder; see below |
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.
should be reverted
@@ -80,6 +84,9 @@ public function __construct($defaultContext = [], int $depth = 512) | |||
* json_decode_options: integer | |||
* Specifies additional options as per documentation for json_decode | |||
* | |||
* JSON_PROPERTY_PATH: string |
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.
lower case: that's the value of the constant, not its name
* @param string $propertyPath | ||
* @param mixed $data | ||
* | ||
* @return array |
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.
the docblock should be removed in favor of real types
array('{"baz": {"foo": "bar"}}', null, array(JsonEncoder::JSON_PROPERTY_PATH => 'baz.inner')), | ||
array('{"baz": {"foo": "bar"}}', $assoc, array(JsonEncoder::JSON_PROPERTY_PATH => '[baz]', 'json_decode_associative' => true)), | ||
array('{"baz": {"foo": "bar"}}', $assoc, array(JsonEncoder::JSON_PROPERTY_PATH => '[baz]', 'json_decode_associative' => true)), | ||
array('{"baz": {"foo": "bar", "inner": {"key": "value"}}}', array('key' => 'value'), array(JsonEncoder::JSON_PROPERTY_PATH => '[baz][inner]', 'json_decode_associative' => true)), |
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.
see fabbot
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.
I'm also not sure if it's something that should be in the core, use case seems to be very special, cannot this be done with a specific normalizer instead ?
Like having a ROOT_OBJECT context option that can be interpreted by a specific normalizer and which will call the underlying denormalizer (DenormalizerAwareInterface)
@@ -57,14 +60,15 @@ public function __construct($defaultContext = [], int $depth = 512) | |||
} | |||
8000
|
|||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); | |||
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
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.
It adds a hard dependency on property access which is still a suggested dependency in the composer
I don't think we want to commit that to the core, like @joelwurtz said it should be in a specific normalizer. |
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
from @nonanerz
I've just rebase the PR with master without the merge cc @fabpot
from #28887
#FOSSHackathons