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

SerializeNull #440

Closed
theofidry opened this issue Jan 26, 2015 · 4 comments
Closed

SerializeNull #440

theofidry opened this issue Jan 26, 2015 · 4 comments

Comments

@theofidry
Copy link

Duplicate of this issue that has been closed although the problem has not been solved (I can't reopen the issue...)

So, the question is how to serialise null attributes? If an attribute has the null value, my serialised entity doesn't have the attribute. Is there a way to specify that we which to have this attribute with the value null in our serialised object?

I found an interesting issue about that. It suggests to use the SerialisationContext:

$context = new SerializationContext();
$context->setSerializeNull(true);
$serializer->serialize($object, 'json', $context);

And indeed it works fine for some entities:

{
    "0": {
        "id": 1,
        "complementary_home": null,
        "complementary_location": "Bâtiment A",
        "street": "60, boulevard Louise Herve",
        "complementary_street": null,
        "postal": "80063",
        "city": "Vidalboeuf",
        "country": "Christmas Island"
    }
}

But when I check it on entities which some attributes are resolved with Doctrine, the problem is still there:

{
    "0": {
        "id": 4,
        "name": "Caroline Rossi",
        "phone": {
            "id": 4,
            "number": "(552)312-9471"
        },
        "address": {
            "id": 4,
            "street": "179, avenue Xavier Peltier",
            "complementary_street": "BP 77 Belleville",
            "postal": "69 875",
            "city": "Leroux",
            "country": "Guyana"
        }
    }
}
@theofidry
Copy link
Author

My bad, seems that there was an error in my code due to the fact that the context cannot be reused.

@lony
Copy link

lony commented Sep 25, 2015

@theofidry Is there something more general? I have multiple controllers (>10) each witch multiple methods and would love to have the serializer do this by default without configuring it for each method directly!

@theofidry
Copy link
Author

I'm sorry @lony it's been so long I don't remember it at all :( That being said now Symfony serializer 2.7 has been greatly improved there is not much interest to use JMSSerializerBundle anymore if you can use it. (and it's more performant too!)

http://thomas.jarrand.fr/blog/serialization/

@lony
Copy link

lony commented Sep 28, 2015

@theofidry thank you but so far I can not switch. I will try around :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants