-
Notifications
You must be signed in to change notification settings - Fork 231
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
Is it possible to perform json.dumps(obj_proxy)
?
#267
Comments
Can you provide a small standalone code example to demonstrate the problem you are having. That will give me something to work with in fully understanding the issue and see what solutions there may be. |
import wrapt
import copy
import json
class Wrapper(wrapt.ObjectProxy):
_metadata = None
def __init__(self, wrapped, data):
super().__init__(wrapped)
self._metadata = data
def __deepcopy__(self, memo):
return Wrapper(copy.deepcopy(self.__wrapped__, memo), copy.deepcopy(self._metadata, memo))
print(json.dumps({"a": {"a": Wrapper({"b": "123453"}, {"tag": "asd", "id": "555"})}})) This is my current code so far. Don't really know how to tackle this. I have reduced from my original codebase to this minimal example. |
Is |
It was another independent part but I left it there to hopefully make it works but not I guess. Removing it will cause same error anyways. |
Is the goal for the resulting JSON to only show the representation of the object wrapped by ObjectProxy instance, or are you expecting the metadata from the custom ObjectProxy instance to also show up in the JSON output. |
I don't expect And hopefully it can works with recursive |
In what I think is quite hilarious behaviour, if you add IOW, running:
yields:
In general though, one has to use a custom encoder for json which needs to be supplied when you call
This yields:
but which doesn't work as intended again if use |
Thanks. Looks like the only option I have is override |
Not sure there is much wrapt can do. The problem is that when you don't supply So the difference in behaviour is the fault of the Python standard library in that the C version of the json encoder is not friendly to Python duck typing. BTW, how buried is the point where |
From reading #245, looks like cpython internally doing type check, not class check. The thing is I can't really touch
json.dumps
call site for my use case (and maybejson.JsonEncoder.default
). Unsure how to go about this.The text was updated successfully, but these errors were encountered: