-
Notifications
You must be signed in to change notification settings - Fork 575
Description
Hi!
I'm trying to support serde
-based serialization on some Protobuf types I have generated using prost
. I do this conditionally by putting the following in the build script for my crate.
fn main() {
let protos =
prost_build::Config::new()
.type_attribute(".", "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]");
.compile_protos(&[/* list of files */], &[/* list of includes */])
.unwrap();
}
This works great until I use any of the well-known types. The problem, of course, is that the well-known types have already been generated and exist in prost-types
. The workaround is to use compile_well_known_types()
, like so.
syntax = "proto3";
import "google/protobuf/timestamp.proto";
message LogWithTime {
string log = 1;
google.protobuf.Timestamp timestamp = 2;
}
fn main() {
let protos =
prost_build::Config::new()
.type_attribute(".", "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]");
.compile_well_known_types() // <---- added this line!
.compile_protos(&[/* list of files */], &[/* list of includes */])
.unwrap();
}
The problem now, however, is that the well-known types I generated are no longer compatible with any other crate's well-known types. So, it becomes difficult to do transformations between the types generated across crates.
My workaround is to generate the well-known types with the appropriate serde
attributes in a new shared crate that can be used by all the other crates in my workspace. But, I was wondering: would it be possible to add a feature flag to prost-types
that would allow one to turn on support for serde
? Or perhaps the feature flag is better situated in prost-build
? I'm not sure exactly where the best place to put it is, but generally having support for serde
would be nice.
Activity
mgoldenberg commentedon Apr 25, 2023
By the way, I did see there was some desire for this at some point, but figured it might be worth bringing up in a separate issue in case things have changed.
clotodex commentedon Dec 23, 2024
Any update on this? I cannot compile it to use Struct.
caspermeijn commentedon Feb 3, 2025
Recently, another optional dependency was added, so I feel like an optional dependency for
serde
makes sense. You are welcome to open an PR.prost/prost-types/Cargo.toml
Line 22 in e617832
Add serde feature flag to prost-types
Add serde feature flag to prost-types
Add serde feature flag to prost-types
Add serde feature flag to prost-types
Add serde feature flag to prost-types
Add serde feature flag to prost-types
NishantJoshi00 commentedon Jun 18, 2025
@caspermeijn any update on the PR that was raised by @allada? I would really like to have this feature, having JSON support for these types is a huge help.
snowfoxsh commentedon Jun 25, 2025
bump