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"],
}
}
}
},
)
)
def write_foo_proto(filename: str):
"""Write out the .proto file for Foo to the given filename"""
with open(filename, "w") as handle:
handle.write(Foo.to_proto_file())
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.2.0-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0683c0d4ff64f115bf169a92a9a57c0d41524d81ddec29248a9c0df34651d1d |
|
MD5 | 2f852b89ed519137b70084fd5cee8c87 |
|
BLAKE2b-256 | 691273c9295c6fee29860066d818645cc0bf0c22def11641b5d6c267319b856a |
Hashes for jtd_to_proto-0.2.0-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 780f4a044b3ba39bb71d75b24a2e5527118d0e9fd4b8719575f578f7c6244c70 |
|
MD5 | 9ac96d12ef87e1d9a190f37e0fcb7835 |
|
BLAKE2b-256 | 5ba49923b68e9de160828557a2061bb2fc00c47838e8940a873c805ac23c917a |
Hashes for jtd_to_proto-0.2.0-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ffa1c849a959574eccf837ef7aafaffbf81d539bbca14c5939dc9ae6f6573df |
|
MD5 | 0fb252ba1f015472285342773b9ddf96 |
|
BLAKE2b-256 | 98e8556a79a9e753f75c3df4ad50b815b4cd84c23330977c9b444621476e5d49 |
Hashes for jtd_to_proto-0.2.0-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cf16d4cc7140171fc0d6eb44eba6f710cde0b36459e0e4abf92e3ac8c5149e4 |
|
MD5 | c5d84b33d668e04fdd5b4616997f642f |
|
BLAKE2b-256 | 0748add83f0bd8a361ee47423ac8cce77023a920cab1559221d43cd130f92da1 |