-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serde: update serialization documentation and examples
Feature request: #63
- Loading branch information
Showing
10 changed files
with
217 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,13 @@ | |
// Copyright 2022-2023, John McNamara, [email protected] | ||
|
||
//! The following example demonstrates serializing instances of a Serde derived | ||
//! data structure to a worksheet using different methods. | ||
//! data structure to a worksheet using different methods (both serialization | ||
//! and deserialization). | ||
use rust_xlsxwriter::{ | ||
CustomSerializeHeader, Format, FormatBorder, SerializeHeadersOptions, Workbook, XlsxError, | ||
}; | ||
use serde::Serialize; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
fn main() -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
|
@@ -30,7 +31,7 @@ fn main() -> Result<(), XlsxError> { | |
let currency_format = Format::new().set_num_format("$0.00"); | ||
|
||
// Create a serializable test struct. | ||
#[derive(Serialize)] | ||
#[derive(Deserialize, Serialize)] | ||
struct Produce { | ||
fruit: &'static str, | ||
cost: f64, | ||
|
@@ -52,9 +53,9 @@ fn main() -> Result<(), XlsxError> { | |
cost: 0.75, | ||
}; | ||
|
||
// 1. Set the serialization location and headers with `serialize_headers()` | ||
// 1. Set the serialization location and headers with `deserialize_headers()` | ||
// and serialize some data. | ||
worksheet.serialize_headers(0, 0, &item1)?; | ||
worksheet.deserialize_headers::<Produce>(0, 0)?; | ||
|
||
worksheet.serialize(&item1)?; | ||
worksheet.serialize(&item2)?; | ||
|
@@ -82,17 +83,15 @@ fn main() -> Result<(), XlsxError> { | |
.set_custom_headers(&custom_headers); | ||
|
||
// Set the serialization location and custom headers. | ||
worksheet.serialize_headers_with_options(0, 6, &item1, &header_options)?; | ||
worksheet.deserialize_headers_with_options::<Produce>(0, 6, &header_options)?; | ||
|
||
worksheet.serialize(&item1)?; | ||
worksheet.serialize(&item2)?; | ||
worksheet.serialize(&item3)?; | ||
|
||
// 4. Set the serialization location and headers with custom headers. We use | ||
// 4. Set the serialization location and headers with custom options. We use | ||
// the customization to turn off the headers. | ||
let header_options = SerializeHeadersOptions::new() | ||
.hide_headers(true) | ||
.set_custom_headers(&custom_headers); | ||
let header_options = SerializeHeadersOptions::new().hide_headers(true); | ||
|
||
// Set the serialization location and custom headers. | ||
worksheet.serialize_headers_with_options(0, 9, &item1, &header_options)?; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,10 @@ | |
// Copyright 2022-2023, John McNamara, [email protected] | ||
|
||
//! The following example demonstrates renaming fields during serialization by | ||
//! specifying custom headers and renaming them there. You must still specify | ||
//! the actual field name to serialize in the `new()` constructor. | ||
//! specifying custom headers and renaming them there. | ||
//! | ||
use rust_xlsxwriter::{CustomSerializeHeader, SerializeHeadersOptions, Workbook, XlsxError}; | ||
use serde::Serialize; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
fn main() -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
|
@@ -16,7 +15,7 @@ fn main() -> Result<(), XlsxError> { | |
let worksheet = workbook.add_worksheet(); | ||
|
||
// Create a serializable test struct. | ||
#[derive(Serialize)] | ||
#[derive(Deserialize, Serialize)] | ||
struct Produce { | ||
fruit: &'static str, | ||
cost: f64, | ||
|
@@ -46,7 +45,7 @@ fn main() -> Result<(), XlsxError> { | |
let header_options = SerializeHeadersOptions::new().set_custom_headers(&custom_headers); | ||
|
||
// Set the serialization location and custom headers. | ||
worksheet.serialize_headers_with_options(0, 0, &item1, &header_options)?; | ||
worksheet.deserialize_headers_with_options::<Produce>(0, 0, &header_options)?; | ||
|
||
// Serialize the data. | ||
worksheet.serialize(&item1)?; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// | ||
// Copyright 2022-2023, John McNamara, [email protected] | ||
|
||
//! The following example demonstrates serializing instances of a Serde derived | ||
//! data structure to a worksheet. This version uses header deserialization. | ||
use rust_xlsxwriter::{Format, FormatBorder, Workbook, XlsxError}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
fn main() -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
|
||
// Add a worksheet to the workbook. | ||
let worksheet = workbook.add_worksheet(); | ||
|
||
// Add some formats to use with the serialization data. | ||
let header_format = Format::new() | ||
.set_bold() | ||
.set_border(FormatBorder::Thin) | ||
.set_background_color("C6E0B4"); | ||
|
||
// Create a serializable test struct. | ||
#[derive(Deserialize, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct Student<'a> { | ||
name: &'a str, | ||
age: u8, | ||
id: u32, | ||
} | ||
|
||
let students = [ | ||
Student { | ||
name: "Aoife", | ||
age: 25, | ||
id: 564351, | ||
}, | ||
Student { | ||
name: "Caoimhe", | ||
age: 21, | ||
id: 443287, | ||
}, | ||
]; | ||
|
||
// Set up the start location and headers of the data to be serialized. | ||
worksheet.deserialize_headers_with_format::<Student>(1, 3, &header_format)?; | ||
|
||
// Serialize the data. | ||
worksheet.serialize(&students)?; | ||
|
||
// Save the file. | ||
workbook.save("serialize.xlsx")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.