Make JSON serialization easier

Related tags

JSON python json python3
Overview

jsonlab

GitHub PyPI PyPI - Format PyPI - Python Version PyPI - Implementation PyPI - Downloads

介绍

众所周知,python内置的json不提供将json字符串序列化成json字符串(__dict__可序列化一层字典,不能递归) ,也不提供将json字符串反向序列化成类对象的功能,为了解决这个痛点,再多方寻找无果后,决心自己开发提供该功能的库,现已开发完毕,在此公布于大众,以造福全人类。

安装

pip3 install jsonlab

使用场景

该库适用于对自定义类型的json序列化和json反序列化,比如我们在网络通信时定义了自己的模型,我们便可通过该库来将自定义类型实例序列化成json字符串发送,或者将接收到得json字符串反序列化成类实例。

注意

由于json序列化和反序列化时我们需要知道对象的类型,然而python语言的弱类型特征不能直观的获取属性类型,所以在使用这个库时有个约定:

1. 自定义的类型必须实现 __init__ 方法,且 __init__ 方法中必须包含所有要序列化反序列化的属性,并且,这些属性必须作为形参出现在 __init__ 方法的形参列表中,并且需要有对应的类型注解

2. 序列化/反序列化时是以 __init__ 函数中形参名作为key值,所以为了防止不必要的bug,请保持形参名和属性名一致

例1:

下面的Person类是一个典型的满足需求的定义

class Person(object):
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age


# or

class Person(object):
    def __init__(self, name: str = "kainhuck", age: int = 18):
        self.name = name
        self.age = age

例2:

下面的Person类中hobby属性将不会被序列化或反序列化

class Person(object):
    def __init__(self, name: str, age: int, hobby):
        self.name = name
        self.age = age
        self.hobby = hobby


# or

class Person(object):
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age
        self.hobby = ""

例3:

下面例子演示了继承类的写法

class B(object):
    def __init__(self, b_name: str):
        self.b_name = b_name


class A(B):
    def __init__(self, a_name: str, b_name: str):
        super().__init__(b_name)
        self.a_name = a_name

例4:

下面的例子演示了属性是其他类型的情况

class B(object):
    def __init__(self, b_name: str):
        self.b_name = b_name


class A(object):
    def __init__(self, a_name: str, b: B):
        self.a_name = a_name
        self.b = b

例5:

下面的例子演示了列表的使用, 需要注意的是:

  1. 如果一个类的属性是个列表,则可以使用 list 或者 [子类型]

  2. 如果采用第二种写法,目前只支持一种类型,也就len([子类型]) == 1

  3. 子类型支持一下几种情况

    子类型名 描述
    str 内置类型 -- 字符串
    int 内置类型 -- 整数
    float 内置类型 -- 浮点数
    bool 内置类型 -- 布尔
    list 内置类型 -- 普通列表(内部不可为自定义类型)
    dict 内置类型 -- 普通字典(内部不可为自定义类型)
    object 表示支持任意类型(但是不支持自定义类型) [object] == list
    自定义类型 自定义类型,目前只支持一个,也就是说一个list内部只有一种自定义类型
    [子类型] 嵌套list
    {key类型:子类型} 嵌套字典
class A:
    def __init__(self, values: [str]):
        self.values = values


# or 

class B:
    def __init__(self, b_name: str):
        self.b_name = b_name


class A:
    def __init__(self, values: [B]):
        self.values = values


# or

class A:
    def __init__(self, values: [[str]]):
        self.values = values


# or

class A:
    def __init__(self, values: [{str: object}]):
        self.values = values

例6:

下面的例子演示了字典的使用,需要注意的是:

  1. 如果一个类的属性是个字典,则可以使用 dict 或者 {key类型:value类型}

  2. 如果采用第二种写法,目前只支持一种类型,也就len({key类型:value类型}) == 1

  3. key类型支持如下

    1. str
    2. int
    3. float
    4. bool
  4. value类型支持如下

    类型名 描述
    str 内置类型 -- 字符串
    int 内置类型 -- 整数
    float 内置类型 -- 浮点数
    bool 内置类型 -- 布尔
    list 内置类型 -- 普通列表(内部不可为自定义类型)
    dict 内置类型 -- 普通字典(内部不可为自定义类型)
    object 表示支持任意类型(但是不支持自定义类型) [object] == list
    自定义类型 自定义类型,目前只支持一个,也就是说一个list内部只有一种自定义类型
    [子类型] 嵌套list
    {key类型:子类型} 嵌套字典
