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.4-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bcf80b61cb447a0de6b143ad2b76336d247c6fad0f14b952ccf89bf1878cc97 |
|
MD5 | 5ac115ff910773452e35965f4d7b28f0 |
|
BLAKE2b-256 | f90ba9906e151fd4bd2748c527c9cf1f14a01e00cdcc8c9da90b23b10b174206 |
Hashes for jtd_to_proto-0.11.4-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ded2d5a6a8d4cae04fe66a6752e6b67845d11588fa4690c3dcf881c01a91d3e |
|
MD5 | b88429381f13e9905a9ba1e06e554244 |
|
BLAKE2b-256 | f28027b0a764f682f84101f86780449ec717825c8b9492c947adf2a259b5670f |
Hashes for jtd_to_proto-0.11.4-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db3235c1c461a4d039fe7ce618c26eee2303d2a0c2bc3931d5fa64dddf8df343 |
|
MD5 | 625641a6710623bf48d55e54f75380b4 |
|
BLAKE2b-256 | 9c03863fde5e2347800cdc2257981b8ff5ea9da88146813a6f3b9eeff698842c |
Hashes for jtd_to_proto-0.11.4-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3680812f215ef93a231d91b6cf4fc44a4ccd21809bbbb860f65569ce8e0e3518 |
|
MD5 | 5633b768e79b872b0268ea7b20904b61 |
|
BLAKE2b-256 | 69babed32847a0e50553cb49a4af69ab29ed191317f7e1259fa710aeca546c9b |