prost
prost copied to clipboard
can prost serialize/deserialize text format and json?
I think I would like to ask the same question... Would be nice if someone can help give some hints? :)
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.
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
.
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.
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 :)