Skip to main content

A Python library written in Rust for JSON/Protobuf conversion

Project description

protoruf

protoruf logo

High-performance JSON ↔ Protobuf conversion for Python, powered by Rust

📖 Documentation💻 Source🐛 Issues


A high-performance Python library written in Rust for converting between JSON and Protobuf messages.

Features

  • Fast — JSON ↔ Protobuf conversion powered by Rust
  • 🔒 Type-safe — Explicit message type specification
  • 📦 Built-in proto compilation — Uses Rust protox, no external dependencies

Installation

pip install protoruf

Quick Start

from protoruf import json_to_protobuf, protobuf_to_json, compile_proto

# 1. Compile your .proto file
descriptor = compile_proto("message.proto")

# 2. Convert JSON to Protobuf
json_data = '{"id": "123", "content": "Hello", "priority": 1}'
protobuf_bytes = json_to_protobuf(json_data, descriptor, message_type="message.Message")

# 3. Convert Protobuf back to JSON
json_str = protobuf_to_json(protobuf_bytes, descriptor, message_type="message.Message", pretty=True)
print(json_str)

API Reference

compile_proto(proto_path, include_paths=None, output_path=None)

Compile a .proto file to a descriptor set.

Parameter Type Description
proto_path str Path to the .proto file
include_paths list[str] | None Optional include paths for imports
output_path str | None Optional path to save the descriptor

Returns: bytes — The compiled descriptor set.


load_descriptor(descriptor_path)

Load a pre-compiled descriptor set from a file.

Parameter Type Description
descriptor_path str Path to the .desc file

Returns: bytes — The descriptor set.


json_to_protobuf(json_str, descriptor_bytes, message_type)

Convert a JSON string to a Protobuf message.

Parameter Type Description
json_str str JSON string to convert
descriptor_bytes bytes Compiled protobuf descriptor set
message_type str Full message type name (e.g. "message.Message")

Returns: bytes — The Protobuf message.


protobuf_to_json(protobuf_bytes, descriptor_bytes, message_type, pretty=False)

Convert a Protobuf message to a JSON string.

Parameter Type Description
protobuf_bytes bytes Protobuf message as bytes
descriptor_bytes bytes Compiled protobuf descriptor set
message_type str Full message type name (e.g. "message.Message")
pretty bool If True, format JSON with indentation

Returns: str — The JSON representation.


pydantic_to_protobuf(pydantic_model, descriptor_bytes, message_type)

Convert a Pydantic model directly to a Protobuf message.

Parameter Type Description
pydantic_model BaseModel Pydantic model instance to convert
descriptor_bytes bytes Compiled protobuf descriptor set
message_type str Full message type name (e.g. "message.Message")

Returns: bytes — The Protobuf message.


protobuf_to_pydantic(protobuf_bytes, descriptor_bytes, model_class, message_type)

Convert a Protobuf message directly to a Pydantic model instance.

Parameter Type Description
protobuf_bytes bytes Protobuf message as bytes
descriptor_bytes bytes Compiled protobuf descriptor set
model_class Type[T] The Pydantic model class to instantiate
message_type str Full message type name (e.g. "message.Message")

Returns: T — An instance of the specified Pydantic model class.

Usage with Pydantic

protoruf provides built-in functions for seamless Pydantic integration:

from pydantic import BaseModel
from protoruf import compile_proto, pydantic_to_protobuf, protobuf_to_pydantic

class Message(BaseModel):
    id: str = ""
    content: str = ""
    priority: int = 0

descriptor = compile_proto("message.proto")
msg = Message(id="123", content="Hello", priority=1)

# Direct conversion from Pydantic to Protobuf
protobuf_bytes = pydantic_to_protobuf(msg, descriptor, message_type="message.Message")

# Direct conversion from Protobuf back to Pydantic
result = protobuf_to_pydantic(protobuf_bytes, descriptor, Message, message_type="message.Message")

Links

License

MIT

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

protoruf-0.1.3.tar.gz (630.4 kB view details)

Uploaded Source

Built Distributions

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

protoruf-0.1.3-cp312-cp312-win_amd64.whl (862.0 kB view details)

Uploaded CPython 3.12Windows x86-64

protoruf-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

protoruf-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (956.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file protoruf-0.1.3.tar.gz.

File metadata

  • Download URL: protoruf-0.1.3.tar.gz
  • Upload date:
  • Size: 630.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for protoruf-0.1.3.tar.gz
Algorithm Hash digest
SHA256 650d80f4578ae4adc1022aa5d805e21fafccbc7286dfbb99015a6cd9f4098d8a
MD5 b500d86cd3fcf356b0a4671aeae0c781
BLAKE2b-256 cf01f9ba58309f9edbb509bf03ea63e615e69ad2c083295469731617ab7606d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoruf-0.1.3.tar.gz:

Publisher: publish-pypi.yml on EdwinAlkins/protoruf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file protoruf-0.1.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: protoruf-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 862.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for protoruf-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ec2752e00b6a006489ea139670fd301e4912d82a256b5bf1aa4973e2f50cf35b
MD5 f7c5cd89c1ffa73dde2de3fe4e8d0e2e
BLAKE2b-256 30689fcf100238268ebee0c9463cf5fc2397ff663ef6f31f378031d6107b8f2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoruf-0.1.3-cp312-cp312-win_amd64.whl:

Publisher: publish-pypi.yml on EdwinAlkins/protoruf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file protoruf-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for protoruf-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 83989c3259eef0b56f88eb5c5382f8e2ee040154a39302ca0afc33afe798afe2
MD5 88632c7327f8d2f64cc33510c65e86eb
BLAKE2b-256 502e6f4bd197687e4f28ba9e2eb755216bb3923b082b210d79f6183dc1704398

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoruf-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-pypi.yml on EdwinAlkins/protoruf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file protoruf-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for protoruf-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 16f00047b4087697d18f9b6ad605ad48422305cebd376da102c282ee6c0c8d87
MD5 a23fe466b29d9e6984d12523cc220772
BLAKE2b-256 a1bd79921ff3b161ec50b14ff161a71f1496e9094a417cf96f82795437a30499

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoruf-0.1.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish-pypi.yml on EdwinAlkins/protoruf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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