# Json-Model
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.
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.
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 | |
|---|---|---|---|
| json-model-1.0.1.tar.gz | 3.6 kB | Details |
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 |