<s> from flask . json import JSONEncoder <EOL> from bson import json_util <EOL> from mongoengine . base import BaseDocument <EOL> from mongoengine . queryset import QuerySet <EOL> def _make_encoder ( superclass ) : <EOL> class MongoEngineJSONEncoder ( superclass ) : <EOL> '''<STR_LIT>''' <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , BaseDocument ) : <EOL> return json_util . _json_convert ( obj . to_mongo ( ) ) <EOL> elif isinstance ( obj , QuerySet ) : <EOL> return json_util . _json_convert ( obj . as_pymongo ( ) ) <EOL> return superclass . default ( self , obj ) <EOL> return MongoEngineJSONEncoder <EOL> MongoEngineJSONEncoder = _make_encoder ( JSONEncoder ) <EOL> def override_json_encoder ( app ) : <EOL> '''<STR_LIT>''' <EOL> app . json_encoder = _make_encoder ( app . json_encoder ) </s>
