Skip to main content

Readable serializable and deserializable Python nested models

Project description

jstruct

An eloquent and opinionated python library for nested object models definition offering simple serialization and deserialization into python dictionaries.

JStruct Codacy Badge Code style: black License: MIT

Why

The deserialization of JSON or yaml into python data types is a common practice useful in many ways.

  • Configuration file reading and writing
  • REST API message response generation and request processing
  • Object-Document Mapping for a document store
  • Data import parsing or export generation

How

JStruct leverage attrs the great Classes without boilerplate library to define structs without boilerplate.

What

The result is a simple and intuitive syntax familiar to a pythonista that brings Validation, Deserialization and Serialization.

Requirements

  • Python 3.6 and +

Installation

Install using pip

pip install jstruct

Usage

import attr
from typing import List
from jstruct import struct, JList

@struct
class Person:
    first_name: str
    last_name: str

@struct
class RoleModels:
    scientists: List[Person] = JList[Person]


payload = {
    "scientists": [{"first_name": "John", "last_name": "Doe"}] 
}

role_models = RoleModels(**payload)

print(role_models)

# RoleModels(scientists=[Person(first_name='John', last_name='Doe')])

print(attr.asdict(role_models))

# {'scientists': [{'first_name': 'John', 'last_name': 'Doe'}]}

Authors

Contribute

Contributors are welcomed.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

jstruct-2021.11-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file jstruct-2021.11-py3-none-any.whl.

File metadata

  • Download URL: jstruct-2021.11-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.6

File hashes

Hashes for jstruct-2021.11-py3-none-any.whl
Algorithm Hash digest
SHA256 0b1a7f3f88e34051b372953e0e6f88c465ddd84cdd7e15e5de10a8eeb6863485
MD5 1ce67a14ce1e2897c9da46915876a1a9
BLAKE2b-256 b523f99cc3785451817849451e0f9fb0f1cc3045ed8f5d2e49292fdced474579

See more details on using hashes here.

Supported by

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