DecodeMaybe.purs 845 Bytes
Newer Older
1
module Gargantext.Utils.DecodeMaybe where
2 3 4

import Prelude

Sudhir Kumar's avatar
Sudhir Kumar committed
5
import Data.Argonaut (class DecodeJson, Json, getFieldOptional)
6
import Data.Argonaut.Decode.Error (JsonDecodeError(..))
7
import Data.Either (Either)
8
import Data.Maybe (Maybe(..), fromMaybe)
Sudhir Kumar's avatar
Sudhir Kumar committed
9
import Foreign.Object (Object)
10 11 12

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

13
getFieldOptional' :: forall a. DecodeJson a => 
14
  Object Json -> String -> Either JsonDecodeError (Maybe a)
15 16 17 18 19 20
getFieldOptional' o s = (case _ of
    Just v -> if isNull v then Nothing else v
    Nothing -> Nothing
  ) <$> (getFieldOptional o s)

infix 7 getFieldOptional' as .?|
21

22
getFieldOptionalAsMempty :: forall a. DecodeJson a => 
23
  Monoid a => Object Json -> String -> Either JsonDecodeError a
24 25
getFieldOptionalAsMempty o s = 
  fromMaybe mempty <$> (getFieldOptional' o s)
26 27

infix 7 getFieldOptionalAsMempty as .|