Commit a7dab291 authored by Justin Woo's avatar Justin Woo

handle sum type nullary constructors in generic JSON

parent 15953f46
......@@ -56,6 +56,11 @@ instance genericSumDecodeJsonRepConstructor ::
argument <- genericSumDecodeJsonRep inner
pure $ GR.Constructor argument
instance genericSumDecodeJsonRepNoArguments ::
GenericSumDecodeJsonRep (GR.NoArguments) where
genericSumDecodeJsonRep _ = do
pure GR.NoArguments
instance genericSumDecodeJsonRepArgument ::
( Argonaut.DecodeJson a
) => GenericSumDecodeJsonRep (GR.Argument a) where
......@@ -79,6 +84,10 @@ instance genericSumEncodeJsonRepConstructor ::
let argument = genericSumEncodeJsonRep inner
Argonaut.jsonSingletonObject name argument
instance genericSumEncodeJsonRepNoArguments ::
GenericSumEncodeJsonRep GR.NoArguments where
genericSumEncodeJsonRep GR.NoArguments = Argonaut.jsonNull
instance genericSumEncodeJsonRepArgument ::
( Argonaut.EncodeJson a
) => GenericSumEncodeJsonRep (GR.Argument a) where
......
......@@ -17,6 +17,7 @@ data Fruit
= Boat { hi :: Int }
| Gravy String
| Pork Int
| Chicken
derive instance eqFruit :: Eq Fruit
derive instance genericFruit :: Generic Fruit _
......@@ -76,3 +77,9 @@ spec =
let result2' = Argonaut.decodeJson result2
Argonaut.stringify result2 `shouldEqual` """{"Gravy":"hi"}"""
result2' `shouldEqual` Right input2
let input3 = Chicken
let result3 = Argonaut.encodeJson input3
let result3' = Argonaut.decodeJson result3
Argonaut.stringify result3 `shouldEqual` """{"Chicken":null}"""
result3' `shouldEqual` Right input3
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment