Skip to main content

A simple library to serialize and deserialize protobuf messages

Project description

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.

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

protolizer-1.4.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

protolizer-1.4.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file protolizer-1.4.0.tar.gz.

File metadata

  • Download URL: protolizer-1.4.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for protolizer-1.4.0.tar.gz
Algorithm Hash digest
SHA256 86170325f1ea35c81249c002bb02e93dd285e6924f5cc318c2c5cc08b7ae03cc
MD5 4fe33d4be71466bef302056f97dbcbd2
BLAKE2b-256 f44809e3a43c8b40c2a01eb727ec12ad10237e6925be8636f8d068dcce2ef5c7

See more details on using hashes here.

File details

Details for the file protolizer-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: protolizer-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for protolizer-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc66140b3481ef20b5d9426476a3597dcd5e3051b887f0a08029f3f4722a7f27
MD5 0eaec089ab5d7b44585880037cf7a51c
BLAKE2b-256 edda72bd96b4793ed7c3a606064f4704bbc44222bbad65098ca6f8f2dabaa589

See more details on using hashes here.

Supported by

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