Created
June 27, 2017 21:45
-
-
Save saarw/0377dfc3558b05f6570f01d555bf8ede 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 erased_serde; | |
use serde_json; | |
#[derive(Serialize)] | |
struct Event { | |
data_point: usize | |
} | |
#[derive(Serialize)] | |
struct Batch { | |
field1: String, | |
field2: String, | |
events: serde_json::Value | |
} | |
fn serialize_batch(events: Vec<Box<erased_serde::Serialize>>) -> String { | |
let serialized = events.iter().map(|event| { | |
let out: Vec<u8> = Vec::new(); | |
event.erased_serialize(&mut serde_json::ser::Serializer::new(out)).unwrap(); | |
json!(out) | |
}).collect(); | |
serde_json::to_string(&Batch{field1: "a".to_string(), field2: "b".to_string(), events: serialized}).unwrap() | |
} | |
#[test] | |
fn test_serialize() { | |
assert!(serialize_batch(vec!(Box::new(Event{data_point: 13}))).len() > 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment