Skip to content
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

Closed

Conversation

leberknecht
Copy link

Added support for passing an array parameter to the @Exclude annotation, refs #470

Example:

use JMS\Serializer\Annotation\Exclude;

/**
* @ExclusionPolicy("none")
*/
class BlogPost
{
    private $title;

    /** @Exclude({"list"}) */
    private $comments;
}

And in the controller:

use JMS\Serializer\SerializationContext;

$serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setExclusionGroups(array('list')));

This will only serialize $title.

* added section for exclusion groups
* fixed rst-style for php-blocks in readme
@leberknecht leberknecht changed the title Feature exclusion groups [feature] exclusion groups Dec 15, 2015
@goetas
Copy link
Collaborator

goetas commented Jul 27, 2016

@leberknecht
Copy link
Author

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)) {
Copy link
Collaborator

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 ?

@goetas
Copy link
Collaborator

goetas commented Sep 5, 2016

Do you have some benchmark for performance impact?

@leberknecht
Copy link
Author

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 i get

 % 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?

@goetas
Copy link
Collaborator

goetas commented Apr 3, 2017

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 $title.

@goetas goetas closed this Apr 3, 2017
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.

2 participants