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

Convert between prost_wkt_types::Value and serde_json::Value? #47

Open
AlJohri opened this issue Jul 25, 2023 · 2 comments
Open

Convert between prost_wkt_types::Value and serde_json::Value? #47

AlJohri opened this issue Jul 25, 2023 · 2 comments

Comments

@AlJohri
Copy link

AlJohri commented Jul 25, 2023

Is there any examples of how to convert between these two types? Or how to go from a rust struct to google.protobuf.Value? I thought with the serde support we would get this out of the box, but I am running into this error:

error[E0277]: the trait bound `prost_wkt_types::Value: From<serde_json::Value>` is not satisfied
   --> src/tonic.rs:108:55
    |
108 |         let aaaaaaaaaa = prost_wkt_types::Value::from(aaaaaaaaaa);
    |                          ---------------------------- ^^^^^^^^^^ the trait `From<serde_json::Value>` is not implemented for `prost_wkt_types::Value`
    |                          |
    |                          required by a bound introduced by this call
    |
    = help: the following other types implement trait `From<T>`:
              <prost_wkt_types::Value as From<HashMap<std::string::String, prost_wkt_types::Value>>>
              <prost_wkt_types::Value as From<Vec<prost_wkt_types::Value>>>
              <prost_wkt_types::Value as From<bool>>
              <prost_wkt_types::Value as From<f64>>
              <prost_wkt_types::Value as From<prost_wkt_types::NullValue>>
              <prost_wkt_types::Value as From<std::string::String>>

I documented how I was able to do this manually without prost-wkt in this StackOverflow answer, but it would be great to use the Serialize/Deserialize machinery here.

@AlJohri
Copy link
Author

AlJohri commented Jul 25, 2023

Ah, looks like I need to use serde_json::from_value as opposed to prost_wkt_types::Value::from like so:

let aaaaaaaaaa = MyRustStruct::default();
let aaaaaaaaaa = serde_json::to_value(aaaaaaaaaa)?
let aaaaaaaaaa: prost_wkt_types::Value = serde_json::from_value(aaaaaaaaaa)?

I think this issue can be closed although I'd be curious why we don't implement prost_wkt_types::Value::from for serde_json::Value?

@fdeantoni
Copy link
Owner

For my use case there was no reason yet to implement this but I suppose it can be added. I'm not sure yet if this should be a From or a TryFrom.

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

2 participants