on master help sign in
Try encoding/decoding maps.
commita1888a75b5f92b45ea4a0ef8ffa7c599117f46bd authorBryan O'Sullivan <bos@serpentine.com> dateMon 19 Sep 2011, 17:13 (15 years ago) parent3b13b80
2 files changed+12−3
aeson.cabal+1 tests/Properties.hs+11−3
aeson.cabal+1
@@ -148,6 +148,7 @@ test-suite tests
148148 aeson,
149149 attoparsec,
150150 base,
151+ containers,
151152 bytestring,
152153 test-framework,
153154 test-framework-quickcheck2,
tests/Properties.hs+11−3
@@ -2,6 +2,7 @@
22 ScopedTypeVariables #-}
33{-# OPTIONS_GHC -fno-warn-orphans #-}
44
5+import Control.Monad
56import Control.Applicative
67import Data.Aeson.Encode
78import Data.Aeson.Parser (value)
@@ -16,6 +17,7 @@ import qualified Data.Aeson.Generic as G
1617import qualified Data.Attoparsec.Lazy as L
1718import qualified Data.ByteString.Lazy.Char8 as L
1819import qualified Data.Text as T
20+import qualified Data.Map as Map
1921
2022encodeDouble :: Double -> Double -> Bool
2123encodeDouble num denom
@@ -52,7 +54,8 @@ approxEq a b = a == b ||
5254data Foo = Foo {
5355 fooInt :: Int
5456 , fooDouble :: Double
55− , fooTuple :: (String, Text)
57+ , fooTuple :: (String, Text, Int)
58+ , fooMap :: Map.Map String Foo
5659 } deriving (Show, Typeable, Data)
5760
5861instance Eq Foo where
@@ -64,20 +67,25 @@ instance ToJSON Foo where
6467 toJSON Foo{..} = object [ "fooInt" .= fooInt
6568 , "fooDouble" .= fooDouble
6669 , "fooTuple" .= fooTuple
70+ , "fooMap" .= fooMap
6771 ]
6872
6973instance FromJSON Foo where
7074 parseJSON (Object v) = Foo <$>
7175 v .: "fooInt" <*>
7276 v .: "fooDouble" <*>
73− v .: "fooTuple"
77+ v .: "fooTuple" <*>
78+ v .: "fooMap"
7479 parseJSON _ = empty
7580
7681instance Arbitrary Text where
7782 arbitrary = T.pack <$> arbitrary
7883
84+instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map.Map k v) where
85+ arbitrary = Map.fromList <$> arbitrary
86+
7987instance Arbitrary Foo where
80− arbitrary = liftA3 Foo arbitrary arbitrary arbitrary
88+ arbitrary = liftM4 Foo arbitrary arbitrary arbitrary arbitrary
8189
8290main :: IO ()
8391main = defaultMain tests