Git Product home page Git Product logo

mongoengine-goodjson's Introduction

More human readable JSON serializer/de-serializer for MongoEngine

Build Status Test Coverage Maintainability Documentation Status Image

What This?

This script has MongoEngine Document json serialization more-natural.

Why this invented?

Using MongoEngine to create something (e.g. RESTful API), sometimes you might want to serialize the data from the db into JSON, but some fields are weird and not suitable for frontend/api:

{
  "_id": {
    "$oid": "5700c32a1cbd5856815051ce"
  },
  "name": "Hiroaki Yamamoto",
  "registered_date": {
      "$date": 1459667811724
  }
}

The points are 2 points:

  • _id might not be wanted because jslint disagrees _ character unless declaring jslint nomen:true
  • There are sub-fields such $oid and $date. These fields are known as MongoDB Extended JSON. However, considering MongoEngine is ODM and therefore it has schema-definition methods, the fields shouldn't have the special fields. In particular problems, you might get No such property $oid of undefined error when you handle above generated data on frontend.

To solve the problems, the generated data should be like this:

{
  "id": "5700c32a1cbd5856815051ce",
  "name": "Hiroaki Yamamoto",
  "registered_date": 1459667811724
}

Making above structure can be possible by doing re-mapping, but if we do it on API's controller object, the code might get super-dirty:

"""Dirty code."""
import mongoengine as db


class User(db.Document):
  """User class."""
  name = db.StringField(required=True, unique=True)
  registered_date = db.DateTimeField()


def get_user(self):
  """Get user."""
  models = [
    {
      ("id" if key == "_id" else key): (
        value.pop("$oid") if "$oid" in value and isinstance(value, dict)
        else value.pop("$date") if "$date" in value and isinstance(value, dict)
        else value  #What if there are the special fields in child dict?
      )
      for (key, value) in doc.items()
    } for doc in User.objects(pk=ObjectId("5700c32a1cbd5856815051ce"))
  ]
  return json.dumps(models, indent=2)

To give the solution of this problem, I developed this scirpt. By using this script, you will not need to make the transform like above. i.e.

"""A little-bit clean code."""

import mongoengine as db
import mongoengine_goodjson as gj


class User(gj.Document):
  """User class."""
  name = db.StringField(required=True, unique=True)
  registered_date = db.DateTimeField()


def get_user(self):
  """Get user."""
  return model_cls.objects(
    pk=ObjectId("5700c32a1cbd5856815051ce")
  ).to_json(indent=2)

How to use it

Generally you can define the document as usual, but you might want to inherits mongoengine_goodjson.Document or mongoengine_goodjson.EmbeddedDocument.

Here is the example:

"""Example schema."""

import mongoengine_goodjson as gj
import mongoengine as db


class Address(gj.EmbeddedDocument):
    """Address schema."""

    street = db.StringField()
    city = db.StringField()
    state = db.StringField()


class User(gj.Document):
    """User data schema."""

    name = db.StringField()
    email = db.EmailField()
    address = db.EmbeddedDocumentListField(Address)

More details... there's the doc!

If you want to know more, there's read the doc that you want to read. You can now read the doc with drinking a cup of coffee!!

Contribute

Please read the doc for the detail.

License (MIT License)

See LICENSE.md

mongoengine-goodjson's People

Contributors

gbroques avatar hiroaki-yamamoto avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongoengine-goodjson's Issues

Consistency of recursion

For example,

import mongoengine_goodjson as gj
import mongoengine as db

class Contributor(gj.Document):
    name = db.StringField()
    age = db.IntField()
    aliases = db.ListField(gj.FollowReferenceField("self", max_depth=4))

class Article(gj.Document):
    title = db.StringField()
    body = db.StringField()
    contributors = db.ListField(gj.FollowReferenceField(Contributor, max_depth=2))

Creating article instance and call to_json, the method serializes article -> contributor -> contributor -> contributor. However, I think recursion limit should be Type-Independent, i.e. article -> contributor -> contributor -> contributor -> contributor in this case.

compatibility problems

In modern versions of pymongo, they've abandoned support for Python 2, and removed the py3 compatibility libraries. This means that in mongoengine-goodjson 1.1.8,

from bson import (
    ObjectId, DBRef, RE_TYPE, Regex, MinKey, MaxKey, Timestamp, Code, Binary,
    PY3, SON
)
from bson.py3compat import text_type, string_type

will fail because of PY3 and bson.py3compat no longer existing. For example:

>>> import pymongo
>>> pymongo.__version__
'4.0'
>>> import mongoengine_goodjson
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/appuser/conda/envs/esg_web/lib/python3.10/site-packages/mongoengine_goodjson/__init__.py", line 6, in <module>
    from .encoder import GoodJSONEncoder
  File "/home/appuser/conda/envs/esg_web/lib/python3.10/site-packages/mongoengine_goodjson/encoder.py", line 16, in <module>
    from bson import (
ImportError: cannot import name 'PY3' from 'bson' (/home/appuser/conda/envs/esg_web/lib/python3.10/site-packages/bson/__init__.py)

At this point I think losing support for 2.7 entirely would be more than justified, but if it's desired to preserve that, we can simply embed the few lines that are actually being used:

PY3 = sys.version_info[0] == 3
string_type, text_type = (str, str) if PY3 else (basestring, unicode)

and leave everything else alone.

To work around this, right now I'm having to do something like

try:
    bson.PY3
except AttributeError:
    bson.PY3 = True
try:
    import bson.py3compat
except ImportError:
    mod = types.ModuleType('bson.py3compat')
    mod.string_type = str
    mod.text_type = str
    bson.py3compat = mod
    sys.modules['bson.py3compat'] = mod

on the client side (as well as a collections.Iterable = collections.abc.Iterable elsewhere, to deal with that issue) but obviously that's not optimal.

Failure to serialize DictField

DictFields appear to be unsupported.

class MyObject(gj.Document):
    my_dict = DictField()
>>> MyObject.objects[0].to_json()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/<user>/.pyenv/versions/automation/lib/python2.7/site-packages/mongoengine_goodjson/document.py", line 239, in to_json
    data = self.__to_json_drop_excluded_data(data)
  File "/Users/<user>/.pyenv/versions/automation/lib/python2.7/site-packages/mongoengine_goodjson/document.py", line 198, in __to_json_drop_excluded_data
    ret[name], fld.document_type._fields
AttributeError: 'DictField' object has no attribute 'document_type'

Pushing an Id to db.ListField(gj.FollowReferenceField()) adds an entire object

Describe the bug
This is my controller:

user = User.objects.get(id=user_id)
company = Company(**body)
company.save()
user.update(push__companies=company.id)

I create a company and push its ID to user companies array.
This is the User Model:

email = db.StringField(required=True, unique=True)
password = db.StringField(required=True, exclude_to_json=True)
auth0 = db.StringField(unique=True)
name = db.StringField()
firstName = db.StringField()
lastName = db.StringField()
isAdmin = db.BooleanField(default=False)

companies = db.ListField(gj.FollowReferenceField(Company, reverse_delete_rule=PULL))

lastLogin = db.DateTimeField()
createdAt = db.DateTimeField()
updatedAt = db.DateTimeField(default=datetime.datetime.now)

So, sometimes when I insert a Company, it pushes the entire object to the user.companies.

To Reproduce
Steps to reproduce the behavior:
It is not deterministic. Sometimes it happens.

Expected behavior
I expect that it push only the ID, as a ReferenceField.

got an unexpected keyword argument 'follow_reference'

Describe the bug
When I tried this, it gets that follow_reference error

products = Product.objects()
result = json.loads(products.to_json(follow_reference=True))

but if I add .first() or .get() it works.
products = Product.objects().first()
result = json.loads(products.to_json(follow_reference=True))

Additional context

from mongoengine import *
import mongoengine_goodjson as gj

class Address(gj.EmbeddedDocument):
street = StringField()
street2 = StringField()
city = StringField
state = StringField()
zip_code = StringField()
country = StringField()
borough = StringField()
area = StringField()
county = StringField()
latitude = IntField()
longitude = IntField()

class Store(gj.Document):
name = StringField()
description = StringField()
domain = StringField()

class ProductPrice(gj.EmbeddedDocument):
store = gj.FollowReferenceField(Store)
price = FloatField()

class Product(gj.Document):
name = StringField()
picture = URLField()
upc = URLField()
description = StringField()
prices = ListField(EmbeddedDocumentField(ProductPrice))
tags = ListField(StringField(choices=['SALE', 'POPULAR']))

Document

Oops! I forget to edit the doc.

Follow Reference

When you use ListField with ReferenceFieldand run to_json, the encoder doesn't follow the reference and needs to provide "follow reference" option for to_json, I think...

Coverall stuck?

I'm not sure but coverall doesn't seem to process my commits.

2.0?

As everyone who have viewed the code knows, the code is very complex and it should be simplified. If possible, I'd like to do it as Version 2.0 before I commit a suicide.

Serialization of DictField of ReferenceField

It appears that DictFields of ReferenceFields are not supported for serialization nor deserialization.

I have implemented the method for serialization and am currently working on the deserialization. I would be glad to propose a PR once I am done if you are interested ?

Exclusion and EmbeddedDocument

Hello,

I have a question regarding exclusion which is a really nice feature.

I tried to use that in EmbeddedDocuments but it does not seems to work.

For example, you have a parent/child models where in parent you have a list of embedded documents, marking a field inside the child model as exclude_to_json does not exclude the field when doing Model.to_json().

Thanks a lot.

Poetry

Is your feature request related to a problem? Please describe.
This feature is not related to a problem, but splitting dependencies with 'production dependencies' and 'development dependencies' seems to be a good idea, and there's some package manager like poetry based on PEP-518.

Describe the solution you'd like
To manage the dependencies more easily than before.

Describe alternatives you've considered
Keep it as-is.

Additional context
None

Failure to serialize lists

I have fields which may or may not be initialized which result in a thrown KeyError when serializing lists with version 1.0.1

Both MyDocument.objects[0].to_json() and MyDocument.objects[1].to_json() will JSON encode as expected. However the following error occurs on trying to serialize both items:

>>> MyDocument.objects[0:1].to_json()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/<user>/.pyenv/versions/automation/lib/python2.7/site-packages/mongoengine_goodjson/queryset.py", line 80, in to_json
    lst = self.as_pymongo()
  File "/Users/<user>/.pyenv/versions/automation/lib/python2.7/site-packages/mongoengine_goodjson/queryset.py", line 70, in as_pymongo
    item[name] = self.__get_doc(fld, item[name])
KeyError: 'stopped_at'

Which is forcing a non-ideal workaround of
json.dumps([ json.loads(obj.to_json()) for obj in MyDocument.objects[0:1] ])

FollowReference decoder

FollowReference doesn't support decode because of its difficulty, but implementing original field might be able to fix this problem.

NoneType' object has no attribute 'to_json'

Find a bug when using document.to_json(follow_reference=True) and the document have optional EmbeddedDocumentField.

If the document dont have the field, when we try to call to_json(follow_reference=True), it gives an exception "NoneType' object has no attribute 'to_json'".

The error is probably on the else clause on line 60 on document.py file, there is not validation if the doc is None.

Unexpected Error while saving Document in MongoEngine after using mongoengine-goodjson

Describe the bug
I am not able to use the Model.from_json() method of mongoengine

To Reproduce
Steps to reproduce the behavior:

  1. Install

dnspython==2.1.0
mongoengine==0.22.1
mongoengine-goodjson==1.1.8

  1. Code Model
import mongoengine as me
import mongoengine-goodjson as gj

class Person(gj.Document):
    first_name = me.StringField(min_length=3, max_length=15, required=True)
    middle_name = me.StringField(min_length=3, max_length=10)
    last_name = me.StringField(min_length=3, max_length=15, required=True)
    dob = me.DateField(required=True)
  1. Code Function Lambda Serverless
from models.user.person import Person

def main(event, _):
    try:
        instance()
        person = Person.from_json(event["body"]).save().to_json()
    except Exception as err:
        print(err)
  1. Run serverless offline and see the error
Traceback (most recent call last):
  File "/home/me/workspace/project/user-management/node_modules/serverless-offline/dist/lambda/handler-runner/python-runner/invoke.py", line 97, in <module>

    result = handler(input['event'], context)
  File "./functions/users/addPersonnelDetails.py", line 17, in main
    person = Person.from_json(event["body"]).save()
  File "/home/me/workspace/project/user-management/venv/lib/python3.8/site-packages/mongoengine_goodjson/document.py", line 342, in from_json
    valueDoc = value.as_doc()
AttributeError: 'NoneType' object has no attribute 'as_doc'

Expected behavior
Should have saved the document as expected since i was getting the expected response before adding this package.
I went through the documentation of mongoengine-goodjson and couldn't find any configurations. I am not sure if i am missing something or is a bug.

Desktop

  • OS: [Ubuntu]
  • Browser [Chrome]
  • Version [ubuntu : 20.10. chrome: 87.0.4280.141]

Self-recursion decoder

Currently FollowReferenceField and to_json with circuit/self-reference don't have a test case to decode json with self-reference and/or circuit reference. i.e. The decoder might not work properly in the case that the model has self-reference and/or circuit references.

FollowReferenceField

I think checking if following the reference is needed or not should be handled by field rather
Document.to_json with follow_reference=True.

Or, I think it would be also good the both ways are provided.

Is FollowReferenceField implemented?

It is unclear to me how to use FollowReferenceField.
The generated JSON only has the ObjectId, and not the referenced document.
Does this replace using the kwarg follow_reference=True ?

My models are setup as per example:

class Book(gj.Document):
  name = db.StringField(required=True)
  isbn = db.StringField(required=True)
  author = db.StringField(required=True)

class User(gj.Document):
  firstname = db.StringField(required=True)
  lastname = db.StringField(required=True)
  books_bought = db.ListField(gj.FollowReferenceField(Book))
  favorite_one = gj.FollowReferenceField(Book)

The generated output only has the ObjectId:

{
  "id": "570ee9d1fec55e755db82129",
  "firstname": "James",
  "lastname": "Smith",
  "books_bought": [
    "570eea0afec55e755db8212a",
    "570eea0bfec55e755db8212b",
    "570eea0bfec55e755db8212c"
  ],
  "favorite_one": "570eea0bfec55e755db8212b"
}

I've also tried using the kwarg follow_reference=True, but receive an error:

...
web_1  |   File "/usr/local/lib/python3.4/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
web_1  |     resp = meth(*args, **kwargs)
web_1  |   File "/mfuse/mfuse/api_resources.py", line 47, in get
web_1  |     return jsonify(json.loads(ww.to_json(follow_reference=True)))
web_1  |   File "/usr/local/lib/python3.4/site-packages/mongoengine_goodjson/queryset.py", line 38, in to_json
web_1  |     return json.dumps(lst, *args, **kwargs)
web_1  |   File "/usr/local/lib/python3.4/json/__init__.py", line 237, in dumps
web_1  |     **kw).encode(obj)
web_1  |   File "/usr/local/lib/python3.4/site-packages/mongoengine_goodjson/encoder.py", line 40, in __init__
web_1  |     super(GoodJSONEncoder, self).__init__(*args, **kwargs)
web_1  | TypeError: __init__() got an unexpected keyword argument 'follow_reference'

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • Lock file maintenance

Detected dependencies

circleci
.circleci/config.yml
docker-compose
docker-compose.yml
dockerfile
Dockerfile
poetry
pyproject.toml
  • mongoengine ^0.25.0
  • tox ^4.0.0
  • flake8 ^6.0.0
  • flake8-docstrings ^1.5.0
  • flake8-commas ^2.0.0
  • mongomock ^4.0.0
  • coverage ^7.0.0
  • flake8-polyfill ^1.0.2

  • Check this box to trigger a request for Renovate to run again on this repository

[FollowReferenceField] Limit recursion

To support self-reference/loop-reference document, implementing something to limit number of recursion.

  • Default Recursion Limit should be 3-depth
  • Allow user to set max depth
  • If the max depth is None, the depth should be unlimited. (i.e. Should raise RuntimeError in self-reference case. This should be the default value.
  • Add test cases when the target field is list and embedded document

exclude metadata

  • Specifying exclude_to_json=True, the json serialization excludes the specified field.
  • Specifying exclude_from_json=True, the json deserialization excludes the specified field.
  • Specifying exclude_json=True, the json serialization and deserialization excludes the specified field.

__unset_gj_flag_sub_field has sometimes raises error.

Describe the bug
A clear and concise description of what the bug is.

The document.py file has a function named "__unset_gj_flag_sub_field"
it sometimes ( not all times ) raises error even the same query usually works but very sometimes.

the exception log is as below.

File "/home/yongwoo/.conda/envs/pingme/lib/python3.6/site-packages/mongoengine_goodjson/document.py", line 155, in unset_flag
   cur_depth_attr = getattr(fld, "$$cur_depth$$")
AttributeError: 'StringField' object has no attribute '$$cur_depth$$'

So here is my fixed code and it works fine again but I never understand how come it happens.

def __unset_gj_flag_sub_field(self, name, fld, cur_depth):
    """Remove current depth to subfield."""
    # def unset_flag(fld, depth_lv):
    #     setattr(fld, "$$cur_depth$$", depth_lv - 1)
    #     cur_depth_attr = getattr(fld, "$$cur_depth$$")
    #     if (not isinstance(cur_depth_attr, int)) or cur_depth_attr < 0:
    #         delattr(fld, "$$cur_depth$$")
    
    def unset_flag(fld, depth_lv):
        __cur_depth = depth_lv - 1
        if isinstance(__cur_depth, int) and __cur_depth >= 0:
            setattr(fld, "$$cur_depth$$", __cur_depth)

    self.__apply_element(
        name, fld, cur_depth, unset_flag, "end_goodjson"
    )

because you declared setattr(fld, "$$cur_depth$$", depth_lv - 1) so that getattr(fld, "$$cur_depth$$") must always have $$cur_depth$$ property but as you see the error log above it raises..

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Make ID attribute the first property

Currently, the id field appears at the bottom, or the last entry of the JSON object.

[
   {
      "name": "Staple",
      "id": "5c16c09d175e50e5268e062b"
   },
   {
      "name": "Treat",
      "id": "5c1847eefe84bd000f866e07"
   }
]

In most JSON representations, the id appears first.

[
   {
      "id": "5c16c09d175e50e5268e062b",
      "name": "Staple"
   },
   {
      "id": "5c1847eefe84bd000f866e07",
      "name": "Treat"
   }
]

I believe this is because we add the id attribute by indexing into the dictionary.
See:
https://github.com/hiroaki-yamamoto/mongoengine-goodjson/blob/master/mongoengine_goodjson/document.py#L53

        # mongoengine_goodjson/document.py

        if "_id" in dct:
            dct["id"] = dct.pop("_id")
        return dct

I propose to create a new dictionary with the id attribute first.

        # mongoengine_goodjson/document.py

        if "_id" in dct:
            id = dct.pop("_id")
            dct = {
                id,
                **dct
            }
        return dct

I haven't tested this though, and this change only adds aesthetic value, not functionality.

There might also be a performance concern with creating a new dictionary.

Any thoughts on whether this change is worth having?

Problem with empty list

Describe the bug
If I have an empty list like something = ListField(required=True, default=[""]) it is not possible to call to_json.
Used version: 1.1.5

Traceback (most recent call last):
  File "debug.py", line 10, in <module>
    print(r.to_json())
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 222, in to_json
    use_db_field, cur_depth=current_depth, good_json=True
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 160, in to_mongo
    self.begin_goodjson(cur_depth)
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 148, in begin_goodjson
    self.__set_gj_flag_sub_field(name, fld, cur_depth=cur_depth)
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 130, in __set_gj_flag_sub_field
    name, fld, cur_depth, set_good_json, "begin_goodjson"
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 122, in __apply_element
    recursive_apply_flag(fld)
  File "C:\Python36\lib\functools.py", line 803, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 106, in set_flag_list
    recursive_apply_flag(fld.field)
  File "C:\Python36\lib\functools.py", line 803, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 102, in recursive_apply_flag
    func(fld, cur_depth)
  File "C:\Python36\lib\site-packages\mongoengine_goodjson\document.py", line 127, in set_good_json
    setattr(traget, "$$cur_depth$$", depth_lv)
AttributeError: 'NoneType' object has no attribute '$$cur_depth$$'

To Reproduce

class User(gj.Document):
    email = StringField(required=True)
    tags= ListField(required=True, default=[])

User.objects(pk=id).first().to_json()

Expected behavior
Get an empty list:

{
   "email": "[email protected]"
   "tags": []
}

I was able to fix this by checking unset_flag and set_flag for None. If wished I can open a PR for this.

FollowReferenceField embedding document in parent document in MongoDB

I'm using Flask-Admin for CRUD maintenance.
When saving a document with db.ListField(gj.FollowReferenceField(MyObject), the reference field behaves like an EmbeddedDocumentField. That is, the referenced document becomes embedded in the parent document when saved to MongoDB.

Is this expected behavior?

I was expecting it continue to save only the reference document's ObjectId in the parent document.

Deprecation Warning When Executing Alongside Pytest

Describe the bug
When importing mongoengine-goodjson or a module that consumes mongoengine-goodjson into a pytest script, a deprecation warning is raised.

DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working @encode.register(collections.Iterable)

To Reproduce

  1. import mongoengine-goodjson into the same file pytest test cases will be executed
  2. run tests with pytest.
  3. Deprecation warning will be displayed

Expected behavior
No deprecation warning should be displayed.

Additional context

  • It does not appear mongoengine-goodjson will be compatible with python 3.9 unless this issue is resolved.
  • I would love to raise a fix for this but its not clear how the encoder and decoder are being generated.

Custom QuerySet cancels goodjson

If I add a custom QuerySet to a document, the output of document.objects is no longer filtered by goodjson

class SessionQuerySet(me.QuerySet):
    def context(self, session):
        f = session['farm_id']
        return self.filter(farm_id=f)


class WinterDocument(gj.Document):
    farm_id = me.ReferenceField('Farm', required=True, reverse_delete_rule=me.NULLIFY)
    meta = {"abstract": True, 'queryset_class': SessionQuerySet}


{"_id": {"$oid": "6071bbe055bc494c8227898e"}, "farm_id": {"$oid": "6071bbe055bc494c82278985"}, "name": "Vegetative - Baril 1", "ph": 5.8, "phMin": 5.6, "phMax": 6.2, "ec": 1800.0, "ecMin": 1600.0, "ecMax": 2001.0}

convert pipeline query result to JSON?

If I use aggregation pipeline. query result comes back as a dictionary instead of a mongo document object.
Is there a way to convert that dict result to goodjson format? because using bson.dumps produces inconsistent results with current goodjson format.

No ID replace when returning list of objects

When I run this:

instance = self.api_class.objects.get_or_404(id=object_id)
return instance.to_json()

I get a single document with the ID field formated correctly:

{"amount": 1, "descreption": "BBB", "pay_method": "5b51fed52c88845720509757", "id": "5b5101de2c88845fdf1429ed"}

But when I do:

return self.api_class.objects.to_json()

I get the following list without the IDs been formatted:

[{"_id": {"$oid": "5b5101de2c88845fdf1429ed"}, "amount": 1, "descreption": "BBB", "pay_method": {"$oid": "5b51fed52c88845720509757"}}, {"_id": {"$oid": "5b5204772c88845720509759"}, "amount": 100, "descreption": "BBB", "pay_method": {"$oid": "5b51fed52c88845720509757"}}]

Am I doing anything wrong?

Documentation

Is your feature request related to a problem? Please describe.
When someone use this code, it spends too much time to find features because
there's no document.

Describe the solution you'd like
Make the document

Describe alternatives you've considered
Auto generate document by pandoc Sphinx

Additional context
None

Refactoring

Currently, mongoengine_goodjson.Document and mongoengine_goodjson.EmbeddedDocumentinherit mongoengine_goodjson.Helper. Therefore the tests should be refactored focused on the class (at least unit tests.).

Recursive limit by condition

Adding check_depth kwarg i.e. FollowReferenceField(..., check_depth=[callable]), the field references its document with checking the return value of calling check_depth. Therefore, check_depth must be callable and must have the following arguments:

  • The document instance that is bound to FollowReferenceField.
  • Current depth
  • Max depth

`DynamicField` to_json error

Using to_json method on document objects that have a DynamicField throws and error

AttributeError: 'DynamicField' object has no attribute 'field'

To Reproduce
Steps to reproduce the behavior:

  1. Create a document Class with DynamicField in it
  2. Save an object of that class
  3. Retrieve that object and use to_json()

Expected behavior
It supposed to return a json representation of the Document including the dynamic field.

Add epoch flag for datetime encode

As of 0.8.19, datetime is encoded into ISO-formatted data, because WTForms and MongoEngine decode datetime with dateutils.parser.parsewhile bson.json_util decodes datetime from epoch-millisecond.

I like both, and I think I can add something like flag to use epoch millisecond and ISO for encode.

Possibility to add model class properties in to_json()?

Is your feature request related to a problem? Please describe.
I have a model that contains a few calculated properties (e.g. time_since_last_update).

Describe the solution you'd like
Is there a way to get these model class properties returned by .to_json()?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.