--------------------------------------------------------------------------------
-- SARIF implementation for Haskell
--------------------------------------------------------------------------------
-- This source code is licensed under the MIT license found in the LICENSE    --
-- file in the root directory of this source tree.                            --
--------------------------------------------------------------------------------

{-# LANGUAGE CPP #-}

-- | Provides extensions to "Data.Aeson" for constructing JSON objects which
-- omit fields that are @null@.
module Data.Aeson.Optional (
    module Aeson,
    object,
    (.=),
    (.=?)
) where

--------------------------------------------------------------------------------

import Data.Aeson as Aeson hiding (object, (.=))
import Data.Aeson.Types (Pair)
import qualified Data.Aeson as JSON (object)
import Data.Maybe
#if !MIN_VERSION_aeson(2,0,0)
import Data.Text
#endif

--------------------------------------------------------------------------------

-- | `object` @pairList@ constructs a JSON objects from @pairList@, omitting
-- all `Nothing` values.
object :: [Maybe Pair] -> Value
object :: [Maybe Pair] -> Value
object = [Pair] -> Value
JSON.object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Maybe a] -> [a]
catMaybes

infixr 8 .=?, .=
-- | Construct an optional field from an optional value. If the value is
-- `Nothing`, then the field will be omitted from the JSON object.
#if MIN_VERSION_aeson(2,0,0)
(.=?) :: ToJSON a => Key -> Maybe a -> Maybe Pair
#else
(.=?) :: ToJSON a => Text -> Maybe a -> Maybe Pair
#endif
Key
_ .=? :: forall a. ToJSON a => Key -> Maybe a -> Maybe Pair
.=? Maybe a
Nothing = forall a. Maybe a
Nothing
Key
key .=? Just a
val = forall a. a -> Maybe a
Just (Key
key, forall a. ToJSON a => a -> Value
toJSON a
val)

-- | Construct a mandatory field from a value. The resulting field will always
-- be present in the resulting JSON object.
#if MIN_VERSION_aeson(2,0,0)
(.=) :: ToJSON a => Key -> a -> Maybe Pair
#else
(.=) :: ToJSON a => Text -> a -> Maybe Pair
#endif
Key
key .= :: forall a. ToJSON a => Key -> a -> Maybe Pair
.= a
val = forall a. a -> Maybe a
Just (Key
key, forall a. ToJSON a => a -> Value
toJSON a
val)

--------------------------------------------------------------------------------