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.2-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a17f1c6686e5c558a6026904949d3cf02be9e4b49aef9dac9a197cd695d04b7 |
|
MD5 | 140d130af0fecb5239fbcda0773b9ec3 |
|
BLAKE2b-256 | aa49472152e921686575769d97f94fba81db100b73b99a84ed900239c21ba124 |
Hashes for jtd_to_proto-0.11.2-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdc331d3a21292b9bf0a789807c08c830ee45c97d5997b48aa783a15c0db4276 |
|
MD5 | 24c486a846df789e8f276e1d4f162faa |
|
BLAKE2b-256 | 50452fa5961e381d24c515f3c70c7f8016f82c2ebec37b0fd0add69adfee1219 |
Hashes for jtd_to_proto-0.11.2-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8aa4c46ea50b7dff9d2a3ee8cd0c8ad57324741f16234799a2a50b7ee6841ece |
|
MD5 | 4ff8f55f36453969c3da98c5a858df6a |
|
BLAKE2b-256 | f32437ed56ddfe6570d32265ed572a3dfd6c41dcc3d03da49eb01dda98bb5ecc |
Hashes for jtd_to_proto-0.11.2-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d14334179447858bf035f53ee210901a0294eea2cd2c869781dbb2bcf2dfe03 |
|
MD5 | d1a972f48ec3b79d2d3235a30ef93df2 |
|
BLAKE2b-256 | 42d46a8bfa0dc872550d04ffaa436b41034dc007af7a9ac7ebc128b516874ab6 |