1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Transparent [`serde`] support for Müsli types.
//!
//! This conveniently and transparently allows Müsli to use fields which are
//! serde types by marking them with `#[musli(with = musli::serde)]`. This can
//! be useful because there is a wide ecosystem of types which implements serde
//! traits.
//!
//! Note that the exact method that fields are serialized and deserialized will
//! not match what Müsli does, since serde requires the use of a fundamentally
//! different model and Müsli metadata such as `#[musli(name = ..)]` is not
//! available in [`serde`].
//!
//! [`serde`]: https://serde.rs
//!
//! <br>
//!
//! ## Examples
//!
//! ```
//! use serde::{Serialize, Deserialize};
//! use musli::{Encode, Decode};
//! use url::Url;
//!
//! #[derive(Serialize, Deserialize)]
//! struct Address {
//! street: String,
//! city: String,
//! zip: u32,
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! struct Person {
//! name: String,
//! #[musli(with = musli::serde)]
//! address: Address,
//! #[musli(with = musli::serde)]
//! url: Url,
//! }
//! ```
//!
//! A compatible Müsli structure would look like this:
//!
//! ```
//! use musli::{Encode, Decode};
//! use url::Url;
//! # use serde::{Serialize, Deserialize};
//! # #[derive(Serialize, Deserialize)]
//! # struct Address { street: String, city: String, zip: u32 }
//! # #[derive(Encode, Decode)]
//! # #[musli(name_all = "name")]
//! # struct Person { name: String, #[musli(with = musli::serde)] address: Address, #[musli(with = musli::serde)] url: Url }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! struct MusliAddress {
//! street: String,
//! city: String,
//! zip: u32,
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! struct MusliPerson {
//! name: String,
//! address: MusliAddress,
//! url: String,
//! }
//!
//! let json = musli::json::to_string(&Person {
//! name: "John Doe".to_string(),
//! address: Address {
//! street: "Main St.".to_string(),
//! city: "Springfield".to_string(),
//! zip: 12345,
//! },
//! url: Url::parse("https://example.com")?,
//! })?;
//!
//! let musli = musli::json::from_str::<MusliPerson>(&json)?;
//!
//! assert_eq!(musli.name, "John Doe");
//! assert_eq!(musli.address.street, "Main St.");
//! assert_eq!(musli.address.city, "Springfield");
//! assert_eq!(musli.address.zip, 12345);
//! assert_eq!(musli.url, "https://example.com/");
//! # Ok::<_, Box<dyn core::error::Error>>(())
//! ```
use ;
use Deserializer;
use Serializer;
use crate::;
/// Encode the given serde value `T` to the given [`Encoder`] using the serde
/// compatibility layer.
///
/// ## Examples
///
/// ```
/// use serde::{Serialize, Deserialize};
/// use musli::{Encode, Decode};
/// use url::Url;
///
/// #[derive(Serialize, Deserialize)]
/// struct Address {
/// street: String,
/// city: String,
/// zip: u32,
/// }
///
/// #[derive(Encode, Decode)]
/// #[musli(name_all = "name")]
/// struct Person {
/// name: String,
/// #[musli(with = musli::serde)]
/// address: Address,
/// #[musli(with = musli::serde)]
/// url: Url,
/// }
/// ```
/// Decode the given serde value `T` from the given [`Decoder`] using the serde
/// compatibility layer.
///
/// ## Examples
///
/// ```
/// use serde::{Serialize, Deserialize};
/// use musli::{Encode, Decode};
/// use url::Url;
///
/// #[derive(Serialize, Deserialize)]
/// struct Address {
/// street: String,
/// city: String,
/// zip: u32,
/// }
///
/// #[derive(Encode, Decode)]
/// #[musli(name_all = "name")]
/// struct Person {
/// name: String,
/// #[musli(with = musli::serde)]
/// address: Address,
/// #[musli(with = musli::serde)]
/// url: Url,
/// }
/// ```