JTO Converter
Description
Convert json object to dataclass and vice versa. This package also provides tool for converting json object to dataclass template.
Requirements
Required structure of dataclass field
field_name: Optional[FieldType] = field(default=Undefined, metadata={'name': 'json_field_name', 'required': False, 'validate': validate_function})
field_name[required] can be any variable name.Optional[optional] indicates that the field is nullable.FieldType[required] should be strongly typed.
For example in case of field containing the list it should look like thisList[SomeClass]default[required] sets default field's value. Set toUndefinedby default.name[required] is the name of the field in original json.required[required] markedTrueif the field is required in the provided json.validate[optional] is the function that validates the field's value. Validate function supports fields with simple types likestr,int,float,boolandListof simple types. The function has one argument - field's value. It should returnTrueif the value is valid andFalseotherwise. Example lambda function:lambda x: x > 0
Additional rules
- If dataclass field value set to
Undefinedthen it will not be converted to json field - If dataclass field type is not
Optionalthen all dataclass fields withNonevalues will not be converted to json fields
Examples
Convert json object to class objects
from dataclasses import dataclass, field
from typing import List, Optional
from jto import JTOConverter
from jto.undefined_field import Undefined
data = {
"status": 200,
"data": {
"first": "qwer",
"last": "qwer",
"test": [
{"f1": "1"},
{"f1": "2"}
]
}
}
@dataclass
class Test:
f1: Optional[str] = field(default=Undefined, metadata={'name': 'f1', 'required': False})
@dataclass
class Data:
first: Optional[str] = field(default=Undefined, metadata={'name': 'first', 'required': False, 'validate': lambda x: x == 'qwer'})
last: Optional[str] = field(default=Undefined, metadata={'name': 'last', 'required': False})
test: Optional[List[Test]] = field(default=Undefined, metadata={'name': 'test', 'required': False})
@dataclass
class Response:
status: Optional[int] = field(default=Undefined, metadata={'name': 'status', 'required': False})
data: Optional[Data] = field(default=Undefined, metadata={'name': 'data', 'required': False})
dataclass_object = JTOConverter.from_json(Response, data)
print(dataclass_object)
dataclass_object.status = None
json_object = JTOConverter.to_json(dataclass_object)
print(json_object)
Get class templates from json object
from jto.dataclass_generator import DataclassGenerator
data = {
"status": 200,
"data": {
"first": "foo",
"last": "bar",
"struct": [
{"f1": "1"},
{"f1": "2"}
]
}
}
classes = DataclassGenerator()
classes_str = classes.build_classes_string('Response', data)
print(classes_str)
Metadata
Release files for jto 1.4.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 | Provenance | Actions |
|---|---|---|---|---|
| jto-1.4.0.tar.gz | 16.2 kB | Without provenance | Details for jto-1.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jto-1.4.0-py3-none-any.whl | Python 3 | none | any | Details for jto-1.4.0-py3-none-any.whl |
Total release size: 30.7 kB
Release files / jto-1.4.0.tar.gz
| Download URL | jto-1.4.0.tar.gz |
|---|---|
| Size | 16.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07331a06709def4af8ca9007b74e168ba72780d9c0a9a39f88ad9cf4b95c1533
|
|
BLAKE2b-256 checksum How to use checksums |
471f910ad5d4d1e7dc1ef2e9d50d48771ac6f4437a3530dc9032b19700e5fcc3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / jto-1.4.0-py3-none-any.whl
| Download URL | jto-1.4.0-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4dfd734231a601f0fb323a59b79fc9e101f840199980549e35d09350ab36bf2d
|
|
BLAKE2b-256 checksum How to use checksums |
af0c739e94e1d59707c5953db734719e631578bd7bc9cb99417599a6064e5601
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for jto 1.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.