Bryan O'Sullivan avatar Bryan O'Sullivan committed a5fa8ca

Try encoding/decoding maps.

Comments (0)

Files changed (2)

     aeson,
     attoparsec,
     base,
+    containers,
     bytestring,
     test-framework,
     test-framework-quickcheck2,

tests/Properties.hs

     ScopedTypeVariables #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 
+import Control.Monad
 import Control.Applicative
 import Data.Aeson.Encode
 import Data.Aeson.Parser (value)
 import qualified Data.Attoparsec.Lazy as L
 import qualified Data.ByteString.Lazy.Char8 as L
 import qualified Data.Text as T
+import qualified Data.Map as Map
 
 encodeDouble :: Double -> Double -> Bool
 encodeDouble num denom
 data Foo = Foo {
       fooInt :: Int
     , fooDouble :: Double
-    , fooTuple :: (String, Text)
+    , fooTuple :: (String, Text, Int)
+    , fooMap :: Map.Map String Foo
     } deriving (Show, Typeable, Data)
 
 instance Eq Foo where
     toJSON Foo{..} = object [ "fooInt" .= fooInt
                             , "fooDouble" .= fooDouble
                             , "fooTuple" .= fooTuple
+                            , "fooMap" .= fooMap
                             ]
 
 instance FromJSON Foo where
     parseJSON (Object v) = Foo <$>
                            v .: "fooInt" <*>
                            v .: "fooDouble" <*>
-                           v .: "fooTuple"
+                           v .: "fooTuple" <*>
+                           v .: "fooMap"
     parseJSON _ = empty
 
 instance Arbitrary Text where
     arbitrary = T.pack <$> arbitrary
 
+instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map.Map k v) where
+    arbitrary = Map.fromList <$> arbitrary
+
 instance Arbitrary Foo where
-    arbitrary = liftA3 Foo arbitrary arbitrary arbitrary
+    arbitrary = liftM4 Foo arbitrary arbitrary arbitrary arbitrary
 
 main :: IO ()
 main = defaultMain tests
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.