pub struct VecSkipError<T, I = ()>(/* private fields */);Available on crate feature
alloc only.Expand description
Deserialize a sequence into Vec<T>, skipping elements which fail to deserialize.
The serialization behavior is identical to Vec<T>. This is an alternative to Vec<T>
which is resilient against unexpected data.
You can be notified of skipped elements by providing a type that implements the InspectError trait.
The second generic argument I defaults to (), which does nothing.
§Examples
§Basic Usage
#[derive(Deserialize, Serialize)]
enum Color {
Red,
Green,
Blue,
}
#[serde_as]
#[derive(Deserialize, Serialize)]
struct Palette(#[serde_as(as = "VecSkipError<_>")] Vec<Color>);
let data = Palette(vec![Blue, Green,]);
let source_json = r#"["Blue", "Yellow", "Green"]"#;
let data_json = r#"["Blue","Green"]"#;
// Ensure serialization and deserialization produce the expected results
assert_eq!(data_json, serde_json::to_string(&data).unwrap());
assert_eq!(data, serde_json::from_str(source_json).unwrap());§Using InspectError to log skipped elements
struct ErrorInspector;
thread_local! {
static ERRORS: RefCell<Vec<String>> = RefCell::new(Vec::new());
}
impl InspectError for ErrorInspector {
fn inspect_error(error: impl serde::de::Error) {
ERRORS.with(|errors| errors.borrow_mut().push(error.to_string()));
}
}
#[serde_as]
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct S {
tag: String,
#[serde_as(as = "VecSkipError<_, ErrorInspector>")]
values: Vec<u8>,
}
let json = r#"{"tag":"type","values":[0, "str", 1, [10, 11], -2, {}, 300]}"#;
let s: S = serde_json::from_str(json).unwrap();
assert_eq!(s.values, vec![0, 1]);
let errors = ERRORS.with(|errors| errors.borrow().clone());
eprintln!("Errors: {errors:#?}");
assert_eq!(errors.len(), 5);
assert!(errors[0].contains("invalid type: string \"str\", expected u8"));
assert!(errors[1].contains("invalid type: sequence, expected u8"));
assert!(errors[2].contains("invalid value: integer `-2`, expected u8"));
assert!(errors[3].contains("invalid type: map, expected u8"));
assert!(errors[4].contains("invalid value: integer `300`, expected u8"));Trait Implementations§
Source§impl<'de, T, U, I> DeserializeAs<'de, Vec<T>> for VecSkipError<U, I>where
U: DeserializeAs<'de, T>,
I: InspectError,
impl<'de, T, U, I> DeserializeAs<'de, Vec<T>> for VecSkipError<U, I>where
U: DeserializeAs<'de, T>,
I: InspectError,
Source§fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
Source§impl<T, TA> JsonSchemaAs<Vec<T>> for VecSkipError<TA>where
TA: JsonSchemaAs<T>,
Available on crate feature schemars_0_8 only.
impl<T, TA> JsonSchemaAs<Vec<T>> for VecSkipError<TA>where
TA: JsonSchemaAs<T>,
Available on crate feature
schemars_0_8 only.Source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Returns a string that uniquely identifies the schema produced by this type. Read more
Source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref keyword. Read moreSource§impl<T, TA> JsonSchemaAs<Vec<T>> for VecSkipError<TA>where
TA: JsonSchemaAs<T>,
Available on crate feature schemars_0_9 only.
impl<T, TA> JsonSchemaAs<Vec<T>> for VecSkipError<TA>where
TA: JsonSchemaAs<T>,
Available on crate feature
schemars_0_9 only.Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Returns a string that uniquely identifies the schema produced by this type. Read more
Source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
Whether JSON schemas generated for this type should be included directly
in arent schemas, rather than being re-used where possible using the
$ref
keyword. Read moreSource§impl<T, TA> JsonSchemaAs<Vec<T>> for VecSkipError<TA>where
TA: JsonSchemaAs<T>,
Available on crate feature schemars_1 only.
impl<T, TA> JsonSchemaAs<Vec<T>> for VecSkipError<TA>where
TA: JsonSchemaAs<T>,
Available on crate feature
schemars_1 only.Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Returns a string that uniquely identifies the schema produced by this type. Read more
Source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
Whether JSON schemas generated for this type should be included directly
in arent schemas, rather than being re-used where possible using the
$ref
keyword. Read moreSource§impl<T, U, I> SerializeAs<Vec<T>> for VecSkipError<U, I>where
U: SerializeAs<T>,
impl<T, U, I> SerializeAs<Vec<T>> for VecSkipError<U, I>where
U: SerializeAs<T>,
Source§fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
Auto Trait Implementations§
impl<T, I> Freeze for VecSkipError<T, I>
impl<T, I> RefUnwindSafe for VecSkipError<T, I>where
T: RefUnwindSafe,
I: RefUnwindSafe,
impl<T, I> Send for VecSkipError<T, I>
impl<T, I> Sync for VecSkipError<T, I>
impl<T, I> Unpin for VecSkipError<T, I>
impl<T, I> UnsafeUnpin for VecSkipError<T, I>
impl<T, I> UnwindSafe for VecSkipError<T, I>where
T: UnwindSafe,
I: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more