pub struct DefaultOnNull<A = Same>(/* private fields */);Expand description
Uses the Default if the value is null.
A null (or a value that is null in the format, like ~ in YAML) is
replaced with the default value, everything else is deserialized with
the adapter A (by default Same). This is for data that sends
null instead of an empty list or a zero. Missing values are handled
by the inner adapter, combine it with #[deser(default)] to also
accept them. Serialization uses the inner adapter.
use deser::Deserialize;
use deser::adapters::DefaultOnNull;
#[derive(Deserialize)]
pub struct Item {
#[deser(as = DefaultOnNull)]
tags: Vec<String>,
#[deser(as = DefaultOnNull, default)]
count: u32,
}
let item: Item = deser_json::from_str(r#"{"tags": null}"#).unwrap();
assert!(item.tags.is_empty());
assert_eq!(item.count, 0);In formats where keys can be given more than once (see
ContainerShape::set_multimap) collections receive the values of all
occurrences of their key from the inner adapter, nulls among them are
not replaced.
Trait Implementations§
Source§impl<'de, T: Default + Send, A: Deserialize<'de, T>> Deserialize<'de, T> for DefaultOnNull<A>
impl<'de, T: Default + Send, A: Deserialize<'de, T>> Deserialize<'de, T> for DefaultOnNull<A>
Source§fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<T>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<T: ?Sized, A: Serialize<T>> Serialize<T> for DefaultOnNull<A>
impl<T: ?Sized, A: Serialize<T>> Serialize<T> for DefaultOnNull<A>
Source§fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn finish(value: &T, state: &mut State) -> Result<(), Error>
fn finish(value: &T, state: &mut State) -> Result<(), Error>
Invoked after the serialization finished. Read more
Source§fn container_shape(value: &T) -> ContainerShape
fn container_shape(value: &T) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Auto Trait Implementations§
impl<A> Freeze for DefaultOnNull<A>
impl<A> RefUnwindSafe for DefaultOnNull<A>
impl<A> Send for DefaultOnNull<A>
impl<A> Sync for DefaultOnNull<A>
impl<A> Unpin for DefaultOnNull<A>
impl<A> UnsafeUnpin for DefaultOnNull<A>
impl<A> UnwindSafe for DefaultOnNull<A>
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