'assertion failed: (left == right)
left: "{\"timestamp\":\"123\",\"sequence\":123,\"data\":\"123\"}"
,
right: "{\"timestamp\": 123,\"sequence\": 123,\"data\": 123}"
'
Created
July 8, 2022 04:41
-
-
Save de-sh/15a195d31dc419ce6ff6c94ea2411261 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{env, path::PathBuf}; | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let mut config = prost_build::Config::new(); | |
config.file_descriptor_set_path( | |
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set")) | |
.join("file_descriptor_set.bin"), | |
); | |
config.compile_protos(&["src/example.proto"], &["src/"])?; | |
Ok(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
message point { | |
uint64 timestamp = 1; | |
sint32 sequence = 2; | |
uint64 data = 3; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use prost_reflect::{prost::Message, DescriptorPool, DynamicMessage}; | |
use serde_json::Deserializer; | |
fn serde() { | |
let file_descriptor_set = | |
include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin")); | |
let context = DescriptorPool::decode(file_descriptor_set.as_ref()).unwrap(); | |
let mut payload = vec![]; | |
let message_descriptor = context.get_message_by_name("point").expect("UnconfiguredStream: can"); | |
let json = r#"{"timestamp": 123,"sequence": 123,"data": 123}"#; | |
let mut deserializer = Deserializer::from_str(json); | |
let dynamic_message = | |
DynamicMessage::deserialize(message_descriptor.clone(), &mut deserializer).unwrap(); | |
deserializer.end().unwrap(); | |
dynamic_message.encode(&mut payload).unwrap(); | |
let dynamic_message = | |
DynamicMessage::decode(message_descriptor, payload.as_slice()).unwrap(); | |
assert_eq!(serde_json::to_string(&dynamic_message).unwrap(), json); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment