on master help sign in
Merge pull request #347 from phadej/th-lift

Add Lift Value instance

commit1441ceace9774b34ec874aa2a59748b1ebbd4bae authorAdam Bergmark <adam@bergmark.nl> dateFri 29 Jan 2016, 19:45 (11 years ago) parent9c61b43125b641
2 files changed+35−2
Data/Aeson/TH.hs+9−2 Data/Aeson/Types/Internal.hs+26
Data/Aeson/TH.hs+9−2
@@ -1,6 +1,13 @@
11{-# LANGUAGE CPP, FlexibleInstances, NamedFieldPuns,
2− NoImplicitPrelude, TemplateHaskell,
3− UndecidableInstances #-}
2+ NoImplicitPrelude, UndecidableInstances #-}
3+#if __GLASGOW_HASKELL >= 800
4+-- a) THQ works on cross-compilers and unregisterised GHCs
5+-- b) may make compilation faster as no dynamic loading is ever needed (not sure about this)
6+-- c) removes one hindrance to have code inferred as SafeHaskell safe
7+{-# LANGUAGE TemplateHaskellQuotes #-}
8+#else
9+{-# LANGUAGE TemplateHaskell #-}
10+#endif
411
512#include "overlapping-compat.h"
613
Data/Aeson/Types/Internal.hs+26
@@ -1,5 +1,13 @@
11{-# LANGUAGE CPP, DeriveDataTypeable, GeneralizedNewtypeDeriving, Rank2Types,
22 RecordWildCards #-}
3+#if __GLASGOW_HASKELL >= 800
4+-- a) THQ works on cross-compilers and unregisterised GHCs
5+-- b) may make compilation faster as no dynamic loading is ever needed (not sure about this)
6+-- c) removes one hindrance to have code inferred as SafeHaskell safe
7+{-# LANGUAGE TemplateHaskellQuotes #-}
8+#else
9+{-# LANGUAGE TemplateHaskell #-}
10+#endif
311
412-- |
513-- Module: Data.Aeson.Types.Internal
@@ -53,6 +61,7 @@ module Data.Aeson.Types.Internal
5361 , DotNetTime(..)
5462 ) where
5563
64+import Control.Arrow (first)
5665import Control.Applicative
5766import Control.DeepSeq (NFData(..))
5867import Control.Monad (MonadPlus(..), ap)
@@ -71,7 +80,9 @@ import Data.Time.Format (FormatTime)
7180import Data.Typeable (Typeable)
7281import Data.Vector (Vector)
7382import qualified Data.HashMap.Strict as H
83+import qualified Data.Scientific as S
7484import qualified Data.Vector as V
85+import qualified Language.Haskell.TH.Syntax as TH
7586
7687#if !MIN_VERSION_base(4,8,0)
7788import Data.Foldable (Foldable(..))
@@ -392,6 +403,21 @@ hashValue s Null = s `hashWithSalt` (5::Int)
392403instance Hashable Value where
393404 hashWithSalt = hashValue
394405
406+-- @since 0.11.0.0
407+instance TH.Lift Value where
408+ lift Null = [| Null |]
409+ lift (Bool b) = [| Bool b |]
410+ lift (Number n) = [| Number (S.scientific c e) |]
411+ where
412+ c = S.coefficient n
413+ e = S.base10Exponent n
414+ lift (String t) = [| String (pack s) |]
415+ where s = unpack t
416+ lift (Array a) = [| Array (V.fromList a') |]
417+ where a' = V.toList a
418+ lift (Object o) = [| Object (H.fromList . map (first pack) $ o') |]
419+ where o' = map (first unpack) . H.toList $ o
420+
395421-- | The empty array.
396422emptyArray :: Value
397423emptyArray = Array V.empty