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
//! Adapters to use [serde](https://serde.rs/) types with deser.
//!
//! This crate provides the [`Serde`] adapter which serializes and
//! deserializes values with their serde implementations. It's useful for
//! types from crates which only support serde:
//!
//! ```
//! use deser::{Deserialize, Serialize};
//! use deser_serde::Serde;
//!
//! #[derive(serde::Serialize, serde::Deserialize)]
//! struct Point {
//! x: i32,
//! y: i32,
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct Shape {
//! name: String,
//! #[deser(as = Vec<Serde>)]
//! points: Vec<Point>,
//! #[deser(as = Serde)]
//! extra: serde_json::Value,
//! }
//!
//! let shape: Shape = deser_json::from_str(
//! r#"{
//! "name": "line",
//! "points": [{"x": 1, "y": 2}, {"x": 3, "y": 4}],
//! "extra": [true]
//! }"#,
//! )
//! .unwrap();
//! assert_eq!(shape.points[1].y, 4);
//! assert_eq!(shape.extra, serde_json::json!([true]));
//! ```
//!
//! Adapters compose with containers (`Vec<Serde>`, `Option<Serde>`, ...),
//! for more information see [`deser::adapters`](deser_core::adapters). To use the adapter
//! outside of the derive, wrap values in [`As`](deser_core::adapters::As).
//!
//! # Data Model
//!
//! serde values are mapped to the deser data model like this:
//!
//! * Integers, floats, booleans, chars, strings and bytes map to the
//! respective atoms. 128 bit integers are extension values like the ones
//! of deser.
//! * `None`, `()` and unit structs are null, `Some` and newtype structs are
//! the value they hold.
//! * Sequences and tuples are sequences, maps and structs are maps.
//! * Enums are externally tagged (the default in serde and deser): unit
//! variants are strings, all others are maps with the variant name as
//! single key.
//!
//! When deserializing, extension values that serde does not know (like
//! date-times) are passed to serde as their fallback atom (for instance a
//! string). Map keys are also parsed from strings if serde asks for a
//! number or boolean, so `HashMap<u32, _>` works with JSON. Borrowing is
//! supported: serde types which borrow (like `&'de str`) can borrow from
//! the data if the format passes it on borrowed.
//!
//! [`is_human_readable`](serde::Serializer::is_human_readable) is always
//! `true`.
//!
//! Missing struct fields are handled like serde: they are `None` if the
//! type deserializes a missing value as option, which is the case for
//! `Option<T>`.
//!
//! # Buffering
//!
//! serde and deser drive values in opposite directions: with serde the
//! value is serialized into a serializer by nested calls and pulls
//! from a deserializer, with deser the value is walked by the driver and
//! events are pushed into deserializers. So [`Serde`] buffers the events
//! of compound values. For atoms (the typical case, like `Url` or
//! `IpAddr`) there is no buffering.
use Cow;
use Deserialize;
use Serialize;
use State;
use SinkHandle;
use Emit;
use crateMissingDe;
use crateRootSink;
/// Returns the value for a missing field like serde does.
/// Adapter that uses the serde implementations of a type.
///
/// Compound values are buffered, see the [crate documentation](crate) for
/// more information.
///
/// ```
/// use std::collections::BTreeMap;
/// use deser::adapters::As;
/// use deser_serde::Serde;
///
/// let value: As<BTreeMap<u32, String>, Serde> =
/// deser_json::from_str(r#"{"1": "a"}"#).unwrap();
/// assert_eq!(value[&1], "a");
/// assert_eq!(deser_json::to_string(&value).unwrap(), r#"{"1":"a"}"#);
/// ```
;