{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
module Data.Aeson.With (
withJSON
, withValue
, withValueMaybe
, withStringField
, withArrayField
, withObjectField
, withNumberField
, withBoolField
, withNullField
)where
import Control.Lens
import Data.Aeson as A
import Data.Aeson.Lens
import qualified Data.HashMap.Lazy as HML
import Data.Scientific
import Data.Text
import qualified Data.Vector as V
withJSON :: (ToJSON a) => a -> Value -> Value
withJSON x (Object obj) = Object $ HML.union obj y
where Object y = toJSON x
withJSON _ _ = error "Can ony add a new TOJSON object to objects"
withNullField :: Text -> Value -> Value
withNullField f = _Object . at f ?~ Null
withStringField :: Text -> Text -> Value -> Value
withStringField f v = _Object . at f ?~ String v
withArrayField :: Text -> [Value] -> Value -> Value
withArrayField f v = _Object . at f ?~ Array (V.fromList v)
withValue :: Text -> Value -> Value -> Value
withValue f v = _Object . at f ?~ v
withValueMaybe :: Text -> Maybe Value -> Value -> Value
withValueMaybe f v = _Object . at f .~ v
withNumberField :: Text -> Scientific -> Value -> Value
withNumberField f v = _Object . at f ?~ Number v
withBoolField :: Text -> Bool -> Value -> Value
withBoolField f v = _Object . at f ?~ Bool v
withObjectField :: Text -> Object -> Value -> Value
withObjectField f v = _Object . at f ?~ Object v