Pykson is a JSON serializer/deserializer in python.
Tested with:
- Python 3.6+
Use the following command to install using pip:
pip install pykson
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)
Use Pykson
class to deserialize json string to JsonObject
s
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)
Use Pykson
class to serialize JsonObject
s to string
Pykson.to_json(student)
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)
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)
Pykson currenty has five fields for handling date
s and datetime
s.
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.