-
-
Notifications
You must be signed in to change notification settings - Fork 587
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
Allow to use custom serializer with primitive type #222
Conversation
ping.... |
I wanted to use this to create a custom type to transform the string of a uploaded file to a full url with VichUploaderBundle. |
I'm using this feature to translate some properties "on-the-fly" (based on accept language) during serialization . |
Please use a custom type in that case instead of decorating built-in types. |
What are custom types? (sorry for my ignorance..) |
Something like |
I have already tried to adopt this solution, but Here https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/GraphNavigator.php#L143 |
I have tried to register a custom handler and i'm getting the exception
|
@cmodijk i have explained the situation just now |
@schmittjoh custom types cannot be used on primitive types currently. See #194 |
Ah, I see. I'm afraid, then you'll probably need to overwrite the visitors or make the changes you proposed in your fork. This use-case is not common enough that I want to add more ifs to the code base and I'm also not really sure that the serializer is the right layer for these transformations (at least the current architecture was not designed for this). |
I think that this feature will be useful. https://gist.github.com/goetas/9110276 (in this way i'm hiding the implementation of my service) |
This feature would be very useful. JMS is powerfull but quite frustrating if we have to create similar virtual methods to handle a recurrent output serialization for a primitive type such as strings and arrays. In many situation, primitive types are best suitable and people would not enjoy to wrap it in object just to get them handled by JMS. Next the JMS process, visiting our object graphs, is quite heavy, so that people like me choosing this library actually expect it to handle a maximum of stuff during this "visit". Custom handlers seems the perfect hook for this, and it is completly leave to developer responsability. Having said this, I think it would be enough to allow people to easily extends graph navigator for this purpose, here it is final and every attribute are private... is there any way to plug a custom graph navigator without having to fully rewrite it ? |
@funkyan, what's your use case for extending primitive type handling? |
Some key value serialized data, stored into string fields (doctrine), that we always parse and output the same way when outputing it in json. I would prefer to handle those same custom types in one unique place. |
Hi @schmittjoh, I'm writing a small tool that converts an XSD into PHP classes and JMS serializer YAML metatata, that allows to serialize/unserialize complex schemas as OTA. XMLSchema defines The current implementation of jms-serializer does not allow There are a lot of use case, where a custom handler on primitive types can be useful. @stof Do you agree with me? |
@goetas, I'd be happy to merge support for |
The same thing will happen with |
Sure, happy to make improvements, just send individual pull requests, but I don't think that we need a general extension point here. |
Any plans to separate this into individual pull requests to get the ball rolling? |
My use case would be to transform simple strings (containing binary data such as image or pdf) to base 64 during the serialization and decode it during the deserialization |
+1 in combination with using the XSD2PHP functionality of @goetas. I encountered potential UseCases when dealing with large object structures in various industries, for example:
Sometimes XML signing / base encoding, or other transformations are needed during (de)serialization, some flexibility / influence during this phase would be nice. Custom handlers seem "the obvious way to go"... |
+1 |
@schmittjoh please consider reopening this, here is an example of a SimpleType that would be useful to have a Custom Handler dealing with: Ok, the |
Rebased again. |
Without custom primitives the validation of input data is more or less impossible. The default implementation for that types creates valid - but wrong - data before validation. |
please re-consider this, is already a long time that some people (a lot) are requesting this feature. some example are #507 goetas-webservices/xsd2php-runtime#1 AuthorizeNet/sdk-php#146 AuthorizeNet/sdk-php#73 and probably many other. the feature is BC and does not introduce a high amount of logic |
👍 |
@goetas it's not so much about the amount of code required, but about the performance impact. Most of the types we handle are primitive types, and moving them through the event system/handler system will severely impact performance. Did you benchmark this? |
@schmittjoh To use a custom mapping on this PR is necessary to declare a custom type. Example: class Author {
/**
* @Type("MyNonExistentClass")
* @var string
*/
private $name; // is a string
} The type of This has no general performance impact, and the serialization of the type I have also tried a general approach in goetas#3, allowing to have custom handlers for |
Ah sorry, I was looking at that other PR yesterday before reading the comments here. Could you fix the coding-style (indentation) so we can merge this. I also cannot re-open this PR because GitHub says there is already one open. Did you create another one? |
Ok, will rebase and create a new one! |
#610 rebased version of this |
🎉 |
@schmittjoh thank you! |
Great, @goetas this means https://github.com/goetas/xsd2php does not rely on a patched version of the Serializer anymore? |
not yet :( there were few more commits that were not yet published as PR here... working on it. One of them is #611 , but probably two more are coming |
Aah, ok, well, kudos for your perseverance 😄 |
This patch will allow to create custom handles for primitive types (strings for example)
(i use it make translations during serialization)