Models to make easier to deal with structures that are converted to, or read from JSON.
Project description
Models to make easier to deal with structures that are converted to, or read from JSON.
Free software: BSD license
Documentation: http://jsonmodels.rtfd.org
Features
Fully tested with Python 2.7, 3.3, 3.4 and PyPy.
Create Django-like models:
from jsonmodels import models, fields, error, validators
class Cat(models.Base):
name = fields.StringField(required=True)
breed = fields.StringField()
class Dog(models.Base):
name = fields.StringField(required=True)
age = fields.IntField()
class Car(models.Base):
registration_number = fields.StringField(required=True)
engine_capacity = fields.FloatField()
color = fields.StringField()
class Person(models.Base):
name = fields.StringField(required=True)
surname = fields.StringField(required=True)
car = fields.EmbeddedField(Car)
pets = fields.ListField([Cat, Dog])
Access to values through attributes:
>>> cat = Cat()
>>> cat.populate(name='Garfield')
>>> cat.name
'Garfield'
>>> cat.breed = 'mongrel'
>>> cat.breed
'mongrel'
Validate models:
>>> person = Person(name='Chuck', surname='Norris')
>>> person.validate()
None
>>> dog = Dog()
>>> dog.validate()
*** ValidationError: Field "name" is required!
Cast models to python struct and JSON:
>>> cat = Cat(name='Garfield')
>>> dog = Dog(name='Dogmeat', age=9)
>>> car = Car(registration_number='ASDF 777', color='red')
>>> person = Person(name='Johny', surname='Bravo', pets=[cat, dog])
>>> person.car = car
>>> person.to_struct()
{
'car': {
'color': 'red',
'registration_number': 'ASDF 777'
},
'surname': 'Bravo',
'name': 'Johny',
'pets': [
{'name': 'Garfield'},
{'age': 9, 'name': 'Dogmeat'}
]
}
>>> import json
>>> person_json = json.dumps(person.to_struct())
You don’t like to write JSON Schema? Let jsonmodels do it for you:
>>> person = Person()
>>> person.to_json_schema()
{
'additionalProperties': False,
'required': ['surname', 'name'],
'type': 'object',
'properties': {
'car': {
'additionalProperties': False,
'required': ['registration_number'],
'type': 'object',
'properties': {
'color': {'type': 'string'},
'engine_capacity': {'type': 'float'},
'registration_number': {'type': 'string'}
}
},
'surname': {'type': 'string'},
'name': {'type': 'string'},
'pets': {
'items': {
'oneOf': [
{
'additionalProperties': False,
'required': ['name'],
'type': 'object',
'properties': {
'breed': {'type': 'string'},
'name': {'type': 'string'}
}
},
{
'additionalProperties': False,
'required': ['name'],
'type': 'object',
'properties': {
'age': {'type': 'integer'},
'name': {'type': 'string'}
}
}
]
},
'type': 'list'
}
}
}
Validate models and use validators, that affect generated schema:
>>> class Person(models.Base):
...
... name = fields.StringField(
... required=True,
... validators=[
... validators.Regex('^[A-Za-z]+$'),
... validators.Length(3, 25),
... ],
... )
... age = fields.IntField(
... required=True,
... validators=[
... validators.Min(18),
... validators.Max(101),
... ]
... )
>>> person = Person()
>>> person.age = 11
>>> person.validate()
*** ValidationError: '11' is lower than minimum ('18').
>>> person.age = 19
>>> person.name = 'Scott_'
>>> person.validate()
*** ValidationError: Value "Scott_" did not match pattern "^[A-Za-z]+$".
>>> person.name = 'Scott'
>>> person.validate()
None
>>> person.to_json_schema()
{
"additionalProperties": false,
"properties": {
"age": {
"maximum": 101,
"minimum": 18,
"type": "integer"
},
"name": {
"maxLength": 25,
"minLength": 3,
"pattern": "/^[A-Za-z]+$/",
"type": "string"
}
},
"required": [
"age",
"name"
],
"type": "object"
}
For more information, please see topic about validation in documentation.
Compare JSON schemas:
>>> from jsonmodels.utils import compare_schemas
>>> schema1 = {'type': 'object'}
>>> schema2 = {'type': 'list'}
>>> compare_schemas(schema1, schema1)
True
>>> compare_schemas(schema1, schema2)
False
More
For more examples and better description see full documentation: http://jsonmodels.rtfd.org.
History
1.4.1 (2014-11-15)
Fixed schema generation for primitives.
1.4 (2014-07-22)
Allowed validators to modify generated schema.
Added validator for maximum value.
Added utils to convert regexes between Python and ECMA formats.
Added validator for regex.
Added validator for minimum value.
By default “validators” of field are empty list.
1.3.1 (2014-07-13)
Fixed generation of schema for BoolField.
1.3 (2014-07-13)
Added new fields (BoolField, TimeField, DateField and DateTimeField).
ListField is always not required.
Schema can be now generated from class itself (not from an instance).
1.2 (2014-06-18)
Fixed values population, when value is not dictionary.
Added custom validators.
Added tool for schema comparison.
1.1.1 (2014-06-07)
Added possibility to populate already initialized data to EmbeddedField.
Added compare_schemas utility.
1.1 (2014-05-19)
Added docs.
Added json schema generation.
Added tests for PEP8 and complexity.
Moved to Python 3.4.
Added PEP257 compatibility.
Added help text to fields.
1.0.5 (2014-04-14)
Added data transformers.
1.0.4 (2014-04-13)
List field now supports simple types.
1.0.3 (2014-04-10)
Fixed compatibility with Python 3.
Fixed str and repr methods.
1.0.2 (2014-04-03)
Added deep data initialization.
1.0.1 (2014-04-03)
Added populate method.
1.0 (2014-04-02)
First stable release on PyPI.
0.1.0 (2014-03-17)
First release on PyPI.
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
Built Distribution
File details
Details for the file jsonmodels-1.4.1.tar.gz
.
File metadata
- Download URL: jsonmodels-1.4.1.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8d9e29fa363e86ebfdb99d99817cf58f8aa3b19f2ec1709144ea7b6c9dd9766 |
|
MD5 | ab03441ce1c4fb1502b5c798bd539d36 |
|
BLAKE2b-256 | c2ec784ad8f19ecf185879ee250e2f5be7aa0da78ff26760d06d22cfd9c83d9a |
File details
Details for the file jsonmodels-1.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: jsonmodels-1.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 044772a71fbe2b1a341fbdea8dd81c4f251e2e3f5242a518e7881c94ed9881bf |
|
MD5 | 16aba322d6d78aa65640d1a76adea532 |
|
BLAKE2b-256 | 9e3a38de1267ad031fbdeb806973686903e9e414c048836bf7229236b510fd47 |