r/purescript • • 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

5 comments sorted by

View all comments

Show parent comment

1

u/vagif May 28 '17

Yeah looks like it. But logically it really should return Nothing if the key is there and has value null. I wonder how easy or hard the fix would be?

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...

foreign import isNull :: forall a. a -> Boolean

getFieldOptional' :: forall a. DecodeJson a => JObject -> String -> Either String (Maybe a)
getFieldOptional' o s = (case _ of
    Just v -> if isNull v then Nothing else v
    Nothing -> Nothing
  ) <$> (getFieldOptional o s)

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.