Catalog PyPI
PACKAGE EXPLORERView in registry

a3json-struct

PyPI

a3json-struct can describe JSON structs using class-based syntax.

Versions collected1
Direct dependencies1From release 0.3.0
Last observed20 Oct, 16:39 UTC

README

Version 0.3.0 documentation

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

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.3.021 Mar 20251

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
three-kinds/a3json-struct

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 16:39 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:b0dc0adb3d03a6e2fa3326feadab2a41ee23ad56b18312a4e4435270ae52d231