{"id":24828207,"url":"https://github.com/kainhuck/jsonlab","last_synced_at":"2025-07-22T21:06:43.098Z","repository":{"id":62572863,"uuid":"424921238","full_name":"kainhuck/jsonlab","owner":"kainhuck","description":"Make JSON serialization easier","archived":false,"fork":false,"pushed_at":"2022-04-29T14:50:02.000Z","size":43,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-22T16:43:08.129Z","etag":null,"topics":["json","python","python3"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/jsonlab","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/kainhuck.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}},"created_at":"2021-11-05T11:15:38.000Z","updated_at":"2022-06-30T09:45:37.000Z","dependencies_parsed_at":"2022-11-03T18:30:26.323Z","dependency_job_id":null,"html_url":"https://github.com/kainhuck/jsonlab","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/kainhuck/jsonlab","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kainhuck%2Fjsonlab","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kainhuck%2Fjsonlab/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kainhuck%2Fjsonlab/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kainhuck%2Fjsonlab/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kainhuck","download_url":"https://codeload.github.com/kainhuck/jsonlab/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kainhuck%2Fjsonlab/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266572704,"owners_count":23950075,"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":["json","python","python3"],"created_at":"2025-01-30T22:51:47.537Z","updated_at":"2025-07-22T21:06:43.073Z","avatar_url":"https://github.com/kainhuck.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonlab\n\n![GitHub](https://img.shields.io/github/license/kainhuck/jsonlab?style=flat-square) ![PyPI](https://img.shields.io/pypi/v/jsonlab?style=flat-square) ![PyPI - Format](https://img.shields.io/pypi/format/jsonlab?style=flat-square) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/jsonlab?style=flat-square) ![PyPI - Implementation](https://img.shields.io/pypi/implementation/jsonlab?style=flat-square) ![PyPI - Downloads](https://img.shields.io/pypi/dm/jsonlab?style=flat-square)\n\n## 介绍\n\n众所周知，python内置的json不提供将类实例序列化成json字符串(`__dict__`可序列化一层字典，不能递归)\n，也不提供将json字符串反向序列化成类实例的功能，为了解决这个痛点，再多方寻找无果后，决心自己开发提供该功能的库，现已开发完毕，在此公布于大众，以造福全人类。\n\n## 安装\n\n\u003e pip3 install jsonlab\n\n## 使用场景\n\n该库适用于对自定义类型的json序列化和json反序列化，比如我们在网络通信时定义了自己的模型，我们便可通过该库来将自定义类型实例序列化成json字符串发送，或者将接收到得json字符串反序列化成类实例。\n\n## 注意\n\n由于json序列化和反序列化时我们需要知道对象的类型，然而python语言的弱类型特征不能直观的获取属性类型，所以在使用这个库时有个约定：\n\n````\n1. 自定义的类型必须实现 __init__ 方法，且 __init__ 方法中必须包含所有要序列化反序列化的属性，并且，这些属性必须作为形参出现在 __init__ 方法的形参列表中，并且需要有对应的类型注解\n\n2. 序列化/反序列化时是以 __init__ 函数中形参名作为key值，所以为了防止不必要的bug，请保持形参名和属性名一致\n````\n\n**例1：**\n\n下面的`Person`类是一个典型的满足需求的定义\n\n```python\nclass Person(object):\n    def __init__(self, name: str, age: int):\n        self.name = name\n        self.age = age\n\n\n# or\n\nclass Person(object):\n    def __init__(self, name: str = \"kainhuck\", age: int = 18):\n        self.name = name\n        self.age = age\n```\n\n**例2：**\n\n下面的`Person`类中`hobby`属性将不会被序列化或反序列化\n\n```python\nclass Person(object):\n    def __init__(self, name: str, age: int, hobby):\n        self.name = name\n        self.age = age\n        self.hobby = hobby\n\n\n# or\n\nclass Person(object):\n    def __init__(self, name: str, age: int):\n        self.name = name\n        self.age = age\n        self.hobby = \"\"\n```\n\n**例3：**\n\n下面例子演示了继承类的写法\n\n```python\nclass B(object):\n    def __init__(self, b_name: str):\n        self.b_name = b_name\n\n\nclass A(B):\n    def __init__(self, a_name: str, b_name: str):\n        super().__init__(b_name)\n        self.a_name = a_name\n```\n\n**例4：**\n\n下面的例子演示了属性是其他类型的情况\n\n```python\nclass B(object):\n    def __init__(self, b_name: str):\n        self.b_name = b_name\n\n\nclass A(object):\n    def __init__(self, a_name: str, b: B):\n        self.a_name = a_name\n        self.b = b\n```\n\n**例5：**\n\n下面的例子演示了列表的使用, 需要注意的是:\n\n1. 如果一个类的属性是个列表，则可以使用 `list` 或者 `[子类型]`\n\n2. 如果采用第二种写法，目前只支持一种类型，也就`len([子类型]) == 1`\n\n3. 子类型支持一下几种情况\n\n   | 子类型名         | 描述                                                         |\n      | ---------------- | ------------------------------------------------------------ |\n   | str              | 内置类型 -- 字符串                                           |\n   | int              | 内置类型 -- 整数                                             |\n   | float            | 内置类型 -- 浮点数                                           |\n   | bool             | 内置类型 -- 布尔                                             |\n   | list             | 内置类型 -- 普通列表(内部不可为自定义类型)                   |\n   | dict             | 内置类型 -- 普通字典(内部不可为自定义类型)                   |\n   | object           | 表示支持任意类型（但是不支持自定义类型） [object] == list    |\n   | 自定义类型       | 自定义类型，目前只支持一个，也就是说一个list内部只有一种自定义类型 |\n   | [子类型]         | 嵌套list                                                     |\n   | {key类型:子类型} | 嵌套字典                                                     |\n\n```python\nclass A:\n    def __init__(self, values: [str]):\n        self.values = values\n\n\n# or \n\nclass B:\n    def __init__(self, b_name: str):\n        self.b_name = b_name\n\n\nclass A:\n    def __init__(self, values: [B]):\n        self.values = values\n\n\n# or\n\nclass A:\n    def __init__(self, values: [[str]]):\n        self.values = values\n\n\n# or\n\nclass A:\n    def __init__(self, values: [{str: object}]):\n        self.values = values\n```\n\n**例6：**\n\n下面的例子演示了字典的使用，需要注意的是：\n\n1. 如果一个类的属性是个字典，则可以使用 `dict` 或者 `{key类型:value类型}`\n\n2. 如果采用第二种写法，目前只支持一种类型，也就`len({key类型:value类型}) == 1`\n\n3. key类型支持如下\n\n    1. str\n    2. int\n    3. float\n    4. bool\n\n4. value类型支持如下\n\n   | 类型名           | 描述                                                         |\n      | ---------------- | ------------------------------------------------------------ |\n   | str              | 内置类型 -- 字符串                                           |\n   | int              | 内置类型 -- 整数                                             |\n   | float            | 内置类型 -- 浮点数                                           |\n   | bool             | 内置类型 -- 布尔                                             |\n   | list             | 内置类型 -- 普通列表(内部不可为自定义类型)                   |\n   | dict             | 内置类型 -- 普通字典(内部不可为自定义类型)                   |\n   | object           | 表示支持任意类型（但是不支持自定义类型） [object] == list    |\n   | 自定义类型       | 自定义类型，目前只支持一个，也就是说一个list内部只有一种自定义类型 |\n   | [子类型]         | 嵌套list                                                     |\n   | {key类型:子类型} | 嵌套字典                                                     |\n\n```python\nclass A:\n    def __init__(self, values: {str: str}):\n        self.values = values\n\n\n# or \n\nclass B:\n    def __init__(self, b_name: str):\n        self.b_name = b_name\n\n\nclass A:\n    def __init__(self, values: {str: B}):\n        self.values = values\n\n\n# or\n\nclass A:\n    def __init__(self, values: {str: [str]}):\n        self.values = values\n\n\n# or\n\nclass A:\n    def __init__(self, values: {str: {str: object}}):\n        self.values = values\n```\n\n## Usage\n\n**接口**\n\n- `marshal(obj) -\u003e str`\n\n  传递一个类实例，返回一个序列化后的json字符串\n\n- `marshal_to_dict(obj) -\u003e dict`\n\n  传递一个类实例，返回一个字典对象\n\n- `unmarshal(json_, type_: type)`\n\n  第一个参数可以是: json字符串，bytes类型的json字符串，字典对象\n\n  第二个参数是自定义类型\n\n  返回自定义类型的实例\n\n**demo**\n\n- 序列化: [demo](example/marshal_demo.py)\n- 反序列化: [demo](example/unmarshal_demo.py)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkainhuck%2Fjsonlab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkainhuck%2Fjsonlab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkainhuck%2Fjsonlab/lists"}