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.7.1-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a485ab03d7cba02baac798b0f5ed302b9d2f05c809d37d86f5b8a6a5fca085a |
|
MD5 | 93e516ca58c972f5980e1bf5f657c847 |
|
BLAKE2b-256 | 9313dee7e2c947248a0a632f19f0145e13a9b414df3841f05d839c4269d533bd |
Hashes for jtd_to_proto-0.7.1-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f4a852711b574a70d7ba644fcd4fda3651fb760a0520089e6ab61c578b2d24c |
|
MD5 | 15802b849846fb85bdb9ff5dd310042b |
|
BLAKE2b-256 | 4a3e2cc997a9636f148ce4804b4f8f6e2e2afdcd09651b3a80c77430d5116bef |
Hashes for jtd_to_proto-0.7.1-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ab01acb1ba694554d497674ee2cb5d50d2def238e36ff82fe3c8847eac97c5b |
|
MD5 | dc255a88052c8a81e0d19b19849191d3 |
|
BLAKE2b-256 | b1cc1b63cde1118432b303d2f0b0d2348f43d83769464c495c274568d2e2694c |
Hashes for jtd_to_proto-0.7.1-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f60eb502e5195e393cdb5269323a43ca3ac419af395d66030564e8475d4e9f1 |
|
MD5 | 6362c97d2899dff37b343440d69f07a1 |
|
BLAKE2b-256 | 5f90065aa31a6ca6d674099ced673a44943c747555c822b1a7a1c5f662410e84 |