on master help sign in
Add Data.Aeson.QQ.Simple
commit40713a925da2ad3ea2ccf56229ca4bcdc67f2891 authorOleg Grenrus <oleg.grenrus@iki.fi> dateMon 15 Oct 2018, 12:54 (8 years ago) parentb8b1820
3 files changed+32
Data/Aeson/QQ/Simple.hs+25 aeson.cabal+1 tests/UnitTests.hs+6
Data/Aeson/QQ/Simple.hsnew file+25
@@ -0,0 +1,25 @@
1+-- | Like "Data.Aeson.QQ" but without interpolation.
2+module Data.Aeson.QQ.Simple (aesonQQ) where
3+
4+import Data.Aeson
5+import qualified Data.Text as T
6+import qualified Data.Text.Encoding as TE
7+import Language.Haskell.TH
8+import Language.Haskell.TH.Quote
9+import Language.Haskell.TH.Syntax (Lift (..))
10+import Prelude ()
11+import Prelude.Compat
12+
13+aesonQQ :: QuasiQuoter
14+aesonQQ = QuasiQuoter
15+ { quoteExp = aesonExp
16+ , quotePat = const $ error "No quotePat defined for jsonQQ"
17+ , quoteType = const $ error "No quoteType defined for jsonQQ"
18+ , quoteDec = const $ error "No quoteDec defined for jsonQQ"
19+ }
20+
21+aesonExp :: String -> ExpQ
22+aesonExp txt =
23+ case eitherDecodeStrict $ TE.encodeUtf8 $ T.pack txt of
24+ Left err -> error $ "Error in aesonExp: " ++ show err
25+ Right val -> lift (val :: Value)
aeson.cabal+1
@@ -75,6 +75,7 @@ library
7575 Data.Aeson.Text
7676 Data.Aeson.Types
7777 Data.Aeson.TH
78+ Data.Aeson.QQ.Simple
7879
7980 Data.Aeson.Encoding.Internal
8081 Data.Aeson.Internal
tests/UnitTests.hs+6
@@ -5,6 +5,7 @@
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE ScopedTypeVariables #-}
77{-# LANGUAGE TemplateHaskell #-}
8+{-# LANGUAGE QuasiQuotes #-}
89
910-- For Data.Aeson.Types.camelTo
1011{-# OPTIONS_GHC -fno-warn-deprecations #-}
@@ -25,6 +26,7 @@ import Prelude.Compat
2526import Control.Monad (forM, forM_)
2627import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(..), FromJSONKey(..), ToJSON1(..), decode, eitherDecode, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject, withEmbeddedJSON)
2728import Data.Aeson.Internal (JSONPathElement(..), formatError)
29+import Data.Aeson.QQ.Simple (aesonQQ)
2830import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)
2931import Data.Aeson.Text (encodeToTextBuilder)
3032import Data.Aeson.Types (Options(..), Result(Success), ToJSON(..), Value(Null, Object), camelTo, camelTo2, defaultOptions, omitNothingFields, parse)
@@ -108,6 +110,10 @@ tests = testGroup "unit" [
108110 , testCase "Big integer decoding" bigIntegerDecoding
109111 , testCase "Big natural decading" bigNaturalDecoding
110112 , testCase "Big integer key decoding" bigIntegerKeyDecoding
113+ , testGroup "QQ.Simple"
114+ [ testCase "example" $
115+ assertEqual "" (object ["foo" .= True]) [aesonQQ| {"foo": true } |]
116+ ]
111117 ]
112118
113119roundTripCamel :: String -> Assertion