2

I have a HashMap as a value in a struct that I'm manually serializing:

pub struct GitInfo {
    pub branches: HashMap<Oid, Branch>,
}

Branch is something I've defined, but Oid is an external type I don't own, with a to_string() method I'd be happy to use...

I've read How do I use Serde to serialize a HashMap with structs as keys to JSON? but it refers to keys the author defined - I can't implement Serialize for Oid as it's not in my crate. And I can't implement Serialize for HashMap<Oid, Branch> for similar reasons.

Is there some way around this? I could build a wrapper struct around HashMap<Oid, Branch> but that seems like overkill.

It was suggested that I look at How to transform fields during serialization using Serde? or How can I implement Serialize using an existing Display trait implementation? - both seem to boil down to using serialize_with - which I could possibly do, but I'd have to use the derive(Serialize) macro, when I was planning to serialize GitInfo manually (see below). Or I could use a wrapper object.

If those are the only options, I could live with it, but it seems a bit surprising that there isn't a simpler way - is there some way to call a function like the serialize_with macro uses, but from within my GitInfo serialization?

Something like:

impl Serialize for GitInfo {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("GitInfo", 2)?;
        state.serialize_field("other bits", &self.other_bits)?;
        state.serialize_field("branches", /* something */)?;
        state.end()
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Korny
  • 1,988
  • 1
  • 18
  • 16
  • Why are you using `serialize_field` instead of `serialize_map`? – Shepmaster Oct 09 '19 at 19:11
  • I'm serializing a struct, `GitInfo`, with a field which is a `HashMap`. So I'm using `serialize_struct` which returns a `SerializeStruct` which has no `serialize_map` method. If there is some way to call `serialize_map` to make a new serialized version of the map, and then insert the results as a parameter to `serialize_field`, that'd be awesome - but I haven't found a way to do that yet. – Korny Oct 09 '19 at 20:42

0 Answers0