Skip to content

Commit

Permalink
Merge pull request #251 from NREL/rjf/serialize_state_model_with_index
Browse files Browse the repository at this point in the history
serialize state model with index
  • Loading branch information
robfitzgerald authored Sep 3, 2024
2 parents 22f24d7 + fa58d62 commit 016ac85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [

[workspace.dependencies]
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
rayon = "1.7.0"
geo = { version = "0.28.0", features = ["use-serde"] }
geo-types = "0.7.12"
Expand Down
14 changes: 13 additions & 1 deletion rust/routee-compass-core/src/model/state/state_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,20 @@ impl StateModel {
}

/// uses the built-in serialization codec to output the state model representation as a JSON object
/// stores the result as a JSON Object (Map).
pub fn serialize_state_model(&self) -> serde_json::Value {
json![self.iter().collect::<HashMap<_, _>>()]
let mut out = serde_json::Map::new();
for (i, (name, feature)) in self.indexed_iter() {
let mut f_json = json![feature];

if let Some(map) = f_json.as_object_mut() {
map.insert(String::from("index"), json![i]);
map.insert(String::from("name"), json![name]);
}
out.insert(name.clone(), f_json);
}

json![out]
}

/// lists the names of the state variables in order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ mod test {
let plugin = GridSearchPlugin {};
plugin.process(&mut input).unwrap();
let expected = vec![
json![{"abc":123,"model_name":"2016_TOYOTA_Camry_4cyl_2WD","name":"d1","weights":{"distance":1,"energy_electric":0,"time":0}}],
json![{"abc":123,"model_name":"2016_TOYOTA_Camry_4cyl_2WD","name":"t1","weights":{"distance":0,"energy_electric":0,"time":1}}],
json![{"abc":123,"model_name":"2016_TOYOTA_Camry_4cyl_2WD","name":"e1","weights":{"distance":0,"energy_electric":1,"time":0}}],
json![{"abc":123,"model_name":"2017_CHEVROLET_Bolt","name":"d1","weights":{"distance":1,"energy_electric":0,"time":0}}],
json![{"abc":123,"model_name":"2017_CHEVROLET_Bolt","name":"t1","weights":{"distance":0,"energy_electric":0,"time":1}}],
json![{"abc":123,"model_name":"2017_CHEVROLET_Bolt","name":"e1","weights":{"distance":0,"energy_electric":1,"time":0}}],
json![{"abc":123,"model_name":"2016_TOYOTA_Camry_4cyl_2WD","name":"d1","weights":{"distance":1,"time":0,"energy_electric":0}}],
json![{"abc":123,"model_name":"2017_CHEVROLET_Bolt","name":"d1","weights":{"distance":1,"time":0,"energy_electric":0}}],
json![{"abc":123,"model_name":"2016_TOYOTA_Camry_4cyl_2WD","name":"t1","weights":{"distance":0,"time":1,"energy_electric":0}}],
json![{"abc":123,"model_name":"2017_CHEVROLET_Bolt","name":"t1","weights":{"distance":0,"time":1,"energy_electric":0}}],
json![{"abc":123,"model_name":"2016_TOYOTA_Camry_4cyl_2WD","name":"e1","weights":{"distance":0,"time":0,"energy_electric":1}}],
json![{"abc":123,"model_name":"2017_CHEVROLET_Bolt","name":"e1","weights":{"distance":0,"time":0,"energy_electric":1}}],
];
match input {
serde_json::Value::Array(result) => assert_eq!(result, expected),
Expand Down

0 comments on commit 016ac85

Please sign in to comment.