Skip to content

Commit ad30fcf

Browse files
code-inflationrobatscandit
authored andcommitted
preserve order when constructing json (#248)
1 parent 4cc3da7 commit ad30fcf

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ reqwest = { version = "0.12", default-features = false, features = ["blocking",
1818
clap = { version = "4.5.43", features = ["derive"] }
1919
serde = { version = "1.0.219", features = ["derive"] }
2020
csv = "1.3.0"
21-
serde_json = "1.0"
21+
serde_json = { version = "1.0", features = ["preserve_order"] }
2222
indexmap = "2.9.0"
2323
clap_complete = "4.5"

src/measurements.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,22 @@ fn compose_output_json(
101101
metadata: Option<&Metadata>,
102102
) -> serde_json::Map<String, serde_json::Value> {
103103
let mut output = serde_json::Map::new();
104-
output.insert(
105-
"speed_measurements".to_string(),
106-
serde_json::to_value(stat_measurements).unwrap(),
107-
);
108-
if let Some(latency) = latency_measurement {
109-
output.insert(
110-
"latency_measurement".to_string(),
111-
serde_json::to_value(latency).unwrap(),
112-
);
113-
}
114104
if let Some(metadata) = metadata {
115105
output.insert(
116106
"metadata".to_string(),
117107
serde_json::to_value(metadata).unwrap(),
118108
);
119109
}
110+
if let Some(latency) = latency_measurement {
111+
output.insert(
112+
"latency_measurement".to_string(),
113+
serde_json::to_value(latency).unwrap(),
114+
);
115+
}
116+
output.insert(
117+
"speed_measurements".to_string(),
118+
serde_json::to_value(stat_measurements).unwrap(),
119+
);
120120
output
121121
}
122122

@@ -379,5 +379,11 @@ mod tests {
379379

380380
assert!(output.get("latency_measurement").is_some());
381381
assert!(output.get("speed_measurements").is_some());
382+
383+
let keys: Vec<&str> = output.keys().map(String::as_str).collect();
384+
assert_eq!(
385+
keys,
386+
vec!["metadata", "latency_measurement", "speed_measurements"]
387+
);
382388
}
383389
}

0 commit comments

Comments
 (0)