A tool to dynamically create protobuf message classes from JSON Typedef
Project description
JTD To Proto
This library holds utilities for converting JSON Typedef to Protobuf.
Why?
The protobuf
langauge is a powerful tool for defining language-agnostic, composable datastructures. JSON Typedef
(JTD
) is also a powerful tool to accomplish the same task. Both have advantages and disadvantages that make each fit better for certain use cases. For example:
Protobuf
:- Advantages
- Compact serialization
- Auto-generated
grpc
client and service libraries - Client libraries can be used from different programming languages
- Disadvantages
- Learning curve to understand the full ecosystem
- Not a familiar tool outside of service engineering
- Static compilation step required to use in code
- Advantages
JTD
:- Advantages
- Can be learned in 5 minutes
- Can be written inline in the programming language of choice (e.g. as a
dict
inpython
)
- Disadvantages
- No optimized serialization beyond
json
- No automated service implementations
- Static
jtd-codegen
step needed to generate native structures
- No optimized serialization beyond
- Advantages
This project aims to bring them together so that a given project can take advantage of the best of both:
- Define your structures in
JTD
for simplicity - Dynamically create
google.protobuf.Descriptor
objects to allow forprotobuf
serialization and deserialization - Reverse render a
.proto
file from the generatedDescriptor
so that stubs can be generated in other languages - No static compiliation needed!
Usage
The usage of this library can be best understood with a simple example:
import jtd_to_proto
# Declare the Foo protobuf message class
Foo = jtd_to_proto.descriptor_to_message_class(
jtd_to_proto.jtd_to_proto(
name="Foo",
package="foobar",
jtd_def={
"properties": {
# Bool field
"foo": {
"type": "boolean",
},
# Array of nested enum values
"bar": {
"elements": {
"enum": ["EXAM", "JOKE_SETTING"],
}
}
}
},
)
)
# Declare an object that references Foo as the type for a field
Bar = jtd_to_proto.descriptor_to_message_class(
jtd_to_proto.jtd_to_proto(
name="Bar",
package="foobar",
jtd_def={
"properties": {
"baz": {
"type": Foo.DESCRIPTOR,
},
},
},
),
)
def write_protos(proto_dir: str):
"""Write out the .proto files for Foo and Bar to the given directory"""
Foo.write_proto_file(proto_dir)
Bar.write_proto_file(proto_dir)
Similar Projects
There are a number of similar projects in this space that offer slightly diferent value:
jtd-codegen
: This project focuses on statically generating language-native code (includingpython
) to represent the JTD schema.py-json-to-proto
: This project aims to deduce a schema from an instance of ajson
object.pure-protobuf
: This project has a very similar aim tojtd-to-proto
, but it skips the intermediatedescriptor
representation and thus is not able to produce nativemessage.Message
classes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for jtd_to_proto-0.8.2-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93317b6fb36a7a8c3087e2a68ecee0149c5da47c352443a86b137f152bc0c2aa |
|
MD5 | fe1b4cff42235685e59fced70259faa9 |
|
BLAKE2b-256 | bf8a195183e14f5cd627d33a55fd79b4a6b9542faf29196d315b6e916180b42b |
Hashes for jtd_to_proto-0.8.2-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e541c88a4c92297440b25ed9af5ebc71314519d00f94b4c52a2830d43358eba |
|
MD5 | 4568c3f7e5f8539e4c11210c91da3e44 |
|
BLAKE2b-256 | a5ba16eed98e5822738df34bb430e7ce82543b6fe3a78211324688c964503150 |
Hashes for jtd_to_proto-0.8.2-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8542dc09e8791270f65326e4deec1b189c15d6e596067d0b1f96b48910c26ad3 |
|
MD5 | 25c39c464d3f93aa8e1653b5d9ae62a1 |
|
BLAKE2b-256 | a39d0aeb9d7a6071feff0e33602a5ad89f8b81d8937b53d764ead786625515ba |
Hashes for jtd_to_proto-0.8.2-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e179e71df37323aac848d5693726f602285c2d4d2f6ebefc856165efbf85d03 |
|
MD5 | 9a45b712a8a2fd1220b02f407d0a78b9 |
|
BLAKE2b-256 | ed46df1070954e475cdcf4741096cc66fe43c2e5db038ee567ac022853cfdcd7 |