Python JSON Objects
Project description
pyjo
Python JSON Objects
Easily specify and (de)serialize data models in Python.
Install
pip install pyjo
How to use
First you need to create a specification of your models and attributes by using the Model and the Field classes:
from pyjo import Model, Field, RangeField, EnumField
class Gender(Enum):
female = 0
male = 1
class Address(Model):
city = Field(type=str)
postal_code = Field(type=int)
address = Field()
class User(Model):
name = Field(type=str, repr=True, required=True)
age = RangeField(min=18, max=120)
# equivalent to: Field(type=int, validator=lambda x: 18 <= x <= 120)
gender = EnumField(enum=Gender)
address = Field(type=Address)
By default any field is considered optional unless specified with required attribute. If required, its presence will be checked during initialization.
u = User()
# ...
# pyjo.exceptions.RequiredField: Field 'name' is required
User(name='john', age=18, address=Address(city='NYC'))
# <User(name=john)>
When the type argument is provided, the type of the value assigned to a field will be checked during initialization and assignment:
User(name=123)
# ...
# pyjo.exceptions.InvalidType: The value of the field 'name' is not of type str, given 123
u.address.city = 1
# ...
# pyjo.exceptions.InvalidType: The value of the field 'city' is not of type str, given 1
In order to serialize a model you need to call to_dict:
u = User(name='john', age=18, address=Address(city='NYC'))
print(u.to_dict())
# {
# "name": "john",
# "age": 18,
# "address": {
# "city": "NYC"
# }
# }
To create a model starting from its python dictionary representation, use from_dict:
u = User.from_dict({
"name": "john",
"gender": "male",
"age": 18,
"address": {
"city": "NYC"
}
})
print(u)
# <User(name=john)>
print(u.gender)
# Gender.male
print(u.address.city)
# NYC
Note that from_dict will also recreate all the nested pyjo objects.
API Documentation
Field
The Field object has several optional arguments:
typespecifies the type of the field. If specified, the type will be checked during initialization and assignmentdefaultspecifies the default value for the field. The value of a default can be a function and it will be computed and assigned during the initialization of the objectrequiredboolean flag to indicate if the field must be specified and can't beNone, even during assignment (Falseby default)reprboolean flag to indicate if the field/value should be shown in the Python representation of the object, when printed (Falseby default)to_dict,from_dict(functions) to add ad-hoc serialization/deserialization for the fieldvalidator(function) function that gets called to validate the input (after cast) of a fieldcast(function) cast value of the field (if not empty) before (validation and) assignment
Model
to_dict(),from_dict()serialize/deserialize to/from python dictionariesto_json(),from_json()shortcuts forjson.dumps(model.to_dict())andModel.from_dict(json.loads(<dict>))
Field subclasses
You can easily create subclasses of Field to handle specific types of objects. Several of them are already integrated in pyjo and more are coming (feel free to create a PR to add more):
ListFieldfor fields containing a list of elementsRegexFieldfor fields containing a string that matches a given regexRangeFieldfor fields containing a int with optional minimum/maximum valueDatetimeFieldfor fields containing a UTC datetime
Extensions
- pyjo_mongo easily interact with MongoDB documents, a lightweight replacement of the
mongoenginelibrary
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pyjo-3.3.0.tar.gz.
File metadata
- Download URL: pyjo-3.3.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f92fd05544902ac6fbcc8eb90e3a2ddc015dc64ad045a7fe28b4cffa59360745
|
|
| MD5 |
a53d382ce40383b6d4b65ceb4f2226a4
|
|
| BLAKE2b-256 |
ed5802231b697319a02adbf35cbeb415cc8726100bfd56ff37b2413e2111d2c1
|