-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
[feature] exclusion groups #539
[feature] exclusion groups #539
Conversation
* added section for exclusion groups * fixed rst-style for php-blocks in readme
is this just a mirror implementation of http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies#creating-different-views-of-your-objects ? |
Kind of, yes. Where "@groups({"test"})" is whitelisting, "@exclude({"test"})" is blacklisting. Can be handy if you serialize a lot without group, but you want to suppress one specific field in one specific case. The work-around (and/or probably the better solution in the long run for most cases) would be to set proper group for all-other-fields. But still would be helpful to have this option available. |
if (false === $metadataGroups) { | ||
return false; | ||
} | ||
if (is_array($metadataGroups) && empty($metadataGroups)) { |
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.
isn't more appropriate use count
instead of empty
?
Do you have some benchmark for performance impact? |
Nope :/ Can you give me a hint how to provide one? when running % php tests/benchmark.php json 200
PHP Fatal error: Uncaught TypeError: Argument 4 passed to JMS\Serializer\Tests\Fixtures\BlogPost::__construct() must be an instance of JMS\Serializer\Tests\Fixtures\Publisher, none given, called in /home/delf/dev/serializer/tests/benchmark.php on line 34 and defined in /home/delf/dev/serializer/tests/JMS/Serializer/Tests/Fixtures/BlogPost.php:116 checking benchmark.php, it indeed looks broken: function createObject()
{
$post = new \JMS\Serializer\Tests\Fixtures\BlogPost('FooooooooooooooooooooooBAR', new \JMS\Serializer\Tests\Fixtures\Author('Foo'), new \DateTime); ..is it broken or am i just missing something? |
This feature can be achieved with #673 use JMS\Serializer\Annotation\Exclude;
/**
* @ExclusionPolicy("none")
*/
class BlogPost
{
private $title;
/** @Exclude(if="context.attributes.containsKey('list')") */
private $comments;
} And in the controller: use JMS\Serializer\SerializationContext;
$serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setAttribute('list', true)); This will only serialize |
Added support for passing an array parameter to the
@Exclude
annotation, refs #470Example:
And in the controller:
This will only serialize
$title
.