Skip to content

Instantly share code, notes, and snippets.

@nguyenbathanh
Last active March 20, 2020 13:59
Show Gist options
  • Save nguyenbathanh/d5014f5568699ba111b2f0eb067eada8 to your computer and use it in GitHub Desktop.
Save nguyenbathanh/d5014f5568699ba111b2f0eb067eada8 to your computer and use it in GitHub Desktop.

Revisions

  1. nguyenbathanh revised this gist Oct 31, 2018. No changes.
  2. nguyenbathanh renamed this gist Oct 31, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. nguyenbathanh created this gist Oct 31, 2018.
    23 changes: 23 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import datetime
    import json

    from bson import ObjectId
    from mongoengine.base import BaseDocument
    from mongoengine.queryset import QuerySet


    class MongoengineEncoder(json.JSONEncoder):
    def default(self, obj):
    if isinstance(obj, BaseDocument):
    return obj.to_mongo()

    if isinstance(obj, QuerySet):
    return list(obj)

    if isinstance(obj, ObjectId):
    return str(obj)

    if isinstance(obj, datetime.datetime):
    return obj.isoformat()

    return json.JSONEncoder.default(self, obj)