Closed candlecare closed 4 years ago
Hello Sithu!
Yes, Thingy instances are not JSON serializable as they are not dictionaries. But you can simply use the user.view()
method, which will output a dictionary corresponding to your object, using the default view.
Another option is to modify your JSON serializer to support Thingy objects and call .view()
on them automatically. For example, as I see that you are using Flask-JSON, you could do something like this:
json = FlaskJson(app)
...
@json.encoder
def encoder(o):
if isinstance(o, Thingy):
return o.view()
Does that help?
After adding this, it worked :)
@json.encoder
def encoder(o):
from bson import ObjectId
if isinstance(o, ObjectId):
return str(o)
if isinstance(o, Thingy):
return o.view()
Thank you, @ramnes
I have a model and API controller:
I am using Flask-JSON to decode Mongo-Thingy models. I tried both json.dumps(user) and both gave me the below error. I am not really sure it is mongo-thingy issue. It looks like it is related to Python 3.7 dataclasses to json conversion failure.
Thanking you in advance.
Sithu