Skip to content

Commit

Permalink
Merge pull request #347 from phadej/th-lift
Browse files Browse the repository at this point in the history
Add Lift Value instance
  • Loading branch information
bergmark committed Jan 29, 2016
2 parents 9c61b43 + 125b641 commit 1441cea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Data/Aeson/TH.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{-# LANGUAGE CPP, FlexibleInstances, NamedFieldPuns,
NoImplicitPrelude, TemplateHaskell,
UndecidableInstances #-}
NoImplicitPrelude, UndecidableInstances #-}
#if __GLASGOW_HASKELL >= 800
-- a) THQ works on cross-compilers and unregisterised GHCs
-- b) may make compilation faster as no dynamic loading is ever needed (not sure about this)
-- c) removes one hindrance to have code inferred as SafeHaskell safe
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE TemplateHaskell #-}
#endif

#include "overlapping-compat.h"

Expand Down
26 changes: 26 additions & 0 deletions Data/Aeson/Types/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{-# LANGUAGE CPP, DeriveDataTypeable, GeneralizedNewtypeDeriving, Rank2Types,
RecordWildCards #-}
#if __GLASGOW_HASKELL >= 800
-- a) THQ works on cross-compilers and unregisterised GHCs
-- b) may make compilation faster as no dynamic loading is ever needed (not sure about this)
-- c) removes one hindrance to have code inferred as SafeHaskell safe
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE TemplateHaskell #-}
#endif

-- |
-- Module: Data.Aeson.Types.Internal
Expand Down Expand Up @@ -53,6 +61,7 @@ module Data.Aeson.Types.Internal
, DotNetTime(..)
) where

import Control.Arrow (first)
import Control.Applicative
import Control.DeepSeq (NFData(..))
import Control.Monad (MonadPlus(..), ap)
Expand All @@ -71,7 +80,9 @@ import Data.Time.Format (FormatTime)
import Data.Typeable (Typeable)
import Data.Vector (Vector)
import qualified Data.HashMap.Strict as H
import qualified Data.Scientific as S
import qualified Data.Vector as V
import qualified Language.Haskell.TH.Syntax as TH

#if !MIN_VERSION_base(4,8,0)
import Data.Foldable (Foldable(..))
Expand Down Expand Up @@ -392,6 +403,21 @@ hashValue s Null = s `hashWithSalt` (5::Int)
instance Hashable Value where
hashWithSalt = hashValue

-- @since 0.11.0.0
instance TH.Lift Value where
lift Null = [| Null |]
lift (Bool b) = [| Bool b |]
lift (Number n) = [| Number (S.scientific c e) |]
where
c = S.coefficient n
e = S.base10Exponent n
lift (String t) = [| String (pack s) |]
where s = unpack t
lift (Array a) = [| Array (V.fromList a') |]
where a' = V.toList a
lift (Object o) = [| Object (H.fromList . map (first pack) $ o') |]
where o' = map (first unpack) . H.toList $ o

-- | The empty array.
emptyArray :: Value
emptyArray = Array V.empty
Expand Down

0 comments on commit 1441cea

Please sign in to comment.