{"id":15705281,"url":"https://github.com/brahmlower/json-cerealizer","last_synced_at":"2025-03-30T15:26:00.321Z","repository":{"id":62572822,"uuid":"101022768","full_name":"brahmlower/json-cerealizer","owner":"brahmlower","description":"A tiny library to make json encoding non-serializable objects easier.","archived":false,"fork":false,"pushed_at":"2018-01-29T10:15:29.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-10T21:50:00.622Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brahmlower.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-22T04:57:33.000Z","updated_at":"2023-10-14T15:44:45.000Z","dependencies_parsed_at":"2022-11-03T18:30:20.835Z","dependency_job_id":null,"html_url":"https://github.com/brahmlower/json-cerealizer","commit_stats":null,"previous_names":["bplower/json-cerealizer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fjson-cerealizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fjson-cerealizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fjson-cerealizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fjson-cerealizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brahmlower","download_url":"https://codeload.github.com/brahmlower/json-cerealizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246336619,"owners_count":20761084,"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":[],"created_at":"2024-10-03T20:15:19.880Z","updated_at":"2025-03-30T15:26:00.300Z","avatar_url":"https://github.com/brahmlower.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON Cerealizer\n[![Build Status](https://api.travis-ci.org/bplower/json-cerealizer.svg?branch=master)](https://api.travis-ci.org/bplower/json-cerealizer.svg?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/bplower/json-cerealizer/badge.svg?branch=master)](https://coveralls.io/github/bplower/json-cerealizer?branch=master)\n\nA simple library for monkey patching the python json library, thereby making it\neasier to add serializers for objects that cannot be encoded using the default\nJSONEncoder.\n\n## Install\n\n```\npip install json-cerealizer\n```\n\n## TL;DR Example\n\nImport the library, run the monkey patch, then register functions to handle\nclass serialization. Call json.dumps as usual and receive output for your\ntypically un-encodable objects.\n\n```python\n\u003e\u003e\u003e from datetime import datetime\n\u003e\u003e\u003e import json\n\u003e\u003e\u003e import json_cerealizer\n\u003e\u003e\u003e\n\u003e\u003e\u003e json_cerealizer.patch()\n\u003e\u003e\u003e json_cerealizer.add_serializer(datetime, datetime.isoformat)\n\u003e\u003e\u003e\n\u003e\u003e\u003e time_dict = {\"now\": datetime.now()}\n\u003e\u003e\u003e json.dumps(time_dict)\n'{\"now\": \"2017-08-21T19:57:31.761091\"}'\n```\n\n## Use Case\n\nThe JSON standard can only represent a handful of data types. If you attempt to\nserialize a non-standard type, you receive a TypeError. The following is an\nexample showing that datetime objects cannot be serialized by the default\nJSON encoder.\n\n```python\n\u003e\u003e\u003e import json\n\u003e\u003e\u003e from datetime import datetime\n\u003e\u003e\u003e\n\u003e\u003e\u003e time_dict = {\"now\": datetime.now()}\n\u003e\u003e\u003e time_dict\n{'now': datetime.datetime(2017, 8, 21, 19, 47, 17, 785813)}\n\u003e\u003e\u003e json.dumps(time_dict)\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n  File \"/usr/lib/python3.5/json/__init__.py\", line 230, in dumps\n    return _default_encoder.encode(obj)\n  File \"/usr/lib/python3.5/json/encoder.py\", line 198, in encode\n    chunks = self.iterencode(o, _one_shot=True)\n  File \"/usr/lib/python3.5/json/encoder.py\", line 256, in iterencode\n    return _iterencode(o, 0)\n  File \"/usr/lib/python3.5/json/encoder.py\", line 179, in default\n    raise TypeError(repr(o) + \" is not JSON serializable\")\nTypeError: datetime.datetime(2017, 8, 21, 19, 47, 17, 785813) is not JSON serializable\n```\n\nThis issue is easily resolved by subclassing the json.JSONEncoder class,\nallowing you to specify how objects should be serialized.\n\n```python\n\u003e\u003e\u003e import json\n\u003e\u003e\u003e from datetime import datetime\n\u003e\u003e\u003e\n\u003e\u003e\u003e class MyEncoder(json.JSONEncoder):\n...     def default(self, obj):\n...         if isinstance(obj, datetime):\n...             return obj.isoformat()\n...\n\u003e\u003e\u003e time_dict = {\"now\": datetime.now()}\n\u003e\u003e\u003e json.dumps(time_dict, cls=MyEncoder)\n'{\"now\": \"2017-08-21T19:57:31.761091\"}'\n```\n\nWhile this works fine in small cases, it becomes bloated when you want to add\nsupport for several more types. This is where json-cerealizer shines. Here we\nmonkey patch the json library, then register a function to handle instances of\na particular class. In this case, we are saying instances of `datetime` should\nbe passed into the function `datetime.isoformat`, which will return a value\nthat is natively serializable.\n\n```python\n\u003e\u003e\u003e from datetime import datetime\n\u003e\u003e\u003e import json\n\u003e\u003e\u003e import json_cerealizer\n\u003e\u003e\u003e\n\u003e\u003e\u003e json_cerealizer.patch()\n\u003e\u003e\u003e json_cerealizer.add_serializer(datetime, datetime.isoformat)\n\u003e\u003e\u003e\n\u003e\u003e\u003e time_dict = {\"now\": datetime.now()}\n\u003e\u003e\u003e json.dumps(time_dict)\n'{\"now\": \"2017-08-21T19:57:31.761091\"}'\n```\n\nTo solidify our understanding, lets handle a class of our own.\n\n```python\n\u003e\u003e\u003e import json\n\u003e\u003e\u003e import json_cerealizer\n\u003e\u003e\u003e\n\u003e\u003e\u003e class Rectangle(object):\n...     def __init__(self, x, y):\n...         self.x = x\n...         self.y = y\n...\n\u003e\u003e\u003e def rectangle_to_dict(rec):\n...     return {\"x\": rec.x, \"y\": rec.y, \"area\": rec.x * rec.y}\n...\n\u003e\u003e\u003e json_cerealizer.patch()\n\u003e\u003e\u003e json_cerealizer.add_serializer(Rectangle, rectangle_to_dict)\n\u003e\u003e\u003e r = Rectangle(3, 4)\n\u003e\u003e\u003e json.dumps(r)\n'{\"x\": 3, \"y\": 4, \"area\": 12}'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrahmlower%2Fjson-cerealizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrahmlower%2Fjson-cerealizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrahmlower%2Fjson-cerealizer/lists"}