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.9.0-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0345e2a9f4c270e133860f76757d2a983b36a6024d4903fabca1ff95dfbe7bcd |
|
MD5 | 048e142437c7dca75dfca492de1e2a41 |
|
BLAKE2b-256 | 3784704124fc929f1067fcc95ee9c047f54cbd54f0e79333c7ded78262044da0 |
Hashes for jtd_to_proto-0.9.0-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2487db12c16288bd3796550fb336ea1bac50d75e20cd434951ff7c8aa6ac0c23 |
|
MD5 | 43fb24d0487f791e0681a1e3f382c806 |
|
BLAKE2b-256 | f2c89f9c951bff99a9b58f76c1c3abf52fb8eeb4c203915d07d61c2597e2bb0c |
Hashes for jtd_to_proto-0.9.0-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a46723a65269af5e073ed8263939ede9f4d9935a5d5123e8abab8179f78170a |
|
MD5 | f5348655b0cbe030e6424c8f16f76829 |
|
BLAKE2b-256 | 694a12588eeca26c38bb5b686893f6619187ef7f57575ccf05062906dd097b54 |
Hashes for jtd_to_proto-0.9.0-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62341757f995b6e6ece3f9fd0fa9cdebbf48cf43b83b64921614b70a11805922 |
|
MD5 | a5ae40914bf7eb862586de7523d546ac |
|
BLAKE2b-256 | 4f83097f81347e158dd4cd99a74bdd3f55faba5e9d056cecbc24d6853c30914f |