Documentation
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

SJ

Copyright (C) 2019-2025  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2019-2025".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Shortcuts for `Json::Null`

use crate::Json;

/// # Shortcuts for [`Null`](#variant.Null)
impl Json {

    /// # Checks to see if this value is [`Null`][#Null]
    ///
    /// [#Null]: #variant.Null
    pub fn is_null(&self) -> bool {
        match self {
            Json::Null => true,
            _ => false,
        }
    }

    /// # Maps this value into something
    ///
    /// If this is [`Null`](#variant.Null), returns `None`.
    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)?)),
        }
    }

    /// # Maps this value into something
    ///
    /// If this is [`Null`](#variant.Null), returns `None`.
    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)?)),
        }
    }

    /// # Tries to convert this value into something
    ///
    /// If this is [`Null`](#variant.Null), returns `default`.
    ///
    /// If your default value would be a result of a function call, you should use [`map_or_else()`](#method.map_or_else).
    ///
    /// ## Examples
    ///
    /// ```
    /// # #[cfg(feature="std")] {
    /// use sj::MapKind;
    ///
    /// let mut json = sj::parse_bytes("[null]", MapKind::HashMap)?;
    /// assert!(json.take_at(0)?.map_or(true)?);
    /// # }
    /// # Ok::<_, sj::Error>(())
    /// ```
    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),
        }
    }

    /// # Tries to convert this value into something
    ///
    /// If this is [`Null`](#variant.Null), calls `default()` and returns its result.
    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),
        }
    }

    /// # Tries to convert a reference of this value into something
    ///
    /// If this is [`Null`](#variant.Null), returns `default`.
    ///
    /// If your default value would be a result of a function call, you should use [`map_ref_or_else()`](#method.map_ref_or_else).
    ///
    /// ## Examples
    ///
    /// ```
    /// # #[cfg(feature="std")] {
    /// use sj::MapKind;
    ///
    /// let value = sj::parse_bytes("[null]", MapKind::HashMap)?;
    /// assert_eq!(value.at(0)?.map_ref_or(0)?, 0);
    /// # }
    /// # Ok::<_, sj::Error>(())
    /// ```
    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),
        }
    }

    /// # Tries to convert a reference of this value into something
    ///
    /// If this is [`Null`](#variant.Null), calls `default()` and returns its result.
    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),
        }
    }

    /// # Unwraps an option of a `Json`
    ///
    /// If this is `None`, returns [`Null`](#variant.Null).
    pub fn unwrap_or_null(json: Option<Self>) -> Self {
        json.unwrap_or(Self::Null)
    }

    /// # Unwraps an option of a `Json`
    ///
    /// If this is `None`, returns [`Null`](#variant.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
    }

}