Expand description
Adapters to use serde 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. To use the adapter
outside of the derive, wrap values in 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,Someand 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 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.
Structs§
- Serde
- Adapter that uses the serde implementations of a type.