-
-
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
Add support for replacing serialization object inside events #74
Comments
I'm not sure what is missing, but feel free to suggest some concrete changes. |
It's missing that you say that in post-serialization you can add fields, but actually it's not possible in post-serialization, just pre-serialization. The difference is that adding in pre-serialization means that you need to have the fields already declared in the entity and with the correct serialization groups (otherwise they don't get serialized), being able to add them in post-serialization makes it easier since you don't have to worry about if the variable is already declared in the entity and such. |
I see. The post-serialize event does not mean to add data to an object, i.e. add fields, but instead to add that data to the serialized output for that object. I have changed the language to make this clearer. |
Exactly, also, could you please explain also how to do that? The only thing you can do on the JMS\Serializer\EventDispatcher\ObjectEvent is the $event->getObject() which returns the original object, not the serialized one, isn't it? Since the class returned by getObject is the one it's being serialized and trying to add fields or change values doesn't affect serialized object. |
your event has also access to the visitor, which is what you need: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/EventDispatcher/Event.php |
@stof You mean $event->getVisitor()->addData()? |
yes |
@stof oh awesome, thanks for the tip! |
It looks like it is not possible to overwrite an existing property (i.e., filtering a certain value before it the serialization proceeds.) Is there a way to do this? |
up @bezhermoso question! |
+1 @bezhermoso question! |
Guys you really expect support? Look at the response rate of other issues 😃 |
The method addData($key, $value) allows to add new keys to serialised objects but no further manipulation to the serialised object is possible is post serialize events. This commit added the 2 methods: removeData($key) and getData(). This will allow manipulation of the current serialised object in post serialize event listeners. This is in response to schmittjoh#74, as I'm facing a similar need.
created a PR related to this #409 |
I think you need to add tests too for those methods |
@schmittjoh: would be really helpful to have removeData and updateData in the GenericSerializationVisitor. |
function replaceData is already in master and appeared more than 2 years ago commit |
@quant61 the PR was merged last week (what you see on github it just the commit timestamp) |
It could be useful to replace the element to be serialized in the post-serialization event as the docs says: "You can for example use this to add additional data to an object that you normally do not save inside objects such as links"
Trying to change the object fields in the post_serialization event doesn't makes any change in the resultant serialized object.
Edit: I've seen that in the pre-serialization event the object is changed, so it should be useful to improve the docs about that
The text was updated successfully, but these errors were encountered: