mongoengine-goodjson-v2


Namemongoengine-goodjson-v2 JSON
Version 2.0.2 PyPI version JSON
download
home_pagehttps://github.com/reminerdino/mongoengine-goodjson
SummaryMore human readable JSON serializer/de-serializer for MongoEngine
upload_time2024-07-14 17:05:59
maintainerNone
docs_urlNone
authorHiroaki Yamamoto, edited by Iman Ashoori
requires_pythonNone
licenseMIT
keywords json mongoengine mongodb
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # More human readable JSON serializer/de-serializer for MongoEngine
[![Build Status]][Status Link]
[![Test Coverage]][Test Coverage Link]
[![Maintainability]][Maintainability Link]
[![Documentation Status Image]][DocLink]

[Build Status]: https://circleci.com/gh/hiroaki-yamamoto/mongoengine-goodjson.svg?style=svg
[Status Link]: https://circleci.com/gh/hiroaki-yamamoto/mongoengine-goodjson
[Test Coverage]: https://api.codeclimate.com/v1/badges/7efc2a1bb3040cda0d4f/test_coverage
[Test Coverage Link]: https://codeclimate.com/github/hiroaki-yamamoto/mongoengine-goodjson/test_coverage
[Maintainability]: https://api.codeclimate.com/v1/badges/7efc2a1bb3040cda0d4f/maintainability
[Maintainability Link]: https://codeclimate.com/github/hiroaki-yamamoto/mongoengine-goodjson/maintainability
[Documentation Status Image]: https://readthedocs.org/projects/mongoengine-goodjson/badge/?version=latest
[DocLink]: https://mongoengine-goodjson.readthedocs.io/en/latest/?badge=latest

## 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:

