-- | This module provides helper definitions for "Data.Aeson".
module Haspara.Internal.Aeson where

import qualified Data.Aeson as Aeson
import qualified Data.Char as C
import Data.List (stripPrefix)
import Data.Maybe (fromMaybe)


-- | Common Aeson encoding/decoding options.
commonAesonOptions :: String -> Aeson.Options
commonAesonOptions :: String -> Options
commonAesonOptions String
prefix =
  Options
Aeson.defaultOptions
    { omitNothingFields :: Bool
Aeson.omitNothingFields = Bool
True
    , fieldLabelModifier :: String -> String
Aeson.fieldLabelModifier = \String
l -> Char -> String -> String
Aeson.camelTo2 Char
'_' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe String
l forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
prefix String
l
    , constructorTagModifier :: String -> String
Aeson.constructorTagModifier = \String
l -> Char -> String -> String
Aeson.camelTo2 Char
'_' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe String
l forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
prefix String
l
    , sumEncoding :: SumEncoding
Aeson.sumEncoding =
        Aeson.TaggedObject
          { tagFieldName :: String
Aeson.tagFieldName = String
"type"
          , contentsFieldName :: String
Aeson.contentsFieldName = String
"value"
          }
    }


-- | Aeson encoding/decoding options for uppercase constructor tag modifiers
aesonOptionsForSingleTag :: String -> Aeson.Options
aesonOptionsForSingleTag :: String -> Options
aesonOptionsForSingleTag String
prefix =
  Options
Aeson.defaultOptions
    { constructorTagModifier :: String -> String
Aeson.constructorTagModifier = \String
l -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
C.toUpper forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
Aeson.camelTo2 Char
'_' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe String
l forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
prefix String
l
    }