自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 中 jsonschema 深度運(yùn)用

開(kāi)發(fā) 前端
jsonschema 是一個(gè)強(qiáng)大的庫(kù),用于驗(yàn)證 JSON 數(shù)據(jù)是否符合特定的模式(Schema)。在實(shí)際應(yīng)用中,你可能會(huì)遇到更復(fù)雜的 JSON 結(jié)構(gòu)和驗(yàn)證需求。下面我將詳細(xì)介紹 jsonschema 的一些高級(jí)用法,包括復(fù)雜的數(shù)據(jù)結(jié)構(gòu)、自定義驗(yàn)證器以及如何處理嵌套的 JSON 對(duì)象。

前言

jsonschema 是一個(gè)強(qiáng)大的庫(kù),用于驗(yàn)證 JSON 數(shù)據(jù)是否符合特定的模式(Schema)。在實(shí)際應(yīng)用中,你可能會(huì)遇到更復(fù)雜的 JSON 結(jié)構(gòu)和驗(yàn)證需求。下面我將詳細(xì)介紹 jsonschema 的一些高級(jí)用法,包括復(fù)雜的數(shù)據(jù)結(jié)構(gòu)、自定義驗(yàn)證器以及如何處理嵌套的 JSON 對(duì)象。

1. 復(fù)雜的數(shù)據(jù)結(jié)構(gòu)

1.1 數(shù)組驗(yàn)證

你可以驗(yàn)證數(shù)組中的每個(gè)元素是否符合特定的模式。

import json
from jsonschema import validate, ValidationError
# 定義 JSON Schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "ages": {
            "type": "array",
            "items": {
                "type": "integer",
                "minimum": 0,
                "maximum": 120
            }
        }
    },
    "required": ["name", "ages"]
}
# 待驗(yàn)證的 JSON 數(shù)據(jù)
data = {
    "name": "Alice",
    "ages": [25, 30, 35]
}
# 驗(yàn)證 JSON 數(shù)據(jù)
try:
    validate(instance=data, schema=schema)
    print("JSON data is valid.")
except ValidationError as e:
    print(f"JSON data is invalid: {e}")

1.2 嵌套對(duì)象驗(yàn)證

你可以驗(yàn)證嵌套的對(duì)象是否符合特定的模式。

# 定義 JSON Schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "address": {
            "type": "object",
            "properties": {
                "street": {"type": "string"},
                "city": {"type": "string"},
                "zipcode": {"type": "string"}
            },
            "required": ["street", "city"]
        }
    },
    "required": ["name", "address"]
}
# 待驗(yàn)證的 JSON 數(shù)據(jù)
data = {
    "name": "Alice",
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "zipcode": "12345"
    }
}
# 驗(yàn)證 JSON 數(shù)據(jù)
try:
    validate(instance=data, schema=schema)
    print("JSON data is valid.")
except ValidationError as e:
    print(f"JSON data is invalid: {e}")

2. 自定義驗(yàn)證器

有時(shí)候你需要進(jìn)行更復(fù)雜的驗(yàn)證邏輯,這時(shí)可以使用 jsonschema 的 Validator 類(lèi)來(lái)創(chuàng)建自定義驗(yàn)證器。

2.1 自定義格式驗(yàn)證

你可以添加自定義的格式驗(yàn)證器,例如驗(yàn)證電話(huà)號(hào)碼格式。

from jsonschema import Draft7Validator, FormatChecker, ValidationError
# 定義自定義格式驗(yàn)證器
def is_valid_phone_number(phone_number):
    return phone_number.isdigit() and len(phone_number) == 10
format_checker = FormatChecker()
format_checker.checks('phone')(is_valid_phone_number)
# 定義 JSON Schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "phone": {"type": "string", "format": "phone"}
    },
    "required": ["name", "phone"]
}
# 待驗(yàn)證的 JSON 數(shù)據(jù)
data = {
    "name": "Alice",
    "phone": "1234567890"
}
# 創(chuàng)建 Validator 并驗(yàn)證 JSON 數(shù)據(jù)
validator = Draft7Validator(schema, format_checker=format_checker)
try:
    validator.validate(data)
    print("JSON data is valid.")
except ValidationError as e:
    print(f"JSON data is invalid: {e}")

2.2 自定義驗(yàn)證函數(shù)

你可以為特定屬性添加自定義的驗(yàn)證邏輯。

from jsonschema import Draft7Validator, ValidationError
# 定義自定義驗(yàn)證函數(shù)
def is_even(value):
    if value % 2 != 0:
        raise ValidationError(f"{value} is not an even number")
# 定義 JSON Schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "number": {"type": "integer"}
    },
    "required": ["name", "number"],
    "additionalProperties": False
}
# 創(chuàng)建 Validator 并添加自定義驗(yàn)證函數(shù)
validator = Draft7Validator(schema)
validator.VALIDATORS["even"] = is_even
schema["properties"]["number"]["even"] = True
# 待驗(yàn)證的 JSON 數(shù)據(jù)
data = {
    "name": "Alice",
    "number": 4
}
# 驗(yàn)證 JSON 數(shù)據(jù)
try:
    validator.validate(data)
    print("JSON data is valid.")
