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/vagif May 28 '17
Current getFieldOptional breaks the law of the least surprise. Json parsing is not a new field. Most programmers coming to purescript already have experience dealing with json parsing using other languages and other libraries. I am not aware of a single json library that has a distinction like that. Maybe it is useful, maybe it is not. But it should not take place of the most anticipated solution.