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

[Wishlist] A way to pass down custom data/context recursively to custom handlers. #452

Open
ks-mw opened this issue Jul 27, 2023 · 4 comments
Labels
enhancement wishlist An important issue where it'd take a lot of effort to make progress on

Comments

@ks-mw
Copy link

ks-mw commented Jul 27, 2023

I wish there was an easy way to pass some custom context/data down the recursive encoding to use it in my custom handlers.
In other words, I want to be able to set custom context/data that has the scope of a single call to encode/decode and can be accessed from inside the custom handler's methods.

Example (a new "context" argument is available in the custom handler's methods):

class A:
    ...

@jsonpickle.handlers.register(A)
class AHandler( jsonpickle.handlers.BaseHandler ):

    def flatten( self, obj: A, data: dict, context: dict ) -> dict:
        return do_something( context['foo'] )

    def restore( self, data: dict, context: dict ) -> object:
        return do_something_else( context['foo'] )

a0 = A()
a1 = A()

jsonpickle.encode( a0, context={'foo': 'bar'})
jsonpickle.encode( a1, context={'foo': 'baz'})

Is something like that planned? Or is there a trick I have overlooked that can be used to make such context data available in the handler?
A workaround I have come up with is to use some global state that is set before the call to encode and is then queried from inside the handler's flatten method. That however is very problematic for async code.

Best wishes,
KS

@Theelx
Copy link
Contributor

Theelx commented Jul 28, 2023

I don't think there's builtin support for that currently, but that's a great idea! I'll see if I can get on it after I fix some of the bugs that have popped up.

@Theelx Theelx added wishlist An important issue where it'd take a lot of effort to make progress on enhancement labels Jul 28, 2023
@ks-mw
Copy link
Author

ks-mw commented Jul 31, 2023

Cool!
I am still wondering what the best signature would be. The context dict could as well be "object" instead I think. And a drawback of my suggestion is that the signatures for "flatten" and "restore" get bloated, which might annoy users who do not make use the context data.

@Theelx
Copy link
Contributor

Theelx commented Jul 31, 2023

I don't believe the signature would get bloated, because context has to be an optional argument. Making it required would be a backwards-incompatible change, and I don't think it's a good idea to release jsonpickle 4 so soon after 3. Also, jsonpickle doesn't currently have typing information built-in because we only dropped support for CPython 2.7 with version 3.0.0, so the type signatures won't matter until I or someone else get to typing the codebase, which is likely fairly far into the future.

@ks-mw
Copy link
Author

ks-mw commented Jul 31, 2023

Ah yes, right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement wishlist An important issue where it'd take a lot of effort to make progress on
Projects
None yet
Development

No branches or pull requests

2 participants