json serialize python and mongoengine object mix -
json serialize python and mongoengine object mix -
json.dumps(o)
converts native python object json o.to_json()
converts mongoengine object such document
json
how convert mixed object?e.g python dict
, mongoengine objects values?
are there tools this? or should create custom jsonencoder
class?
if override encoder, create decoder reconstructs mongoengine objects?
the next encoder serializes python\mongoengine object mixes
import json mongoengine.base import basedocument class mongoengineobjectsjsonencoder(json.jsonencoder): def default(self, o): if isinstance(o, basedocument): homecoming o._data elif isinstance(o, datetime): homecoming o.isoformat() else: homecoming json.jsonencoder.default(self, o)
notes:
this encoder not add together signature regarding python objects end , mongoengine objects begin, can't automatically deserialized correctly python\mongoengine objects, rather deserialize single python object i've addeddatetime
object serialization iso 8601 format python json serialization deserialization mongoengine
Comments
Post a Comment