a3json-struct
English | 简体中文
a3json-structcan describe JSON structs using class-based syntax.
1. Introduction
- Support multiple data type fields
- Custom format validators
- Class instances can be exported as JSON, BSON
- The OpenAPI schema can be exported
- The attributes of a class can be exported as a JSON description, and the class can be restored from the JSON description
2. Usage
Install
pip install a3json-struct
Examples
from datetime import datetime
from a3json_struct import struct
class Comment(struct.JsonStruct):
content = struct.CharField()
post_time = struct.DateTimeField()
class Video(struct.JsonStruct):
id = struct.IntegerField(verbose_name='video id')
url = struct.CharField()
title = struct.CharField(min_length=5, max_length=30)
description = struct.CharField()
score = struct.DecimalField()
view_count = struct.IntegerField(default=0)
tag_list = struct.ListField(element_field=struct.CharField(min_length=2, max_length=8), required=False)
comment_list = struct.ListField(element_field=struct.ObjectField(obj_kls=Comment))
if __name__ == '__main__':
comment = Comment()
comment.content = "content"
comment.post_time = "2023-09-10 20:32"
video = Video()
video.id = 12345
video.url = "https://xxx.xxx/12345.mp4"
video.title = "video title"
video.description = "video description"
video.score = '2.3'
video.comment_list = [
comment, # It can be an object instance
{"content": "content", "post_time": datetime.now()} # or a dictionary
]
video.full_clean() # Validate data (this step can be omitted if using to_json or to_bson)
assert isinstance(video.to_json()["comment_list"][0]["post_time"], str)
assert isinstance(video.to_bson()["comment_list"][0]["post_time"], datetime)
openapi_schema = Video.generate_openapi_schema()
print(openapi_schema)
meta_schema = Video.generate_meta_schema()
RestoredVideo = struct.JsonStruct.build_variant_from_meta_schema(meta_schema, "RestoredVideo")
assert RestoredVideo.generate_meta_schema() == meta_schema
Metadata
Release files for a3json-struct 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| a3json-struct-0.3.0.tar.gz | 11.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| a3json_struct-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.5 kB
Release files / a3json-struct-0.3.0.tar.gz
| Download URL | a3json-struct-0.3.0.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e969aed206a246cc455ee91b9e323434575c34f849abf929870554bcb91f0f51
|
|
BLAKE2b-256 checksum How to use checksums |
06760a08b5f04d8756a92870a89db9390ba682068fcb77c19e626458dd6e3448
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.2
|
Release files / a3json_struct-0.3.0-py3-none-any.whl
| Download URL | a3json_struct-0.3.0-py3-none-any.whl |
|---|---|
| Size | 19.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b94bf931bcbebce6791afec8ec89899ba5e4504201246565532b6f82149d3154
|
|
BLAKE2b-256 checksum How to use checksums |
ed3f780f637483b23fa94175048a9c039bcc003aaf3b84c58cce5f41f3bdf883
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.2
|