Skip to main content

miniserde_miku/ser/
mod.rs

1//! Serialization traits.
2//!
3//! Serialization in miniserde works by traversing an input object and
4//! decomposing it iteratively into a stream of fragments.
5//!
6//! ## Serializing a primitive
7//!
8//! ```rust
9//! use miniserde::ser::{Fragment, Serialize};
10//!
11//! // The data structure that we want to serialize as a primitive.
12//! struct MyBoolean(bool);
13//!
14//! impl Serialize for MyBoolean {
15//!     fn begin(&self) -> Fragment {
16//!         Fragment::Bool(self.0)
17//!     }
18//! }
19//! ```
20//!
21//! ## Serializing a sequence
22//!
23//! ```rust
24//! use miniserde::ser::{Fragment, Seq, Serialize};
25//!
26//! // Some custom sequence type that we want to serialize.
27//! struct MyVec<T>(Vec<T>);
28//!
29//! impl<T: Serialize> Serialize for MyVec<T> {
30//!     fn begin(&self) -> Fragment {
31//!         Fragment::Seq(Box::new(SliceStream { iter: self.0.iter() }))
32//!     }
33//! }
34//!
35//! struct SliceStream<'a, T: 'a> {
36//!     iter: std::slice::Iter<'a, T>,
37//! }
38//!
39//! impl<'a, T: Serialize> Seq for SliceStream<'a, T> {
40//!     fn next(&mut self) -> Option<&dyn Serialize> {
41//!         let element = self.iter.next()?;
42//!         Some(element)
43//!     }
44//! }
45//! ```
46//!
47//! ## Serializing a map or struct
48//!
49//! This code demonstrates what is generated for structs by
50//! `#[derive(Serialize)]`.
51//!
52//! ```rust
53//! use miniserde::ser::{Fragment, Map, Serialize};
54//! use std::borrow::Cow;
55//!
56//! // The struct that we would like to serialize.
57//! struct Demo {
58//!     code: u32,
59//!     message: String,
60//! }
61//!
62//! impl Serialize for Demo {
63//!     fn begin(&self) -> Fragment {
64//!         Fragment::Map(Box::new(DemoStream {
65//!             data: self,
66//!             state: 0,
67//!         }))
68//!     }
69//! }
70//!
71//! struct DemoStream<'a> {
72//!     data: &'a Demo,
73//!     state: usize,
74//! }
75//!
76//! impl<'a> Map for DemoStream<'a> {
77//!     fn next(&mut self) -> Option<(Cow<str>, &dyn Serialize)> {
78//!         let state = self.state;
79//!         self.state += 1;
80//!         match state {
81//!             0 => Some((Cow::Borrowed("code"), &self.data.code)),
82//!             1 => Some((Cow::Borrowed("message"), &self.data.message)),
83//!             _ => None,
84//!         }
85//!     }
86//! }
87//! ```
88
89mod impls;
90
91use alloc::borrow::Cow;
92use alloc::boxed::Box;
93
94/// One unit of output produced during serialization.
95///
96/// [Refer to the module documentation for examples.][crate::ser]
97pub enum Fragment<'a> {
98    Null,
99    Bool(bool),
100    Str(Cow<'a, str>),
101    U64(u64),
102    I64(i64),
103    F64(f64),
104    Seq(Box<dyn Seq + 'a>),
105    Map(Box<dyn Map + 'a>),
106}
107
108/// Trait for data structures that can be serialized to a JSON string.
109///
110/// [Refer to the module documentation for examples.][crate::ser]
111pub trait Serialize {
112    fn begin(&self) -> Fragment;
113}
114
115/// Trait that can iterate elements of a sequence.
116///
117/// [Refer to the module documentation for examples.][crate::ser]
118pub trait Seq {
119    fn next(&mut self) -> Option<&dyn Serialize>;
120}
121
122/// Trait that can iterate key-value entries of a map or struct.
123///
124/// [Refer to the module documentation for examples.][crate::ser]
125pub trait Map {
126    fn next(&mut self) -> Option<(Cow<str>, &dyn Serialize)>;
127}