Serde, document converter?

I wonder if you have a complex document that you serialize using serde, how do you deal with document converter when you change the file format?

What exactly do you mean by "changing file format" and "dealing with document converter"?

Let's say that in v1 you have a given data structure, in v2 you to negate some boolean values, modify some path, scale some values, merge two attributes into one new bigger attribute, etc... or even remove attributes.

In simple cases you can use Option and #[serde(default)], #[serde(skip_if="fn")] to make one struct deserialize from multiple versions of a document.

In more complex cases you will need DocumentV1 and DocumentV2 structs separately and move stuff from one struct to another. You will also need to signal the version somehow. You could put it somewhere outside serialized data, or make the whole document a tagged enum of versioned documents, or try deserializing multiple versions until it parses.

For very messy cases, most serde deserializers offer a generic Value enum that can represent any type in the document, and then you can try to extract data out of it the hard way (e.g. if value.is_array() {…}).


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.