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

Add support for replacing serialization object inside events #74

Closed
alex88 opened this issue Apr 3, 2013 · 17 comments
Closed

Add support for replacing serialization object inside events #74

alex88 opened this issue Apr 3, 2013 · 17 comments

Comments

@alex88
Copy link

alex88 commented Apr 3, 2013

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

@schmittjoh
Copy link
Owner

I'm not sure what is missing, but feel free to suggest some concrete changes.

@alex88
Copy link
Author

alex88 commented May 16, 2013

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.

schmittjoh added a commit that referenced this issue May 16, 2013
@schmittjoh
Copy link
Owner

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.

@alex88
Copy link
Author

alex88 commented May 16, 2013

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.

@stof
Copy link
Contributor

stof commented May 16, 2013

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

@alex88
Copy link
Author

alex88 commented May 16, 2013

@stof You mean $event->getVisitor()->addData()?

@stof
Copy link
Contributor

stof commented May 16, 2013

yes

@alex88
Copy link
Author

alex88 commented May 16, 2013

@stof oh awesome, thanks for the tip!

@bezhermoso
Copy link

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?

@enrike1983
Copy link

up @bezhermoso question!

@ahsanity
Copy link

ahsanity commented Mar 2, 2015

+1 @bezhermoso question!

@alex88
Copy link
Author

alex88 commented Mar 2, 2015

Guys you really expect support? Look at the response rate of other issues 😃

ahsanity added a commit to ahsanity/serializer that referenced this issue Mar 3, 2015
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.
@ahsanity
Copy link

ahsanity commented Mar 3, 2015

created a PR related to this #409

@alex88
Copy link
Author

alex88 commented Mar 3, 2015

I think you need to add tests too for those methods

@helmut-hoffer-von-ankershoffen

@schmittjoh: would be really helpful to have removeData and updateData in the GenericSerializationVisitor.

@quant61
Copy link

quant61 commented Sep 12, 2016

function replaceData is already in master and appeared more than 2 years ago commit
But it is still no in release.
Why?

@goetas
Copy link
Collaborator

goetas commented Sep 12, 2016

@quant61 the PR was merged last week (what you see on github it just the commit timestamp)

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

9 participants