Skip to main content

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


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)

Uploaded Source

Built Distribution

pyrializer-0.1.0-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page