-
Notifications
You must be signed in to change notification settings - Fork 322
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
Allow accepting missing fields of other types than Maybe #646
Comments
I would say it is related to: #323 |
@tolysz Thanks. It seems that your proposal could fix this issue. However I am not sure it is a good idea to modify Since the problem is specific to records, what I would like to have is some way to specify whether a record field is allowed to have a default type or not (and what the default value should be) according to its type. |
I can only say it is a good idea ;) It is transparent to virtually all applications. I am porting my old patches to the new version of |
@tolysz It works, but I am not sure it is the most appropriate solution. Since this issue only concerns records, I would prefer a solution that only concerns records. If you add new constructors to |
|
@lortabac I prefer a situation where I can encode all possible scenarios, rather than a limited subset, even if some would be invalid (a stricter validation would remove those invalid cases). My proposed approach composes well and it is really useful, but I do get that people are afraid of changing the holly library. Good thing is that I do not see any other useful constructors ;) but the new one is essential if we go via the Anyway, this will be coming back, let me update the old patches. You can look at them. Each time I improve something there. |
What's the latest news on this? Is it possible to derive FromJSON and have the instance allow Maybe fields to be missing, or do such instances have to be written manually? |
@torgeirsh You can already do that with Generic instances. If a Maybe field is missing, it defaults to In this issue I propose to extend this behavior to other types than |
@lortabac I can't get it working:
Do you mean something else by "Generic instances"? |
@torgeirsh That's a somewhat different issue, see also #690 and the extra documentation added here. Don't hesitate to ask for further clarification though! |
@Lysxia Thanks, I can see that I have the FromJSON version of that issue. |
Any update here? I actually have a datatype where certain fields have a type isomorphic to (). I would like them to be populated with the same value, regardless of whether the field is there or not. (NB. in the presence of type families, this isn't as silly as it sounds.) I've written a manual |
Sorry, no work has been done on this as far as I know. Seems like @Lysxia's suggestion is a promising approach:
|
The workaround I've used is to transform the value with this function prior to parsing, so that it can work with any parser that can accept import qualified Data.HashMap.Strict as HM
addNullField :: Text -> Value -> Value
addNullField s (Object o) = Object $ HM.insertWith (\_new old -> old) s Null o
addNullField _ v = v I would use it like this: data Foo = Foo
{ someField :: SomePossiblyNullType
} deriving (Show, Generic)
instance FromJSON Foo where
parseJSON = genericParseJSON defaultOptions . addNullField "someField" This also works for polymorphic types: data Foo a = Foo
{ someField :: a
} deriving (Show, Generic)
instance FromJSON a => FromJSON (Foo a) where
parseJSON = genericParseJSON defaultOptions . addNullField "someField"
-- >>> decode "{}" :: Maybe (Foo (Maybe Int))
-- Just (Foo {someField = Nothing})
-- >>> decode "{\"someField\": 3}" :: Maybe (Foo (Maybe Int))
-- Just (Foo {someField = Just 3}) |
Maybe we could add a new option called |
@anka-213 |
@tolysz That was my first thought too, but I feel like that might be too much of a breaking change. Object (fromList [("a",Missing)]) is now equivalent to Object (fromList []) My suggestion would be to add an option to the It could also resolve #690, since there would now be an option to treat null and missing fields equivalently for a data type. |
Those changes are not breaking... you can compile the whole stack... |
I meant that if anyone is pattern matching on It also might not be sufficient to solve my specific problem, since it is a combination of this and #690. |
I updated it to the latest aeson: https://github.com/tolysz/aeson/tree/Missing_1.5 (the same failing test) This does exactly what you need... check the examples (but you need to replace |
Object (fromList [("a",Missing)]) would look like |
When deriving
FromJSON
instances generically, there is no way to provide a default value to a missing field of a record if the field is not aMaybe
.This seems related to #614, in that the proposed solution may help fixing both issues.
The text was updated successfully, but these errors were encountered: