prost icon indicating copy to clipboard operation
prost copied to clipboard

can prost serialize/deserialize text format and json?

Open binbowang1987 opened this issue 3 years ago • 5 comments

binbowang1987 avatar Sep 30 '21 09:09 binbowang1987

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

liufuyang avatar Oct 11 '21 20:10 liufuyang

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.

liufuyang avatar Oct 11 '21 20:10 liufuyang

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.

andrewhickman avatar Jan 18 '22 01:01 andrewhickman

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.

WestXu avatar May 26 '22 10:05 WestXu

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 :)

liufuyang avatar May 26 '22 10:05 liufuyang