Mã python này tạo tải trọng [với các giá trị ngẫu nhiên] từ lược đồ JSON chung
https. //github. com/BhanuAkaveeti/Python/tree/master/GenerateJSONPayloadFromSchema
Mã có thể dễ dàng sửa đổi/tùy chỉnh cho bất kỳ tình huống bổ sung nào
Phản hồi để cải thiện sản phẩm sẽ hữu ích
Định nghĩa kiểu JSON, hay còn gọi là RFC 8927, là một cách dễ học, được chuẩn hóa để xác định lược đồ cho dữ liệu JSON. Bạn có thể sử dụng JSON Typedef để xác thực hợp lệ dữ liệu trên các ngôn ngữ lập trình, tạo dữ liệu giả, tạo mã, v.v.
Bài viết này nói về cách bạn có thể sử dụng JSON Typedef để tạo mã Python từ các lược đồ. Nếu bạn quan tâm đến việc tạo mã bằng các ngôn ngữ khác, hãy xem bài viết này trên
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6. Phần còn lại của bài viết này tập trung vào việc sử dụng 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 với Python nói riêngTạo Python với 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
Như một điều kiện tiên quyết, trước tiên bạn cần cài đặt
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6. hướng dẫn cài đặt có sẵnBạn có thể tạo Python với
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 bằng cách sử dụng tùy chọn from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
1, giá trị của nó phải là một thư mục mà 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 có thể tạo mã vàoVí dụ: nếu bạn có lược đồ này trong
from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
3________số 8
Sau đó, bạn có thể tạo mã Python vào thư mục
from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
4 bằng cách chạy📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
0Cái nào sẽ xuất ra một cái gì đó như
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
Và bạn sẽ thấy mã dọc theo những dòng này trong
from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
5from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
Ghi chú. tại thời điểm viết, mã được tạo không phải lúc nào cũng được định dạng theo cách đẹp. Nếu bạn yêu cầu mã có định dạng đẹp, bạn nên sử dụng trình định dạng mã trên mã do
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 tạoSử dụng mã Python được tạo
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 sẽ luôn xuất mã thành một from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
8 bên trong thư mục bạn chỉ định với from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
1. Trong ví dụ trước, chúng tôi đã xuất mã thành from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin
def _from_json[cls, data]:
if data is None or cls in [bool, int, float, str, object] or cls is Any:
return data
if get_origin[cls] is Union:
return _from_json[get_args[cls][0], data]
if get_origin[cls] is list:
return [_from_json[get_args[cls][0], d] for d in data]
if get_origin[cls] is dict:
return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] }
return cls.from_json[data]
def _to_json[data]:
if data is None or type[data] in [bool, int, float, str, object]:
return data
if type[data] is list:
return [_to_json[d] for d in data]
if type[data] is dict:
return { k: _to_json[v] for k, v in data.items[] }
return data.to_json[]
@dataclass
class User:
created_at: 'str'
id: 'str'
is_admin: 'bool'
karma: 'int'
@classmethod
def from_json[cls, data] -> 'User':
return cls[
_from_json[str, data.get["createdAt"]],
_from_json[str, data.get["id"]],
_from_json[bool, data.get["isAdmin"]],
_from_json[int, data.get["karma"]],
]
def to_json[self]:
data = {}
data["createdAt"] = _to_json[self.created_at]
data["id"] = _to_json[self.id]
data["isAdmin"] = _to_json[self.is_admin]
data["karma"] = _to_json[self.karma]
return data
4, vì vậy chúng tôi có thể nhập mã như vậyMã Python được tạo không giả sử triển khai JSON cụ thể. Bạn có thể sử dụng các loại do
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 tạo với mô-đun 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
02 của thư viện tiêu chuẩn hoặc bạn có thể sử dụng các lựa chọn thay thế như 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
03 hoặc 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
04Mã được tạo độc lập với thư viện JSON vì mọi lớp hoặc enum được tạo đều có phương thức
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
05 và 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
06. 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
05 nhận dữ liệu JSON đã được phân tích cú pháp, chẳng hạn như dữ liệu được trả về từ 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
08 và trả về một thể hiện của loại. 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
06 làm ngược lại, xây dựng dữ liệu mà bạn có thể chuyển đến 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
80Không chuyển trực tiếp các thể hiện của các loại do
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 tạo vào thư viện JSON. Bạn sẽ nhận được kết quả JSON sai nếu bạn làm điều này. Luôn đi qua 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
05 hoặc 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
06 trướcĐây là một ví dụ về việc bạn sẽ sử dụng lớp
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
84 mà chúng tôi đã nhập ở trên📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
0Trong ví dụ trên, chúng tôi trực tiếp sử dụng
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
05 để phân tích dữ liệu JSON. Điều này sẽ chỉ hoạt động chính xác nếu đầu vào JSON hợp lệ. Mặt khác, bạn có thể gặp phải các ngoại lệ trong thời gian chạy và các loại thuộc tính của lớp có thể khác với các loại được chỉ định trên chú thích loại của chúng. Nếu bạn gặp phải các ngoại lệ trong thời gian chạy, các ngoại lệ này sẽ dành riêng cho Python và ở cấp độ thấpĐể ngăn điều này xảy ra, trước tiên bạn nên xác thực đầu vào JSON bằng cách triển khai xác thực JTD, chẳng hạn như gói
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
86. Những gì bạn sẽ làm là- Phân tích cú pháp đầu vào bằng chương trình phụ trợ JSON ưa thích của bạn
- Xác thực rằng JSON được phân tích cú pháp là hợp lệ đối với lược đồ mà bạn đã tạo các loại của mình từ đó, bằng cách sử dụng triển khai xác thực JTD. Nếu có lỗi xác thực, bạn có thể trả lại những lỗi đó, vì lỗi xác thực JTD được chuẩn hóa và không phụ thuộc vào nền tảng
- Nếu đầu vào hợp lệ, thì hãy xây dựng một thể hiện của loại đã tạo của bạn bằng phương thức
05 của nó. Chuyển JSON mà bạn vừa xác thực tới📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
05📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
Giải pháp này cho phép bạn tạo ra các lỗi xác thực di động và cho phép bạn cân nhắc kỹ hơn về những đầu vào bạn chấp nhận và không chấp nhận
Tùy chỉnh đầu ra Python
Tạo mã Python hỗ trợ các thuộc tính siêu dữ liệu sau được chia sẻ trên tất cả các ngôn ngữ được hỗ trợ bởi
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6
90 tùy chỉnh nhận xét tài liệu để đưa vào một loại hoặc thuộc tính trong mã được tạo. Ví dụ, lược đồ này📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
8📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
tạo thành
9📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
91 giống như📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
90, nhưng đối với các thành viên của một📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
93. Các khóa của ________ 391 phải tương ứng với các giá trị trong ________ 393 của lược đồ và các giá trị phải là mô tả cho các giá trị đó. Ví dụ, lược đồ này📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
7📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
tạo thành
8📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
Ngoài ra, việc tạo mã Python hỗ trợ các tùy chọn dành riêng cho Python sau đây
96 ghi đè loại mà📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
6 sẽ tạo.📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
6 sẽ không tạo bất kỳ mã nào cho các lược đồ với📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
96 và thay vào đó sử dụng giá trị của📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
96 nguyên trạng📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
Bạn có trách nhiệm đảm bảo rằng giá trị của
96 là mã hợp lệ.📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
6 sẽ không cố gắng xác thực giá trị của nó📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
Ví dụ, lược đồ này
6from dataclasses import dataclass from typing import Any, Optional, Union, get_args, get_origin def _from_json[cls, data]: if data is None or cls in [bool, int, float, str, object] or cls is Any: return data if get_origin[cls] is Union: return _from_json[get_args[cls][0], data] if get_origin[cls] is list: return [_from_json[get_args[cls][0], d] for d in data] if get_origin[cls] is dict: return { k: _from_json[get_args[cls][1], v] for k, v in data.items[] } return cls.from_json[data] def _to_json[data]: if data is None or type[data] in [bool, int, float, str, object]: return data if type[data] is list: return [_to_json[d] for d in data] if type[data] is dict: return { k: _to_json[v] for k, v in data.items[] } return data.to_json[] @dataclass class User: created_at: 'str' id: 'str' is_admin: 'bool' karma: 'int' @classmethod def from_json[cls, data] -> 'User': return cls[ _from_json[str, data.get["createdAt"]], _from_json[str, data.get["id"]], _from_json[bool, data.get["isAdmin"]], _from_json[int, data.get["karma"]], ] def to_json[self]: data = {} data["createdAt"] = _to_json[self.created_at] data["id"] = _to_json[self.id] data["isAdmin"] = _to_json[self.is_admin] data["karma"] = _to_json[self.karma] return data
tạo thành
00📝 Writing Python code to: src/user 📦 Generated Python code. 📦 Root schema converted into type: User
Mã Python được tạo
Phần này nêu chi tiết loại mã Python mà
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
6 sẽ tạoMã được tạo từ các lược đồ "Trống"
sẽ được chuyển đổi thành một Python
📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
006 và mỗi ánh xạ sẽ là một Python 📝 Writing Python code to: src/user
📦 Generated Python code.
📦 Root schema converted into type: User
006 mở rộng bộ phân biệt đối xử