Serde and format drivers, what's possible?

I've read some docs on serde.rs, watched the yesterday's video that's on its front page now, and read some crates sources to get just some understanding how it works on the side of formats (not structs).

And it still not clear. The example in the docs shows calls to from_str, from_bytes, etc.

So, question #1: is serde made to work only with streams of bytes or text? (I know it's not a parsing library.)

But what if I have a format driver, like flatgeobuf (or GeoPackage), which gives me an iterator and fields accessors. Right now I process this with custom code, rewriting it each time for a struct and file:

struct MyStruct { field1: i32, field2: f64, geometry: Point }
for row in my_fgb_reader.iter()? {
    let feature = row?;
    let field1 = feature.read_field::<i32>("field1")?;
    let field2 = feature.read_field::<f64>("field2")?;
    let geometry = feature.read_geometry::<Point>()?;
    do_something_with(MyStruct { field1, field2, geometry })?;
}

I'd love to have a tool to do this kind of thing:

#[derive(deserializer trait from some crate)]
struct MyStruct { field1: i32, field2: f64, geometry: Point }
for item in MyStruct::iter_from(my_fgb_reader) {
    let obj:MyStruct = item?; 
    do_something_with(obj)?;
}

Question #2: Is this kind of thing doable with Serde, or any other crate?

Keeping in mind that when data is being transferred or stored, it is necessarily in a binary format¹ of some kind:

What other than either binary or text streams could possibly make sense for a de/serialization library?

¹Note that this is true even for text streams, as they themselves are composed of binary data; they're just viewed through a encoding lens of some kind (generally UTF-8 for Rust strings) rather than viewed as raw bytes.

What might be possible is serializing your data such that it can be deserialized by another struct family, analogous to how you can serialize a value of some struct type, then deserialize to a HashMap.
But what that would look like in practice is fairly tied to your data structures.

2 Likes

No, but.

Someone would have to write adapter. I have no idea if one exist for GeoPackage.

1 Like

This gives a better understanding, thanks!