Skip to main content

Protolizer Documentation

Introduction

Protolizer is a simple library to serialize and deserialize protobuf messages (JSON/dict ↔ protobuf). It does not require gRPC—only the protobuf package.

Installation

pip install protolizer

For running the project's tests and development tools:

pip install -e ".[dev]"

Or using requirements files:

pip install -r requirements.txt -r requirements-dev.txt

Usage for serialization

from protolizer import Serializer, fields

class AccountSerializer(Serializer):
    username = fields.CharField()
    balance = fields.IntField()

    class Meta:
        # schema must be your generated protobuf message class (e.g. from *_pb2 import Account)
        # Your .proto might look like:
        # message Account {
        #     string username = 1;
        #     int32 balance = 2;
        # }
        schema = Account

# Define an account in JSON and Protobuf format
protobuf_data = Account(username='John', balance=100)
json_data = {'username': 'John', 'balance': 100}

# Serialize the data to Protobuf format
protobuf_serializer = AccountSerializer(protobuf_data)
print(protobuf_serializer.protobuf)

# Serialize the data to JSON format
json_serializer = AccountSerializer(json_data)
print(json_serializer.data)

# Deserialize the data from Protobuf format to JSON format
protobuf_deserializer = AccountSerializer(protobuf_serializer.protobuf)
print(protobuf_deserializer.data)

# Deserialize the data from JSON format to Protobuf format
json_deserializer = AccountSerializer(json_serializer.data)
print(json_deserializer.protobuf)

If you want to see more examples, please check the examples directory.

Enum fields

from myapp_pb2 import Account, Status

class AccountSerializer(Serializer):
    username = fields.CharField()
    status = fields.EnumField(Status)

    class Meta:
        schema = Account

Partial updates

Pass partial=True to validate only the fields present in input (useful for PATCH-style updates):

serializer = AccountSerializer(data={"balance": 200}, partial=True)
serializer.is_valid(raise_exception=True)

Note: Meta.schema must be the generated protobuf message class (from your *_pb2 module). gRPC (grpcio) is only needed if you use gRPC services; for JSON ↔ protobuf conversion, the protobuf package alone is enough.

Supported fields

  • CharField
  • BytesField
  • IntField
  • FloatField
  • BooleanField
  • DateTimeField
  • TimestampField
  • EnumField
  • DictField
  • ListField
  • CustomField (for custom responses)

Field options: read_only, write_only, required, allow_null.

Contribute

See CONTRIBUTING.md for development setup and guidelines.

Security

See SECURITY.md to report vulnerabilities.

License

This project is licensed under the MIT License - see the LICENSE file for details

Author

This project is authored by @uwsgi on Telegram.

Metadata

Release files for protolizer 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for protolizer 1.4.1
File Size Uploaded
protolizer-1.4.1.tar.gz 18.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for protolizer 1.4.1
File Interpreter ABI Platform
protolizer-1.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.6 kB

Release files / protolizer-1.4.1.tar.gz

Download URL protolizer-1.4.1.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
26d0be629a5458e3af907910485bdae24706438cf8d12bcb3252e408d8a4be71
BLAKE2b-256 checksum
How to use checksums
3fdc88f689f4bc6a9380b858add37d4c0cac00e1e710df7be1bb5cbe07d7e45a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / protolizer-1.4.1-py3-none-any.whl

Download URL protolizer-1.4.1-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4d7a6da035a72aa127a416425d271a0f4788568cd358533f71e37900cb40858
BLAKE2b-256 checksum
How to use checksums
b87ffc73af29144ba96ba16334e9bf6ab28c13554d0caa8621a45bc2acb754aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.4.1 This release

2 release files

1.4.0

2 release files

1.1.1

1 release file

1.1.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page