From af07de5d8e3e8b9cf4be5ad7143286840a8a619c Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 19 Jan 2021 11:27:56 -0800 Subject: [PATCH] Fix deserializing a map in JSON. #392 --- map_model/src/pathfind/node_map.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/map_model/src/pathfind/node_map.rs b/map_model/src/pathfind/node_map.rs index 878cbe59f7..18e8cc3557 100644 --- a/map_model/src/pathfind/node_map.rs +++ b/map_model/src/pathfind/node_map.rs @@ -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 { + // 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, id_to_node: Vec, @@ -48,7 +51,12 @@ impl NodeMap { } } -// 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 { + id_to_node: Vec, +} + pub fn deserialize_nodemap< 'de, D: Deserializer<'de>, @@ -56,9 +64,8 @@ pub fn deserialize_nodemap< >( d: D, ) -> Result, 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 = >::deserialize(d)?; + let inner = >::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);