# 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.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
json-model-1.0.1.tar.gz
(3.6 kB
view details)
File details
Details for the file json-model-1.0.1.tar.gz.
File metadata
- Download URL: json-model-1.0.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52f2ae4ed06f81a423a92fd422e44ca41b42723b0ece91215982add9c28efbec
|
|
| MD5 |
1692d23d00266699035bf298bb3cfaf8
|
|
| BLAKE2b-256 |
010674799af96912e148242ac02c0b7c9fe95733de635542b9aa43520af8045c
|