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.3.1-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f70d95ab2aa92d43d4a9f9315f8bc237335d52e3a686ca109c05b36c2a3a445 |
|
MD5 | 782e01ff5947d1ba09f5245a823da6d0 |
|
BLAKE2b-256 | 4c0143e39241f07502c99aea071dd2df2912242edd960b8f4347137c5b444c6c |
Hashes for jtd_to_proto-0.3.1-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cbefb37047e31579a053b8b4ad82ca8fbbe734edad82aa0baa5f4a93941b6c5 |
|
MD5 | b579254a488408f588e124c07d661ba8 |
|
BLAKE2b-256 | f15d9f3ebb9e1bbf0a5fc22f8afeb89943c76ba3902eb1a83507438fbb1773df |
Hashes for jtd_to_proto-0.3.1-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e65a63db014dd2e4be798fd2b820bd9982a56e68c174340ad7e4ffe915d681b |
|
MD5 | 4b878b283b9580e4265465bdbb24a7a0 |
|
BLAKE2b-256 | d6c1834ae157447959e85be877ead8b5b64c52175bec10c752cec33fd4884177 |
Hashes for jtd_to_proto-0.3.1-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e58f9338efb15d6a1bdb93e064041b79dea988ce22e32a3f4ba4b2df97ece0e2 |
|
MD5 | 2fbf0af9233d2732dca6e8f4ed9c76d1 |
|
BLAKE2b-256 | 90e8bd8f9b6271a2c60d5762ec72e380734e863584c971cc25a803fb031467d6 |