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.4.0-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d85b05351790869e3b536bbcb20964315cab3c4a9a8de1912a85e1f9c04b2da6 |
|
MD5 | a00332ae6ac8d835f548317e0c8406d3 |
|
BLAKE2b-256 | a9fcd712e1b2cb2af5b1bdee1d05c4eba3d52bdf06f4813f2aa3b1fdb59fdec5 |
Hashes for jtd_to_proto-0.4.0-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc40cda1d824c2ac604772dfae2af086d4ad7d7786b6fde6f8577907396c547f |
|
MD5 | f5a0a68720c6fa7bdbc28a13fed0408a |
|
BLAKE2b-256 | 01c3eda79cd62403dcc15618c54286c3b42583a1c0da8a1662cacbcfd472afd7 |
Hashes for jtd_to_proto-0.4.0-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c41af8e1d84ef90f165e56b27592427769e9a805a8015c2a7d673b97d543386f |
|
MD5 | 23dfa8de63cc2c1570596cbf2584dd51 |
|
BLAKE2b-256 | 7a20514fea5639bfb84c16a9810b52b58f2b20724a6bb95912ba8935729b087f |
Hashes for jtd_to_proto-0.4.0-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 347b1424c7efa6b08e291ca9cda28435172d5acda70be89578d60651e02a2746 |
|
MD5 | c799926821ccca1bf5ab3b398b87c9a3 |
|
BLAKE2b-256 | 8ad9c5fe1c1bc125e2d287b4d6d2a66f8e21124e6913e835fd5cb09a773a4a65 |