JSchema2py
JSchema2py is able to generate python classes starting from JSONSchema and provides the automatic support for types and constraints checking.
Installation
The package can be installed through pip:
$ pip install jschema2py
or downloaded from GitHub.
Examples
For example, given the following schema:
{
"title": "UserInfo",
"type": "object",
"properties": {
"name": {
"type": "string",
"pattern": "^[^a-z0-9]"
},
"userName": {
"type": "string"
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 100
}
}
}
jschema2py can easily convert it into python class in this way (Assume here that the schema is stored into variable called schema):
from jschema2py import build_class
UserInfo = build_class(schema)
user = UserInfo()
user.name = "Jacopo"
user.userName = "JDL"
user.age = 24
print(user)
validation will be performed on the object manipulation:
user.name = "jacopo" # raise ConstraintError (pattern: ^[^a-z0-9])
user.age = "24" # raise TypeError
The object can be serialized into a JSON document:
jsdoc = str(user)
Accessing generated classes
If one of the property of the schema refers to another object, you can access the class that represents the referred object by using method get_class:
{
"title": "Nested",
"type": "object",
"properties": {
"inner": {
"title": "Inner",
"type": "object",
"properties": {
"string": {
"type": "string",
"default": "I'm inner!"
}
}
}
}
}
from jschema2py import build_class
Nested = build_class(schema)
nested = Nested()
nested.inner = nested.get_class("inner")() # Gets the class associated with the property "inner"
print(nested.inner.string)
Metadata
Release files for jschema2py 0.1.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 | |
|---|---|---|---|
| jschema2py-0.1.0.tar.gz | 6.2 kB | Details |
Release files / jschema2py-0.1.0.tar.gz
| Download URL | jschema2py-0.1.0.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
859f9720bf11024d3fe30e736bf38487616627a7a98d4ca4f6591d0221728302
|
|
BLAKE2b-256 checksum How to use checksums |
86630b291fc527149cf4c67d16074aa5d4e422b010f62542e095be2376473f20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |