r/purescript • u/vagif • May 20 '17
Decoding Maybe value fails.
using Argonaut, when decoding Maybe Int. I'm getting this error:
Couldn't decode List Value is not a Number.
Here's the code:
newtype VRILang = VRILang
{ recLangId :: Int
, langName :: String
, langId :: Int
, sortOrder :: Maybe Int
}
instance decodeJsonVRILang :: DecodeJson VRILang where
decodeJson json = do
obj <- decodeJson json
rlID <- obj .? "recLangId"
lName <- obj .? "langname"
lid <- obj .? "langId"
sOrder <- obj .?? "sortOrder"
pure $ VRILang { recLangId: rlID
, langName: lName
, langId: lid
, sortOrder: sOrder
}This object is part of the other object. It comes as a list of objects.
When sortOrder of all items in the list is present, it works fine. But if some of them are null, then it errors out.
2
Upvotes
View all comments
Show parent comment
1
u/albtzrly May 28 '17
I think for most situations you're probably right that it makes sense to return Nothing if the value is null, but I don't think it's a bug. The reason it does that is so someone could read an optional null value if they wanted to.
It might be nice to have a function like this in the library though...