except ValidationError as e:
    print(f"JSON data is invalid: {e}")

3. 使用 RefResolver 進(jìn)行模塊化驗(yàn)證

對(duì)于大型項(xiàng)目,你可能希望將 Schema 分成多個(gè)文件,并使用引用($ref)來(lái)組合它們。RefResolver 可以幫助你管理這些引用。

3.1 模塊化 Schema 文件

假設(shè)你有兩個(gè)文件:person_schema.json 和 address_schema.json。

person_schema.json
{
    "$id": "http://example.com/schemas/person",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "address": {"$ref": "http://example.com/schemas/address"}
    },
    "required": ["name", "address"]
}
address_schema.json
{
    "$id": "http://example.com/schemas/address",
    "type": "object",
    "properties": {
        "street": {"type": "string"},
        "city": {"type": "string"},
        "zipcode": {"type": "string"}
    },
    "required": ["street", "city"]
}

3.2 使用 RefResolver 進(jìn)行驗(yàn)證

import json
from jsonschema import RefResolver, Draft7Validator, ValidationError
# 讀取 Schema 文件
with open('person_schema.json') as f:
    person_schema = json.load(f)
with open('address_schema.json') as f:
    address_schema = json.load(f)
# 創(chuàng)建 RefResolver
resolver = RefResolver(
    base_uri="http://example.com/schemas/",
    referrer=person_schema,
    store={
        "http://example.com/schemas/person": person_schema,
        "http://example.com/schemas/address": address_schema
    }
)
# 創(chuàng)建 Validator
validator = Draft7Validator(person_schema, resolver=resolver)
# 待驗(yàn)證的 JSON 數(shù)據(jù)
data = {
    "name": "Alice",
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "zipcode": "12345"
    }
}
# 驗(yàn)證 JSON 數(shù)據(jù)
try:
    validator.validate(data)
    print("JSON data is valid.")
except ValidationError as e:
    print(f"JSON data is invalid: {e}")

4. 使用 jsonschema 進(jìn)行數(shù)據(jù)轉(zhuǎn)換

雖然 jsonschema 主要用于驗(yàn)證,但你可以結(jié)合其他庫(kù)(如 voluptuous 或 marshmallow)來(lái)進(jìn)行數(shù)據(jù)轉(zhuǎn)換和驗(yàn)證。

4.1 結(jié)合 voluptuous 進(jìn)行數(shù)據(jù)轉(zhuǎn)換

voluptuous 是一個(gè)輕量級(jí)的數(shù)據(jù)驗(yàn)證和轉(zhuǎn)換庫(kù),可以與 jsonschema 結(jié)合使用。

import json
import voluptuous as vol
from jsonschema import validate, ValidationError
# 定義 JSON Schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0},
        "email": {"type": "string", "format": "email"}
    },
    "required": ["name", "age"]
}
# 定義 Voluptuous Schema
vol_schema = vol.Schema({
    vol.Required('name'): str,
    vol.Required('age'): int,
    vol.Optional('email'): vol.Email()
})
# 待驗(yàn)證的 JSON 數(shù)據(jù)
data = {
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com"
}
# 驗(yàn)證 JSON 數(shù)據(jù)
try:
    validate(instance=data, schema=schema)
    transformed_data = vol_schema(data)
    print("JSON data is valid and transformed:", transformed_data)
except (ValidationError, vol.Invalid) as e:
    print(f"JSON data is invalid: {e}")
責(zé)任編輯:華軒 來(lái)源: 測(cè)試開(kāi)發(fā)學(xué)習(xí)交流
相關(guān)推薦

2017-08-01 09:37:00

深度學(xué)習(xí)美團(tuán)機(jī)器學(xué)習(xí)

2016-12-21 16:40:22

TensorFlow深度學(xué)習(xí)算法大數(shù)據(jù)

2023-08-23 07:21:44

JsonSchema測(cè)試

2016-10-21 14:57:10

2009-12-30 20:44:04

IT系統(tǒng)管理運(yùn)維管理摩卡軟件

2010-03-10 18:32:45

Python多線程

2019-12-23 15:10:10

人臉識(shí)別AI人工智能

2011-01-11 10:21:56

TCPIP流量整形

2013-01-28 10:15:55

MPLS華為路由器

2009-07-30 14:03:04

ASP.NET中的se

2010-11-12 09:18:13

SQL Server存

2021-08-18 16:05:40

鴻蒙HarmonyOS應(yīng)用

2010-03-15 14:52:02

Python 3.0

2019-06-17 07:59:32

大數(shù)據(jù)CTOTeradata

2024-05-15 09:05:43

Python文檔處理工具自動(dòng)化文檔生成

2022-02-15 23:38:22

Python機(jī)器學(xué)習(xí)算法

2019-03-06 09:55:54

Python 開(kāi)發(fā)編程語(yǔ)言

2021-01-21 05:46:22

JavaLambda開(kāi)發(fā)

2023-11-13 22:30:16

C++開(kāi)發(fā)

2011-12-30 13:15:53

Java
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)