{-# OPTIONS_HADDOCK not-home #-}

-- | Internal to the arbiter packages. Not covered by the PVP.
--
-- Shared Aeson helpers. Patch decoders are triple-state. An omitted field leaves
-- the value unchanged. An explicit @null@ clears the override.
module Arbiter.Core.Json
  ( patchOptions
  , decodeOrEmpty
  ) where

import Data.Aeson (FromJSON, Options (..), Value, defaultOptions, parseJSON)
import Data.Aeson.Types (parseMaybe)
import Data.Maybe (fromMaybe)

-- | Generic options that omit @Nothing@ fields. An unchanged field is absent. A
-- cleared override is @null@.
patchOptions :: Options
patchOptions :: Options
patchOptions = Options
defaultOptions {omitNothingFields = True}

-- | Decode a value, or 'mempty' when it has another shape.
decodeOrEmpty :: (FromJSON a, Monoid a) => Value -> a
decodeOrEmpty :: forall a. (FromJSON a, Monoid a) => Value -> a
decodeOrEmpty = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
forall a. Monoid a => a
mempty (Maybe a -> a) -> (Value -> Maybe a) -> Value -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Parser a) -> Value -> Maybe a
forall a b. (a -> Parser b) -> a -> Maybe b
parseMaybe Value -> Parser a
forall a. FromJSON a => Value -> Parser a
parseJSON