on master help sign in
Add Lift Value instance
commit32908ac6462a2d446a711836aded5663356e5f8b authorOleg Grenrus <oleg.grenrus@iki.fi> dateThu 28 Jan 2016, 09:47 (11 years ago) parentf03928f
1 file changed+19−1
Data/Aeson/Types/Internal.hs+19−1
Data/Aeson/Types/Internal.hs+19−1
@@ -1,5 +1,5 @@
11{-# LANGUAGE CPP, DeriveDataTypeable, GeneralizedNewtypeDeriving, Rank2Types,
2− RecordWildCards #-}
2+ RecordWildCards, TemplateHaskell #-}
33
44-- |
55-- Module: Data.Aeson.Types.Internal
@@ -53,6 +53,7 @@ module Data.Aeson.Types.Internal
5353 , DotNetTime(..)
5454 ) where
5555
56+import Control.Arrow (first)
5657import Control.Applicative
5758import Control.DeepSeq (NFData(..))
5859import Control.Monad (MonadPlus(..), ap)
@@ -71,7 +72,9 @@ import Data.Time.Format (FormatTime)
7172import Data.Typeable (Typeable)
7273import Data.Vector (Vector)
7374import qualified Data.HashMap.Strict as H
75+import qualified Data.Scientific as S
7476import qualified Data.Vector as V
77+import qualified Language.Haskell.TH.Syntax as TH
7578
7679#if !MIN_VERSION_base(4,8,0)
7780import Data.Foldable (Foldable(..))
@@ -392,6 +395,21 @@ hashValue s Null = s `hashWithSalt` (5::Int)
392395instance Hashable Value where
393396 hashWithSalt = hashValue
394397
398+-- @since 0.11.0.0
399+instance TH.Lift Value where
400+ lift Null = [| Null |]
401+ lift (Bool b) = [| Bool b |]
402+ lift (Number n) = [| Number (S.scientific c e) |]
403+ where
404+ c = S.coefficient n
405+ e = S.base10Exponent n
406+ lift (String t) = [| String (pack s) |]
407+ where s = unpack t
408+ lift (Array a) = [| Array (V.fromList a') |]
409+ where a' = V.toList a
410+ lift (Object o) = [| Object (H.fromList . map (first pack) $ o') |]
411+ where o' = map (first unpack) . H.toList $ o
412+
395413-- | The empty array.
396414emptyArray :: Value
397415emptyArray = Array V.empty