Pykson: A JSON Serializer/Deserializer for Python
Pykson is a JSON serializer/deserializer in python.
Tested with:
- Python 3.6+
Use the following command to install using pip:
pip install pykson
Usage example
Create Object Models
First, create your object model which extends JsonObject
from pykson import JsonObject, IntegerField, StringField, ObjectListField
class Course(JsonObject):
name = StringField()
teacher = StringField()
class Score(JsonObject):
score = IntegerField()
course = Course()
class Student(JsonObject):
first_name = StringField()
last_name = StringField()
age = IntegerField()
scores = ObjectListField(Score)
Deserialize json strings
Use Pykson class to deserialize json string to JsonObjects
from pykson import Pykson
json_text = '{"first_name":"John", "last_name":"Smith", "age": 25, "scores": [ {"course": {"name": "Algebra", "teacher" :"Mr. Schmidt"}, "score": 100}, {"course": {"name": "Statistics", "teacher": "Mrs. Lee"}, "score": 90} ]}'
student = Pykson().from_json(json_text, Student)
Serialize objects
Use Pykson class to serialize JsonObjects to string
Pykson().to_json(student)
Fields
There are different types of predefined fields: IntegerField, FloatField, BooleanField, StringField, ListField, ObjectField, ObjectListField, DateField, TimeField, DateTimeField, TimestampSecondsField and TimestampMillisecondsField.
There are four other types of fields which help with storing fields with specific integer or string values. To create a field with multiple choice integer values, use MultipleChoiceIntegerField or EnumIntegerField classes. To create a field with multiple choice string values, use MultipleChoiceStringField or EnumStringField classes.
Example for MultipleChoiceStringField:
from pykson import MultipleChoiceStringField
class WeatherInfo(JsonObject):
condition = MultipleChoiceStringField(options=['sunny','cloudy','rainy'], null=False)
Example for EnumStringField:
from enum import Enum
from pykson import EnumStringField
class WeatherCondition(Enum):
SUNNY = 'sunny'
CLOUDY = 'cloudy'
RAINY = 'rainy'
class WeatherInfo(JsonObject):
condition = EnumStringField(enum=WeatherCondition, null=False)
Advanced usage
Serialized names
It is possible to use change name of fields during serialization/deserialization. For this purpose, use serialized_name input in the fields
from pykson import Pykson, JsonObject, IntegerField, StringField, ObjectField
class Score(JsonObject):
score = IntegerField(serialized_name="s")
course = StringField(serialized_name="c")
class Student(JsonObject):
first_name = StringField(serialized_name="fn")
last_name = StringField(serialized_name="ln")
age = IntegerField(serialized_name="a")
score = ObjectField(Score, serialized_name="s")
json_text = '{"fn":"John", "ln":"Smith", "a": 25, "s": {"s": 100, "c":"Algebra"}}'
student = Pykson().from_json(json_text, Student)
Work with dates and datetimes
Pykson currenty has five fields for handling dates and datetimes.
Three of them, DateField, TimeField and DateTimeField, use date/time formats to serialize/deserialize values. The other ones, TimestampSecondsField and TimestampMillisecondsField use integer values to serialize/deserialize datetimes.
Accept unknown key/value pairs when deserializing
from_json method currently has an input parameter named accept_unknown with default value of false. If you want to deserialize an string to a JsonObject and ignore unknown keys which are not defined in your model class as fields, you can set this parameter to true. If this parameter is false, an error is raised when facing an unknown key in the json.
json_text = '{"fn":"John", "ln":"Smith", "a": 25, "up":"some unknown parameter", "s": {"s": 100, "c":"Algebra"}}'
student = Pykson().from_json(json_text, Student, accept_unknown=True)
Type hierarchy adapter
You can register multiple type hierarchy adapters using register_type_hierarchy_adapter method of 'Pykson' class.
from pykson import TypeHierarchyAdapter
class Student(JsonObject):
name = StringField(serialized_name="n")
class HighSchoolStudent(Student):
high_school_name = StringField(serialized_name="sn")
class UniversityStudent(Student):
university_name = StringField(serialized_name="un")
students = [
HighSchoolStudent(name="john", high_school_name="Redstone High"),
UniversityStudent(name="alice", university_name="Green Institute of Tech.")
]
pson = Pykson()
pson.register_type_hierarchy_adapter(
Student,
"student_type",
{
"highschool": HighSchoolStudent,
"university": UniversityStudent
}
)
students_json = pson.to_json(students)
decoded_students = pson.from_json(students_json, Student)
assert decoded_students == students
Metadata
Release files for pykson 0.9.9.8.17
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 |
|---|---|---|---|---|
| pykson-0.9.9.8.17.tar.gz | 19.0 kB | Without provenance | Details for pykson-0.9.9.8.17.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pykson-0.9.9.8.17-py3-none-any.whl | Python 3 | none | any | Details for pykson-0.9.9.8.17-py3-none-any.whl |
Total release size: 37.1 kB
Release files / pykson-0.9.9.8.17.tar.gz
| Download URL | pykson-0.9.9.8.17.tar.gz |
|---|---|
| Size | 19.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4954f79d3206b108d6297bbb6af626d8492babdf3c1dfa035c7298205b43ec0b
|
|
BLAKE2b-256 checksum How to use checksums |
bd016e5c92da51a01345830896d4cfb9ee912837da813242f0aed27e19833c06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
|
Release files / pykson-0.9.9.8.17-py3-none-any.whl
| Download URL | pykson-0.9.9.8.17-py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
58feb5a27d512f81f0a590d4acc830b80a9cfcc2de47deea203516aea765f909
|
|
BLAKE2b-256 checksum How to use checksums |
6bc1ad8e78eab124306638633e7792eba64f3ab093099927ca8a7fd7a95d8a5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
|
Security for pykson 0.9.9.8.17
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.