aeson
aeson copied to clipboard
omitNothingFields not used in Generic Decode
omitNothingFields option is used for generic encoding, but it is not used for generic decoding. Is this a bug?
Yes that sounds like a bug. Are you able to provide a reproduction?
> import Data.Aeson
> data F = F { a :: Maybe Int, b :: Maybe Int } deriving (Show, GHC.Generics.Generic)
> instance FromJSON F where parseJSON = genericParseJSON defaultOptions
> -- omitNothingFields = False by default
> decode "{}" :: Maybe F
Just (F {a = Nothing, b = Nothing})
I guess we want that to fail?
Whoops big typo in my deleted comment! I'm not sure if we should consider this a bug as
> eitherDecode "{\"a\":null,\"b\":null}" :: Either String F
Right (F {a = Nothing, b = Nothing})
Does it hurt that parsing is more lenient?
Coming back to this, I think it's a good thing that parsing is more lenient. I'd be open to add this as a part of Options so you can opt-in if you want it.
With https://github.com/haskell/aeson/pull/1039 this will fail.
I will first add a test to master to see how things work now, and then decide what to do in #1039.
#1039 will add allowOmittedFields option (True by default) to control whether omitted fields are ok when parsing.