rust-csv icon indicating copy to clipboard operation
rust-csv copied to clipboard

Question about deserializing with `serde::de::DeserializeSeed`

Open sqwishy opened this issue 3 years ago • 1 comments

What I'm trying to do is deserialize a StringRecord but ignore some columns. The columns aren't known at compile time.

It's easy to find their positions and grab the cells with StringRecord::get but this doesn't make use of the deserializer so I have to infer/guess the type myself. Which really isn't all that bad really, but it might be favourable to use the serde infrastructure.

In my scenario, the columns I'm interested in deserializing is only known at run time. I can't figure out how to get this to work in the current API this crate provides. Here's the two things I've tried.

  1. I could use DeserializeSeed to deserialize a row using a stateful object that knows what what cells to skip over and which to deserialize. However, this takes a Deserializer and this library doesn't appear to offer that in the API.

  2. Since I know what cells I need, I could also use a form of StringRecord::get that also takes a type parameter that implements Deserialize and deserializes the cell to that type. Sort of like StringRecord::deserialize but for just the cell instead of the entire row.

Is there any way to accomplish what I'm trying to do using the current API that I've missed?

sqwishy avatar Dec 29 '21 02:12 sqwishy

I don't believe there is any way to do what you want with the current API, and I think making it possible would be a fair bit of work and an addition of a decent bit of complexity.

BurntSushi avatar Dec 29 '21 03:12 BurntSushi