-
Notifications
You must be signed in to change notification settings - Fork 641
Passing serializers dynamically #2992
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
Comments
Do you mean that you want the ability to specify a serializer factory instead of a serializer? In any case it would require a good amount of work, not only to implement, but especially to determine what the right semantics of such would be. |
An example of I want would be as follows. In a library I have this:
Also in the same library I want to serialize and deserialize E.g. I want to expose:
So the consumers of the library would create their own implementations of |
As to formats, that is always up to the code that calls the (de)serialization. By all means create your own format. On In the alternate, the library also supports generated serializers with parameters when given a type parameter. The serializer for the type parameter is resolved on creation of the relevant serializer. That can be in a consuming library (you need an |
Thanks for the suggestion. I'm aware of the The generic type suggestion is also not suitable in my particular case. It also makes the API surface an the usage more complex than I would like it to be. E.g. That's why I created this feature request. |
@arkivanov I'm still not 100% of what you want. Another option can be the "self-serializable" data is where you store the serializer in the object. E.g:
This certainly works (although it needs a custom serializer). It comes into place where you have code that has dynamic return types that need (de)serialization based upon the dynamic type (without polymorphism). |
I want to be able to serialize polymorphic types without using serializer modules. Another example would be as follows. Consider a library that we don't control and can't change with the following API.
Now I have this code in my project consuming the library.
Finally, somewhere in my code I want to pass
I would like to do something like this, but it's not possible currently.
Hope this explains it better. Btw, your code snippet doesn't compile. |
I've had another shot at it (now with the compiler/testing involved). But notice that using a generic parameter just makes things easier. And you can always pass an explicit serializer that is constructed however you want. If you want the serializers to be transparently provided for you, you have to follow the rules though:
You could store the element serializer in the value for encoding purposes (a good technique for the returns in web frameworks like ktor), but deserialization doesn't have a value, so can't actually determine the deserializer to use from the data (unless you have an alternative way that determines the appropriate child deserializer from the context). |
It would be really useful if we could specify serializers for non-serializable properties at runtime, similarly to how we specify serializers for generic properties. An imaginatory API could look like this.
The code from a library:
Then the library consumer's (client) code:
The text was updated successfully, but these errors were encountered: