Permalink
Browse files

Merge pull request #102 from nikita-volkov/master

Fix the Maybe being treated as ADT in Generics bug
  • Loading branch information...
2 parents 48546d9 + f7e4916 commit 72ae354a59f44714eba71f6cda8dffb69b1dc106 @bos committed Jan 15, 2013
Showing with 8 additions and 0 deletions.
  1. +6 −0 Data/Aeson/Generic.hs
  2. +2 −0 tests/Properties.hs
View
@@ -84,6 +84,7 @@ type T a = a -> Value
toJSON :: (Data a) => a -> Value
toJSON = toJSON_generic
+ `ext1Q` maybe
`ext1Q` list
`ext1Q` vector
`ext1Q` set
@@ -119,6 +120,8 @@ toJSON = toJSON_generic
`extQ` (T.toJSON :: T ())
--`extQ` (T.toJSON :: T Ordering)
where
+ maybe (Just a) = toJSON a
+ maybe Nothing = Null
list xs = Array . V.fromList . map toJSON $ xs
vector v = Array . V.map toJSON $ v
set s = Array . V.fromList . map toJSON . Set.toList $ s
@@ -187,6 +190,7 @@ type F a = Parser a
parseJSON :: (Data a) => Value -> Parser a
parseJSON j = parseJSON_generic j
+ `ext1R` maybeP
`ext1R` list
`ext1R` vector
`ext2R'` mapAny
@@ -222,6 +226,8 @@ parseJSON j = parseJSON_generic j
where
value :: (T.FromJSON a) => Parser a
value = T.parseJSON j
+ maybeP :: (Data a) => Parser (Maybe a)
+ maybeP = if j == Null then return Nothing else Just <$> parseJSON j
list :: (Data a) => Parser [a]
list = V.toList <$> parseJSON j
vector :: (Data a) => Parser (V.Vector a)
View
@@ -151,12 +151,14 @@ tests = [
, testProperty "Double" $ genericFrom (1::Double)
, testProperty "Int" $ genericFrom (1::Int)
, testProperty "Foo" $ genericFrom (undefined::Foo)
+ , testProperty "Maybe" $ genericFrom (Just 1 :: Maybe Int)
],
testGroup "genericTo" [
testProperty "Bool" $ genericTo True
, testProperty "Double" $ genericTo (1::Double)
, testProperty "Int" $ genericTo (1::Int)
, testProperty "Foo" $ genericTo (undefined::Foo)
+ , testProperty "Maybe" $ genericTo (Just 1 :: Maybe Int)
],
testGroup "roundTrip" [
testProperty "Bool" $ roundTripEq True

0 comments on commit 72ae354

Please sign in to comment.