Skip to content

Commit

Permalink
Merge pull request #102 from nikita-volkov/master
Browse files Browse the repository at this point in the history
Fix the Maybe being treated as ADT in Generics bug
  • Loading branch information
bos committed Jan 15, 2013
2 parents 48546d9 + f7e4916 commit 72ae354
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Data/Aeson/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 72ae354

Please sign in to comment.