Skip to main content

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 example
  • 02_ecommerce_example.py - E-commerce order example
  • 03_iot_sensors_example.py - IoT sensors data example
  • 04_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 .proto file
  • 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 .desc file

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

protoruf-0.1.0.tar.gz (37.7 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.0-cp312-cp312-win_amd64.whl (862.1 kB view details)

Uploaded CPython 3.12Windows x86-64

protoruf-0.1.0-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.0-cp312-cp312-macosx_11_0_arm64.whl (957.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Hashes for protoruf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eaa56c48cbe16674e174751d3446175586ea9a926b65b320850fc77a7c7f4adf
MD5 af8a4e1fa913a113ab0928d9bb6fcbde
BLAKE2b-256 0f87a06877395b6cba6835000a8a4a1f0854e1e0713ac82f865434eb798ccd05

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoruf-0.1.0.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.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

Hashes for protoruf-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6c5c9bf7032c33a9d0f0463ffeef9cceff2083a730287dcdc04cfd05536ee805
MD5 ac1f3a2e290a08919fc0e0bf62a23787
BLAKE2b-256 3c5455243a9e049e98b0f84fcfc12da1fa6bb168d0ccb2d4f14bb03de4f45a26

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoruf-0.1.0-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.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for protoruf-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca8bdbf21bf0eaf2120b034159135d89fd5f28f6485f6ef459bd8e7bfeea5336
MD5 11065015c1ca4fe7f1740f18ceb7188f
BLAKE2b-256 89538e40ed6234d1f79898bc1931b4a6fda92379f2c179ac77c755f234044dd0

See more details on using hashes here.

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

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.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for protoruf-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bda93bb25f1db65e2ce06b5cae28054861da4d147a049a2f4bcdca24a085951e
MD5 6ffbbdbae528b7a9119a935d3e8881a1
BLAKE2b-256 d62cbe731da6731cd8737a066d29d6b662ffff3920e57830aeeb05c52ab70d04

See more details on using hashes here.

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

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