A Python library written in Rust for JSON/Protobuf conversion
Project description
protoruf
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 with explicit message type specification
- 📦 Built-in proto compilation with Rust protox (no external dependencies!)
Installation
uv pip install -e .
Usage
from protoruf import json_to_protobuf, protobuf_to_json, compile_proto
# Step 1: Compile your .proto file to a descriptor set (using Rust protox)
descriptor = compile_proto("proto/message.proto")
# Or save to a file for reuse
# compile_proto("proto/message.proto", output_path="proto/message.desc")
# descriptor = load_descriptor("proto/message.desc")
# Step 2: Convert JSON to Protobuf (message_type is required)
json_data = '{"id": "123", "content": "Hello", "priority": 1, "tags": ["greeting"]}'
protobuf_bytes = json_to_protobuf(json_data, descriptor, message_type="message.Message")
# Step 3: Convert Protobuf back to JSON (message_type is required)
json_str = protobuf_to_json(protobuf_bytes, descriptor, message_type="message.Message", pretty=True)
print(json_str)
With Pydantic models (in your own code)
You can use Pydantic models in your application code to structure your data:
from pydantic import BaseModel, Field
from protoruf import compile_proto, json_to_protobuf, protobuf_to_json
# Define your own models in your project
class Metadata(BaseModel):
author: str = ""
created_at: int = 0
attributes: dict[str, str] = {}
class Message(BaseModel):
id: str = ""
content: str = ""
priority: int = 0
tags: list[str] = []
metadata: Metadata = None
# Compile your proto file
descriptor = compile_proto("proto/message.proto")
# Create a message using Pydantic
msg = Message(
id="123",
content="Hello",
priority=1,
tags=["greeting"],
metadata=Metadata(author="Alice")
)
# Convert to Protobuf (message_type is required)
protobuf_bytes = json_to_protobuf(msg.model_dump_json(), descriptor, message_type="message.Message")
# Convert back to JSON
result = protobuf_to_json(protobuf_bytes, descriptor, message_type="message.Message")
Examples
See the examples/ directory for more usage examples:
01_basic_user_example.py- Basic user service example02_ecommerce_example.py- E-commerce order example03_iot_sensors_example.py- IoT sensors data example04_pydantic_integration.py- Pydantic integration example
Development
# Install dependencies
uv sync
# Build the Rust extension
uv run maturin develop
# Run Python tests
uv run pytest
# Run Rust tests
cargo test --lib
# Type checking with mypy
uv run mypy python/protoruf/ --ignore-missing-imports
# Run all checks (tests + type checking)
uv run pytest && cargo test --lib && uv run mypy python/protoruf/ --ignore-missing-imports
Pre-commit checklist
Before committing, make sure all checks pass:
# 1. Python tests
uv run pytest tests/ -v
# 2. Rust tests
cargo test --lib
# 3. Type checking
uv run mypy python/protoruf/ --ignore-missing-imports
# 4. Build verification
uv run maturin develop
Project Structure
rust-json-probuff/
├── python/protoruf/ # Python package
│ ├── __init__.py # Main API: json_to_protobuf, protobuf_to_json
│ ├── _protoruf.pyi # Type stubs for Rust extension
│ ├── compiler.py # Proto compilation utilities
│ └── py.typed # PEP 561 marker
├── src/ # Rust source code
│ ├── core.rs # Core logic (pure Rust, testable)
│ └── lib.rs # Python bindings (PyO3)
├── tests/ # Python test suite
│ ├── proto/ # Test proto files
│ ├── test_models.py # Pydantic models for tests
│ └── test_rust_json_probuff.py
├── examples/ # Usage examples with their own .proto files
└── doc/ # Documentation
API Reference
compile_proto(proto_path, include_paths=None, output_path=None)
Compile a .proto file to a descriptor set.
- proto_path: Path to the
.protofile - include_paths: Optional list of include paths for imports
- output_path: Optional path to save the descriptor set
Returns the descriptor set as bytes.
load_descriptor(descriptor_path)
Load a pre-compiled descriptor set from a file.
- descriptor_path: Path to the
.descfile
Returns the descriptor set as bytes.
json_to_protobuf(json_str, descriptor_bytes, message_type)
Convert a JSON string to a Protobuf message.
- json_str: JSON string to convert
- descriptor_bytes: Compiled protobuf descriptor set
- message_type: Full message type name (e.g.,
"user.User","ecommerce.Order")
Returns the Protobuf message as bytes.
protobuf_to_json(protobuf_bytes, descriptor_bytes, message_type, pretty=False)
Convert a Protobuf message to a JSON string.
- protobuf_bytes: Protobuf message as bytes
- descriptor_bytes: Compiled protobuf descriptor set
- message_type: Full message type name (e.g.,
"user.User","ecommerce.Order") - pretty: If
True, format JSON with indentation
Returns the JSON string representation.
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file protoruf-0.1.0.tar.gz.
File metadata
- Download URL: protoruf-0.1.0.tar.gz
- Upload date:
- Size: 37.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eaa56c48cbe16674e174751d3446175586ea9a926b65b320850fc77a7c7f4adf
|
|
| MD5 |
af8a4e1fa913a113ab0928d9bb6fcbde
|
|
| BLAKE2b-256 |
0f87a06877395b6cba6835000a8a4a1f0854e1e0713ac82f865434eb798ccd05
|
Provenance
The following attestation bundles were made for protoruf-0.1.0.tar.gz:
Publisher:
publish-pypi.yml on EdwinAlkins/protoruf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
protoruf-0.1.0.tar.gz -
Subject digest:
eaa56c48cbe16674e174751d3446175586ea9a926b65b320850fc77a7c7f4adf - Sigstore transparency entry: 1258492853
- Sigstore integration time:
-
Permalink:
EdwinAlkins/protoruf@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/EdwinAlkins
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file protoruf-0.1.0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: protoruf-0.1.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 862.1 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c5c9bf7032c33a9d0f0463ffeef9cceff2083a730287dcdc04cfd05536ee805
|
|
| MD5 |
ac1f3a2e290a08919fc0e0bf62a23787
|
|
| BLAKE2b-256 |
3c5455243a9e049e98b0f84fcfc12da1fa6bb168d0ccb2d4f14bb03de4f45a26
|
Provenance
The following attestation bundles were made for protoruf-0.1.0-cp312-cp312-win_amd64.whl:
Publisher:
publish-pypi.yml on EdwinAlkins/protoruf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
protoruf-0.1.0-cp312-cp312-win_amd64.whl -
Subject digest:
6c5c9bf7032c33a9d0f0463ffeef9cceff2083a730287dcdc04cfd05536ee805 - Sigstore transparency entry: 1258492855
- Sigstore integration time:
-
Permalink:
EdwinAlkins/protoruf@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/EdwinAlkins
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file protoruf-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: protoruf-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca8bdbf21bf0eaf2120b034159135d89fd5f28f6485f6ef459bd8e7bfeea5336
|
|
| MD5 |
11065015c1ca4fe7f1740f18ceb7188f
|
|
| BLAKE2b-256 |
89538e40ed6234d1f79898bc1931b4a6fda92379f2c179ac77c755f234044dd0
|
Provenance
The following attestation bundles were made for protoruf-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
publish-pypi.yml on EdwinAlkins/protoruf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
protoruf-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
ca8bdbf21bf0eaf2120b034159135d89fd5f28f6485f6ef459bd8e7bfeea5336 - Sigstore transparency entry: 1258492856
- Sigstore integration time:
-
Permalink:
EdwinAlkins/protoruf@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/EdwinAlkins
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file protoruf-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: protoruf-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 957.0 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bda93bb25f1db65e2ce06b5cae28054861da4d147a049a2f4bcdca24a085951e
|
|
| MD5 |
6ffbbdbae528b7a9119a935d3e8881a1
|
|
| BLAKE2b-256 |
d62cbe731da6731cd8737a066d29d6b662ffff3920e57830aeeb05c52ab70d04
|
Provenance
The following attestation bundles were made for protoruf-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:
Publisher:
publish-pypi.yml on EdwinAlkins/protoruf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
protoruf-0.1.0-cp312-cp312-macosx_11_0_arm64.whl -
Subject digest:
bda93bb25f1db65e2ce06b5cae28054861da4d147a049a2f4bcdca24a085951e - Sigstore transparency entry: 1258492858
- Sigstore integration time:
-
Permalink:
EdwinAlkins/protoruf@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/EdwinAlkins
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@11e3f60e5ce883cbbc3380387f3830fda00b79b4 -
Trigger Event:
release
-
Statement type: