Skip to content

serde support for prost-types #852

@mgoldenberg

Description

@mgoldenberg

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

mgoldenberg commented on Apr 25, 2023

@mgoldenberg
Author

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.

I'm very keen to add support for the protobuf JSON encoding to prost by having an option to output the right serde annotations, however I haven't found time to actually work on this. Once that's done, they could be enabled for prost-types through a cargo feature. I can't give a timeline on when this might land, though.

clotodex

clotodex commented on Dec 23, 2024

@clotodex

Any update on this? I cannot compile it to use Struct.

caspermeijn

caspermeijn commented on Feb 3, 2025

@caspermeijn
Contributor

Recently, another optional dependency was added, so I feel like an optional dependency for serde makes sense. You are welcome to open an PR.

arbitrary = { version = "1.4", features = ["derive"], optional = true }

added a commit that references this issue on Feb 27, 2025
d321884
linked a pull request that will close this issue on Feb 27, 2025
added 4 commits that reference this issue on Feb 27, 2025
8a999f2
9cfc78e
391a5e6
67f74b1
added a commit that references this issue on Mar 17, 2025
ea27b80
NishantJoshi00

NishantJoshi00 commented on Jun 18, 2025

@NishantJoshi00

@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

snowfoxsh commented on Jun 25, 2025

@snowfoxsh

@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.

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @caspermeijn@clotodex@mgoldenberg@NishantJoshi00@snowfoxsh

      Issue actions

        `serde` support for `prost-types` · Issue #852 · tokio-rs/prost