{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RecordWildCards            #-}
{-# LANGUAGE TemplateHaskell            #-}

{-|
Module: Data.Aeson.TextValue
Description: Type for things that can be embedded in a JSON string

Provides @FromJSON@ and @ToJSON@ instances for anything that
has @FromText@ and @ToText@ instances, e.g. @TextValue Text@,
@(FromJSON a, ToJSON a) => TextValue (Embedded a)@,
@TextValue Base64@
-}
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 { TextValue a -> a
_unTextValue :: a } deriving (TextValue a -> TextValue a -> Bool
(TextValue a -> TextValue a -> Bool)
-> (TextValue a -> TextValue a -> Bool) -> Eq (TextValue a)
forall a. Eq a => TextValue a -> TextValue a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TextValue a -> TextValue a -> Bool
$c/= :: forall a. Eq a => TextValue a -> TextValue a -> Bool
== :: TextValue a -> TextValue a -> Bool
$c== :: forall a. Eq a => TextValue a -> TextValue a -> Bool
Eq, Int -> TextValue a -> ShowS
[TextValue a] -> ShowS
TextValue a -> String
(Int -> TextValue a -> ShowS)
-> (TextValue a -> String)
-> ([TextValue a] -> ShowS)
-> Show (TextValue a)
forall a. Show a => Int -> TextValue a -> ShowS
forall a. Show a => [TextValue a] -> ShowS
forall a. Show a => TextValue a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextValue a] -> ShowS
$cshowList :: forall a. Show a => [TextValue a] -> ShowS
show :: TextValue a -> String
$cshow :: forall a. Show a => TextValue a -> String
showsPrec :: Int -> TextValue a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> TextValue a -> ShowS
Show, String -> TextValue a
(String -> TextValue a) -> IsString (TextValue a)
forall a. IsString a => String -> TextValue a
forall a. (String -> a) -> IsString a
fromString :: String -> TextValue a
$cfromString :: forall a. IsString a => String -> TextValue a
IsString)

instance FromText a => FromJSON (TextValue a) where
  parseJSON :: Value -> Parser (TextValue a)
parseJSON = String
-> (Text -> Parser (TextValue a)) -> Value -> Parser (TextValue a)
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"TextValue" ((Text -> Parser (TextValue a)) -> Value -> Parser (TextValue a))
-> (Text -> Parser (TextValue a)) -> Value -> Parser (TextValue a)
forall a b. (a -> b) -> a -> b
$ (a -> TextValue a) -> Parser a -> Parser (TextValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> TextValue a
forall a. a -> TextValue a
TextValue (Parser a -> Parser (TextValue a))
-> (Text -> Parser a) -> Text -> Parser (TextValue a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Parser a)
-> (a -> Parser a) -> Either String a -> Parser a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either String a -> Parser a)
-> (Text -> Either String a) -> Text -> Parser a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either String a
forall a. FromText a => Text -> Either String a
fromText

instance ToText a => ToJSON (TextValue a) where
  toJSON :: TextValue a -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> (TextValue a -> Text) -> TextValue a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. ToText a => a -> Text
toText (a -> Text) -> (TextValue a -> a) -> TextValue a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextValue a -> a
forall a. TextValue a -> a
_unTextValue
  toEncoding :: TextValue a -> Encoding
toEncoding = Text -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Text -> Encoding)
-> (TextValue a -> Text) -> TextValue a -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. ToText a => a -> Text
toText (a -> Text) -> (TextValue a -> a) -> TextValue a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextValue a -> a
forall a. TextValue a -> a
_unTextValue

$(makeLenses ''TextValue)