use crate::Json;
impl Json {
pub fn is_null(&self) -> bool {
match self {
Json::Null => true,
_ => false,
}
}
pub fn map<T, E>(self) -> Result<Option<T>, E> where T: TryFrom<Self, Error=E> {
match self {
Self::Null => Ok(None),
_ => Ok(Some(T::try_from(self)?)),
}
}
pub fn map_ref<'a, T, E>(&'a self) -> Result<Option<T>, E> where T: TryFrom<&'a Self, Error=E> {
match self {
Self::Null => Ok(None),
_ => Ok(Some(T::try_from(self)?)),
}
}
pub fn map_or<T, E>(self, default: T) -> Result<T, E> where T: TryFrom<Self, Error=E> {
match self {
Json::Null => Ok(default),
_ => T::try_from(self),
}
}
pub fn map_or_else<T, E, F>(self, default: F) -> Result<T, E> where T: TryFrom<Self, Error=E>, F: FnOnce() -> T {
match self {
Json::Null => Ok(default()),
_ => T::try_from(self),
}
}
pub fn map_ref_or<'a, T, E>(&'a self, default: T) -> Result<T, E> where T: TryFrom<&'a Self, Error=E> {
match self {
Json::Null => Ok(default),
_ => T::try_from(self),
}
}
pub fn map_ref_or_else<'a, T, E, F>(&'a self, default: F) -> Result<T, E> where T: TryFrom<&'a Self, Error=E>, F: FnOnce() -> T {
match self {
Json::Null => Ok(default()),
_ => T::try_from(self),
}
}
pub fn unwrap_or_null(json: Option<Self>) -> Self {
json.unwrap_or(Self::Null)
}
pub fn unwrap_ref_or_null<'a>(json: Option<&'a Self>) -> &'a Self {
json.unwrap_or(&Self::Null)
}
}
impl<T> From<Option<T>> for Json where T: Into<Json> {
fn from(t: Option<T>) -> Self {
match t {
Some(t) => t.into(),
None => Json::Null,
}
}
}
impl From<()> for Json {
fn from(_: ()) -> Self {
Self::Null
}
}