Skip to content
This repository

Fix the Maybe being treated as ADT in Generics bug #102

Merged
merged 2 commits into from about 1 year ago

2 participants

Nikita Volkov Bryan O'Sullivan
Nikita Volkov

This also fixes this bug: #101

Bryan O'Sullivan bos merged commit 72ae354 into from
Bryan O'Sullivan bos closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
6  Data/Aeson/Generic.hs
@@ -84,6 +84,7 @@ type T a = a -> Value
84 84
 
85 85
 toJSON :: (Data a) => a -> Value
86 86
 toJSON = toJSON_generic
  87
+         `ext1Q` maybe
87 88
          `ext1Q` list
88 89
          `ext1Q` vector
89 90
          `ext1Q` set
@@ -119,6 +120,8 @@ toJSON = toJSON_generic
119 120
          `extQ` (T.toJSON :: T ())
120 121
          --`extQ` (T.toJSON :: T Ordering)
121 122
   where
  123
+    maybe (Just a) = toJSON a
  124
+    maybe Nothing = Null
122 125
     list xs = Array . V.fromList . map toJSON $ xs
123 126
     vector v = Array . V.map toJSON $ v
124 127
     set s = Array . V.fromList . map toJSON . Set.toList $ s
@@ -187,6 +190,7 @@ type F a = Parser a
187 190
 
188 191
 parseJSON :: (Data a) => Value -> Parser a
189 192
 parseJSON j = parseJSON_generic j
  193
+             `ext1R` maybeP
190 194
              `ext1R` list
191 195
              `ext1R` vector
192 196
              `ext2R'` mapAny
@@ -222,6 +226,8 @@ parseJSON j = parseJSON_generic j
222 226
   where
223 227
     value :: (T.FromJSON a) => Parser a
224 228
     value = T.parseJSON j
  229
+    maybeP :: (Data a) => Parser (Maybe a)
  230
+    maybeP = if j == Null then return Nothing else Just <$> parseJSON j
225 231
     list :: (Data a) => Parser [a]
226 232
     list = V.toList <$> parseJSON j
227 233
     vector :: (Data a) => Parser (V.Vector a)
2  tests/Properties.hs
@@ -143,12 +143,14 @@ tests = [
143 143
     , testProperty "Double" $ genericFrom (1::Double)
144 144
     , testProperty "Int" $ genericFrom (1::Int)
145 145
     , testProperty "Foo" $ genericFrom (undefined::Foo)
  146
+    , testProperty "Maybe" $ genericFrom (Just 1 :: Maybe Int)
146 147
     ],
147 148
   testGroup "genericTo" [
148 149
       testProperty "Bool" $ genericTo True
149 150
     , testProperty "Double" $ genericTo (1::Double)
150 151
     , testProperty "Int" $ genericTo (1::Int)
151 152
     , testProperty "Foo" $ genericTo (undefined::Foo)
  153
+    , testProperty "Maybe" $ genericTo (Just 1 :: Maybe Int)
152 154
     ],
153 155
   testGroup "roundTrip" [
154 156
       testProperty "Bool" $ roundTripEq True
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.