我正在接收传感器数据流,我需要汇总并执行基本统计数据(平均值,最大值,最小值等)。有多个值但传感器数据可能不一致,并且某些值可能会丢失。
从阅读本书开始,当Option
或luminosity
的值丢失时,似乎应该使用color
,但我对此感到困惑。
这是我传感器数据的一个例子:
[
{
"sensor": "left",
"luminosity": "50",
"color": "(255,0,0)"
},
{
"sensor": "left",
"color": "#0f0"
},
{
"sensor": "right",
"luminosity": "20"
},
{
"sensor": "right",
"luminosity": "40",
"color": "(255,0,0)"
},
{
"sensor": "left",
"luminosity": "30"
},
{
"sensor": "top",
"luminosity": "10"
},
{
"sensor": "right",
"color": "(0,0,0)"
}
]
每个传感器的数据将存储在以下结构的实例中:
struct Data {
pub luminosity: Vec<String>,
pub color: Vec<String>,
}
我想迭代上面的JSON对象,将传感器与正确的struct实例匹配(“right”传感器与“right”传感器结构)并将每个JSON观察的内容推送到向量上(在每个struct实例中) )。
需要记录缺失值,以便对于每个“观察”,对于相应传感器的结构实例,结构中的每个向量都有一个推送动作。
答案 0 :(得分:3)
这样的事情应该有效。它使用Serde将每个JSON数组元素读入一个helper结构,其中包含传感器名称String
和传感器每个值的Option<String>
数据。然后它循环遍历那些读数并将它们插入到一个映射中,其中键是传感器名称,值是每个传感器值的数据向量。
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
use std::collections::BTreeMap as Map;
use std::error::Error;
#[derive(Debug, Default)]
struct Data {
luminosity: Vec<Option<String>>,
color: Vec<Option<String>>,
}
fn main() {
let input = r##"[
{
"sensor": "left",
"luminosity": "50",
"color": "(255,0,0)"
},
{
"sensor": "left",
"color": "#0f0"
},
{
"sensor": "right",
"luminosity": "20"
},
{
"sensor": "right",
"luminosity": "40",
"color": "(255,0,0)"
},
{
"sensor": "left",
"luminosity": "30"
},
{
"sensor": "top",
"luminosity": "10"
},
{
"sensor": "right",
"color": "(0,0,0)"
}
]"##;
let m = read_sensor_data(input).unwrap();
println!("{:#?}", m);
}
fn read_sensor_data(input: &str) -> Result<Map<String, Data>, Box<Error>> {
// Private helper struct that matches the format of the raw JSON
#[derive(Deserialize)]
struct RawReading {
sensor: String,
luminosity: Option<String>,
color: Option<String>,
}
// Deserialize the raw data
let raw_readings: Vec<RawReading> = serde_json::from_str(input)?;
// Loop over raw data and insert each reading into the right sensor's struct
let mut m = Map::new();
for raw in raw_readings {
// Look up this sensor's Data struct
let sensor = m.entry(raw.sensor).or_insert_with(Data::default);
// One push for every vector in the struct, even for missing observations
sensor.luminosity.push(raw.luminosity);
sensor.color.push(raw.color);
}
Ok(m)
}
答案 1 :(得分:1)
您可以稍微更高效,代价是更多代码。如果您创建自己的Vec
实现,则无需反序列化Visitor
:
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use std::collections::HashMap;
use std::fmt;
use serde::de::{Deserialize, Deserializer, Visitor};
#[derive(Debug, Default)]
struct Data {
luminosity: Vec<Option<String>>,
color: Vec<Option<String>>,
}
struct Wrapper(HashMap<String, Data>);
impl<'de> Deserialize<'de> for Wrapper {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(WrapperVisitor)
}
}
struct WrapperVisitor;
impl<'de> Visitor<'de> for WrapperVisitor {
type Value = Wrapper;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a sequence of measurement objects")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
#[derive(Debug, Deserialize)]
struct DataPoint {
sensor: String,
luminosity: Option<String>,
color: Option<String>,
}
let mut all_data = HashMap::new();
while let Some(data_point) = seq.next_element::<DataPoint>()? {
let data = all_data
.entry(data_point.sensor)
.or_insert_with(Data::default);
data.luminosity.push(data_point.luminosity);
data.color.push(data_point.color);
}
Ok(Wrapper(all_data))
}
}
fn main() {
let input = r###"
[
{
"sensor": "left",
"luminosity": "50",
"color": "(255,0,0)"
},
{
"sensor": "left",
"color": "#0f0"
},
{
"sensor": "right",
"luminosity": "20"
},
{
"sensor": "right",
"luminosity": "40",
"color": "(255,0,0)"
},
{
"sensor": "left",
"luminosity": "30"
},
{
"sensor": "top",
"luminosity": "10"
},
{
"sensor": "right",
"color": "(0,0,0)"
}
]
"###;
let data = serde_json::from_str::<Wrapper>(input).expect("Nope");
let data = data.0;
println!("{:#?}", data);
}
这会产生输出:
{
"left": Data {
luminosity: [
Some("50"),
None,
Some("30")
],
color: [
Some("(255,0,0)"),
Some("#0f0"),
None
]
},
"right": Data {
luminosity: [
Some("20"),
Some("40"),
None
],
color: [
None,
Some("(255,0,0)"),
Some("(0,0,0)")
]
},
"top": Data {
luminosity: [
Some("10")
],
color: [
None
]
}
}