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.1-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5ac93afda8a79e759eb2c5972420f760edf0f43a6f36aecc58206a98b306842 |
|
MD5 | f713bcd0a3085a2d19bc93bffbeaba93 |
|
BLAKE2b-256 | d969c8cb98c09da9592eb453fef896a64924ec0228381fbd4ca8a174d328e8c4 |
Hashes for jtd_to_proto-0.8.1-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31e441e36163d5aa5c57665b8c532832f08a4d856b76c15305c03ba988a4d6fc |
|
MD5 | 23423930e03843ebbefce0e2f9932221 |
|
BLAKE2b-256 | 7fb5936768bc71a1650bba0e4d188040d4408e249a137b17245409c87fa70fe6 |
Hashes for jtd_to_proto-0.8.1-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e65a1e05ea69f47e26860fb344112f0fae9a20bdd04545fb60721ca0c40353b |
|
MD5 | 7c5da43a5f36afc5be235a68c249c793 |
|
BLAKE2b-256 | b0243f4fd48fad49823012eb54e51c4e456ed74ef6315de759185c39ac0e81be |
Hashes for jtd_to_proto-0.8.1-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c60742fda333f1de9caa52ce9e9d719198f44f5a4906065030ffe721cbf60bb8 |
|
MD5 | d649c7ac406b07a1c3ec9130245807e8 |
|
BLAKE2b-256 | 4a6c4403c034285a21cf9f7cbc7777c67876beb90a265b0be5dc0af9ec4a7820 |