DecodeMaybe.purs 771 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.Either (Either)
7
import Data.Maybe (Maybe(..), fromMaybe)
Sudhir Kumar's avatar
Sudhir Kumar committed
8
import Foreign.Object (Object)
9 10 11

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

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

infix 7 getFieldOptional' as .?|
20

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

infix 7 getFieldOptionalAsMempty as .|