{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Data.Aeson.TextValue where
import Control.Lens.TH
import Data.Aeson
import Data.String
import Network.AWS.Data.Text (FromText (..), ToText (..), fromText)
newtype TextValue a = TextValue { _unTextValue :: a } deriving (Eq, Show, IsString)
instance FromText a => FromJSON (TextValue a) where
parseJSON = withText "TextValue" $ fmap TextValue . either fail pure . fromText
instance ToText a => ToJSON (TextValue a) where
toJSON = toJSON . toText . _unTextValue
toEncoding = toEncoding . toText . _unTextValue
$(makeLenses ''TextValue)