pub struct Serializer { /* private fields */ }Expand description
Serializes records into delimited text.
Every value is a record, the names of the columns are written before the first one.
use deser_csv::Serializer;
#[derive(deser::Serialize)]
struct Row {
name: &'static str,
age: u32,
}
let mut serializer = Serializer::new();
serializer.serialize(&Row { name: "jane", age: 42 }).unwrap();
serializer.serialize(&Row { name: "john", age: 23 }).unwrap();
assert_eq!(serializer.finish(), "name,age\njane,42\njohn,23\n");The serializer is also the stream serializer of delimited text (see
StreamSerializer): the output can be taken
while records are written. To write to a Write use
SerializerConfig::writer. A serializer created with
document writes the records of sequences instead,
like SerializerConfig::to_string.
Implementations§
Source§impl Serializer
impl Serializer
Sourcepub fn new() -> Serializer
pub fn new() -> Serializer
Creates a serializer.
Sourcepub fn with_config(config: SerializerConfig) -> Serializer
pub fn with_config(config: SerializerConfig) -> Serializer
Creates a serializer with the given configuration.
Sourcepub fn with_headers<I, S>(config: SerializerConfig, names: I) -> Serializer
pub fn with_headers<I, S>(config: SerializerConfig, names: I) -> Serializer
Creates a serializer for a stream that continues with the given names of the columns.
The names are not written, for instance because the records are appended to an existing file.
use std::collections::BTreeMap;
use deser_csv::{Serializer, SerializerConfig};
let mut serializer =
Serializer::with_headers(SerializerConfig::new(), ["b", "a"]);
serializer.serialize(&BTreeMap::from([("a", 1), ("b", 2)])).unwrap();
assert_eq!(serializer.finish(), "2,1\n");Sourcepub fn document(config: SerializerConfig) -> Serializer
pub fn document(config: SerializerConfig) -> Serializer
Creates a serializer whose values are sequences of records.
Every value is written like with SerializerConfig::to_string:
the elements of the sequence are the records. The records are
written while they are serialized, so large documents can be
written in parts (see
StreamSerializer::drive_partial).
use deser_csv::{Serializer, SerializerConfig};
let mut serializer = Serializer::document(SerializerConfig::new());
serializer.serialize(&vec![(1, "a"), (2, "b")]).unwrap();
assert_eq!(serializer.finish(), "1,a\n2,b\n");Sourcepub fn config(&self) -> &SerializerConfig
pub fn config(&self) -> &SerializerConfig
Returns the configuration.
Sourcepub fn headers(&self) -> Option<&[String]>
pub fn headers(&self) -> Option<&[String]>
Returns the names of the columns.
This is None until the first record was written (unless the names
were given, see with_headers).
Sourcepub fn serialize<T: Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Error>
pub fn serialize<T: Serialize + ?Sized>( &mut self, value: &T, ) -> Result<(), Error>
Serializes a record (or the records of a sequence, see
document).
If the record fails to serialize, nothing is written.
Trait Implementations§
Source§impl Clone for Serializer
impl Clone for Serializer
Source§impl Debug for Serializer
impl Debug for Serializer
Source§impl Default for Serializer
impl Default for Serializer
Source§fn default() -> Serializer
fn default() -> Serializer
Source§impl Serializer for Serializer
impl Serializer for Serializer
Source§fn drive(&mut self, driver: &mut SerializeDriver<'_>) -> Result<(), Error>
fn drive(&mut self, driver: &mut SerializeDriver<'_>) -> Result<(), Error>
Source§fn serialize_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
fn serialize_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
Source§fn serialize_ref(&mut self, value: SerializeRef<'_>) -> Result<(), Error>
fn serialize_ref(&mut self, value: SerializeRef<'_>) -> Result<(), Error>
Source§impl StreamSerializer for Serializer
impl StreamSerializer for Serializer
Source§fn supports_partial(&self) -> bool
fn supports_partial(&self) -> bool
Documents are written in parts (between records).
Source§fn output(&self) -> &[u8] ⓘ
fn output(&self) -> &[u8] ⓘ
Source§fn clear_output(&mut self)
fn clear_output(&mut self)
output, for
instance because they were written.Source§fn drive_partial(
&mut self,
driver: &mut SerializeDriver<'_>,
limit: usize,
) -> Result<bool, Error>
fn drive_partial( &mut self, driver: &mut SerializeDriver<'_>, limit: usize, ) -> Result<bool, Error>
Source§fn in_progress(&self) -> bool
fn in_progress(&self) -> bool
true if a value is partially serialized. Read more