-
-
Notifications
You must be signed in to change notification settings - Fork 311
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
Nullable #177
Nullable #177
Conversation
$this->nullable = (bool) $serializeNull; | ||
} | ||
|
||
public function isNullable() |
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 naming looks weird. The visitor is not nullable. It serializes null values
Thanks @stof, I've cleaned those up. |
+1 for serialize_null :-) |
Could you rebase this pull request on master branch, and add a method |
You have a possible null pointer method call: Could you also add some more tests. For example, visitNull is not covered at the moment: I soon plan to split the serializer code into a separate standalone library. Please let me know whether you have time to finish this until Friday, otherwise it would need to be rebased again on the new repository. |
@schmittjoh I've added a test on serializing/desreializing |
$this->serializeNull = (bool) $serializeNull; | ||
} | ||
|
||
public function getSerializeNull() |
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.
Can you rename this to shouldSerializeNull
?
There are just a few minor things, otherwise this looks good. |
The method in the serializer could be more flexible and possibly take over the config. I've left it simple for the moment. |
I've reverted the config changes for now. Thanks for your work! |
Great feature but why can't I enable this inside the configuration? I want this to happen automatically, not with a function call (because I use FOSRestBundle with the ViewResponseListener) |
For now I fixed this by creating my own serializer: <service id="nullable_serializer" parent="jms_serializer.serializer">
<call method="setSerializeNull">
<argument>true</argument>
</call>
</service> fos_rest:
service:
serializer: nullable_serializer |
I used the fix that @ruudk said (creating my own serializer) and worked properly, but I update throw composer today and stopped working:
This is because the setSerializeNull function isn't more in JMS\Serializer\Serializer... Any solution? Thanks a lot!! |
Please read the upgrade file. in 0.12-dev, it is now in the SerializationContext |
Thanks @stof, I saw the changes, now you can pass a Context as third parameter in the serialize call, but I want to set the serialize null by configuration because I use FOSRestBundle with the ViewResponseListener. Really I don't know how to do this now... Any help will be appreciated! |
Any news on this? |
@fernandopg same problem for me |
@artworkad and @fernandopg even im facing the same problem. Did you'll find a way to get it working with FOSRestBundle? |
@ankitkhedekar Instead of just checking jObject.field != null, where field is some json object attribute. I have implemented a routine that checks if the field exists. Could avoid that if jmsserializer would output null values. This issued definitely should be solved. |
@fernandopg Did you find a solution? We're also using FOSRestBundle and it's incredibly frustrating that we can't serialize NULL values with their keys. |
No.... @AAtticus @ankitkhedekar @artworkad :-( Finally that I do is create the Views in the controller:
I hope this help us! |
@fernandopg Thanks! I looked at the source in FOSRest for this issue and it looks like it will be a bit of an effort to refactor to make it honor the config. With your solution one could add a check for the serialize_null setting in the config and take the appropriate action. |
There is a setting on FOSRestBundle for serializing null values, but its about serializing a null value in the data of the View, not about configuring the serializer to enable serializing nulls within an object graph. We might need to adjust this setting to allow separate configuration of these two things. |
@lsmith77 Thanks for the clarification. I actually never realized that there is a distinction between the two until you mentioned it. From what I read here and on similar issues I think what most people are looking for is the ability to serialize the data as it is and not have it drop keys with null values - the version where the SerializationContext is configured to serialize null values). |
@hglattergotz Exactly my opinion on this. I find it rather strange that keys with NULL values will be dropped by default. One of the constraints of our API is that it's HATEOAS. We use a front-end system where the client traverses through our API by resource links but they can't discover the attributes of the resource when they are NULL and omitted from the json or xml response. |
I dont think its a good idea for FOSRestBundle to have a different default, furthermore you are seeing mostly comments from people that want NULLs serialized, because the others already get what they want and therefore do not need to post tickets. Anyway, hopefully someone can work on the referenced FOSRestBundle ticket and make it possible to at least configure the behavior globally. That being said, any global option like this can lead to incompatible Bundle code (ie. one bundle wanted the setting set to off the other to on). |
We've decided we want to preserve keys when serializing null values, this adds a flag on each visitor (default false) as to whether to use this behaviour.