```JSON
{
  "_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:

```JSON
{
  "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:

```Python
"""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.

```Python

"""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)
```


[MongoEngine]: http://mongoengine.org/
[MongoDB Extended JSON]: https://docs.mongodb.org/manual/reference/mongodb-extended-json/
[API's controller object]: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

## 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:

```Python
"""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.

[read the doc]: https://mongoengine-goodjson.readthedocs.io/

## License (MIT License)
See [LICENSE.md](LICENSE.md)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/reminerdino/mongoengine-goodjson",
    "name": "mongoengine-goodjson-v2",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "json mongoengine mongodb",
    "author": "Hiroaki Yamamoto, edited by Iman Ashoori",
    "author_email": "imanashoorii.77@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9c/de/9ca76da8f3cb62ae1fe8614d9813d3d58b2a24bd665bb6c25a1d41b78537/mongoengine_goodjson_v2-2.0.2.tar.gz",
    "platform": null,
    "description": "# More human readable JSON serializer/de-serializer for MongoEngine\n[![Build Status]][Status Link]\n[![Test Coverage]][Test Coverage Link]\n[![Maintainability]][Maintainability Link]\n[![Documentation Status Image]][DocLink]\n\n[Build Status]: https://circleci.com/gh/hiroaki-yamamoto/mongoengine-goodjson.svg?style=svg\n[Status Link]: https://circleci.com/gh/hiroaki-yamamoto/mongoengine-goodjson\n[Test Coverage]: https://api.codeclimate.com/v1/badges/7efc2a1bb3040cda0d4f/test_coverage\n[Test Coverage Link]: https://codeclimate.com/github/hiroaki-yamamoto/mongoengine-goodjson/test_coverage\n[Maintainability]: https://api.codeclimate.com/v1/badges/7efc2a1bb3040cda0d4f/maintainability\n[Maintainability Link]: https://codeclimate.com/github/hiroaki-yamamoto/mongoengine-goodjson/maintainability\n[Documentation Status Image]: https://readthedocs.org/projects/mongoengine-goodjson/badge/?version=latest\n[DocLink]: https://mongoengine-goodjson.readthedocs.io/en/latest/?badge=latest\n\n## What This?\nThis script has MongoEngine Document json serialization more-natural.\n\n## Why this invented?\n\nUsing MongoEngine to create something (e.g. RESTful API), sometimes you\nmight want to serialize the data from the db into JSON, but some fields\nare weird and not suitable for frontend/api:\n\n```JSON\n{\n  \"_id\": {\n    \"$oid\": \"5700c32a1cbd5856815051ce\"\n  },\n  \"name\": \"Hiroaki Yamamoto\",\n  \"registered_date\": {\n      \"$date\": 1459667811724\n  }\n}\n```\n\nThe points are 2 points:\n\n* `_id` might not be wanted because jslint disagrees `_` character unless\n  declaring `jslint nomen:true`\n* There are sub-fields such `$oid` and `$date`. These fields are known as\n  [MongoDB Extended JSON]. However, considering MongoEngine is ODM and\n  therefore it has schema-definition methods, the fields shouldn't have the\n  special fields. In particular problems, you might get\n  `No such property $oid of undefined` error when you handle above generated\n  data on frontend.\n\nTo solve the problems, the generated data should be like this:\n\n```JSON\n{\n  \"id\": \"5700c32a1cbd5856815051ce\",\n  \"name\": \"Hiroaki Yamamoto\",\n  \"registered_date\": 1459667811724\n}\n```\n\nMaking above structure can be possible by doing re-mapping, but if we do it on\n[API's controller object], the code might get super-dirty:\n\n```Python\n\"\"\"Dirty code.\"\"\"\nimport mongoengine as db\n\n\nclass User(db.Document):\n  \"\"\"User class.\"\"\"\n  name = db.StringField(required=True, unique=True)\n  registered_date = db.DateTimeField()\n\n\ndef get_user(self):\n  \"\"\"Get user.\"\"\"\n  models = [\n    {\n      (\"id\" if key == \"_id\" else key): (\n        value.pop(\"$oid\") if \"$oid\" in value and isinstance(value, dict)\n        else value.pop(\"$date\") if \"$date\" in value and isinstance(value, dict)\n        else value  #What if there are the special fields in child dict?\n      )\n      for (key, value) in doc.items()\n    } for doc in User.objects(pk=ObjectId(\"5700c32a1cbd5856815051ce\"))\n  ]\n  return json.dumps(models, indent=2)\n```\n\nTo give the solution of this problem, I developed this scirpt. By using this\nscript, you will not need to make the transform like above. i.e.\n\n```Python\n\n\"\"\"A little-bit clean code.\"\"\"\n\nimport mongoengine as db\nimport mongoengine_goodjson as gj\n\n\nclass User(gj.Document):\n  \"\"\"User class.\"\"\"\n  name = db.StringField(required=True, unique=True)\n  registered_date = db.DateTimeField()\n\n\ndef get_user(self):\n  \"\"\"Get user.\"\"\"\n  return model_cls.objects(\n    pk=ObjectId(\"5700c32a1cbd5856815051ce\")\n  ).to_json(indent=2)\n```\n\n\n[MongoEngine]: http://mongoengine.org/\n[MongoDB Extended JSON]: https://docs.mongodb.org/manual/reference/mongodb-extended-json/\n[API's controller object]: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html\n\n## How to use it\n\nGenerally you can define the document as usual, but you might want to inherits\n`mongoengine_goodjson.Document` or `mongoengine_goodjson.EmbeddedDocument`.\n\nHere is the example:\n\n```Python\n\"\"\"Example schema.\"\"\"\n\nimport mongoengine_goodjson as gj\nimport mongoengine as db\n\n\nclass Address(gj.EmbeddedDocument):\n    \"\"\"Address schema.\"\"\"\n\n    street = db.StringField()\n    city = db.StringField()\n    state = db.StringField()\n\n\nclass User(gj.Document):\n    \"\"\"User data schema.\"\"\"\n\n    name = db.StringField()\n    email = db.EmailField()\n    address = db.EmbeddedDocumentListField(Address)\n```\n\n## More details... there's the doc!\nIf you want to know more, there's [read the doc] that you want to read.\nYou can now [read the doc] with drinking a cup of coffee!!\n\n## Contribute\nPlease [read the doc] for the detail.\n\n[read the doc]: https://mongoengine-goodjson.readthedocs.io/\n\n## License (MIT License)\nSee [LICENSE.md](LICENSE.md)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "More human readable JSON serializer/de-serializer for MongoEngine",
    "version": "2.0.2",
    "project_urls": {
        "Homepage": "https://github.com/reminerdino/mongoengine-goodjson"
    },
    "split_keywords": [
        "json",
        "mongoengine",
        "mongodb"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d4b3a5c7488c7ba43b268b613fdd3a2ab2e8623addb9b0e823ee97bb48454c94",
                "md5": "b35501e1e7ef5baf05f91d00882bbe36",
                "sha256": "e6086585355d2375e22f1fbf0a26e82f2f89f15bea2a52a61c3ad06ae69d66d1"
            },
            "downloads": -1,
            "filename": "mongoengine_goodjson_v2-2.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b35501e1e7ef5baf05f91d00882bbe36",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 38851,
            "upload_time": "2024-07-14T17:05:57",
            "upload_time_iso_8601": "2024-07-14T17:05:57.305719Z",
            "url": "https://files.pythonhosted.org/packages/d4/b3/a5c7488c7ba43b268b613fdd3a2ab2e8623addb9b0e823ee97bb48454c94/mongoengine_goodjson_v2-2.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9cde9ca76da8f3cb62ae1fe8614d9813d3d58b2a24bd665bb6c25a1d41b78537",
                "md5": "c3ca613c6d23acf764f3a2146206c5b3",
                "sha256": "b899974ab66efc085d07cdf9f948a4c0b0a09bbcb46711221f7cb5fd9583acdc"
            },
            "downloads": -1,
            "filename": "mongoengine_goodjson_v2-2.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "c3ca613c6d23acf764f3a2146206c5b3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 31248,
            "upload_time": "2024-07-14T17:05:59",
            "upload_time_iso_8601": "2024-07-14T17:05:59.234882Z",
            "url": "https://files.pythonhosted.org/packages/9c/de/9ca76da8f3cb62ae1fe8614d9813d3d58b2a24bd665bb6c25a1d41b78537/mongoengine_goodjson_v2-2.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-14 17:05:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "reminerdino",
    "github_project": "mongoengine-goodjson",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "circle": true,
    "tox": true,
    "lcname": "mongoengine-goodjson-v2"
}
        
Elapsed time: 1.52876s