pub enum Nullable<T> {
Absent,
Null,
Value(T),
}Expand description
A member that may be absent, null, or a value, keeping the three apart.
LSP and MCP use both TypeScript’s optional key?: T and nullable
key: T | null, and sometimes key?: T | null, where “left out” and
“null” mean different things (null often clears a setting). An
Option<T> reads both as None; a Nullable<T> tells them apart, and
writes back what it read: ObjectWriter::optional
leaves out Absent and writes Null as null.
§Examples
use jsonx_lib::{DecodeError, FromJson, Nullable, Reader, ToJson, ValueWriter, Written};
struct Settings { tab_size: Nullable<u8> }
impl<'de> FromJson<'de> for Settings {
fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
let mut tab_size = None;
let mut o = reader.read_object()?;
while let Some(key) = o.next_key()? {
if key == "tabSize" { o.field(&mut tab_size)?; }
}
Ok(Settings { tab_size: o.required(tab_size, "tabSize")? })
}
}
impl ToJson for Settings {
fn to_json(&self, out: ValueWriter<'_>) -> Written {
out.object(|o| o.optional("tabSize", &self.tab_size))
}
}
for text in ["{}", r#"{"tabSize":null}"#, r#"{"tabSize":4}"#] {
let s: Settings = jsonx_lib::from_str(text)?;
assert_eq!(jsonx_lib::to_string(&s), text);
}Variants§
Absent
The member is not there.
Null
The member is there, and null.
Value(T)
The member is there, with a value.
Implementations§
Source§impl<T> Nullable<T>
impl<T> Nullable<T>
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
The value, if there is one: Absent and Null are both None.
§Examples
use jsonx_lib::Nullable;
assert_eq!(Nullable::Value(3).into_option(), Some(3));
assert_eq!(Nullable::<u8>::Null.into_option(), None);Trait Implementations§
impl<T: Copy> Copy for Nullable<T>
impl<T: Eq> Eq for Nullable<T>
Source§impl<'de, T: FromJson<'de>> FromJson<'de> for Nullable<T>
impl<'de, T: FromJson<'de>> FromJson<'de> for Nullable<T>
Source§impl<T: Ord> Ord for Nullable<T>
impl<T: Ord> Ord for Nullable<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl<T: PartialOrd> PartialOrd for Nullable<T>
impl<T: PartialOrd> PartialOrd for Nullable<T>
impl<T: PartialEq> StructuralPartialEq for Nullable<T>
Auto Trait Implementations§
impl<T> Forget for Nullable<T>
impl<T> Freeze for Nullable<T>where
T: Freeze,
impl<T> Move for Nullable<T>
impl<T> RefUnwindSafe for Nullable<T>where
T: RefUnwindSafe,
impl<T> Send for Nullable<T>where
T: Send,
impl<T> Sync for Nullable<T>where
T: Sync,
impl<T> Unpin for Nullable<T>where
T: Unpin,
impl<T> UnsafeUnpin for Nullable<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Nullable<T>where
T: 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