{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.Aeson.Extra.TH (
mkValue,
mkValue',
) where
import Language.Haskell.TH
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Aeson
mkValue :: String -> Q Exp
mkValue :: String -> Q Exp
mkValue String
s = case ByteString -> Either String Value
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict' ByteString
bs :: Either String Value of
Left String
err -> String -> Q Exp
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Q Exp) -> String -> Q Exp
forall a b. (a -> b) -> a -> b
$ String
"mkValue: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err
Right Value
v -> [| v |]
where bs :: ByteString
bs = Text -> ByteString
TE.encodeUtf8 (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
s
mkValue' :: String -> Q Exp
mkValue' :: String -> Q Exp
mkValue' = String -> Q Exp
mkValue (String -> Q Exp) -> (String -> String) -> String -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
f
where f :: Char -> Char
f Char
'\'' = Char
'"'
f Char
x = Char
x