Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can prost serialize/deserialize text format and json? #541

Closed
binbowang1987 opened this issue Sep 30, 2021 · 6 comments
Closed

can prost serialize/deserialize text format and json? #541

binbowang1987 opened this issue Sep 30, 2021 · 6 comments

Comments

@binbowang1987
Copy link

No description provided.

@liufuyang
Copy link

I think I would like to ask the same question... Would be nice if someone can help give some hints? :)

@liufuyang
Copy link

As I was told by someone:

the typical answer is using serde. an example found on github code search: https://github.com/rucron/proto/blob/c951c4ceb2e4036d2af6b6267a72aabf45d39799/build.rs#L10. that doesn't necessarily match the proto spec for json mapping though, and this crate is attempting to solve that: https://github.com/influxdata/pbjson (i'm not sure if it's production ready though)

So perhaps pbjson is the solution for now? But it looks super new, and a few features waiting to be implemented in the issue board.

@andrewhickman
Copy link
Contributor

andrewhickman commented Jan 18, 2022

I've implemented JSON serialization and deserialization in prost_reflect. It passes all the protobuf conformance tests so I'd consider it "complete", although it would be great to get some real-world testing as well.

Unlike pbjson however, it doesn't support serializing existing Message structs directly; instead it requires converting to/from DynamicMessage.

@WestXu
Copy link

WestXu commented May 26, 2022

So I've found this, if anyone doesn't know already:

// build.rs

use std::io::Result;
fn main() -> Result<()> {
    let mut prost_build = prost_build::Config::new();
    prost_build
        .type_attribute(".", "#[derive(serde::Serialize,serde::Deserialize)]")
        .compile_protos(&["src/jzs.proto", "src/md.proto"], &["src/"])?;

    Ok(())
}

Then the generated code will derive serde, with json ser/de capability.

@liufuyang
Copy link

Indeed, those type_attribute and field_attribute can be helpful, but for some reason, it is not working for all the protobuf types, such as Any.

I found this thing - https://github.com/fdeantoni/prost-wkt and it seems could help with those issues. I wish prost could have some sort of good support directly to allow the generated Rust code to work with json :)

@caspermeijn
Copy link
Collaborator

The answer to the question is: no, currently prost can't convert to/from JSON. But it is something that has been requested a lot. See #75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants