{"id":15068438,"url":"https://github.com/hiroaki-yamamoto/mongoengine-goodjson","last_synced_at":"2025-10-05T06:30:59.345Z","repository":{"id":6361533,"uuid":"54262044","full_name":"hiroaki-yamamoto/mongoengine-goodjson","owner":"hiroaki-yamamoto","description":"More human-readable json serializer/deserializer for MongoEngine","archived":true,"fork":false,"pushed_at":"2023-01-02T08:10:47.000Z","size":803,"stargazers_count":62,"open_issues_count":10,"forks_count":19,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-02T06:49:26.368Z","etag":null,"topics":["json-serialization","mongoengine","serialization"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hiroaki-yamamoto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-19T11:18:56.000Z","updated_at":"2024-11-04T13:29:01.000Z","dependencies_parsed_at":"2023-01-13T13:58:53.137Z","dependency_job_id":null,"html_url":"https://github.com/hiroaki-yamamoto/mongoengine-goodjson","commit_stats":null,"previous_names":[],"tags_count":58,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroaki-yamamoto%2Fmongoengine-goodjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroaki-yamamoto%2Fmongoengine-goodjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroaki-yamamoto%2Fmongoengine-goodjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroaki-yamamoto%2Fmongoengine-goodjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hiroaki-yamamoto","download_url":"https://codeload.github.com/hiroaki-yamamoto/mongoengine-goodjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235370461,"owners_count":18979093,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-serialization","mongoengine","serialization"],"created_at":"2024-09-25T01:35:48.099Z","updated_at":"2025-10-05T06:30:54.070Z","avatar_url":"https://github.com/hiroaki-yamamoto.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiroaki-yamamoto%2Fmongoengine-goodjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhiroaki-yamamoto%2Fmongoengine-goodjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiroaki-yamamoto%2Fmongoengine-goodjson/lists"}