Deserr
Introduction
Deserr is a crate for deserializing data, with the ability to return custom, type-specific errors upon failure.
Unlike serde, Deserr does not parse the data in its serialization format itself, but offload the work to other crates. Instead, it deserializes the already-parsed serialized data into the final type. For example:
// bytes of the serialized value
let s: &str = ".." ;
// parse serialized data using another crate, such as `serde_json`
let json: Value = from_str.unwrap;
// finally deserialize with deserr
let data = deserialize_from_value.unwrap;
// `T` must implements `DeserializeFromValue`.
Thus, Deserr is slower than crates that immediately deserialize a value while parsing at the same time.
The main parts of Deserr are:
- [
DeserializeFromValue<E>
] is the main trait for deserialization - [
IntoValue
] and [Value
] describe the shape that the parsed serialized data must have - [
DeserializeError
] is the trait that all deserialization errors must conform to - [
MergeWithError<E>
] describes how to combine multiple errors together. It allows Deserr to return multiple deserialization errors at once. - [
ValuePointerRef
] and [ValuePointer
] point to locations within the value. They are used to locate the origin of an error. - [
deserialize
] is the main function to use to deserialize a value - The
DeserializeFromValue
derive proc macro
If the feature serde
is activated, then an implementation of [IntoValue
] is provided
for the type serde_json::Value
. This allows using Deserr to deserialize from JSON.
Example
Implementing deserialize for a custom type
use ;
;
Using macros