class A:
    def __init__(self, values: {str: str}):
        self.values = values


# or 

class B:
    def __init__(self, b_name: str):
        self.b_name = b_name


class A:
    def __init__(self, values: {str: B}):
        self.values = values


# or

class A:
    def __init__(self, values: {str: [str]}):
        self.values = values


# or

class A:
    def __init__(self, values: {str: {str: object}}):
        self.values = values

Usage

接口

  • marshal(obj) -> str

    传递一个类实例,返回一个序列化后的json字符串

  • marshal_to_dict(obj) -> dict

    传递一个类实例,返回一个字典对象

  • unmarshal(json_, type_: type)

    第一个参数可以是: json字符串,bytes类型的json字符串,字典对象

    第二个参数是自定义类型

    返回自定义类型的实例

demo

You might also like...
simplejson is a simple, fast, extensible JSON encoder/decoder for Python

simplejson simplejson is a simple, fast, complete, correct and extensible JSON http://json.org encoder and decoder for Python 3.3+ with legacy suppo

A fast JSON parser/generator for C++ with both SAX/DOM style API
A fast JSON parser/generator for C++ with both SAX/DOM style API

A fast JSON parser/generator for C++ with both SAX/DOM style API Tencent is pleased to support the open source community by making RapidJSON available

 simdjson : Parsing gigabytes of JSON per second
simdjson : Parsing gigabytes of JSON per second

JSON is everywhere on the Internet. Servers spend a *lot* of time parsing it. We need a fresh approach. The simdjson library uses commonly available SIMD instructions and microparallel algorithms to parse JSON 4x faster than RapidJSON and 25x faster than JSON for Modern C++.

import json files directly in your python scripts
import json files directly in your python scripts

Install Install from git repository pip install git+https://github.com/zaghaghi/direct-json-import.git Use With the following json in a file named inf

An tiny CLI to load data from a JSON File during development.

JSON Server - An tiny CLI to load data from a JSON File during development.

Convert your subscriptions csv file into a valid json for Newpipe!
Convert your subscriptions csv file into a valid json for Newpipe!

Newpipe-CSV-Fixer Convert your Google subscriptions CSV file into a valid JSON for Newpipe! Thanks to nikcorg for sharing how to convert the CSV into

Low code JSON to extract data in one line

JSON Inline Low code JSON to extract data in one line ENG RU Installation pip install json-inline Usage Rules Modificator Description ?key:value Searc

JSON for Modern C++ Release Scripts

JSON for Modern C++ Release Scripts Preparations Install required tools: make install_requirements. Add required keys to config.json (apparently not c

jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

A python library to convert arbitrary strings representing business opening hours into a JSON format that's easier to use in code

A python library to convert arbitrary strings representing business opening hours into a JSON format that's easier to use in code

Adrian Edwards 9 Dec 2, 2022
Creates fake JSON files from a JSON schema

Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.

Andy Challis 86 Jan 3, 2023
Json utils is a python module that you can use when working with json files.

Json-utils Json utils is a python module that you can use when working with json files. it comes packed with a lot of featrues Features Converting jso

Advik 4 Apr 24, 2022
Random JSON Key:Pair Json Generator

Random JSON Key:Value Pair Generator This simple script take an engish dictionary of words and and makes random key value pairs. The dictionary has ap

Chris Edwards 1 Oct 14, 2021
With the help of json txt you can use your txt file as a json file in a very simple way

json txt With the help of json txt you can use your txt file as a json file in a very simple way Dependencies re filemod pip install filemod Installat

Kshitij 1 Dec 14, 2022
Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

boris 5 Dec 1, 2021
Ibmi-json-beautify - Beautify json string with python

Ibmi-json-beautify - Beautify json string with python

Jefferson Vaughn 3 Feb 2, 2022
JSON Interoperability Vulnerability Labs

JSON Interoperability Vulnerability Labs Description These are the companion labs to my research article "An Exploration of JSON Interoperability Vuln

Bishop Fox 168 Dec 25, 2022
A tools to find the path of a specific key in deep nested JSON.

如何快速从深层嵌套 JSON 中找到特定的 Key #公众号 在爬虫开发的过程中,我们经常遇到一些 Ajax 加载的接口会返回 JSON 数据。

kingname 56 Dec 13, 2022
cysimdjson - Very fast Python JSON parsing library

Fast JSON parsing library for Python, 7-12 times faster than standard Python JSON parser.

TeskaLabs 235 Dec 29, 2022