-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP and RFC: Initial support for Serde serialization #61
Comments
Wouldn't it be nicer to have |
Thanks for the input. That is probably a common use case in JSON style responses. The good news is that this would work with the current API once the For example: use rust_xlsxwriter::{Format, Workbook, XlsxError};
use serde::Serialize;
fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();
// Add a simple format for the headers.
let format = Format::new().set_bold();
// Create a serializable test struct.
#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
struct Produce {
fruit: &'static str,
cost: f64,
}
// Create some data instances.
let items = vec![
Produce {
fruit: "Peach",
cost: 1.05,
},
Produce {
fruit: "Plum",
cost: 0.15,
},
Produce {
fruit: "Pear",
cost: 0.75,
},
];
// Set up the start location and headers of the data to be serialized.
worksheet.serialize_headers_with_format(0, 0, &items.get(0).unwrap(), &format)?;
// Serialize the data.
worksheet.serialize(&items)?;
// Save the file.
workbook.save("serialize.xlsx")?;
Ok(())
} This would also work for a tuple of structs: // Create some data instances.
let items = (
Produce {
fruit: "Peach",
cost: 1.05,
},
Produce {
fruit: "Plum",
cost: 0.15,
},
Produce {
fruit: "Pear",
cost: 0.75,
},
);
// Set up the start location and headers of the data to be serialized.
worksheet.serialize_headers_with_format(0, 0, &items.1, &format)?;
// Serialize the data.
worksheet.serialize(&items)?; Both examples produces the same output as the first example above. Would that work for you? |
Because worksheet.serialize_headers::<Produce>(0, 0)?;
worksheet.serialize_headers_with_format::<Produce>(0, 0, &format)?; |
@Mingun That would be nice. However, I think an instance is required, and not just the type, since it needs to be serialized to find the fields to convert them to worksheet headers. Or am I missing something? For reference the code of the method is here: rust_xlsxwriter/src/serializer.rs Lines 404 to 425 in 1844dfe
|
I have added support for Serde serialization in v0.57.0. See Working with Serde in the Some additional serialisation features and helpers will be added in upcoming releases. |
I've added initial support for serialization of Serde structures to
rust_xlsxwriter
main under theserde
feature flag.The support works like this:
#[derive(Serialize)]
struct.worksheet.serialize()
repeatedly to write data, without having to specify the row/col position.For example:
Which would give this output:
The output can be positioned anywhere in the worksheet. For example if we change this line in the previous example:
We get this output:
It will also serialize vectors in the struct:
This gives the same output as the first example.
From the docs:
I am looking for feedback on the workability of this technique for any serialization use case that people may have. In particular I'd appreciate feedback from trying to make it work with existing serialize structures (within reason of what could be applied to a worksheet). Please leave comments below.
The code is on main. You will need to enable it in a local project with the following or similar:
There is some ongoing work to add support for ExcelDateTime and Chrono date/times. I will also be adding a method for adding formatting to each field value, and options to ignore fields apart from Serde
#[serde(skip_serializing)]
, and also to reorder the fields. I may also change the error handling to just ignore unknown structs/fields.The text was updated successfully, but these errors were encountered: