A Python object (de)serializer
Project description
Pyrializer
A Python object (de)serializer
Basic usage
You must define classes and describe what attributes and their types using class attributes like this:
class Example
field_name = type1
other_field_name = type2
See Supported types below.
Decoding from a serialized value
Decoding an object maps a serialized value into a Python object:
from serializer import decode
payload = {
'name': 'John Doe',
'age': 52,
'job': {
'name': 'Software Engineer',
'salary': 24000
}},
'hobbies': ['fishing', 'skating']
}
class Job:
name = str
role = str
salary = float
class Person:
name = str
age = int
job = Job
hobbies = [str]
person = decode(Person, person_payload)
person.name # "John Doe'
person.job.salary # 24000
person.job.role # None
person.job.hobbies[1] # "skating"
Encoding to a serialized value
Encoding an object transform a Python object into a serializable format that can be easily exported to others formats, such as JSON:
from serializer import encode
encode(Person, person) # --> { 'name': 'John Doe', ... }
Additionaly, you can decorate the classes you want to (de)serialize with the
serializable
decorator. This decorator extends the classes with two
additional methods:
from pyrializer import serializable
@serializable
class Person:
...
person = Person.decode(person_payload)
person.encode() # --> { 'name': 'John Doe', ... }
Supported types
Here is some examples of supported types
<type> | JSON equivalent |
---|---|
None |
Any value |
str |
String |
int |
Integer |
float |
Float |
bool |
Boolean |
[<type>] |
Array of <type> |
ClassName |
Object |
More advanced examples:
class Example:
array_of_array_of_ints = [ [ int ] ] # [ [1,2], [3, 4], [], [5, 6] ]
whatever = None # 42, False, AnotherObject(), etc...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyrializer-0.1.0.tar.gz
(3.2 kB
view hashes)
Built Distribution
Close
Hashes for pyrializer-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b10dd35b3c284069897b4bc2594aef71b2c015a7d9968447b4bd1dde4ffbfcf |
|
MD5 | c867e61e6e2a2dc39f03beee2a82b4a5 |
|
BLAKE2b-256 | 3b21bc2ff89849089946e27765d33ea0a872017f232096d91bd84c742fce09e7 |