{"id":29656557,"url":"https://github.com/zincware/znjson","last_synced_at":"2025-07-22T08:36:00.699Z","repository":{"id":43261465,"uuid":"435230415","full_name":"zincware/ZnJSON","owner":"zincware","description":"Package to Encode/Decode some common file formats to json","archived":false,"fork":false,"pushed_at":"2025-06-23T19:34:34.000Z","size":231,"stargazers_count":4,"open_issues_count":9,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-23T20:31:03.895Z","etag":null,"topics":["deserialization","json","python","serialization"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zincware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-12-05T17:15:37.000Z","updated_at":"2025-05-06T10:55:29.000Z","dependencies_parsed_at":"2024-07-23T01:18:03.403Z","dependency_job_id":"ae233bd7-66f5-4873-9785-b6dd5aa89f3e","html_url":"https://github.com/zincware/ZnJSON","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/zincware/ZnJSON","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zincware%2FZnJSON","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zincware%2FZnJSON/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zincware%2FZnJSON/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zincware%2FZnJSON/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zincware","download_url":"https://codeload.github.com/zincware/ZnJSON/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zincware%2FZnJSON/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266456412,"owners_count":23931406,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["deserialization","json","python","serialization"],"created_at":"2025-07-22T08:35:56.057Z","updated_at":"2025-07-22T08:36:00.691Z","avatar_url":"https://github.com/zincware.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnJSON/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnJSON?branch=main)\n[![Code Style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black/)\n[![Tests](https://github.com/zincware/ZnJSON/actions/workflows/pytest.yaml/badge.svg)](https://coveralls.io/github/zincware/ZnJSON?branch=main)\n[![PyPI version](https://badge.fury.io/py/znjson.svg)](https://badge.fury.io/py/znjson)\n\n\n# ZnJSON\n\nPackage to Encode/Decode some common file formats to json\n\nAvailable via ``pip install znjson``\n\nIn comparison to `pickle` this allows having readable json files combined with some\nserialized data.\n\n# Example\n\n````python\nimport numpy as np\nimport json\nimport znjson\n\ndata = json.dumps(\n    obj={\"data_np\": np.arange(2), \"data\": [x for x in range(10)]},\n    cls=znjson.ZnEncoder,\n    indent=4\n)\n_ = json.loads(data, cls=znjson.ZnDecoder)\n````\nThe resulting ``*.json`` file is partially readable and looks like this:\n\n````json\n{\n    \"data_np\": {\n        \"_type\": \"np.ndarray_small\",\n        \"value\": [\n            0,\n            1\n        ]\n    },\n    \"data\": [\n        0,\n        1,\n        2,\n        3,\n        4\n    ]\n}\n````\n\n# Custom Converter\n\nZnJSON allows you to easily add custom converters.\nLet's write a serializer for ``datetime.datetime``. \n\n````python\nfrom znjson import ConverterBase\nfrom datetime import datetime\n\nclass DatetimeConverter(ConverterBase):\n    \"\"\"Encode/Decode datetime objects\n\n    Attributes\n    ----------\n    level: int\n        Priority of this converter over others.\n        A higher level will be used first, if there\n        are multiple converters available\n    representation: str\n        An unique identifier for this converter.\n    instance:\n        Used to select the correct converter.\n        This should fulfill isinstance(other, self.instance)\n        or __eq__ should be overwritten.\n    \"\"\"\n    level = 100\n    representation = \"datetime\"\n    instance = datetime\n\n    def encode(self, obj: datetime) -\u003e str:\n        \"\"\"Convert the datetime object to str / isoformat\"\"\"\n        return obj.isoformat()\n    def decode(self, value: str) -\u003e datetime:\n        \"\"\"Create datetime object from str / isoformat\"\"\"\n        return datetime.fromisoformat(value)\n````\n\nThis allows us to use this new serializer:\n````python\nznjson.config.register(DatetimeConverter) # we need to register the new converter first\njson_string = json.dumps(dt, cls=znjson.ZnEncoder, indent=4)\njson.loads(json_string, cls=znjson.ZnDecoder)\n````\n\nand will result in\n````json\n{\n    \"_type\": \"datetime\",\n    \"value\": \"2022-03-11T09:47:35.280331\"\n}\n````\n\nIf you don't want to register your converter to be used everywhere, simply use:\n\n```python\njson_string = json.dumps(dt, cls=znjson.ZnEncoder.from_converters(DatetimeConverter))\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzincware%2Fznjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzincware%2Fznjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzincware%2Fznjson/lists"}