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.11.5-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 538f2444ef9037794a9eba7c236d7a60645cdcc6fe0fb29fb6d0a0f69e168579 |
|
MD5 | 90b2edf8431edb4aa80c40bd3d29826f |
|
BLAKE2b-256 | 9e2707d16e4b684890f62b812dee18fc61d1b3d25ba4faf97508223ec3d0f41a |
Hashes for jtd_to_proto-0.11.5-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d2491d1bc9aa58706363f0661eb4f27f05b69c4ff68f6f7d30ec798f03bb526 |
|
MD5 | 2c9a563c1b2bec1931531d817395e64d |
|
BLAKE2b-256 | d0d00bf522242566dc2d59e91be69eaea9c209be3ca8ccf1fa2a7a68ef05c993 |
Hashes for jtd_to_proto-0.11.5-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbe0efb41cb17b66575faf6eace07f4ec18b0ab982cb61400591a6f325a94890 |
|
MD5 | de37f7b64413520a15a1e07d606bfb44 |
|
BLAKE2b-256 | 640931a42440881963b0b0f140569def91262d42c3eb1032d31514e920cbf00a |
Hashes for jtd_to_proto-0.11.5-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee8752961d15a6c448f337a347f5623039d4132bc7b50d998d6f2f7f737f76fc |
|
MD5 | d3aac6fc35f26f9d9a1aad8c6ce014ac |
|
BLAKE2b-256 | 767870e4a180eae8109143992534fa4da4ab261e2b283511b248227d0da6ae86 |