Skip to main content

describe json struct with python class

Project description

a3json-struct

  • describe json struct with python class

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, # instance
        {"content": "content", "post_time": datetime.now()} # or dict
    ]
    
    video.full_clean() # clean instance (the step can be omitted)
    video.to_json()    # return json dict

Project details


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.2.2.tar.gz (13.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: a3json-struct-0.2.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for a3json-struct-0.2.2.tar.gz
Algorithm Hash digest
SHA256 8be091f6b551138a080bd9f21164c99bbd44b94a8f4425fc1dd0a00aecedfd89
MD5 cc5cd0cb75b43fd040aafb38f50823fe
BLAKE2b-256 11e85af986c33b684b6e247a6dd37d65c7d49eb19621e9b09cfa3b45575804e8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page