on master help sign in
Add DList instances
commit8a1eb2a55ff8643db224f86af0b985a09fa78306 authorOleg Grenrus <oleg.grenrus@iki.fi> dateWed 8 Jun 2016, 00:52 (10 years ago) parent0a03013
6 files changed+52−2
Data/Aeson/Types/FromJSON.hs+15 Data/Aeson/Types/ToJSON.hs+18 aeson.cabal+2 tests/Instances.hs+8−2 tests/Properties.hs+4 tests/UnitTests.hs+5
Data/Aeson/Types/FromJSON.hs+15
@@ -104,6 +104,7 @@ import GHC.Generics
104104import Numeric.Natural (Natural)
105105import Text.ParserCombinators.ReadP (readP_to_S)
106106
107+import qualified Data.DList as DList
107108import qualified Data.HashMap.Strict as H
108109import qualified Data.HashSet as HashSet
109110import qualified Data.IntMap as IntMap
@@ -1146,6 +1147,20 @@ instance FromJSON Scientific where
11461147 parseJSON = withScientific "Scientific" pure
11471148 {-# INLINE parseJSON #-}
11481149
1150+-------------------------------------------------------------------------------
1151+-- DList
1152+-------------------------------------------------------------------------------
1153+
1154+instance FromJSON1 DList.DList where
1155+ liftParseJSON p _ = withArray "DList a" $
1156+ fmap DList.fromList .
1157+ Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1158+ {-# INLINE liftParseJSON #-}
1159+
1160+instance (FromJSON a) => FromJSON (DList.DList a) where
1161+ parseJSON = parseJSON1
1162+ {-# INLINE parseJSON #-}
1163+
11491164-------------------------------------------------------------------------------
11501165-- containers
11511166-------------------------------------------------------------------------------
Data/Aeson/Types/ToJSON.hs+18
@@ -1368,6 +1368,24 @@ instance ToJSON Scientific where
13681368 toEncoding = Encoding . EB.number
13691369 {-# INLINE toEncoding #-}
13701370
1371+-------------------------------------------------------------------------------
1372+-- DList
1373+-------------------------------------------------------------------------------
1374+
1375+instance ToJSON1 DList.DList where
1376+ liftToJSON t _ = listValue t . toList
1377+ {-# INLINE liftToJSON #-}
1378+
1379+ liftToEncoding t _ = listEncoding t . toList
1380+ {-# INLINE liftToEncoding #-}
1381+
1382+instance (ToJSON a) => ToJSON (DList.DList a) where
1383+ toJSON = toJSON1
1384+ {-# INLINE toJSON #-}
1385+
1386+ toEncoding = toEncoding1
1387+ {-# INLINE toEncoding #-}
1388+
13711389-------------------------------------------------------------------------------
13721390-- containers
13731391-------------------------------------------------------------------------------
aeson.cabal+2
@@ -148,9 +148,11 @@ test-suite tests
148148 aeson,
149149 attoparsec,
150150 base,
151+ base-compat,
151152 base-orphans >= 0.5.3 && <0.6,
152153 bytestring,
153154 containers,
155+ dlist,
154156 generic-deriving >= 1.10 && < 1.11,
155157 ghc-prim >= 0.2,
156158 hashable >= 1.1.2.0,
tests/Instances.hs+8−2
@@ -7,6 +7,9 @@
77
88module Instances where
99
10+import Prelude ()
11+import Prelude.Compat
12+
1013import Types
1114import Data.Function (on)
1215import Control.Monad
@@ -17,11 +20,11 @@ import Data.Time (ZonedTime(..), TimeZone(..))
1720import Data.List.NonEmpty (NonEmpty(..))
1821import Data.Version
1922import Data.Aeson.Types
20−import Control.Applicative (Const(..))
23+import Control.Applicative (Const(..), empty)
2124import Data.Tagged (Tagged(..))
2225import Data.Proxy (Proxy(..))
23−import Control.Applicative
2426import Functions
27+import qualified Data.DList as DList
2528
2629import Data.Orphans ()
2730import Test.QuickCheck.Instances ()
@@ -171,3 +174,6 @@ instance Arbitrary b => Arbitrary (Tagged a b) where
171174
172175instance Arbitrary a => Arbitrary (Const a b) where
173176 arbitrary = Const <$> arbitrary
177+
178+instance Arbitrary a => Arbitrary (DList.DList a) where
179+ arbitrary = DList.fromList <$> arbitrary
tests/Properties.hs+4
@@ -10,6 +10,8 @@ import Data.Aeson.Parser (value)
1010import Data.Aeson.Types
1111import Data.ByteString.Builder (toLazyByteString)
1212import Data.Int (Int8)
13+import Data.Sequence (Seq)
14+import Data.DList (DList)
1315import Data.List.NonEmpty (NonEmpty)
1416import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay, UTCTime,
1517 ZonedTime)
@@ -152,6 +154,8 @@ tests = testGroup "properties" [
152154 , testProperty "Proxy" $ roundTripEq (undefined :: Proxy Int)
153155 , testProperty "Tagged" $ roundTripEq (undefined :: Tagged Int Char)
154156 , testProperty "Const" $ roundTripEq (undefined :: Const Int Char)
157+ , testProperty "DList" $ roundTripEq (undefined :: DList Int)
158+ , testProperty "Seq" $ roundTripEq (undefined :: Seq Int)
155159 , testGroup "ghcGenerics" [
156160 testProperty "OneConstructor" $ roundTripEq OneConstructor
157161 , testProperty "Product2" $ roundTripEq (undefined :: Product2 Int Bool)
tests/UnitTests.hs+5
@@ -35,6 +35,9 @@ import qualified Data.Text.Lazy.Encoding as TLE
3535import qualified Data.Text.Lazy as LT
3636import qualified Data.Text.Lazy.Encoding as LT
3737
38+import qualified Data.Sequence as Seq
39+import qualified Data.DList as DList
40+
3841tests :: Test
3942tests = testGroup "unit" [
4043 testGroup "camelCase" [
@@ -240,6 +243,8 @@ jsonEncoding = [
240243 , assertEqual "Const Char Int" "\"c\"" $ encode (Const 'c' :: Const Char Int)
241244 , assertEqual "Tuple" "[1,2]" $ encode ((1, 2) :: (Int, Int))
242245 , assertEqual "NonEmpty" "[1,2,3]" $ encode (1 :| [2, 3] :: NonEmpty Int)
246+ , assertEqual "Seq" "[1,2,3]" $ encode (Seq.fromList [1, 2, 3] :: Seq.Seq Int)
247+ , assertEqual "DList" "[1,2,3]" $ encode (DList.fromList [1, 2, 3] :: DList.DList Int)
243248 , assertEqual "()" "[]" $ encode ()
244249 ]
245250