You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, sorry for the very slow response. You're correct at the moment this isn't possible.
I wonder if Pythonize and Depythonize traits could solve this problem. They could have blanket implementations for T: Serialize (or Deserialize) and then Python types which aren't serde-compatible could be handled separately. Other than that, I'm not aware of a way that we could hook into serde to achieve this.
To deal with this in a current project, I've been using a proxy class that can wrap an object and make it look like a dict to depythonize, that looks something like:
I only need to go in the Python -> Rust direction, but I think a similar approach could work in the other direction as well. One semi-major frustration at the moment, though: as currently implemented, the deserializer for mappings calls both .keys() and .values() on the incoming map, and eagerly evaluates the returned iterators, which means every field on the object gets accessed, including @property fields (which get evaluated), even if those fields aren't actually needed in the deserialization. At least in my application, this is resulting in some unnecessary slow/expensive calls, and I haven't yet figured out a way around it.
It doesn't seem like there's any reason it has to work that way, but that's how it works now. I think if object deserialization were to be explicitly supported, some kind of laziness would be important.
Given that pythonize doesn't yet support arbitrary python objects it is not as powerful as pickle which means that it doesn't have the same security concerns as pickle does. If you manage to support arbitrary python objects it's worth considering to leave a function that doesn't support the full set of python objects as that is a feature in of itself in terms of keeping security tight.
Activity
davidhewitt commentedon Dec 27, 2022
Hello, sorry for the very slow response. You're correct at the moment this isn't possible.
I wonder if
PythonizeandDepythonizetraits could solve this problem. They could have blanket implementations forT: Serialize(orDeserialize) and then Python types which aren't serde-compatible could be handled separately. Other than that, I'm not aware of a way that we could hook intoserdeto achieve this.(This problem is very related to #1 I think.)
apendleton commentedon Mar 8, 2023
To deal with this in a current project, I've been using a proxy class that can wrap an object and make it look like a dict to
depythonize, that looks something like:I only need to go in the Python -> Rust direction, but I think a similar approach could work in the other direction as well. One semi-major frustration at the moment, though: as currently implemented, the deserializer for mappings calls both
.keys()and.values()on the incoming map, and eagerly evaluates the returned iterators, which means every field on the object gets accessed, including@propertyfields (which get evaluated), even if those fields aren't actually needed in the deserialization. At least in my application, this is resulting in some unnecessary slow/expensive calls, and I haven't yet figured out a way around it.It doesn't seem like there's any reason it has to work that way, but that's how it works now. I think if object deserialization were to be explicitly supported, some kind of laziness would be important.
jonathan-s commentedon Jul 5, 2023
Given that pythonize doesn't yet support arbitrary python objects it is not as powerful as pickle which means that it doesn't have the same security concerns as pickle does. If you manage to support arbitrary python objects it's worth considering to leave a function that doesn't support the full set of python objects as that is a feature in of itself in terms of keeping security tight.
Stargateur commentedon Jul 18, 2024
I'm using dill (or pickle) to serialize any python object with a little code you can do a serde with module and it's work nice.