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.5.0-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26ffda40ccb59658f8f62b4341d21ac04fc453c6d04d53b69c84e0b5cf48aecf |
|
MD5 | 9cdc73dfa3002744d2a62b5cb5da6d2a |
|
BLAKE2b-256 | f4b3c6486f0a0a722ef30c4c3fb3c5ad120f048227b38b31b2709117d15faa27 |
Hashes for jtd_to_proto-0.5.0-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cccebf0a3b9ed87e35e5caceaacf8872a803d3d6df3b001c70a7598b60162076 |
|
MD5 | 2d7a4fd7d6862a227afbada8fa50716f |
|
BLAKE2b-256 | 7cd6adf479f09f0644b6b6f1bf1adaf38971229eb45cc4e53d01c61fcccdfe6a |
Hashes for jtd_to_proto-0.5.0-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94429b46ef0896664a0717dbf2c63e289613ff839ff27d5c0de3b148eec39276 |
|
MD5 | 39ea3f8598c47787823ec13922941b2d |
|
BLAKE2b-256 | aae2fb77b59cb3631ec146c41a3299b8b78447230792129269de5dff5b33a755 |
Hashes for jtd_to_proto-0.5.0-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a39e7f2bdebbd5df37953ee4d6878efb6e416b7a4c4b3a093b65c95861d0892 |
|
MD5 | 9dcc9d6d3a85c20a7eae4b0872dc2c39 |
|
BLAKE2b-256 | 71ebb4d3fa242d57be67b95dd0a13036a7d0d45950ddf691c70270d4d012a195 |