Handle invalid CSV data with serde

This commit is contained in:
Alisha 2017-07-11 11:34:10 +05:30 committed by Brian Anderson
parent a2dbd8dadc
commit ca13c3b10c
2 changed files with 59 additions and 0 deletions

View file

@ -10,6 +10,7 @@
| [Encode and decode base64][ex-base64] | [![base64-badge]][base64] | [![cat-encoding-badge]][cat-encoding] |
| [Serialize records to CSV][ex-serialize-csv] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
| [Serialize records to CSV using Serde][ex-csv-serde] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
| [Handle invalid CSV data with Serde][ex-invalid-csv] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
[ex-json-value]: #ex-json-value
<a name="ex-json-value"></a>
@ -433,6 +434,58 @@ fn run() -> Result<()> {
wtr.serialize(rec3)?;
wtr.flush()?;
Ok(())
}
#
# quick_main!(run);
```
[ex-invalid-csv]: #ex-invalid-csv
<a name="ex-invalid-csv"></a>
## Handle invalid CSV data with Serde
[![csv-badge]][csv] [![serde-badge]][serde] [![cat-encoding-badge]][cat-encoding]
CSV files often contain invalid data. For these cases, the csv crate
provides a custom deserializer, [`csv::invalid_option`], which automatically
converts invalid data to None values.
```rust
# #[macro_use]
# extern crate error_chain;
extern crate csv;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Deserialize)]
struct Record {
name: String,
place: String,
#[serde(deserialize_with = "csv::invalid_option")]
id: Option<u64>,
}
#
# error_chain! {
# foreign_links {
# CsvError(csv::Error);
# }
# }
fn run() -> Result<()> {
let data = "name,place,id
mark,sydney,46.5
ashley,zurich,92
akshat,delhi,37
alisha,colombo,xyz";
let mut rdr = csv::Reader::from_reader(data.as_bytes());
for result in rdr.deserialize() {
let record: Record = result?;
println!("{:?}", record);
}
Ok(())
}
#
@ -469,7 +522,11 @@ fn run() -> Result<()> {
[application/x-www-form-urlencoded]: https://url.spec.whatwg.org/#application/x-www-form-urlencoded
[`form_urlencoded::byte_serialize`]: https://docs.rs/url/1.4.0/url/form_urlencoded/fn.byte_serialize.html
[`form_urlencoded::parse`]: https://docs.rs/url/*/url/form_urlencoded/fn.parse.html
<<<<<<< HEAD
[`csv::Writer`]: https://docs.rs/csv/*/csv/struct.Writer.html
[`write_record`]: https://docs.rs/csv/*/csv/struct.Writer.html#method.write_record
[`serialize`]: https://docs.rs/csv/*/csv/struct.Writer.html#method.serialize
[`flush`]: https://docs.rs/csv/*/csv/struct.Writer.html#method.flush
=======
[`csv::invalid_option`]: https://docs.rs/csv/*/csv/fn.invalid_option.html
>>>>>>> Handle invalid CSV data with serde

View file

@ -45,6 +45,7 @@ community. It needs and welcomes help. For details see
| [Encode and decode base64][ex-base64] | [![base64-badge]][base64] | [![cat-encoding-badge]][cat-encoding] |
| [Serialize records to CSV][ex-serialize-csv] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
| [Serialize records to CSV using Serde][ex-csv-serde] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
| [Handle invalid CSV data with Serde][ex-invalid-csv] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
## [Concurrency](concurrency.html)
@ -250,6 +251,7 @@ Keep lines sorted.
[ex-log-syslog]: logging.html#ex-log-syslog
[ex-log-timestamp]: logging.html#ex-log-timestamp
[ex-log-stdout]: logging.html#ex-log-stdout
[ex-invalid-csv]: encoding.html#ex-invalid-csv
[ex-paginated-api]: net.html#ex-paginated-api
[ex-parse-subprocess-output]: basics.html#ex-parse-subprocess-output
[ex-percent-encode]: encoding.html#ex-percent-encode