Skip to main content

a3json-struct

English | 简体中文

  • a3json-struct can 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a3json-struct-0.3.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

a3json_struct-0.3.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file a3json-struct-0.3.0.tar.gz.

File metadata

  • Download URL: a3json-struct-0.3.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for a3json-struct-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e969aed206a246cc455ee91b9e323434575c34f849abf929870554bcb91f0f51
MD5 6f4ec4048df59d9b010f5102e5618123
BLAKE2b-256 06760a08b5f04d8756a92870a89db9390ba682068fcb77c19e626458dd6e3448

See more details on using hashes here.

File details

Details for the file a3json_struct-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: a3json_struct-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for a3json_struct-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b94bf931bcbebce6791afec8ec89899ba5e4504201246565532b6f82149d3154
MD5 a41c0df2406769b5a5920b17ac460e94
BLAKE2b-256 ed3f780f637483b23fa94175048a9c039bcc003aaf3b84c58cce5f41f3bdf883

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.2

1 file

0.2.1

1 file

0.2.0

1 file

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page