Json-Model is simple library to create Json Models from Python Objects. Library supports field validation by Python Types and required fields.
# Usage
Usage is really simple. Json-Model on this moment supports 7 basics fields:
- `Integer`
- `String`
- `Float`
- `List`
- `Timestamp`
- `Datetime`
- `ForeignField`
Each field has attribute:
- `required` - Boolean attribute in default is set to `False`.
- `default` - set default value when field is empty.
To start work with JsonModel, create Json-Model class with all fields what you need in Json object.
```python
import datetime
import time
from json_model import fields
from json_model import libs
class Scholarship(libs.JsonModel):
amount = fields.Float(required=True)
currency = fields.String(default='USD')
months = fields.List(required=True)
class Student(libs.JsonModel):
name = fields.String(required=True)
surname = fields.String(required=True)
age = fields.Integer()
day_of_birth = fields.Datetime()
scholarship = fields.ForeignField()
created_at = fields.Timestamp()
if __name__ == '__main__':
scholarship = Scholarship()
scholarship.amount = 500.00
scholarship.months = [1, 2, 3, 4, 5, 8, 9, 10]
student = Student()
student.name = "Andrew"
student.surname = "Gardner"
student.age = 26
student.day_of_birth = datetime.datetime.strptime('Jun 1 1999 1:33PM', '%b %d %Y %I:%M%p')
student.scholarship = scholarship
student.created_at = int(time.time())
print(student.to_json())
# Create objects by dictionary objects as parameter of JsonModel class.
student = Student(**{
'name': 'Andrew',
'surname': 'Gardner',
'age': 26,
'day_of_birth': datetime.datetime.strptime('Jun 1 1999 1:33PM', '%b %d %Y %I:%M%p'),
'scholarship': Scholarship(**{'amount': 500.00, 'months': [1, 2, 3, 4]}),
'created_at': int(time.time())
}
)
print(student.to_json())
```
Congratulation You have just created your first JsonModel implementation.
Metadata
Release files for json-model 1.0.1
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 |
|---|---|---|---|---|
| json-model-1.0.1.tar.gz | 3.6 kB | Without provenance | Details for json-model-1.0.1.tar.gz |
Release files / json-model-1.0.1.tar.gz
| Download URL | json-model-1.0.1.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
52f2ae4ed06f81a423a92fd422e44ca41b42723b0ece91215982add9c28efbec
|
|
BLAKE2b-256 checksum How to use checksums |
010674799af96912e148242ac02c0b7c9fe95733de635542b9aa43520af8045c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for json-model 1.0.1
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.