mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-25 07:25:47 +03:00
Fix deserializing a map in JSON. #392
This commit is contained in:
parent
dfd1d2af93
commit
af07de5d8e
@ -6,9 +6,12 @@ use std::fmt::Debug;
|
||||
use fast_paths::{NodeId, ShortestPath};
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
|
||||
/// A bidirectional mapping between fast_paths NodeId and some custom ID type.
|
||||
// TODO Upstream this in fast_paths when this is more solid.
|
||||
#[derive(Serialize)]
|
||||
pub struct NodeMap<T: Copy + Ord + Debug + Serialize> {
|
||||
// These two fields are redundant and large, so don't serialize the bigger one, to cut down
|
||||
// file size.
|
||||
#[serde(skip_serializing)]
|
||||
node_to_id: BTreeMap<T, NodeId>,
|
||||
id_to_node: Vec<T>,
|
||||
@ -48,7 +51,12 @@ impl<T: Copy + Ord + Debug + Serialize> NodeMap<T> {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Still can't figure out how to derive Deserialize on NodeMap directly.
|
||||
// A serialized NodeMap has this form in JSON. Use this to deserialize.
|
||||
#[derive(Deserialize)]
|
||||
struct InnerNodeMap<T: Copy + Ord + Debug> {
|
||||
id_to_node: Vec<T>,
|
||||
}
|
||||
|
||||
pub fn deserialize_nodemap<
|
||||
'de,
|
||||
D: Deserializer<'de>,
|
||||
@ -56,9 +64,8 @@ pub fn deserialize_nodemap<
|
||||
>(
|
||||
d: D,
|
||||
) -> Result<NodeMap<T>, D::Error> {
|
||||
// TODO I'm offline and can't look up hw to use Deserializer twice in sequence. Since the two
|
||||
// fields are redundant, just serialize one of them.
|
||||
let id_to_node = <Vec<T>>::deserialize(d)?;
|
||||
let inner = <InnerNodeMap<T>>::deserialize(d)?;
|
||||
let id_to_node = inner.id_to_node;
|
||||
let mut node_to_id = BTreeMap::new();
|
||||
for (id, node) in id_to_node.iter().enumerate() {
|
||||
node_to_id.insert(*node, id);
|
||||
|
Loading…
Reference in New Issue
Block a user