-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserializing null values to Option #87
Comments
just an update, just tried aeson, and what I'm reporting here is aeson's behavior {-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Aeson
import Data.Text (Text)
data Customer = Customer
{ id :: Integer
, name :: String
, address :: Maybe Address }
deriving Show
data Address = Address
{ country :: String }
deriving Show
instance FromJSON Address where
parseJSON (Object v) = Address <$> v .: "country"
parseJSON _ = empty
instance FromJSON Customer where
parseJSON (Object v) = Customer <$> v .: "id" <*> v .: "name" <*> v .:? "address"
parseJSON _ = empty
instance ToJSON Address where
toJSON (Address country) = object ["country" .= country]
instance ToJSON Customer where
toJSON (Customer id name address) = object ["id" .= id, "name" .= name, "address" .= address] |
from what I see here, the problem is when some folks might actually want to get nulls back. //edit |
Note that as from v 0.10.0 Not sure if this solves your specific problem, I think if they supply nulls and the field is Nullable it should work now. Otherwise please let me know. |
Trying to deserialize a null value into an option produces an obj expected value (json type mismatch). The behavior should be consistent whether the value is null or doesn't exist, but that's not the case. It correctly returns a
None
when the value is absent, and and error when the value is present, but set to null.Repro
Succeds - Returns Some { id = 1; name = "Joe"; address = None}
Fails - Should return Some { id = 1; name = "Joe"; address = None}
a potential solution for this issue would be to modify
jgetOptionWith
function like://edited to update the test cases and confirm the solution works for other libraries as well.
The text was updated successfully, but these errors were encountered: